blob: 4500744a10e8cd1988ad655f4524715a03aeacf4 [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 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
19import java.util.ArrayList;
20import java.util.Collections;
Winson Chung34efdaf2011-05-24 14:19:56 -070021import java.util.Iterator;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import java.util.List;
23
Winson Chung46af2e82011-05-09 16:00:53 -070024import org.xmlpull.v1.XmlPullParser;
25
Winson Chung4b576be2011-04-27 17:40:20 -070026import android.animation.Animator;
27import android.animation.AnimatorListenerAdapter;
28import android.animation.ObjectAnimator;
29import android.animation.PropertyValuesHolder;
Winson Chung46af2e82011-05-09 16:00:53 -070030import android.app.WallpaperManager;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
33import android.content.ComponentName;
34import android.content.Context;
35import android.content.Intent;
Winson Chung46af2e82011-05-09 16:00:53 -070036import android.content.pm.ActivityInfo;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.content.pm.PackageManager;
38import android.content.pm.ResolveInfo;
39import android.content.res.Resources;
40import android.content.res.TypedArray;
Winson Chung46af2e82011-05-09 16:00:53 -070041import android.content.res.XmlResourceParser;
Winson Chung785d2eb2011-04-14 16:08:02 -070042import android.graphics.Bitmap;
43import android.graphics.Bitmap.Config;
44import android.graphics.Canvas;
45import android.graphics.Rect;
46import android.graphics.drawable.Drawable;
47import android.util.AttributeSet;
48import android.util.Log;
Winson Chung1ed747a2011-05-03 16:18:34 -070049import android.util.LruCache;
Winson Chung46af2e82011-05-09 16:00:53 -070050import android.util.Slog;
51import android.util.TypedValue;
52import android.util.Xml;
Winson Chung785d2eb2011-04-14 16:08:02 -070053import android.view.LayoutInflater;
54import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070055import android.view.ViewGroup;
Winson Chung4b576be2011-04-27 17:40:20 -070056import android.view.animation.DecelerateInterpolator;
57import android.view.animation.LinearInterpolator;
Winson Chung785d2eb2011-04-14 16:08:02 -070058import android.widget.ImageView;
59import android.widget.TextView;
60
61import com.android.launcher.R;
62
63public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
64 AllAppsView, View.OnClickListener, DragSource {
65 static final String LOG_TAG = "AppsCustomizePagedView";
66
67 /**
68 * The different content types that this paged view can show.
69 */
70 public enum ContentType {
71 Applications,
Winson Chung46af2e82011-05-09 16:00:53 -070072 Widgets,
73 Wallpapers
Winson Chung785d2eb2011-04-14 16:08:02 -070074 }
75
76 // Refs
77 private Launcher mLauncher;
78 private DragController mDragController;
79 private final LayoutInflater mLayoutInflater;
80 private final PackageManager mPackageManager;
81
82 // Content
83 private ContentType mContentType;
84 private ArrayList<ApplicationInfo> mApps;
Winson Chung1ed747a2011-05-03 16:18:34 -070085 private List<Object> mWidgets;
Winson Chung46af2e82011-05-09 16:00:53 -070086 private List<ResolveInfo> mWallpapers;
Winson Chung1ed747a2011-05-03 16:18:34 -070087
88 // Caching
89 private Drawable mDefaultWidgetBackground;
90 private final int sWidgetPreviewCacheSize = 1 * 1024 * 1024; // 1 MiB
91 private LruCache<Object, Bitmap> mWidgetPreviewCache;
Winson Chung4dbea792011-05-05 14:21:32 -070092 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -070093
94 // Dimens
95 private int mContentWidth;
Winson Chung4b576be2011-04-27 17:40:20 -070096 private int mMaxWidgetSpan, mMinWidgetSpan;
Winson Chung46af2e82011-05-09 16:00:53 -070097 private int mCellWidthGap, mCellHeightGap;
Winson Chung4b576be2011-04-27 17:40:20 -070098 private int mWidgetCountX, mWidgetCountY;
Winson Chung46af2e82011-05-09 16:00:53 -070099 private int mWallpaperCountX, mWallpaperCountY;
Winson Chung1ed747a2011-05-03 16:18:34 -0700100 private final int mWidgetPreviewIconPaddedDimension;
101 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700102 private PagedViewCellLayout mWidgetSpacingLayout;
103
Winson Chung4b576be2011-04-27 17:40:20 -0700104 // Animations
105 private final float ANIMATION_SCALE = 0.5f;
106 private final int TRANSLATE_ANIM_DURATION = 400;
107 private final int DROP_ANIM_DURATION = 200;
108
Winson Chung785d2eb2011-04-14 16:08:02 -0700109 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
110 super(context, attrs);
111 mLayoutInflater = LayoutInflater.from(context);
112 mPackageManager = context.getPackageManager();
113 mContentType = ContentType.Applications;
114 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700115 mWidgets = new ArrayList<Object>();
Winson Chung46af2e82011-05-09 16:00:53 -0700116 mWallpapers = new ArrayList<ResolveInfo>();
Winson Chung4dbea792011-05-05 14:21:32 -0700117 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chung1ed747a2011-05-03 16:18:34 -0700118 mWidgetPreviewCache = new LruCache<Object, Bitmap>(sWidgetPreviewCacheSize) {
119 protected int sizeOf(Object key, Bitmap value) {
120 return value.getByteCount();
121 }
122 };
123
124 // Save the default widget preview background
125 Resources resources = context.getResources();
126 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview);
Winson Chung785d2eb2011-04-14 16:08:02 -0700127
128 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0);
129 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
130 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
131 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700132 a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chung46af2e82011-05-09 16:00:53 -0700133 mCellWidthGap =
Winson Chung4b576be2011-04-27 17:40:20 -0700134 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 10);
Winson Chung46af2e82011-05-09 16:00:53 -0700135 mCellHeightGap =
Winson Chung4b576be2011-04-27 17:40:20 -0700136 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 10);
137 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
138 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung46af2e82011-05-09 16:00:53 -0700139 mWallpaperCountX = a.getInt(R.styleable.AppsCustomizePagedView_wallpaperCountX, 2);
140 mWallpaperCountY = a.getInt(R.styleable.AppsCustomizePagedView_wallpaperCountY, 2);
Winson Chung4b576be2011-04-27 17:40:20 -0700141 a.recycle();
Winson Chung785d2eb2011-04-14 16:08:02 -0700142
Winson Chung4b576be2011-04-27 17:40:20 -0700143 // Create a dummy page that we can use to approximate the cell dimensions of widgets and
144 // the content width (to be used by our parent)
Winson Chung785d2eb2011-04-14 16:08:02 -0700145 mWidgetSpacingLayout = new PagedViewCellLayout(context);
Winson Chung4b576be2011-04-27 17:40:20 -0700146 setupPage(mWidgetSpacingLayout);
147 mContentWidth = mWidgetSpacingLayout.getContentWidth();
148
149 // The max widget span is the length N, such that NxN is the largest bounds that the widget
150 // preview can be before applying the widget scaling
151 mMinWidgetSpan = 1;
152 mMaxWidgetSpan = 3;
Winson Chung1ed747a2011-05-03 16:18:34 -0700153
154 // The padding on the non-matched dimension for the default widget preview icons
155 // (top + bottom)
156 int iconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
157 mWidgetPreviewIconPaddedDimension =
158 (int) (iconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Winson Chung785d2eb2011-04-14 16:08:02 -0700159 }
160
161 @Override
162 protected void init() {
163 super.init();
164 mCenterPagesVertically = false;
165
166 Context context = getContext();
167 Resources r = context.getResources();
168 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
169 }
170
Winson Chung34efdaf2011-05-24 14:19:56 -0700171 /** Removes and returns the ResolveInfo with the specified ComponentName */
172 private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list,
173 ComponentName cn) {
174 Iterator<ResolveInfo> iter = list.iterator();
175 while (iter.hasNext()) {
176 ResolveInfo rinfo = iter.next();
177 ActivityInfo info = rinfo.activityInfo;
178 ComponentName c = new ComponentName(info.packageName, info.name);
179 if (c.equals(cn)) {
180 iter.remove();
181 return rinfo;
182 }
183 }
184 return null;
185 }
186
Winson Chung785d2eb2011-04-14 16:08:02 -0700187 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700188 // Get the list of widgets and shortcuts
189 mWidgets.clear();
190 mWidgets.addAll(AppWidgetManager.getInstance(mLauncher).getInstalledProviders());
Winson Chung785d2eb2011-04-14 16:08:02 -0700191 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
Winson Chung1ed747a2011-05-03 16:18:34 -0700192 mWidgets.addAll(mPackageManager.queryIntentActivities(shortcutsIntent, 0));
193 Collections.sort(mWidgets,
194 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung46af2e82011-05-09 16:00:53 -0700195
196 // Get the list of wallpapers
197 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
198 mWallpapers = mPackageManager.queryIntentActivities(wallpapersIntent,
199 PackageManager.GET_META_DATA);
200 Collections.sort(mWallpapers,
201 new LauncherModel.ShortcutNameComparator(mPackageManager));
Winson Chung34efdaf2011-05-24 14:19:56 -0700202 // Move Live Wallpapers to the front of the list
203 Context c = getContext();
204 ResolveInfo liveWallpapers = removeResolveInfoWithComponentName(mWallpapers,
205 new ComponentName(c.getString(R.string.live_wallpaper_picker_package_name),
206 c.getString(R.string.live_wallpaper_picker_class_name)));
207 if (liveWallpapers != null) {
208 mWallpapers.add(0, liveWallpapers);
209 }
210 // Move Wallpapers to the front of the list
211 ResolveInfo wallpapers = removeResolveInfoWithComponentName(mWallpapers,
212 new ComponentName(c.getPackageName(), WallpaperChooser.class.getName()));
213 if (wallpapers != null) {
214 mWallpapers.add(0, wallpapers);
215 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700216 }
217
Winson Chung4b576be2011-04-27 17:40:20 -0700218 /**
219 * Animates the given item onto the center of a home screen, and then scales the item to
220 * look as though it's disappearing onto that screen.
221 */
222 private void animateItemOntoScreen(View dragView,
223 final CellLayout layout, final ItemInfo info) {
224 // On the phone, we only want to fade the widget preview out
225 float[] position = new float[2];
226 position[0] = layout.getWidth() / 2;
227 position[1] = layout.getHeight() / 2;
228
229 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, position);
230
231 int dragViewWidth = dragView.getMeasuredWidth();
232 int dragViewHeight = dragView.getMeasuredHeight();
233 float heightOffset = 0;
234 float widthOffset = 0;
235
236 if (dragView instanceof ImageView) {
237 Drawable d = ((ImageView) dragView).getDrawable();
238 int width = d.getIntrinsicWidth();
239 int height = d.getIntrinsicHeight();
240
241 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
242 float f = (dragViewWidth / (width * 1.0f));
243 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
244 } else {
245 float f = (dragViewHeight / (height * 1.0f));
246 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
247 }
248 }
249 final float toX = position[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
250 final float toY = position[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
251
252 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
253 final View dragCopy = dragLayer.createDragView(dragView);
254 dragCopy.setAlpha(1.0f);
255
256 // Translate the item to the center of the appropriate home screen
257 animateIntoPosition(dragCopy, toX, toY, null);
258
259 // The drop-onto-screen animation begins a bit later, but ends at the same time.
260 final int startDelay = TRANSLATE_ANIM_DURATION - DROP_ANIM_DURATION;
261
262 // Scale down the icon and fade out the alpha
263 animateDropOntoScreen(dragCopy, info, DROP_ANIM_DURATION, startDelay);
264 }
265
266 /**
267 * Animation which scales the view down and animates its alpha, making it appear to disappear
268 * onto a home screen.
269 */
270 private void animateDropOntoScreen(
271 final View view, final ItemInfo info, int duration, int delay) {
272 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
273 final CellLayout layout = mLauncher.getWorkspace().getCurrentDropLayout();
274
275 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
276 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
277 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
278 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
279 anim.setInterpolator(new LinearInterpolator());
280 if (delay > 0) {
281 anim.setStartDelay(delay);
282 }
283 anim.setDuration(duration);
284 anim.addListener(new AnimatorListenerAdapter() {
285 public void onAnimationEnd(Animator animation) {
286 dragLayer.removeView(view);
287 mLauncher.addExternalItemToScreen(info, layout);
288 info.dropPos = null;
Winson Chung785d2eb2011-04-14 16:08:02 -0700289 }
290 });
Winson Chung4b576be2011-04-27 17:40:20 -0700291 anim.start();
292 }
293
294 /**
295 * Animates the x,y position of the view, and optionally execute a Runnable on animation end.
296 */
297 private void animateIntoPosition(
298 View view, float toX, float toY, final Runnable endRunnable) {
299 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
300 PropertyValuesHolder.ofFloat("x", toX),
301 PropertyValuesHolder.ofFloat("y", toY));
302 anim.setInterpolator(new DecelerateInterpolator(2.5f));
303 anim.setDuration(TRANSLATE_ANIM_DURATION);
304 if (endRunnable != null) {
305 anim.addListener(new AnimatorListenerAdapter() {
306 @Override
307 public void onAnimationEnd(Animator animation) {
308 endRunnable.run();
309 }
310 });
311 }
312 anim.start();
313 }
314
315 @Override
316 public void onClick(View v) {
317 if (v instanceof PagedViewIcon) {
318 // Animate some feedback to the click
319 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
320 animateClickFeedback(v, new Runnable() {
321 @Override
322 public void run() {
323 mLauncher.startActivitySafely(appInfo.intent, appInfo);
324 }
325 });
326 } else if (v instanceof PagedViewWidget) {
Winson Chung59d0bb02011-05-23 11:34:11 -0700327 if (v.getTag() instanceof ResolveInfo) {
328 final ResolveInfo info = (ResolveInfo) v.getTag();
329 if (mWallpapers.contains(info)) {
330 // Start the wallpaper picker
331 animateClickFeedback(v, new Runnable() {
332 @Override
333 public void run() {
334 // add the shortcut
335 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
336 ComponentName name = new ComponentName(info.activityInfo.packageName,
337 info.activityInfo.name);
338 createWallpapersIntent.setComponent(name);
339 mLauncher.processWallpaper(createWallpapersIntent);
340 }
341 });
342 }
Winson Chung46af2e82011-05-09 16:00:53 -0700343 } else {
344 // Add the widget to the current workspace screen
345 Workspace w = mLauncher.getWorkspace();
346 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
347 final CellLayout cl = (CellLayout) w.getChildAt(currentWorkspaceScreen);
348 final View dragView = v.findViewById(R.id.widget_preview);
349 final ItemInfo itemInfo = (ItemInfo) v.getTag();
350 animateClickFeedback(v, new Runnable() {
351 @Override
352 public void run() {
353 cl.calculateSpans(itemInfo);
354 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700355 if (LauncherApplication.isScreenLarge()) {
Winson Chung46af2e82011-05-09 16:00:53 -0700356 animateItemOntoScreen(dragView, cl, itemInfo);
357 } else {
358 mLauncher.addExternalItemToScreen(itemInfo, cl);
359 itemInfo.dropPos = null;
360 }
361
362 // Hide the pane so we can see the workspace we dropped on
363 mLauncher.showWorkspace(true);
364 } else {
365 mLauncher.showOutOfSpaceMessage();
366 }
367 }
368 });
369 }
Winson Chung4b576be2011-04-27 17:40:20 -0700370 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700371 }
372
373 /*
374 * PagedViewWithDraggableItems implementation
375 */
376 @Override
377 protected void determineDraggingStart(android.view.MotionEvent ev) {
378 // Disable dragging by pulling an app down for now.
379 }
Winson Chung4b576be2011-04-27 17:40:20 -0700380 private void beginDraggingApplication(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700381 // Make a copy of the ApplicationInfo
382 ApplicationInfo appInfo = new ApplicationInfo((ApplicationInfo) v.getTag());
383
384 // Show the uninstall button if the app is uninstallable.
385 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
386 DeleteZone allAppsDeleteZone = (DeleteZone)
387 mLauncher.findViewById(R.id.all_apps_delete_zone);
388 allAppsDeleteZone.setDragAndDropEnabled(true);
389
390 if ((appInfo.flags & ApplicationInfo.UPDATED_SYSTEM_APP_FLAG) != 0) {
391 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps_system_app);
392 } else {
393 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps);
394 }
395 }
396
397 // Show the info button
398 ApplicationInfoDropTarget allAppsInfoButton =
399 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
400 allAppsInfoButton.setDragAndDropEnabled(true);
401
402 // Compose the drag image (top compound drawable, index is 1)
403 final TextView tv = (TextView) v;
404 final Drawable icon = tv.getCompoundDrawables()[1];
405 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
406 Bitmap.Config.ARGB_8888);
407 Canvas c = new Canvas(b);
408 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop());
409 icon.draw(c);
410
411 // Compose the visible rect of the drag image
412 Rect dragRect = null;
413 if (v instanceof TextView) {
414 int iconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size);
415 int top = v.getPaddingTop();
416 int left = (b.getWidth() - iconSize) / 2;
417 int right = left + iconSize;
418 int bottom = top + iconSize;
419 dragRect = new Rect(left, top, right, bottom);
420 }
421
422 // Start the drag
423 mLauncher.lockScreenOrientation();
424 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
425 mDragController.startDrag(v, b, this, appInfo, DragController.DRAG_ACTION_COPY, dragRect);
426 b.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700427 }
428 private void beginDraggingWidget(View v) {
429 // Get the widget preview as the drag representation
430 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700431 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700432
433 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700434 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700435 Drawable preview = image.getDrawable();
436 int w = preview.getIntrinsicWidth();
437 int h = preview.getIntrinsicHeight();
Winson Chung1ed747a2011-05-03 16:18:34 -0700438 if (createItemInfo instanceof PendingAddWidgetInfo) {
439 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
440 int[] spanXY = CellLayout.rectToCell(getResources(),
441 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
442 createItemInfo.spanX = spanXY[0];
443 createItemInfo.spanY = spanXY[1];
444
445 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
446 renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1);
447 } else {
448 // Workaround for the fact that we don't keep the original ResolveInfo associated with
449 // the shortcut around. To get the icon, we just render the preview image (which has
450 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
451 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
452 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
453 Canvas c = new Canvas(b);
454 preview.draw(c);
455 createItemInfo.spanX = createItemInfo.spanY = 1;
456 }
Winson Chung4b576be2011-04-27 17:40:20 -0700457
458 // Start the drag
Winson Chung4b576be2011-04-27 17:40:20 -0700459 mLauncher.lockScreenOrientation();
Winson Chung1ed747a2011-05-03 16:18:34 -0700460 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
461 createItemInfo.spanY, b);
462 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700463 DragController.DRAG_ACTION_COPY, null);
464 b.recycle();
465 }
466 @Override
467 protected boolean beginDragging(View v) {
468 if (!super.beginDragging(v)) return false;
469
Winson Chungfc79c802011-05-02 13:35:34 -0700470 // Hide the pane so that the user can drop onto the workspace, we must do this first,
471 // due to how the drop target layout is computed when we start dragging to the workspace.
472 mLauncher.showWorkspace(true);
473
Winson Chung4b576be2011-04-27 17:40:20 -0700474 if (v instanceof PagedViewIcon) {
475 beginDraggingApplication(v);
476 } else if (v instanceof PagedViewWidget) {
477 beginDraggingWidget(v);
478 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700479
Winson Chung785d2eb2011-04-14 16:08:02 -0700480 return true;
481 }
482 private void endDragging(boolean success) {
483 post(new Runnable() {
484 // Once the drag operation has fully completed, hence the post, we want to disable the
485 // deleteZone and the appInfoButton in all apps, and re-enable the instance which
486 // live in the workspace
487 public void run() {
488 // if onDestroy was called on Launcher, we might have already deleted the
489 // all apps delete zone / info button, so check if they are null
490 DeleteZone allAppsDeleteZone =
491 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone);
492 ApplicationInfoDropTarget allAppsInfoButton =
493 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
494
495 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false);
496 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false);
497 }
498 });
499 mLauncher.getWorkspace().onDragStopped(success);
500 mLauncher.unlockScreenOrientation();
501 }
502
503 /*
504 * DragSource implementation
505 */
506 @Override
507 public void onDragViewVisible() {}
508 @Override
509 public void onDropCompleted(View target, Object dragInfo, boolean success) {
510 endDragging(success);
Winson Chungfc79c802011-05-02 13:35:34 -0700511
512 // Display an error message if the drag failed due to there not being enough space on the
513 // target layout we were dropping on.
514 if (!success) {
515 boolean showOutOfSpaceMessage = false;
516 if (target instanceof Workspace) {
517 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
518 Workspace workspace = (Workspace) target;
519 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
520 ItemInfo itemInfo = (ItemInfo) dragInfo;
521 if (layout != null) {
522 layout.calculateSpans(itemInfo);
523 showOutOfSpaceMessage =
524 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
525 }
526 }
527 // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later.
528 if (showOutOfSpaceMessage) {
529 mLauncher.showOutOfSpaceMessage();
530 }
531 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700532 }
533
534 public void setContentType(ContentType type) {
535 mContentType = type;
536 setCurrentPage(0);
537 invalidatePageData();
538 }
539
540 /*
541 * Apps PagedView implementation
542 */
Winson Chung63257c12011-05-05 17:06:13 -0700543 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
544 int childCount = layout.getChildCount();
545 for (int i = 0; i < childCount; ++i) {
546 layout.getChildAt(i).setVisibility(visibility);
547 }
548 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700549 private void setupPage(PagedViewCellLayout layout) {
550 layout.setCellCount(mCellCountX, mCellCountY);
551 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
552 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
553 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
554
Winson Chung63257c12011-05-05 17:06:13 -0700555 // Note: We force a measure here to get around the fact that when we do layout calculations
556 // immediately after syncing, we don't have a proper width. That said, we already know the
557 // expected page width, so we can actually optimize by hiding all the TextView-based
558 // children that are expensive to measure, and let that happen naturally later.
559 setVisibilityOnChildren(layout, View.GONE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700560 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
561 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700562 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700563 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700564 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700565 }
566 public void syncAppsPages() {
567 // Ensure that we have the right number of pages
568 Context context = getContext();
569 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
570 for (int i = 0; i < numPages; ++i) {
571 PagedViewCellLayout layout = new PagedViewCellLayout(context);
572 setupPage(layout);
573 addView(layout);
574 }
575 }
576 public void syncAppsPageItems(int page) {
577 // ensure that we have the right number of items on the pages
578 int numPages = getPageCount();
579 int numCells = mCellCountX * mCellCountY;
580 int startIndex = page * numCells;
581 int endIndex = Math.min(startIndex + numCells, mApps.size());
582 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
583 layout.removeAllViewsOnPage();
584 for (int i = startIndex; i < endIndex; ++i) {
585 ApplicationInfo info = mApps.get(i);
586 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
587 R.layout.apps_customize_application, layout, false);
Michael Jurkab9b8ce92011-05-05 15:05:07 -0700588 icon.applyFromApplicationInfo(
589 info, mPageViewIconCache, true, isHardwareAccelerated() && (numPages > 1));
Winson Chung785d2eb2011-04-14 16:08:02 -0700590 icon.setOnClickListener(this);
591 icon.setOnLongClickListener(this);
592 icon.setOnTouchListener(this);
593
594 int index = i - startIndex;
595 int x = index % mCellCountX;
596 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700597 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung785d2eb2011-04-14 16:08:02 -0700598 }
599 }
600 /*
601 * Widgets PagedView implementation
602 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700603 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700604 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
605 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700606
607 // Note: We force a measure here to get around the fact that when we do layout calculations
608 // immediately after syncing, we don't have a proper width. That said, we already know the
609 // expected page width, so we can actually optimize by hiding all the TextView-based
610 // children that are expensive to measure, and let that happen naturally later.
611 setVisibilityOnChildren(layout, View.GONE);
612 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
613 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700614 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700615 layout.measure(widthSpec, heightSpec);
616 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700617 }
618 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
619 float scaleX, float scaleY) {
620 Canvas c = new Canvas();
621 if (bitmap != null) c.setBitmap(bitmap);
622 c.save();
623 c.scale(scaleX, scaleY);
624 Rect oldBounds = d.copyBounds();
625 d.setBounds(x, y, x + w, y + h);
626 d.draw(c);
627 d.setBounds(oldBounds); // Restore the bounds
628 c.restore();
629 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700630 private FastBitmapDrawable getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
631 // Return the cached version if necessary
632 Bitmap cachedBitmap = mWidgetPreviewCache.get(info);
633 if (cachedBitmap != null) {
634 return new FastBitmapDrawable(cachedBitmap);
635 }
636
637 Resources resources = mLauncher.getResources();
638 int iconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
639 // We only need to make it wide enough so as not allow the preview to be scaled
640 int expectedWidth = cellWidth;
641 int expectedHeight = mWidgetPreviewIconPaddedDimension;
642 int offset = (int) (iconSize * sWidgetPreviewIconPaddingPercentage);
643
644 // Render the icon
645 Bitmap preview = Bitmap.createBitmap(expectedWidth, expectedHeight, Config.ARGB_8888);
Winson Chung4dbea792011-05-05 14:21:32 -0700646 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chung1ed747a2011-05-03 16:18:34 -0700647 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0,
648 mWidgetPreviewIconPaddedDimension, mWidgetPreviewIconPaddedDimension, 1f, 1f);
649 renderDrawableToBitmap(icon, preview, offset, offset, iconSize, iconSize, 1f, 1f);
650 FastBitmapDrawable iconDrawable = new FastBitmapDrawable(preview);
651 iconDrawable.setBounds(0, 0, expectedWidth, expectedHeight);
652 mWidgetPreviewCache.put(info, preview);
653 return iconDrawable;
654 }
Winson Chung4b576be2011-04-27 17:40:20 -0700655 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info, int cellHSpan,
656 int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700657 // Return the cached version if necessary
658 Bitmap cachedBitmap = mWidgetPreviewCache.get(info);
659 if (cachedBitmap != null) {
660 return new FastBitmapDrawable(cachedBitmap);
661 }
662
Winson Chung4b576be2011-04-27 17:40:20 -0700663 // Calculate the size of the drawable
664 cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan));
665 cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan));
666 int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
667 int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
668
669 // Scale down the bitmap to fit the space
670 float widgetPreviewScale = (float) cellWidth / expectedWidth;
671 expectedWidth = (int) (widgetPreviewScale * expectedWidth);
672 expectedHeight = (int) (widgetPreviewScale * expectedHeight);
673
674 // Load the preview image if possible
675 String packageName = info.provider.getPackageName();
676 Drawable drawable = null;
677 FastBitmapDrawable newDrawable = null;
678 if (info.previewImage != 0) {
679 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
680 if (drawable == null) {
681 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
682 + " for provider: " + info.provider);
683 } else {
684 // Scale down the preview to the dimensions we want
685 int imageWidth = drawable.getIntrinsicWidth();
686 int imageHeight = drawable.getIntrinsicHeight();
687 float aspect = (float) imageWidth / imageHeight;
688 int newWidth = imageWidth;
689 int newHeight = imageHeight;
690 if (aspect > 1f) {
691 newWidth = expectedWidth;
692 newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth));
693 } else {
694 newHeight = expectedHeight;
695 newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight));
696 }
697
698 Bitmap preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
699 renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f);
700 newDrawable = new FastBitmapDrawable(preview);
701 newDrawable.setBounds(0, 0, newWidth, newHeight);
Winson Chung1ed747a2011-05-03 16:18:34 -0700702 mWidgetPreviewCache.put(info, preview);
Winson Chung4b576be2011-04-27 17:40:20 -0700703 }
704 }
705
706 // Generate a preview image if we couldn't load one
707 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700708 Resources resources = mLauncher.getResources();
709 int iconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
710
711 // Specify the dimensions of the bitmap
712 if (info.minWidth >= info.minHeight) {
713 expectedWidth = cellWidth;
714 expectedHeight = mWidgetPreviewIconPaddedDimension;
715 } else {
716 // Note that in vertical widgets, we might not have enough space due to the text
717 // label, so be conservative and use the width as a height bound
718 expectedWidth = mWidgetPreviewIconPaddedDimension;
719 expectedHeight = cellWidth;
720 }
Winson Chung4b576be2011-04-27 17:40:20 -0700721
722 Bitmap preview = Bitmap.createBitmap(expectedWidth, expectedHeight, Config.ARGB_8888);
Winson Chung1ed747a2011-05-03 16:18:34 -0700723 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth,
724 expectedHeight, 1f,1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700725
726 // Draw the icon in the top left corner
727 try {
728 Drawable icon = null;
729 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
730 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
731
Winson Chung1ed747a2011-05-03 16:18:34 -0700732 int offset = (int) (iconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung4b576be2011-04-27 17:40:20 -0700733 renderDrawableToBitmap(icon, preview, offset, offset, iconSize, iconSize, 1f, 1f);
734 } catch (Resources.NotFoundException e) {}
735
736 newDrawable = new FastBitmapDrawable(preview);
737 newDrawable.setBounds(0, 0, expectedWidth, expectedHeight);
Winson Chung1ed747a2011-05-03 16:18:34 -0700738 mWidgetPreviewCache.put(info, preview);
Winson Chung4b576be2011-04-27 17:40:20 -0700739 }
740 return newDrawable;
Winson Chung785d2eb2011-04-14 16:08:02 -0700741 }
742 public void syncWidgetPages() {
743 // Ensure that we have the right number of pages
744 Context context = getContext();
Winson Chung4b576be2011-04-27 17:40:20 -0700745 int numWidgetsPerPage = mWidgetCountX * mWidgetCountY;
746 int numPages = (int) Math.ceil(mWidgets.size() / (float) numWidgetsPerPage);
Winson Chung785d2eb2011-04-14 16:08:02 -0700747 for (int i = 0; i < numPages; ++i) {
Winson Chung4e6a9762011-05-09 11:56:34 -0700748 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
749 mWidgetCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -0700750 setupPage(layout);
751 addView(layout);
752 }
753 }
754 public void syncWidgetPageItems(int page) {
Winson Chung4e6a9762011-05-09 11:56:34 -0700755 PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(page);
Winson Chung4b576be2011-04-27 17:40:20 -0700756 layout.removeAllViews();
Winson Chung785d2eb2011-04-14 16:08:02 -0700757
Winson Chung4b576be2011-04-27 17:40:20 -0700758 // Calculate the dimensions of each cell we are giving to each widget
Winson Chung4b576be2011-04-27 17:40:20 -0700759 int numWidgetsPerPage = mWidgetCountX * mWidgetCountY;
760 int offset = page * numWidgetsPerPage;
761 int cellWidth = ((mWidgetSpacingLayout.getContentWidth() - mPageLayoutWidthGap
Winson Chung46af2e82011-05-09 16:00:53 -0700762 - ((mWidgetCountX - 1) * mCellWidthGap)) / mWidgetCountX);
Winson Chung4b576be2011-04-27 17:40:20 -0700763 int cellHeight = ((mWidgetSpacingLayout.getContentHeight() - mPageLayoutHeightGap
Winson Chung46af2e82011-05-09 16:00:53 -0700764 - ((mWidgetCountY - 1) * mCellHeightGap)) / mWidgetCountY);
Winson Chung4b576be2011-04-27 17:40:20 -0700765 for (int i = 0; i < Math.min(numWidgetsPerPage, mWidgets.size() - offset); ++i) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700766 Object rawInfo = mWidgets.get(offset + i);
767 PendingAddItemInfo createItemInfo = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700768 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
769 R.layout.apps_customize_widget, layout, false);
Winson Chung1ed747a2011-05-03 16:18:34 -0700770 if (rawInfo instanceof AppWidgetProviderInfo) {
771 // Fill in the widget information
772 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
773 createItemInfo = new PendingAddWidgetInfo(info, null, null);
774 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
775 info.minHeight, null);
776 FastBitmapDrawable preview = getWidgetPreview(info, cellSpans[0], cellSpans[1],
777 cellWidth, cellHeight);
778 widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans, null, false);
779 widget.setTag(createItemInfo);
780 } else if (rawInfo instanceof ResolveInfo) {
781 // Fill in the shortcuts information
782 ResolveInfo info = (ResolveInfo) rawInfo;
783 createItemInfo = new PendingAddItemInfo();
784 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
785 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
786 info.activityInfo.name);
787 FastBitmapDrawable preview = getShortcutPreview(info, cellWidth, cellHeight);
788 widget.applyFromResolveInfo(mPackageManager, info, preview, null, false);
789 widget.setTag(createItemInfo);
790 }
Winson Chung4b576be2011-04-27 17:40:20 -0700791 widget.setOnClickListener(this);
792 widget.setOnLongClickListener(this);
793 widget.setOnTouchListener(this);
794
795 // Layout each widget
Winson Chung4b576be2011-04-27 17:40:20 -0700796 int ix = i % mWidgetCountX;
797 int iy = i / mWidgetCountX;
Winson Chung4e6a9762011-05-09 11:56:34 -0700798 PagedViewGridLayout.LayoutParams lp = new PagedViewGridLayout.LayoutParams(cellWidth,
799 cellHeight);
Winson Chung46af2e82011-05-09 16:00:53 -0700800 lp.leftMargin = (ix * cellWidth) + (ix * mCellWidthGap);
801 lp.topMargin = (iy * cellHeight) + (iy * mCellHeightGap);
Winson Chung4e6a9762011-05-09 11:56:34 -0700802 layout.addView(widget, lp);
Winson Chung785d2eb2011-04-14 16:08:02 -0700803 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700804 }
Winson Chung46af2e82011-05-09 16:00:53 -0700805
806 /*
807 * This method fetches an xml file specified in the manifest identified by
808 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
809 * an image which will be used as the wallpaper preview for an activity
810 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
811 * in the customize drawer.
812 */
813 private Drawable parseWallpaperPreviewXml(ResolveInfo ri) {
814 ActivityInfo activityInfo = ri.activityInfo;
815 XmlResourceParser parser = null;
816 ComponentName component = new ComponentName(ri.activityInfo.packageName,
817 ri.activityInfo.name);
818 try {
819 parser = activityInfo.loadXmlMetaData(mPackageManager,
820 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
821 if (parser == null) {
822 Slog.w(LOG_TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA
823 + " meta-data for " + "wallpaper provider '" + component + '\'');
824 return null;
825 }
826
827 AttributeSet attrs = Xml.asAttributeSet(parser);
828
829 int type;
830 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
831 && type != XmlPullParser.START_TAG) {
832 // drain whitespace, comments, etc.
833 }
834
835 String nodeName = parser.getName();
836 if (!"wallpaper-preview".equals(nodeName)) {
837 Slog.w(LOG_TAG, "Meta-data does not start with wallpaper-preview tag for "
838 + "wallpaper provider '" + component + '\'');
839 return null;
840 }
841
842 // If metaData was null, we would have returned earlier when getting
843 // the parser No need to do the check here
844 Resources res = mPackageManager.getResourcesForApplication(
845 activityInfo.applicationInfo);
846
847 TypedArray sa = res.obtainAttributes(attrs,
848 com.android.internal.R.styleable.WallpaperPreviewInfo);
849
850 TypedValue value = sa.peekValue(
851 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
852 if (value == null) return null;
853
854 return res.getDrawable(value.resourceId);
855 } catch (Exception e) {
856 Slog.w(LOG_TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
857 return null;
858 } finally {
859 if (parser != null) parser.close();
860 }
861 }
862 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info, int cellWidth, int cellHeight){
863 // Return the cached version if necessary
864 Bitmap cachedBitmap = mWidgetPreviewCache.get(info);
865 if (cachedBitmap != null) {
866 return new FastBitmapDrawable(cachedBitmap);
867 }
868
869 // Get the preview
870 Resources resources = getContext().getResources();
871 Drawable wallpaperPreview = parseWallpaperPreviewXml(info);
872 Drawable wallpaperIcon = null;
873 int expectedWidth;
874 int expectedHeight;
875 if (wallpaperPreview != null) {
876 expectedWidth = wallpaperPreview.getIntrinsicWidth();
877 expectedHeight = wallpaperPreview.getIntrinsicHeight();
878 } else {
879 wallpaperPreview = mDefaultWidgetBackground;
880 expectedWidth = expectedHeight = Math.min(cellWidth, cellHeight);
881
882 // Draw the icon in the top left corner
883 String packageName = info.activityInfo.packageName;
884 try {
885 if (info.icon > 0) {
886 wallpaperIcon = mPackageManager.getDrawable(packageName, info.icon, null);
887 }
888 if (wallpaperIcon == null) {
889 wallpaperIcon = resources.getDrawable(R.drawable.ic_launcher_application);
890 }
891 } catch (Resources.NotFoundException e) {}
892 }
893
894 // Create the bitmap
895 Bitmap preview = Bitmap.createBitmap(expectedWidth, expectedHeight, Config.ARGB_8888);
896 renderDrawableToBitmap(wallpaperPreview, preview, 0, 0, expectedWidth, expectedHeight,
897 1f, 1f);
898 if (wallpaperIcon != null) {
899 int iconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
900 int offset = (int) (iconSize * sWidgetPreviewIconPaddingPercentage);
901 renderDrawableToBitmap(wallpaperIcon, preview, offset, offset, iconSize, iconSize,
902 1f, 1f);
903 }
904
905 FastBitmapDrawable previewDrawable = new FastBitmapDrawable(preview);
906 previewDrawable.setBounds(0, 0, expectedWidth, expectedHeight);
907 mWidgetPreviewCache.put(info, preview);
908 return previewDrawable;
909 }
910 /*
911 * Wallpapers PagedView implementation
912 */
913 public void syncWallpaperPages() {
914 // Ensure that we have the right number of pages
915 Context context = getContext();
916 int numWidgetsPerPage = mWallpaperCountX * mWallpaperCountY;
917 int numPages = (int) Math.ceil(mWallpapers.size() / (float) numWidgetsPerPage);
918 for (int i = 0; i < numPages; ++i) {
919 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWallpaperCountX,
920 mWallpaperCountY);
921 setupPage(layout);
922 addView(layout);
923 }
924 }
925 public void syncWallpaperPageItems(int page) {
926 PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(page);
927 layout.removeAllViews();
928
929 // Calculate the dimensions of each cell we are giving to each widget
930 int numWidgetsPerPage = mWallpaperCountX * mWallpaperCountY;
931 int offset = page * numWidgetsPerPage;
932 int cellWidth = ((mWidgetSpacingLayout.getContentWidth() - mPageLayoutWidthGap
933 - ((mWallpaperCountX - 1) * mCellWidthGap)) / mWallpaperCountX);
934 int cellHeight = ((mWidgetSpacingLayout.getContentHeight() - mPageLayoutHeightGap
935 - ((mWallpaperCountY - 1) * mCellHeightGap)) / mWallpaperCountY);
936 for (int i = 0; i < Math.min(numWidgetsPerPage, mWallpapers.size() - offset); ++i) {
937 ResolveInfo info = mWallpapers.get(offset + i);
938 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
939 R.layout.apps_customize_wallpaper, layout, false);
940
941 // Fill in the shortcuts information
942 FastBitmapDrawable preview = getWallpaperPreview(info, cellWidth, cellHeight);
943 widget.applyFromResolveInfo(mPackageManager, info, preview, null, false);
944 widget.setTag(info);
945 widget.setOnClickListener(this);
946 widget.setOnLongClickListener(this);
947 widget.setOnTouchListener(this);
948
949 // Layout each widget
950 int ix = i % mWallpaperCountX;
951 int iy = i / mWallpaperCountX;
952 PagedViewGridLayout.LayoutParams lp = new PagedViewGridLayout.LayoutParams(cellWidth,
953 cellHeight);
954 lp.leftMargin = (ix * cellWidth) + (ix * mCellWidthGap);
955 lp.topMargin = (iy * cellHeight) + (iy * mCellHeightGap);
956 layout.addView(widget, lp);
957 }
958 }
959
Winson Chung785d2eb2011-04-14 16:08:02 -0700960 @Override
961 public void syncPages() {
962 removeAllViews();
963 switch (mContentType) {
964 case Applications:
965 syncAppsPages();
966 break;
967 case Widgets:
968 syncWidgetPages();
969 break;
Winson Chung46af2e82011-05-09 16:00:53 -0700970 case Wallpapers:
971 syncWallpaperPages();
972 break;
Winson Chung785d2eb2011-04-14 16:08:02 -0700973 }
974 }
975 @Override
976 public void syncPageItems(int page) {
977 switch (mContentType) {
978 case Applications:
979 syncAppsPageItems(page);
980 break;
981 case Widgets:
982 syncWidgetPageItems(page);
983 break;
Winson Chung46af2e82011-05-09 16:00:53 -0700984 case Wallpapers:
985 syncWallpaperPageItems(page);
986 break;
Winson Chung785d2eb2011-04-14 16:08:02 -0700987 }
988 }
989
990 /**
991 * Used by the parent to get the content width to set the tab bar to
992 * @return
993 */
994 public int getPageContentWidth() {
995 return mContentWidth;
996 }
997
998 /*
999 * AllAppsView implementation
1000 */
1001 @Override
1002 public void setup(Launcher launcher, DragController dragController) {
1003 mLauncher = launcher;
1004 mDragController = dragController;
1005 }
1006 @Override
1007 public void zoom(float zoom, boolean animate) {
1008 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1009 }
1010 @Override
1011 public boolean isVisible() {
1012 return (getVisibility() == VISIBLE);
1013 }
1014 @Override
1015 public boolean isAnimating() {
1016 return false;
1017 }
1018 @Override
1019 public void setApps(ArrayList<ApplicationInfo> list) {
1020 mApps = list;
1021 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
1022 invalidatePageData();
1023 }
1024 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1025 // We add it in place, in alphabetical order
1026 int count = list.size();
1027 for (int i = 0; i < count; ++i) {
1028 ApplicationInfo info = list.get(i);
1029 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1030 if (index < 0) {
1031 mApps.add(-(index + 1), info);
1032 }
1033 }
1034 }
1035 @Override
1036 public void addApps(ArrayList<ApplicationInfo> list) {
1037 addAppsWithoutInvalidate(list);
1038 invalidatePageData();
1039 }
1040 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1041 ComponentName removeComponent = item.intent.getComponent();
1042 int length = list.size();
1043 for (int i = 0; i < length; ++i) {
1044 ApplicationInfo info = list.get(i);
1045 if (info.intent.getComponent().equals(removeComponent)) {
1046 return i;
1047 }
1048 }
1049 return -1;
1050 }
1051 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1052 // loop through all the apps and remove apps that have the same component
1053 int length = list.size();
1054 for (int i = 0; i < length; ++i) {
1055 ApplicationInfo info = list.get(i);
1056 int removeIndex = findAppByComponent(mApps, info);
1057 if (removeIndex > -1) {
1058 mApps.remove(removeIndex);
1059 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
1060 }
1061 }
1062 }
1063 @Override
1064 public void removeApps(ArrayList<ApplicationInfo> list) {
1065 removeAppsWithoutInvalidate(list);
1066 invalidatePageData();
1067 }
1068 @Override
1069 public void updateApps(ArrayList<ApplicationInfo> list) {
1070 // We remove and re-add the updated applications list because it's properties may have
1071 // changed (ie. the title), and this will ensure that the items will be in their proper
1072 // place in the list.
1073 removeAppsWithoutInvalidate(list);
1074 addAppsWithoutInvalidate(list);
1075 invalidatePageData();
1076 }
1077 @Override
1078 public void reset() {
Winson Chungfc79c802011-05-02 13:35:34 -07001079 if (mContentType != ContentType.Applications) {
1080 // Reset to the first page of the Apps pane
1081 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
1082 mLauncher.findViewById(R.id.apps_customize_pane);
1083 tabs.setCurrentTabByTag(tabs.getTabTagForContentType(ContentType.Applications));
1084 } else {
1085 setCurrentPage(0);
1086 invalidatePageData();
1087 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001088 }
1089 @Override
1090 public void dumpState() {
1091 // TODO: Dump information related to current list of Applications, Widgets, etc.
1092 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1093 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1094 }
1095 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chung1ed747a2011-05-03 16:18:34 -07001096 List<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001097 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001098 for (Object i: list) {
1099 if (i instanceof AppWidgetProviderInfo) {
1100 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1101 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1102 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1103 + " initialLayout=" + info.initialLayout
1104 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1105 } else if (i instanceof ResolveInfo) {
1106 ResolveInfo info = (ResolveInfo) i;
1107 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1108 + info.icon);
1109 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001110 }
1111 }
1112 @Override
1113 public void surrender() {
1114 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1115 // should stop this now.
1116 }
1117}