blob: fabd9e40b2f4f128c558233edeff7000e423def0 [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;
21import java.util.List;
22
Winson Chung46af2e82011-05-09 16:00:53 -070023import org.xmlpull.v1.XmlPullParser;
24
Winson Chung4b576be2011-04-27 17:40:20 -070025import android.animation.Animator;
26import android.animation.AnimatorListenerAdapter;
27import android.animation.ObjectAnimator;
28import android.animation.PropertyValuesHolder;
Winson Chung46af2e82011-05-09 16:00:53 -070029import android.app.WallpaperManager;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.appwidget.AppWidgetManager;
31import android.appwidget.AppWidgetProviderInfo;
32import android.content.ComponentName;
33import android.content.Context;
34import android.content.Intent;
Winson Chung46af2e82011-05-09 16:00:53 -070035import android.content.pm.ActivityInfo;
Winson Chung785d2eb2011-04-14 16:08:02 -070036import android.content.pm.PackageManager;
37import android.content.pm.ResolveInfo;
38import android.content.res.Resources;
39import android.content.res.TypedArray;
Winson Chung46af2e82011-05-09 16:00:53 -070040import android.content.res.XmlResourceParser;
Winson Chung785d2eb2011-04-14 16:08:02 -070041import android.graphics.Bitmap;
42import android.graphics.Bitmap.Config;
43import android.graphics.Canvas;
44import android.graphics.Rect;
45import android.graphics.drawable.Drawable;
46import android.util.AttributeSet;
47import android.util.Log;
Winson Chung1ed747a2011-05-03 16:18:34 -070048import android.util.LruCache;
Winson Chung46af2e82011-05-09 16:00:53 -070049import android.util.Slog;
50import android.util.TypedValue;
51import android.util.Xml;
Winson Chung785d2eb2011-04-14 16:08:02 -070052import android.view.LayoutInflater;
53import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070054import android.view.ViewGroup;
Winson Chung4b576be2011-04-27 17:40:20 -070055import android.view.animation.DecelerateInterpolator;
56import android.view.animation.LinearInterpolator;
Winson Chung785d2eb2011-04-14 16:08:02 -070057import android.widget.ImageView;
58import android.widget.TextView;
59
60import com.android.launcher.R;
61
62public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
63 AllAppsView, View.OnClickListener, DragSource {
64 static final String LOG_TAG = "AppsCustomizePagedView";
65
66 /**
67 * The different content types that this paged view can show.
68 */
69 public enum ContentType {
70 Applications,
Winson Chung46af2e82011-05-09 16:00:53 -070071 Widgets,
72 Wallpapers
Winson Chung785d2eb2011-04-14 16:08:02 -070073 }
74
75 // Refs
76 private Launcher mLauncher;
77 private DragController mDragController;
78 private final LayoutInflater mLayoutInflater;
79 private final PackageManager mPackageManager;
80
81 // Content
82 private ContentType mContentType;
83 private ArrayList<ApplicationInfo> mApps;
Winson Chung1ed747a2011-05-03 16:18:34 -070084 private List<Object> mWidgets;
Winson Chung46af2e82011-05-09 16:00:53 -070085 private List<ResolveInfo> mWallpapers;
Winson Chung1ed747a2011-05-03 16:18:34 -070086
87 // Caching
88 private Drawable mDefaultWidgetBackground;
89 private final int sWidgetPreviewCacheSize = 1 * 1024 * 1024; // 1 MiB
90 private LruCache<Object, Bitmap> mWidgetPreviewCache;
Winson Chung4dbea792011-05-05 14:21:32 -070091 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -070092
93 // Dimens
94 private int mContentWidth;
Winson Chung4b576be2011-04-27 17:40:20 -070095 private int mMaxWidgetSpan, mMinWidgetSpan;
Winson Chung46af2e82011-05-09 16:00:53 -070096 private int mCellWidthGap, mCellHeightGap;
Winson Chung4b576be2011-04-27 17:40:20 -070097 private int mWidgetCountX, mWidgetCountY;
Winson Chung46af2e82011-05-09 16:00:53 -070098 private int mWallpaperCountX, mWallpaperCountY;
Winson Chung1ed747a2011-05-03 16:18:34 -070099 private final int mWidgetPreviewIconPaddedDimension;
100 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700101 private PagedViewCellLayout mWidgetSpacingLayout;
102
Winson Chung4b576be2011-04-27 17:40:20 -0700103 // Animations
104 private final float ANIMATION_SCALE = 0.5f;
105 private final int TRANSLATE_ANIM_DURATION = 400;
106 private final int DROP_ANIM_DURATION = 200;
107
Winson Chung785d2eb2011-04-14 16:08:02 -0700108 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
109 super(context, attrs);
110 mLayoutInflater = LayoutInflater.from(context);
111 mPackageManager = context.getPackageManager();
112 mContentType = ContentType.Applications;
113 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700114 mWidgets = new ArrayList<Object>();
Winson Chung46af2e82011-05-09 16:00:53 -0700115 mWallpapers = new ArrayList<ResolveInfo>();
Winson Chung4dbea792011-05-05 14:21:32 -0700116 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chung1ed747a2011-05-03 16:18:34 -0700117 mWidgetPreviewCache = new LruCache<Object, Bitmap>(sWidgetPreviewCacheSize) {
118 protected int sizeOf(Object key, Bitmap value) {
119 return value.getByteCount();
120 }
121 };
122
123 // Save the default widget preview background
124 Resources resources = context.getResources();
125 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview);
Winson Chung785d2eb2011-04-14 16:08:02 -0700126
127 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0);
128 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
129 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
130 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700131 a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chung46af2e82011-05-09 16:00:53 -0700132 mCellWidthGap =
Winson Chung4b576be2011-04-27 17:40:20 -0700133 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 10);
Winson Chung46af2e82011-05-09 16:00:53 -0700134 mCellHeightGap =
Winson Chung4b576be2011-04-27 17:40:20 -0700135 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 10);
136 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
137 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung46af2e82011-05-09 16:00:53 -0700138 mWallpaperCountX = a.getInt(R.styleable.AppsCustomizePagedView_wallpaperCountX, 2);
139 mWallpaperCountY = a.getInt(R.styleable.AppsCustomizePagedView_wallpaperCountY, 2);
Winson Chung4b576be2011-04-27 17:40:20 -0700140 a.recycle();
Winson Chung785d2eb2011-04-14 16:08:02 -0700141
Winson Chung4b576be2011-04-27 17:40:20 -0700142 // Create a dummy page that we can use to approximate the cell dimensions of widgets and
143 // the content width (to be used by our parent)
Winson Chung785d2eb2011-04-14 16:08:02 -0700144 mWidgetSpacingLayout = new PagedViewCellLayout(context);
Winson Chung4b576be2011-04-27 17:40:20 -0700145 setupPage(mWidgetSpacingLayout);
146 mContentWidth = mWidgetSpacingLayout.getContentWidth();
147
148 // The max widget span is the length N, such that NxN is the largest bounds that the widget
149 // preview can be before applying the widget scaling
150 mMinWidgetSpan = 1;
151 mMaxWidgetSpan = 3;
Winson Chung1ed747a2011-05-03 16:18:34 -0700152
153 // The padding on the non-matched dimension for the default widget preview icons
154 // (top + bottom)
155 int iconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
156 mWidgetPreviewIconPaddedDimension =
157 (int) (iconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Winson Chung785d2eb2011-04-14 16:08:02 -0700158 }
159
160 @Override
161 protected void init() {
162 super.init();
163 mCenterPagesVertically = false;
164
165 Context context = getContext();
166 Resources r = context.getResources();
167 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
168 }
169
170 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700171 // Get the list of widgets and shortcuts
172 mWidgets.clear();
173 mWidgets.addAll(AppWidgetManager.getInstance(mLauncher).getInstalledProviders());
Winson Chung785d2eb2011-04-14 16:08:02 -0700174 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
Winson Chung1ed747a2011-05-03 16:18:34 -0700175 mWidgets.addAll(mPackageManager.queryIntentActivities(shortcutsIntent, 0));
176 Collections.sort(mWidgets,
177 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung46af2e82011-05-09 16:00:53 -0700178
179 // Get the list of wallpapers
180 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
181 mWallpapers = mPackageManager.queryIntentActivities(wallpapersIntent,
182 PackageManager.GET_META_DATA);
183 Collections.sort(mWallpapers,
184 new LauncherModel.ShortcutNameComparator(mPackageManager));
Winson Chung785d2eb2011-04-14 16:08:02 -0700185 }
186
Winson Chung4b576be2011-04-27 17:40:20 -0700187 /**
188 * Animates the given item onto the center of a home screen, and then scales the item to
189 * look as though it's disappearing onto that screen.
190 */
191 private void animateItemOntoScreen(View dragView,
192 final CellLayout layout, final ItemInfo info) {
193 // On the phone, we only want to fade the widget preview out
194 float[] position = new float[2];
195 position[0] = layout.getWidth() / 2;
196 position[1] = layout.getHeight() / 2;
197
198 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, position);
199
200 int dragViewWidth = dragView.getMeasuredWidth();
201 int dragViewHeight = dragView.getMeasuredHeight();
202 float heightOffset = 0;
203 float widthOffset = 0;
204
205 if (dragView instanceof ImageView) {
206 Drawable d = ((ImageView) dragView).getDrawable();
207 int width = d.getIntrinsicWidth();
208 int height = d.getIntrinsicHeight();
209
210 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
211 float f = (dragViewWidth / (width * 1.0f));
212 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
213 } else {
214 float f = (dragViewHeight / (height * 1.0f));
215 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
216 }
217 }
218 final float toX = position[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
219 final float toY = position[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
220
221 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
222 final View dragCopy = dragLayer.createDragView(dragView);
223 dragCopy.setAlpha(1.0f);
224
225 // Translate the item to the center of the appropriate home screen
226 animateIntoPosition(dragCopy, toX, toY, null);
227
228 // The drop-onto-screen animation begins a bit later, but ends at the same time.
229 final int startDelay = TRANSLATE_ANIM_DURATION - DROP_ANIM_DURATION;
230
231 // Scale down the icon and fade out the alpha
232 animateDropOntoScreen(dragCopy, info, DROP_ANIM_DURATION, startDelay);
233 }
234
235 /**
236 * Animation which scales the view down and animates its alpha, making it appear to disappear
237 * onto a home screen.
238 */
239 private void animateDropOntoScreen(
240 final View view, final ItemInfo info, int duration, int delay) {
241 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
242 final CellLayout layout = mLauncher.getWorkspace().getCurrentDropLayout();
243
244 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
245 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
246 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
247 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
248 anim.setInterpolator(new LinearInterpolator());
249 if (delay > 0) {
250 anim.setStartDelay(delay);
251 }
252 anim.setDuration(duration);
253 anim.addListener(new AnimatorListenerAdapter() {
254 public void onAnimationEnd(Animator animation) {
255 dragLayer.removeView(view);
256 mLauncher.addExternalItemToScreen(info, layout);
257 info.dropPos = null;
Winson Chung785d2eb2011-04-14 16:08:02 -0700258 }
259 });
Winson Chung4b576be2011-04-27 17:40:20 -0700260 anim.start();
261 }
262
263 /**
264 * Animates the x,y position of the view, and optionally execute a Runnable on animation end.
265 */
266 private void animateIntoPosition(
267 View view, float toX, float toY, final Runnable endRunnable) {
268 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
269 PropertyValuesHolder.ofFloat("x", toX),
270 PropertyValuesHolder.ofFloat("y", toY));
271 anim.setInterpolator(new DecelerateInterpolator(2.5f));
272 anim.setDuration(TRANSLATE_ANIM_DURATION);
273 if (endRunnable != null) {
274 anim.addListener(new AnimatorListenerAdapter() {
275 @Override
276 public void onAnimationEnd(Animator animation) {
277 endRunnable.run();
278 }
279 });
280 }
281 anim.start();
282 }
283
284 @Override
285 public void onClick(View v) {
286 if (v instanceof PagedViewIcon) {
287 // Animate some feedback to the click
288 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
289 animateClickFeedback(v, new Runnable() {
290 @Override
291 public void run() {
292 mLauncher.startActivitySafely(appInfo.intent, appInfo);
293 }
294 });
295 } else if (v instanceof PagedViewWidget) {
Winson Chung59d0bb02011-05-23 11:34:11 -0700296 if (v.getTag() instanceof ResolveInfo) {
297 final ResolveInfo info = (ResolveInfo) v.getTag();
298 if (mWallpapers.contains(info)) {
299 // Start the wallpaper picker
300 animateClickFeedback(v, new Runnable() {
301 @Override
302 public void run() {
303 // add the shortcut
304 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
305 ComponentName name = new ComponentName(info.activityInfo.packageName,
306 info.activityInfo.name);
307 createWallpapersIntent.setComponent(name);
308 mLauncher.processWallpaper(createWallpapersIntent);
309 }
310 });
311 }
Winson Chung46af2e82011-05-09 16:00:53 -0700312 } else {
313 // Add the widget to the current workspace screen
314 Workspace w = mLauncher.getWorkspace();
315 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
316 final CellLayout cl = (CellLayout) w.getChildAt(currentWorkspaceScreen);
317 final View dragView = v.findViewById(R.id.widget_preview);
318 final ItemInfo itemInfo = (ItemInfo) v.getTag();
319 animateClickFeedback(v, new Runnable() {
320 @Override
321 public void run() {
322 cl.calculateSpans(itemInfo);
323 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700324 if (LauncherApplication.isScreenLarge()) {
Winson Chung46af2e82011-05-09 16:00:53 -0700325 animateItemOntoScreen(dragView, cl, itemInfo);
326 } else {
327 mLauncher.addExternalItemToScreen(itemInfo, cl);
328 itemInfo.dropPos = null;
329 }
330
331 // Hide the pane so we can see the workspace we dropped on
332 mLauncher.showWorkspace(true);
333 } else {
334 mLauncher.showOutOfSpaceMessage();
335 }
336 }
337 });
338 }
Winson Chung4b576be2011-04-27 17:40:20 -0700339 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700340 }
341
342 /*
343 * PagedViewWithDraggableItems implementation
344 */
345 @Override
346 protected void determineDraggingStart(android.view.MotionEvent ev) {
347 // Disable dragging by pulling an app down for now.
348 }
Winson Chung4b576be2011-04-27 17:40:20 -0700349 private void beginDraggingApplication(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700350 // Make a copy of the ApplicationInfo
351 ApplicationInfo appInfo = new ApplicationInfo((ApplicationInfo) v.getTag());
352
353 // Show the uninstall button if the app is uninstallable.
354 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
355 DeleteZone allAppsDeleteZone = (DeleteZone)
356 mLauncher.findViewById(R.id.all_apps_delete_zone);
357 allAppsDeleteZone.setDragAndDropEnabled(true);
358
359 if ((appInfo.flags & ApplicationInfo.UPDATED_SYSTEM_APP_FLAG) != 0) {
360 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps_system_app);
361 } else {
362 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps);
363 }
364 }
365
366 // Show the info button
367 ApplicationInfoDropTarget allAppsInfoButton =
368 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
369 allAppsInfoButton.setDragAndDropEnabled(true);
370
371 // Compose the drag image (top compound drawable, index is 1)
372 final TextView tv = (TextView) v;
373 final Drawable icon = tv.getCompoundDrawables()[1];
374 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
375 Bitmap.Config.ARGB_8888);
376 Canvas c = new Canvas(b);
377 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop());
378 icon.draw(c);
379
380 // Compose the visible rect of the drag image
381 Rect dragRect = null;
382 if (v instanceof TextView) {
383 int iconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size);
384 int top = v.getPaddingTop();
385 int left = (b.getWidth() - iconSize) / 2;
386 int right = left + iconSize;
387 int bottom = top + iconSize;
388 dragRect = new Rect(left, top, right, bottom);
389 }
390
391 // Start the drag
392 mLauncher.lockScreenOrientation();
393 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
394 mDragController.startDrag(v, b, this, appInfo, DragController.DRAG_ACTION_COPY, dragRect);
395 b.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700396 }
397 private void beginDraggingWidget(View v) {
398 // Get the widget preview as the drag representation
399 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700400 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700401
402 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700403 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700404 Drawable preview = image.getDrawable();
405 int w = preview.getIntrinsicWidth();
406 int h = preview.getIntrinsicHeight();
Winson Chung1ed747a2011-05-03 16:18:34 -0700407 if (createItemInfo instanceof PendingAddWidgetInfo) {
408 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
409 int[] spanXY = CellLayout.rectToCell(getResources(),
410 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
411 createItemInfo.spanX = spanXY[0];
412 createItemInfo.spanY = spanXY[1];
413
414 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
415 renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1);
416 } else {
417 // Workaround for the fact that we don't keep the original ResolveInfo associated with
418 // the shortcut around. To get the icon, we just render the preview image (which has
419 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
420 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
421 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
422 Canvas c = new Canvas(b);
423 preview.draw(c);
424 createItemInfo.spanX = createItemInfo.spanY = 1;
425 }
Winson Chung4b576be2011-04-27 17:40:20 -0700426
427 // Start the drag
Winson Chung4b576be2011-04-27 17:40:20 -0700428 mLauncher.lockScreenOrientation();
Winson Chung1ed747a2011-05-03 16:18:34 -0700429 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
430 createItemInfo.spanY, b);
431 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700432 DragController.DRAG_ACTION_COPY, null);
433 b.recycle();
434 }
435 @Override
436 protected boolean beginDragging(View v) {
437 if (!super.beginDragging(v)) return false;
438
Winson Chungfc79c802011-05-02 13:35:34 -0700439 // Hide the pane so that the user can drop onto the workspace, we must do this first,
440 // due to how the drop target layout is computed when we start dragging to the workspace.
441 mLauncher.showWorkspace(true);
442
Winson Chung4b576be2011-04-27 17:40:20 -0700443 if (v instanceof PagedViewIcon) {
444 beginDraggingApplication(v);
445 } else if (v instanceof PagedViewWidget) {
446 beginDraggingWidget(v);
447 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700448
Winson Chung785d2eb2011-04-14 16:08:02 -0700449 return true;
450 }
451 private void endDragging(boolean success) {
452 post(new Runnable() {
453 // Once the drag operation has fully completed, hence the post, we want to disable the
454 // deleteZone and the appInfoButton in all apps, and re-enable the instance which
455 // live in the workspace
456 public void run() {
457 // if onDestroy was called on Launcher, we might have already deleted the
458 // all apps delete zone / info button, so check if they are null
459 DeleteZone allAppsDeleteZone =
460 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone);
461 ApplicationInfoDropTarget allAppsInfoButton =
462 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
463
464 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false);
465 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false);
466 }
467 });
468 mLauncher.getWorkspace().onDragStopped(success);
469 mLauncher.unlockScreenOrientation();
470 }
471
472 /*
473 * DragSource implementation
474 */
475 @Override
476 public void onDragViewVisible() {}
477 @Override
478 public void onDropCompleted(View target, Object dragInfo, boolean success) {
479 endDragging(success);
Winson Chungfc79c802011-05-02 13:35:34 -0700480
481 // Display an error message if the drag failed due to there not being enough space on the
482 // target layout we were dropping on.
483 if (!success) {
484 boolean showOutOfSpaceMessage = false;
485 if (target instanceof Workspace) {
486 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
487 Workspace workspace = (Workspace) target;
488 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
489 ItemInfo itemInfo = (ItemInfo) dragInfo;
490 if (layout != null) {
491 layout.calculateSpans(itemInfo);
492 showOutOfSpaceMessage =
493 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
494 }
495 }
496 // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later.
497 if (showOutOfSpaceMessage) {
498 mLauncher.showOutOfSpaceMessage();
499 }
500 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700501 }
502
503 public void setContentType(ContentType type) {
504 mContentType = type;
505 setCurrentPage(0);
506 invalidatePageData();
507 }
508
509 /*
510 * Apps PagedView implementation
511 */
Winson Chung63257c12011-05-05 17:06:13 -0700512 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
513 int childCount = layout.getChildCount();
514 for (int i = 0; i < childCount; ++i) {
515 layout.getChildAt(i).setVisibility(visibility);
516 }
517 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700518 private void setupPage(PagedViewCellLayout layout) {
519 layout.setCellCount(mCellCountX, mCellCountY);
520 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
521 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
522 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
523
Winson Chung63257c12011-05-05 17:06:13 -0700524 // Note: We force a measure here to get around the fact that when we do layout calculations
525 // immediately after syncing, we don't have a proper width. That said, we already know the
526 // expected page width, so we can actually optimize by hiding all the TextView-based
527 // children that are expensive to measure, and let that happen naturally later.
528 setVisibilityOnChildren(layout, View.GONE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700529 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
530 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700531 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700532 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700533 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700534 }
535 public void syncAppsPages() {
536 // Ensure that we have the right number of pages
537 Context context = getContext();
538 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
539 for (int i = 0; i < numPages; ++i) {
540 PagedViewCellLayout layout = new PagedViewCellLayout(context);
541 setupPage(layout);
542 addView(layout);
543 }
544 }
545 public void syncAppsPageItems(int page) {
546 // ensure that we have the right number of items on the pages
547 int numPages = getPageCount();
548 int numCells = mCellCountX * mCellCountY;
549 int startIndex = page * numCells;
550 int endIndex = Math.min(startIndex + numCells, mApps.size());
551 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
552 layout.removeAllViewsOnPage();
553 for (int i = startIndex; i < endIndex; ++i) {
554 ApplicationInfo info = mApps.get(i);
555 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
556 R.layout.apps_customize_application, layout, false);
Michael Jurkab9b8ce92011-05-05 15:05:07 -0700557 icon.applyFromApplicationInfo(
558 info, mPageViewIconCache, true, isHardwareAccelerated() && (numPages > 1));
Winson Chung785d2eb2011-04-14 16:08:02 -0700559 icon.setOnClickListener(this);
560 icon.setOnLongClickListener(this);
561 icon.setOnTouchListener(this);
562
563 int index = i - startIndex;
564 int x = index % mCellCountX;
565 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700566 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung785d2eb2011-04-14 16:08:02 -0700567 }
568 }
569 /*
570 * Widgets PagedView implementation
571 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700572 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700573 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
574 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700575
576 // Note: We force a measure here to get around the fact that when we do layout calculations
577 // immediately after syncing, we don't have a proper width. That said, we already know the
578 // expected page width, so we can actually optimize by hiding all the TextView-based
579 // children that are expensive to measure, and let that happen naturally later.
580 setVisibilityOnChildren(layout, View.GONE);
581 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
582 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700583 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700584 layout.measure(widthSpec, heightSpec);
585 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700586 }
587 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
588 float scaleX, float scaleY) {
589 Canvas c = new Canvas();
590 if (bitmap != null) c.setBitmap(bitmap);
591 c.save();
592 c.scale(scaleX, scaleY);
593 Rect oldBounds = d.copyBounds();
594 d.setBounds(x, y, x + w, y + h);
595 d.draw(c);
596 d.setBounds(oldBounds); // Restore the bounds
597 c.restore();
598 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700599 private FastBitmapDrawable getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
600 // Return the cached version if necessary
601 Bitmap cachedBitmap = mWidgetPreviewCache.get(info);
602 if (cachedBitmap != null) {
603 return new FastBitmapDrawable(cachedBitmap);
604 }
605
606 Resources resources = mLauncher.getResources();
607 int iconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
608 // We only need to make it wide enough so as not allow the preview to be scaled
609 int expectedWidth = cellWidth;
610 int expectedHeight = mWidgetPreviewIconPaddedDimension;
611 int offset = (int) (iconSize * sWidgetPreviewIconPaddingPercentage);
612
613 // Render the icon
614 Bitmap preview = Bitmap.createBitmap(expectedWidth, expectedHeight, Config.ARGB_8888);
Winson Chung4dbea792011-05-05 14:21:32 -0700615 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chung1ed747a2011-05-03 16:18:34 -0700616 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0,
617 mWidgetPreviewIconPaddedDimension, mWidgetPreviewIconPaddedDimension, 1f, 1f);
618 renderDrawableToBitmap(icon, preview, offset, offset, iconSize, iconSize, 1f, 1f);
619 FastBitmapDrawable iconDrawable = new FastBitmapDrawable(preview);
620 iconDrawable.setBounds(0, 0, expectedWidth, expectedHeight);
621 mWidgetPreviewCache.put(info, preview);
622 return iconDrawable;
623 }
Winson Chung4b576be2011-04-27 17:40:20 -0700624 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info, int cellHSpan,
625 int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700626 // Return the cached version if necessary
627 Bitmap cachedBitmap = mWidgetPreviewCache.get(info);
628 if (cachedBitmap != null) {
629 return new FastBitmapDrawable(cachedBitmap);
630 }
631
Winson Chung4b576be2011-04-27 17:40:20 -0700632 // Calculate the size of the drawable
633 cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan));
634 cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan));
635 int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
636 int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
637
638 // Scale down the bitmap to fit the space
639 float widgetPreviewScale = (float) cellWidth / expectedWidth;
640 expectedWidth = (int) (widgetPreviewScale * expectedWidth);
641 expectedHeight = (int) (widgetPreviewScale * expectedHeight);
642
643 // Load the preview image if possible
644 String packageName = info.provider.getPackageName();
645 Drawable drawable = null;
646 FastBitmapDrawable newDrawable = null;
647 if (info.previewImage != 0) {
648 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
649 if (drawable == null) {
650 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
651 + " for provider: " + info.provider);
652 } else {
653 // Scale down the preview to the dimensions we want
654 int imageWidth = drawable.getIntrinsicWidth();
655 int imageHeight = drawable.getIntrinsicHeight();
656 float aspect = (float) imageWidth / imageHeight;
657 int newWidth = imageWidth;
658 int newHeight = imageHeight;
659 if (aspect > 1f) {
660 newWidth = expectedWidth;
661 newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth));
662 } else {
663 newHeight = expectedHeight;
664 newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight));
665 }
666
667 Bitmap preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
668 renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f);
669 newDrawable = new FastBitmapDrawable(preview);
670 newDrawable.setBounds(0, 0, newWidth, newHeight);
Winson Chung1ed747a2011-05-03 16:18:34 -0700671 mWidgetPreviewCache.put(info, preview);
Winson Chung4b576be2011-04-27 17:40:20 -0700672 }
673 }
674
675 // Generate a preview image if we couldn't load one
676 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700677 Resources resources = mLauncher.getResources();
678 int iconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
679
680 // Specify the dimensions of the bitmap
681 if (info.minWidth >= info.minHeight) {
682 expectedWidth = cellWidth;
683 expectedHeight = mWidgetPreviewIconPaddedDimension;
684 } else {
685 // Note that in vertical widgets, we might not have enough space due to the text
686 // label, so be conservative and use the width as a height bound
687 expectedWidth = mWidgetPreviewIconPaddedDimension;
688 expectedHeight = cellWidth;
689 }
Winson Chung4b576be2011-04-27 17:40:20 -0700690
691 Bitmap preview = Bitmap.createBitmap(expectedWidth, expectedHeight, Config.ARGB_8888);
Winson Chung1ed747a2011-05-03 16:18:34 -0700692 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth,
693 expectedHeight, 1f,1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700694
695 // Draw the icon in the top left corner
696 try {
697 Drawable icon = null;
698 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
699 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
700
Winson Chung1ed747a2011-05-03 16:18:34 -0700701 int offset = (int) (iconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung4b576be2011-04-27 17:40:20 -0700702 renderDrawableToBitmap(icon, preview, offset, offset, iconSize, iconSize, 1f, 1f);
703 } catch (Resources.NotFoundException e) {}
704
705 newDrawable = new FastBitmapDrawable(preview);
706 newDrawable.setBounds(0, 0, expectedWidth, expectedHeight);
Winson Chung1ed747a2011-05-03 16:18:34 -0700707 mWidgetPreviewCache.put(info, preview);
Winson Chung4b576be2011-04-27 17:40:20 -0700708 }
709 return newDrawable;
Winson Chung785d2eb2011-04-14 16:08:02 -0700710 }
711 public void syncWidgetPages() {
712 // Ensure that we have the right number of pages
713 Context context = getContext();
Winson Chung4b576be2011-04-27 17:40:20 -0700714 int numWidgetsPerPage = mWidgetCountX * mWidgetCountY;
715 int numPages = (int) Math.ceil(mWidgets.size() / (float) numWidgetsPerPage);
Winson Chung785d2eb2011-04-14 16:08:02 -0700716 for (int i = 0; i < numPages; ++i) {
Winson Chung4e6a9762011-05-09 11:56:34 -0700717 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
718 mWidgetCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -0700719 setupPage(layout);
720 addView(layout);
721 }
722 }
723 public void syncWidgetPageItems(int page) {
Winson Chung4e6a9762011-05-09 11:56:34 -0700724 PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(page);
Winson Chung4b576be2011-04-27 17:40:20 -0700725 layout.removeAllViews();
Winson Chung785d2eb2011-04-14 16:08:02 -0700726
Winson Chung4b576be2011-04-27 17:40:20 -0700727 // Calculate the dimensions of each cell we are giving to each widget
Winson Chung4b576be2011-04-27 17:40:20 -0700728 int numWidgetsPerPage = mWidgetCountX * mWidgetCountY;
729 int offset = page * numWidgetsPerPage;
730 int cellWidth = ((mWidgetSpacingLayout.getContentWidth() - mPageLayoutWidthGap
Winson Chung46af2e82011-05-09 16:00:53 -0700731 - ((mWidgetCountX - 1) * mCellWidthGap)) / mWidgetCountX);
Winson Chung4b576be2011-04-27 17:40:20 -0700732 int cellHeight = ((mWidgetSpacingLayout.getContentHeight() - mPageLayoutHeightGap
Winson Chung46af2e82011-05-09 16:00:53 -0700733 - ((mWidgetCountY - 1) * mCellHeightGap)) / mWidgetCountY);
Winson Chung4b576be2011-04-27 17:40:20 -0700734 for (int i = 0; i < Math.min(numWidgetsPerPage, mWidgets.size() - offset); ++i) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700735 Object rawInfo = mWidgets.get(offset + i);
736 PendingAddItemInfo createItemInfo = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700737 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
738 R.layout.apps_customize_widget, layout, false);
Winson Chung1ed747a2011-05-03 16:18:34 -0700739 if (rawInfo instanceof AppWidgetProviderInfo) {
740 // Fill in the widget information
741 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
742 createItemInfo = new PendingAddWidgetInfo(info, null, null);
743 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
744 info.minHeight, null);
745 FastBitmapDrawable preview = getWidgetPreview(info, cellSpans[0], cellSpans[1],
746 cellWidth, cellHeight);
747 widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans, null, false);
748 widget.setTag(createItemInfo);
749 } else if (rawInfo instanceof ResolveInfo) {
750 // Fill in the shortcuts information
751 ResolveInfo info = (ResolveInfo) rawInfo;
752 createItemInfo = new PendingAddItemInfo();
753 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
754 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
755 info.activityInfo.name);
756 FastBitmapDrawable preview = getShortcutPreview(info, cellWidth, cellHeight);
757 widget.applyFromResolveInfo(mPackageManager, info, preview, null, false);
758 widget.setTag(createItemInfo);
759 }
Winson Chung4b576be2011-04-27 17:40:20 -0700760 widget.setOnClickListener(this);
761 widget.setOnLongClickListener(this);
762 widget.setOnTouchListener(this);
763
764 // Layout each widget
Winson Chung4b576be2011-04-27 17:40:20 -0700765 int ix = i % mWidgetCountX;
766 int iy = i / mWidgetCountX;
Winson Chung4e6a9762011-05-09 11:56:34 -0700767 PagedViewGridLayout.LayoutParams lp = new PagedViewGridLayout.LayoutParams(cellWidth,
768 cellHeight);
Winson Chung46af2e82011-05-09 16:00:53 -0700769 lp.leftMargin = (ix * cellWidth) + (ix * mCellWidthGap);
770 lp.topMargin = (iy * cellHeight) + (iy * mCellHeightGap);
Winson Chung4e6a9762011-05-09 11:56:34 -0700771 layout.addView(widget, lp);
Winson Chung785d2eb2011-04-14 16:08:02 -0700772 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700773 }
Winson Chung46af2e82011-05-09 16:00:53 -0700774
775 /*
776 * This method fetches an xml file specified in the manifest identified by
777 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
778 * an image which will be used as the wallpaper preview for an activity
779 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
780 * in the customize drawer.
781 */
782 private Drawable parseWallpaperPreviewXml(ResolveInfo ri) {
783 ActivityInfo activityInfo = ri.activityInfo;
784 XmlResourceParser parser = null;
785 ComponentName component = new ComponentName(ri.activityInfo.packageName,
786 ri.activityInfo.name);
787 try {
788 parser = activityInfo.loadXmlMetaData(mPackageManager,
789 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
790 if (parser == null) {
791 Slog.w(LOG_TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA
792 + " meta-data for " + "wallpaper provider '" + component + '\'');
793 return null;
794 }
795
796 AttributeSet attrs = Xml.asAttributeSet(parser);
797
798 int type;
799 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
800 && type != XmlPullParser.START_TAG) {
801 // drain whitespace, comments, etc.
802 }
803
804 String nodeName = parser.getName();
805 if (!"wallpaper-preview".equals(nodeName)) {
806 Slog.w(LOG_TAG, "Meta-data does not start with wallpaper-preview tag for "
807 + "wallpaper provider '" + component + '\'');
808 return null;
809 }
810
811 // If metaData was null, we would have returned earlier when getting
812 // the parser No need to do the check here
813 Resources res = mPackageManager.getResourcesForApplication(
814 activityInfo.applicationInfo);
815
816 TypedArray sa = res.obtainAttributes(attrs,
817 com.android.internal.R.styleable.WallpaperPreviewInfo);
818
819 TypedValue value = sa.peekValue(
820 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
821 if (value == null) return null;
822
823 return res.getDrawable(value.resourceId);
824 } catch (Exception e) {
825 Slog.w(LOG_TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
826 return null;
827 } finally {
828 if (parser != null) parser.close();
829 }
830 }
831 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info, int cellWidth, int cellHeight){
832 // Return the cached version if necessary
833 Bitmap cachedBitmap = mWidgetPreviewCache.get(info);
834 if (cachedBitmap != null) {
835 return new FastBitmapDrawable(cachedBitmap);
836 }
837
838 // Get the preview
839 Resources resources = getContext().getResources();
840 Drawable wallpaperPreview = parseWallpaperPreviewXml(info);
841 Drawable wallpaperIcon = null;
842 int expectedWidth;
843 int expectedHeight;
844 if (wallpaperPreview != null) {
845 expectedWidth = wallpaperPreview.getIntrinsicWidth();
846 expectedHeight = wallpaperPreview.getIntrinsicHeight();
847 } else {
848 wallpaperPreview = mDefaultWidgetBackground;
849 expectedWidth = expectedHeight = Math.min(cellWidth, cellHeight);
850
851 // Draw the icon in the top left corner
852 String packageName = info.activityInfo.packageName;
853 try {
854 if (info.icon > 0) {
855 wallpaperIcon = mPackageManager.getDrawable(packageName, info.icon, null);
856 }
857 if (wallpaperIcon == null) {
858 wallpaperIcon = resources.getDrawable(R.drawable.ic_launcher_application);
859 }
860 } catch (Resources.NotFoundException e) {}
861 }
862
863 // Create the bitmap
864 Bitmap preview = Bitmap.createBitmap(expectedWidth, expectedHeight, Config.ARGB_8888);
865 renderDrawableToBitmap(wallpaperPreview, preview, 0, 0, expectedWidth, expectedHeight,
866 1f, 1f);
867 if (wallpaperIcon != null) {
868 int iconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
869 int offset = (int) (iconSize * sWidgetPreviewIconPaddingPercentage);
870 renderDrawableToBitmap(wallpaperIcon, preview, offset, offset, iconSize, iconSize,
871 1f, 1f);
872 }
873
874 FastBitmapDrawable previewDrawable = new FastBitmapDrawable(preview);
875 previewDrawable.setBounds(0, 0, expectedWidth, expectedHeight);
876 mWidgetPreviewCache.put(info, preview);
877 return previewDrawable;
878 }
879 /*
880 * Wallpapers PagedView implementation
881 */
882 public void syncWallpaperPages() {
883 // Ensure that we have the right number of pages
884 Context context = getContext();
885 int numWidgetsPerPage = mWallpaperCountX * mWallpaperCountY;
886 int numPages = (int) Math.ceil(mWallpapers.size() / (float) numWidgetsPerPage);
887 for (int i = 0; i < numPages; ++i) {
888 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWallpaperCountX,
889 mWallpaperCountY);
890 setupPage(layout);
891 addView(layout);
892 }
893 }
894 public void syncWallpaperPageItems(int page) {
895 PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(page);
896 layout.removeAllViews();
897
898 // Calculate the dimensions of each cell we are giving to each widget
899 int numWidgetsPerPage = mWallpaperCountX * mWallpaperCountY;
900 int offset = page * numWidgetsPerPage;
901 int cellWidth = ((mWidgetSpacingLayout.getContentWidth() - mPageLayoutWidthGap
902 - ((mWallpaperCountX - 1) * mCellWidthGap)) / mWallpaperCountX);
903 int cellHeight = ((mWidgetSpacingLayout.getContentHeight() - mPageLayoutHeightGap
904 - ((mWallpaperCountY - 1) * mCellHeightGap)) / mWallpaperCountY);
905 for (int i = 0; i < Math.min(numWidgetsPerPage, mWallpapers.size() - offset); ++i) {
906 ResolveInfo info = mWallpapers.get(offset + i);
907 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
908 R.layout.apps_customize_wallpaper, layout, false);
909
910 // Fill in the shortcuts information
911 FastBitmapDrawable preview = getWallpaperPreview(info, cellWidth, cellHeight);
912 widget.applyFromResolveInfo(mPackageManager, info, preview, null, false);
913 widget.setTag(info);
914 widget.setOnClickListener(this);
915 widget.setOnLongClickListener(this);
916 widget.setOnTouchListener(this);
917
918 // Layout each widget
919 int ix = i % mWallpaperCountX;
920 int iy = i / mWallpaperCountX;
921 PagedViewGridLayout.LayoutParams lp = new PagedViewGridLayout.LayoutParams(cellWidth,
922 cellHeight);
923 lp.leftMargin = (ix * cellWidth) + (ix * mCellWidthGap);
924 lp.topMargin = (iy * cellHeight) + (iy * mCellHeightGap);
925 layout.addView(widget, lp);
926 }
927 }
928
Winson Chung785d2eb2011-04-14 16:08:02 -0700929 @Override
930 public void syncPages() {
931 removeAllViews();
932 switch (mContentType) {
933 case Applications:
934 syncAppsPages();
935 break;
936 case Widgets:
937 syncWidgetPages();
938 break;
Winson Chung46af2e82011-05-09 16:00:53 -0700939 case Wallpapers:
940 syncWallpaperPages();
941 break;
Winson Chung785d2eb2011-04-14 16:08:02 -0700942 }
943 }
944 @Override
945 public void syncPageItems(int page) {
946 switch (mContentType) {
947 case Applications:
948 syncAppsPageItems(page);
949 break;
950 case Widgets:
951 syncWidgetPageItems(page);
952 break;
Winson Chung46af2e82011-05-09 16:00:53 -0700953 case Wallpapers:
954 syncWallpaperPageItems(page);
955 break;
Winson Chung785d2eb2011-04-14 16:08:02 -0700956 }
957 }
958
959 /**
960 * Used by the parent to get the content width to set the tab bar to
961 * @return
962 */
963 public int getPageContentWidth() {
964 return mContentWidth;
965 }
966
967 /*
968 * AllAppsView implementation
969 */
970 @Override
971 public void setup(Launcher launcher, DragController dragController) {
972 mLauncher = launcher;
973 mDragController = dragController;
974 }
975 @Override
976 public void zoom(float zoom, boolean animate) {
977 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
978 }
979 @Override
980 public boolean isVisible() {
981 return (getVisibility() == VISIBLE);
982 }
983 @Override
984 public boolean isAnimating() {
985 return false;
986 }
987 @Override
988 public void setApps(ArrayList<ApplicationInfo> list) {
989 mApps = list;
990 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
991 invalidatePageData();
992 }
993 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
994 // We add it in place, in alphabetical order
995 int count = list.size();
996 for (int i = 0; i < count; ++i) {
997 ApplicationInfo info = list.get(i);
998 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
999 if (index < 0) {
1000 mApps.add(-(index + 1), info);
1001 }
1002 }
1003 }
1004 @Override
1005 public void addApps(ArrayList<ApplicationInfo> list) {
1006 addAppsWithoutInvalidate(list);
1007 invalidatePageData();
1008 }
1009 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1010 ComponentName removeComponent = item.intent.getComponent();
1011 int length = list.size();
1012 for (int i = 0; i < length; ++i) {
1013 ApplicationInfo info = list.get(i);
1014 if (info.intent.getComponent().equals(removeComponent)) {
1015 return i;
1016 }
1017 }
1018 return -1;
1019 }
1020 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1021 // loop through all the apps and remove apps that have the same component
1022 int length = list.size();
1023 for (int i = 0; i < length; ++i) {
1024 ApplicationInfo info = list.get(i);
1025 int removeIndex = findAppByComponent(mApps, info);
1026 if (removeIndex > -1) {
1027 mApps.remove(removeIndex);
1028 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
1029 }
1030 }
1031 }
1032 @Override
1033 public void removeApps(ArrayList<ApplicationInfo> list) {
1034 removeAppsWithoutInvalidate(list);
1035 invalidatePageData();
1036 }
1037 @Override
1038 public void updateApps(ArrayList<ApplicationInfo> list) {
1039 // We remove and re-add the updated applications list because it's properties may have
1040 // changed (ie. the title), and this will ensure that the items will be in their proper
1041 // place in the list.
1042 removeAppsWithoutInvalidate(list);
1043 addAppsWithoutInvalidate(list);
1044 invalidatePageData();
1045 }
1046 @Override
1047 public void reset() {
Winson Chungfc79c802011-05-02 13:35:34 -07001048 if (mContentType != ContentType.Applications) {
1049 // Reset to the first page of the Apps pane
1050 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
1051 mLauncher.findViewById(R.id.apps_customize_pane);
1052 tabs.setCurrentTabByTag(tabs.getTabTagForContentType(ContentType.Applications));
1053 } else {
1054 setCurrentPage(0);
1055 invalidatePageData();
1056 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001057 }
1058 @Override
1059 public void dumpState() {
1060 // TODO: Dump information related to current list of Applications, Widgets, etc.
1061 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1062 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1063 }
1064 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chung1ed747a2011-05-03 16:18:34 -07001065 List<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001066 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001067 for (Object i: list) {
1068 if (i instanceof AppWidgetProviderInfo) {
1069 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1070 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1071 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1072 + " initialLayout=" + info.initialLayout
1073 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1074 } else if (i instanceof ResolveInfo) {
1075 ResolveInfo info = (ResolveInfo) i;
1076 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1077 + info.icon);
1078 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001079 }
1080 }
1081 @Override
1082 public void surrender() {
1083 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1084 // should stop this now.
1085 }
1086}