AppsCustomizePagedView.java revision d4d7aa551ffdc80d810ff970fa72a6509960401e
1/* 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 android.animation.AnimatorSet; 20import android.animation.ObjectAnimator; 21import android.animation.ValueAnimator; 22import android.appwidget.AppWidgetManager; 23import android.appwidget.AppWidgetProviderInfo; 24import android.content.ComponentName; 25import android.content.Context; 26import android.content.Intent; 27import android.content.pm.ActivityInfo; 28import android.content.pm.PackageManager; 29import android.content.pm.ResolveInfo; 30import android.content.res.Configuration; 31import android.content.res.Resources; 32import android.content.res.TypedArray; 33import android.graphics.Bitmap; 34import android.graphics.Bitmap.Config; 35import android.graphics.Canvas; 36import android.graphics.Rect; 37import android.graphics.drawable.Drawable; 38import android.os.AsyncTask; 39import android.os.Process; 40import android.util.AttributeSet; 41import android.util.Log; 42import android.view.LayoutInflater; 43import android.view.View; 44import android.view.ViewGroup; 45import android.view.animation.AccelerateInterpolator; 46import android.widget.GridLayout; 47import android.widget.ImageView; 48import android.widget.TextView; 49import android.widget.Toast; 50 51import com.android.launcher.R; 52import com.android.launcher2.DropTarget.DragObject; 53 54import java.util.ArrayList; 55import java.util.Collections; 56import java.util.Iterator; 57import java.util.List; 58 59/** 60 * A simple callback interface which also provides the results of the task. 61 */ 62interface AsyncTaskCallback { 63 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data); 64} 65 66/** 67 * The data needed to perform either of the custom AsyncTasks. 68 */ 69class AsyncTaskPageData { 70 enum Type { 71 LoadWidgetPreviewData, 72 LoadHolographicIconsData 73 } 74 75 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR, 76 AsyncTaskCallback postR) { 77 page = p; 78 items = l; 79 sourceImages = si; 80 generatedImages = new ArrayList<Bitmap>(); 81 cellWidth = cellHeight = -1; 82 doInBackgroundCallback = bgR; 83 postExecuteCallback = postR; 84 } 85 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, int ccx, AsyncTaskCallback bgR, 86 AsyncTaskCallback postR) { 87 page = p; 88 items = l; 89 generatedImages = new ArrayList<Bitmap>(); 90 cellWidth = cw; 91 cellHeight = ch; 92 cellCountX = ccx; 93 doInBackgroundCallback = bgR; 94 postExecuteCallback = postR; 95 } 96 int page; 97 ArrayList<Object> items; 98 ArrayList<Bitmap> sourceImages; 99 ArrayList<Bitmap> generatedImages; 100 int cellWidth; 101 int cellHeight; 102 int cellCountX; 103 AsyncTaskCallback doInBackgroundCallback; 104 AsyncTaskCallback postExecuteCallback; 105} 106 107/** 108 * A generic template for an async task used in AppsCustomize. 109 */ 110class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> { 111 AppsCustomizeAsyncTask(int p, AppsCustomizePagedView.ContentType t, AsyncTaskPageData.Type ty) { 112 page = p; 113 pageContentType = t; 114 threadPriority = Process.THREAD_PRIORITY_DEFAULT; 115 dataType = ty; 116 } 117 @Override 118 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) { 119 if (params.length != 1) return null; 120 // Load each of the widget previews in the background 121 params[0].doInBackgroundCallback.run(this, params[0]); 122 return params[0]; 123 } 124 @Override 125 protected void onPostExecute(AsyncTaskPageData result) { 126 // All the widget previews are loaded, so we can just callback to inflate the page 127 result.postExecuteCallback.run(this, result); 128 } 129 130 void setThreadPriority(int p) { 131 threadPriority = p; 132 } 133 void syncThreadPriority() { 134 Process.setThreadPriority(threadPriority); 135 } 136 137 // The page that this async task is associated with 138 AsyncTaskPageData.Type dataType; 139 int page; 140 AppsCustomizePagedView.ContentType pageContentType; 141 int threadPriority; 142} 143 144/** 145 * The Apps/Customize page that displays all the applications, widgets, and shortcuts. 146 */ 147public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements 148 AllAppsView, View.OnClickListener, DragSource { 149 static final String LOG_TAG = "AppsCustomizePagedView"; 150 151 /** 152 * The different content types that this paged view can show. 153 */ 154 public enum ContentType { 155 Applications, 156 Widgets 157 } 158 159 // Refs 160 private Launcher mLauncher; 161 private DragController mDragController; 162 private final LayoutInflater mLayoutInflater; 163 private final PackageManager mPackageManager; 164 165 // Content 166 private ContentType mContentType; 167 private ArrayList<ApplicationInfo> mApps; 168 private ArrayList<Object> mWidgets; 169 private ArrayList<Object> mShortcuts; 170 171 // Caching 172 private Canvas mCanvas; 173 private Drawable mDefaultWidgetBackground; 174 private IconCache mIconCache; 175 176 // Dimens 177 private int mContentWidth; 178 private int mAppIconSize; 179 private int mMaxWidgetSpan, mMinWidgetSpan; 180 private int mWidgetCountX, mWidgetCountY; 181 private int mWidgetWidthGap, mWidgetHeightGap; 182 private int mShortcutCountX, mShortcutCountY; 183 private int mShortcutWidthGap, mShortcutHeightGap; 184 private final int mWidgetPreviewIconPaddedDimension; 185 private final float sWidgetPreviewIconPaddingPercentage = 0.25f; 186 private PagedViewCellLayout mWidgetSpacingLayout; 187 188 // Previews & outlines 189 ArrayList<AppsCustomizeAsyncTask> mRunningTasks; 190 private HolographicOutlineHelper mHolographicOutlineHelper; 191 192 public AppsCustomizePagedView(Context context, AttributeSet attrs) { 193 super(context, attrs); 194 mLayoutInflater = LayoutInflater.from(context); 195 mPackageManager = context.getPackageManager(); 196 mContentType = ContentType.Applications; 197 mApps = new ArrayList<ApplicationInfo>(); 198 mWidgets = new ArrayList<Object>(); 199 mShortcuts = new ArrayList<Object>(); 200 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache(); 201 mHolographicOutlineHelper = new HolographicOutlineHelper(); 202 mCanvas = new Canvas(); 203 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>(); 204 205 // Save the default widget preview background 206 Resources resources = context.getResources(); 207 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo); 208 mAppIconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size); 209 210 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0); 211 // TODO-APPS_CUSTOMIZE: remove these unnecessary attrs after 212 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6); 213 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4); 214 a.recycle(); 215 a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0); 216 mWidgetWidthGap = 217 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0); 218 mWidgetHeightGap = 219 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0); 220 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2); 221 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2); 222 a.recycle(); 223 mWidgetSpacingLayout = new PagedViewCellLayout(getContext()); 224 225 // The max widget span is the length N, such that NxN is the largest bounds that the widget 226 // preview can be before applying the widget scaling 227 mMinWidgetSpan = 1; 228 mMaxWidgetSpan = 3; 229 230 // The padding on the non-matched dimension for the default widget preview icons 231 // (top + bottom) 232 mWidgetPreviewIconPaddedDimension = 233 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage))); 234 } 235 236 @Override 237 protected void init() { 238 super.init(); 239 mCenterPagesVertically = false; 240 241 Context context = getContext(); 242 Resources r = context.getResources(); 243 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f); 244 } 245 246 @Override 247 protected void onWallpaperTap(android.view.MotionEvent ev) { 248 mLauncher.showWorkspace(true); 249 } 250 251 /** 252 * This differs from isDataReady as this is the test done if isDataReady is not set. 253 */ 254 private boolean testDataReady() { 255 // We only do this test once, and we default to the Applications page, so we only really 256 // have to wait for there to be apps. 257 return !mApps.isEmpty(); 258 } 259 260 protected void onDataReady(int width, int height) { 261 // Note that we transpose the counts in portrait so that we get a similar layout 262 boolean isLandscape = getResources().getConfiguration().orientation == 263 Configuration.ORIENTATION_LANDSCAPE; 264 int maxCellCountX = Integer.MAX_VALUE; 265 int maxCellCountY = Integer.MAX_VALUE; 266 if (LauncherApplication.isScreenLarge()) { 267 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() : 268 LauncherModel.getCellCountY()); 269 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() : 270 LauncherModel.getCellCountX()); 271 } 272 273 // Now that the data is ready, we can calculate the content width, the number of cells to 274 // use for each page 275 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); 276 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, 277 mPageLayoutPaddingRight, mPageLayoutPaddingBottom); 278 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY); 279 mCellCountX = mWidgetSpacingLayout.getCellCountX(); 280 mCellCountY = mWidgetSpacingLayout.getCellCountY(); 281 mWidgetCountX = Math.max(1, (int) Math.round(mCellCountX / 2f)); 282 mWidgetCountY = Math.max(1, (int) Math.round(mCellCountY / 3f)); 283 mShortcutCountX = Math.max(1, (int) Math.round(mCellCountX / 2f)); 284 mShortcutCountY = Math.max(1, (int) Math.round(mCellCountY / 2f)); 285 286 // Force a measure to update recalculate the gaps 287 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); 288 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); 289 mWidgetSpacingLayout.measure(widthSpec, heightSpec); 290 mContentWidth = mWidgetSpacingLayout.getContentWidth(); 291 292 invalidatePageData(); 293 } 294 295 @Override 296 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 297 int width = MeasureSpec.getSize(widthMeasureSpec); 298 int height = MeasureSpec.getSize(heightMeasureSpec); 299 if (!isDataReady()) { 300 if (testDataReady()) { 301 setDataIsReady(); 302 setMeasuredDimension(width, height); 303 onDataReady(width, height); 304 } 305 } 306 307 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 308 } 309 310 /** Removes and returns the ResolveInfo with the specified ComponentName */ 311 private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list, 312 ComponentName cn) { 313 Iterator<ResolveInfo> iter = list.iterator(); 314 while (iter.hasNext()) { 315 ResolveInfo rinfo = iter.next(); 316 ActivityInfo info = rinfo.activityInfo; 317 ComponentName c = new ComponentName(info.packageName, info.name); 318 if (c.equals(cn)) { 319 iter.remove(); 320 return rinfo; 321 } 322 } 323 return null; 324 } 325 326 public void onPackagesUpdated() { 327 // Get the list of widgets and shortcuts 328 boolean wasEmpty = mWidgets.isEmpty() && mShortcuts.isEmpty(); 329 mWidgets.clear(); 330 mShortcuts.clear(); 331 List<AppWidgetProviderInfo> widgets = 332 AppWidgetManager.getInstance(mLauncher).getInstalledProviders(); 333 Collections.sort(widgets, 334 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager)); 335 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); 336 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0); 337 Collections.sort(shortcuts, 338 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager)); 339 mWidgets.addAll(widgets); 340 mShortcuts.addAll(shortcuts); 341 342 if (wasEmpty) { 343 // The next layout pass will trigger data-ready if both widgets and apps are set, so request 344 // a layout to do this test and invalidate the page data when ready. 345 if (testDataReady()) requestLayout(); 346 } else { 347 invalidatePageData(); 348 } 349 } 350 351 @Override 352 public void onClick(View v) { 353 if (v instanceof PagedViewIcon) { 354 // Animate some feedback to the click 355 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag(); 356 animateClickFeedback(v, new Runnable() { 357 @Override 358 public void run() { 359 mLauncher.startActivitySafely(appInfo.intent, appInfo); 360 } 361 }); 362 } else if (v instanceof PagedViewWidget) { 363 // Let the user know that they have to long press to add a widget 364 Toast.makeText(getContext(), R.string.long_press_widget_to_add, 365 Toast.LENGTH_SHORT).show(); 366 367 // Create a little animation to show that the widget can move 368 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY); 369 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview); 370 AnimatorSet bounce = new AnimatorSet(); 371 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY); 372 tyuAnim.setDuration(125); 373 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f); 374 tydAnim.setDuration(100); 375 bounce.play(tyuAnim).before(tydAnim); 376 bounce.setInterpolator(new AccelerateInterpolator()); 377 bounce.start(); 378 } 379 } 380 381 /* 382 * PagedViewWithDraggableItems implementation 383 */ 384 @Override 385 protected void determineDraggingStart(android.view.MotionEvent ev) { 386 // Disable dragging by pulling an app down for now. 387 } 388 389 private void beginDraggingApplication(View v) { 390 mLauncher.getWorkspace().onDragStartedWithItem(v); 391 mLauncher.getWorkspace().beginDragShared(v, this); 392 } 393 394 private void beginDraggingWidget(View v) { 395 // Get the widget preview as the drag representation 396 ImageView image = (ImageView) v.findViewById(R.id.widget_preview); 397 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag(); 398 399 // Compose the drag image 400 Bitmap b; 401 Drawable preview = image.getDrawable(); 402 int w = preview.getIntrinsicWidth(); 403 int h = preview.getIntrinsicHeight(); 404 if (createItemInfo instanceof PendingAddWidgetInfo) { 405 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo; 406 int[] spanXY = CellLayout.rectToCell(getResources(), 407 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null); 408 createItemInfo.spanX = spanXY[0]; 409 createItemInfo.spanY = spanXY[1]; 410 411 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 412 renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1); 413 } else { 414 // Workaround for the fact that we don't keep the original ResolveInfo associated with 415 // the shortcut around. To get the icon, we just render the preview image (which has 416 // the shortcut icon) to a new drag bitmap that clips the non-icon space. 417 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension, 418 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888); 419 mCanvas.setBitmap(b); 420 mCanvas.save(); 421 preview.draw(mCanvas); 422 mCanvas.restore(); 423 createItemInfo.spanX = createItemInfo.spanY = 1; 424 } 425 426 // Start the drag 427 mLauncher.lockScreenOrientation(); 428 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX, 429 createItemInfo.spanY, b); 430 mDragController.startDrag(image, b, this, createItemInfo, 431 DragController.DRAG_ACTION_COPY, null); 432 b.recycle(); 433 } 434 @Override 435 protected boolean beginDragging(View v) { 436 if (!super.beginDragging(v)) return false; 437 438 // Go into spring loaded mode (must happen before we startDrag()) 439 int currentPageIndex = mLauncher.getWorkspace().getCurrentPage(); 440 CellLayout currentPage = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPageIndex); 441 mLauncher.enterSpringLoadedDragMode(currentPage); 442 443 if (v instanceof PagedViewIcon) { 444 beginDraggingApplication(v); 445 } else if (v instanceof PagedViewWidget) { 446 beginDraggingWidget(v); 447 } 448 return true; 449 } 450 private void endDragging(View target, boolean success) { 451 mLauncher.getWorkspace().onDragStopped(success); 452 if (!success || (target != mLauncher.getWorkspace() && 453 !(target instanceof DeleteDropTarget))) { 454 // Exit spring loaded mode if we have not successfully dropped or have not handled the 455 // drop in Workspace 456 mLauncher.exitSpringLoadedDragMode(); 457 } 458 mLauncher.unlockScreenOrientation(); 459 460 } 461 462 /* 463 * DragSource implementation 464 */ 465 @Override 466 public void onDragViewVisible() {} 467 @Override 468 public void onDropCompleted(View target, DragObject d, boolean success) { 469 endDragging(target, success); 470 471 // Display an error message if the drag failed due to there not being enough space on the 472 // target layout we were dropping on. 473 if (!success) { 474 boolean showOutOfSpaceMessage = false; 475 if (target instanceof Workspace) { 476 int currentScreen = mLauncher.getCurrentWorkspaceScreen(); 477 Workspace workspace = (Workspace) target; 478 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen); 479 ItemInfo itemInfo = (ItemInfo) d.dragInfo; 480 if (layout != null) { 481 layout.calculateSpans(itemInfo); 482 showOutOfSpaceMessage = 483 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY); 484 } 485 } 486 // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later. 487 if (showOutOfSpaceMessage) { 488 mLauncher.showOutOfSpaceMessage(); 489 } 490 } 491 } 492 493 public void setContentType(ContentType type) { 494 mContentType = type; 495 setCurrentPage(0); 496 invalidatePageData(); 497 } 498 499 public boolean isContentType(ContentType type) { 500 return (mContentType == type); 501 } 502 503 /* 504 * Apps PagedView implementation 505 */ 506 private void setVisibilityOnChildren(ViewGroup layout, int visibility) { 507 int childCount = layout.getChildCount(); 508 for (int i = 0; i < childCount; ++i) { 509 layout.getChildAt(i).setVisibility(visibility); 510 } 511 } 512 private void setupPage(PagedViewCellLayout layout) { 513 layout.setCellCount(mCellCountX, mCellCountY); 514 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); 515 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, 516 mPageLayoutPaddingRight, mPageLayoutPaddingBottom); 517 518 // Note: We force a measure here to get around the fact that when we do layout calculations 519 // immediately after syncing, we don't have a proper width. That said, we already know the 520 // expected page width, so we can actually optimize by hiding all the TextView-based 521 // children that are expensive to measure, and let that happen naturally later. 522 setVisibilityOnChildren(layout, View.GONE); 523 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); 524 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); 525 layout.setMinimumWidth(getPageContentWidth()); 526 layout.measure(widthSpec, heightSpec); 527 setVisibilityOnChildren(layout, View.VISIBLE); 528 } 529 public void syncAppsPages() { 530 // Ensure that we have the right number of pages 531 Context context = getContext(); 532 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY)); 533 for (int i = 0; i < numPages; ++i) { 534 PagedViewCellLayout layout = new PagedViewCellLayout(context); 535 setupPage(layout); 536 addView(layout); 537 } 538 } 539 public void syncAppsPageItems(int page) { 540 // ensure that we have the right number of items on the pages 541 int numPages = getPageCount(); 542 int numCells = mCellCountX * mCellCountY; 543 int startIndex = page * numCells; 544 int endIndex = Math.min(startIndex + numCells, mApps.size()); 545 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); 546 547 layout.removeAllViewsOnPage(); 548 ArrayList<Object> items = new ArrayList<Object>(); 549 ArrayList<Bitmap> images = new ArrayList<Bitmap>(); 550 for (int i = startIndex; i < endIndex; ++i) { 551 ApplicationInfo info = mApps.get(i); 552 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate( 553 R.layout.apps_customize_application, layout, false); 554 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper); 555 icon.setOnClickListener(this); 556 icon.setOnLongClickListener(this); 557 icon.setOnTouchListener(this); 558 559 int index = i - startIndex; 560 int x = index % mCellCountX; 561 int y = index / mCellCountX; 562 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); 563 564 items.add(info); 565 images.add(info.iconBitmap); 566 } 567 568 // Create the hardware layers 569 layout.allowHardwareLayerCreation(); 570 layout.createHardwareLayers(); 571 572 prepareGenerateHoloOutlinesTask(page, items, images); 573 } 574 575 /** 576 * Return the appropriate thread priority for loading for a given page (we give the current 577 * page much higher priority) 578 */ 579 private int getThreadPriorityForPage(int page) { 580 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below 581 int pageDiff = Math.abs(page - mCurrentPage); 582 if (pageDiff <= 0) { 583 // return Process.THREAD_PRIORITY_DEFAULT; 584 return Process.THREAD_PRIORITY_MORE_FAVORABLE; 585 } else if (pageDiff <= 1) { 586 // return Process.THREAD_PRIORITY_BACKGROUND; 587 return Process.THREAD_PRIORITY_DEFAULT; 588 } else { 589 // return Process.THREAD_PRIORITY_LOWEST; 590 return Process.THREAD_PRIORITY_DEFAULT; 591 } 592 } 593 /** 594 * Creates and executes a new AsyncTask to load a page of widget previews. 595 */ 596 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets, 597 int cellWidth, int cellHeight, int cellCountX) { 598 // Prune all tasks that are no longer needed 599 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); 600 while (iter.hasNext()) { 601 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); 602 int taskPage = task.page; 603 if ((taskPage == page) || 604 taskPage < getAssociatedLowerPageBound(mCurrentPage) || 605 taskPage > getAssociatedUpperPageBound(mCurrentPage)) { 606 task.cancel(false); 607 iter.remove(); 608 } else { 609 task.setThreadPriority(getThreadPriorityForPage(taskPage)); 610 } 611 } 612 613 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight, 614 cellCountX, new AsyncTaskCallback() { 615 @Override 616 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { 617 // Ensure that this task starts running at the correct priority 618 task.syncThreadPriority(); 619 620 // Load each of the widget/shortcut previews 621 ArrayList<Object> items = data.items; 622 ArrayList<Bitmap> images = data.generatedImages; 623 int count = items.size(); 624 int cellWidth = data.cellWidth; 625 int cellHeight = data.cellHeight; 626 for (int i = 0; i < count && !task.isCancelled(); ++i) { 627 // Before work on each item, ensure that this task is running at the correct 628 // priority 629 task.syncThreadPriority(); 630 631 Object rawInfo = items.get(i); 632 if (rawInfo instanceof AppWidgetProviderInfo) { 633 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo; 634 int[] cellSpans = CellLayout.rectToCell(getResources(), 635 info.minWidth, info.minHeight, null); 636 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1], 637 cellWidth, cellHeight)); 638 } else if (rawInfo instanceof ResolveInfo) { 639 // Fill in the shortcuts information 640 ResolveInfo info = (ResolveInfo) rawInfo; 641 images.add(getShortcutPreview(info, cellWidth, cellHeight)); 642 } 643 } 644 } 645 }, 646 new AsyncTaskCallback() { 647 @Override 648 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { 649 mRunningTasks.remove(task); 650 if (task.isCancelled()) return; 651 if (task.page > getPageCount()) return; 652 if (task.pageContentType != mContentType) return; 653 onSyncWidgetPageItems(data); 654 } 655 }); 656 657 // Ensure that the task is appropriately prioritized and runs in parallel 658 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, mContentType, 659 AsyncTaskPageData.Type.LoadWidgetPreviewData); 660 t.setThreadPriority(getThreadPriorityForPage(page)); 661 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData); 662 mRunningTasks.add(t); 663 } 664 /** 665 * Creates and executes a new AsyncTask to load the outlines for a page of content. 666 */ 667 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items, 668 ArrayList<Bitmap> images) { 669 // Prune old tasks for this page 670 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); 671 while (iter.hasNext()) { 672 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); 673 int taskPage = task.page; 674 if ((taskPage == page) && 675 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) { 676 task.cancel(false); 677 iter.remove(); 678 } 679 } 680 681 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images, 682 new AsyncTaskCallback() { 683 @Override 684 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { 685 // Ensure that this task starts running at the correct priority 686 task.syncThreadPriority(); 687 688 ArrayList<Bitmap> images = data.generatedImages; 689 ArrayList<Bitmap> srcImages = data.sourceImages; 690 int count = srcImages.size(); 691 Canvas c = new Canvas(); 692 for (int i = 0; i < count && !task.isCancelled(); ++i) { 693 // Before work on each item, ensure that this task is running at the correct 694 // priority 695 task.syncThreadPriority(); 696 697 Bitmap b = srcImages.get(i); 698 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(), 699 Bitmap.Config.ARGB_8888); 700 701 c.setBitmap(outline); 702 c.save(); 703 c.drawBitmap(b, 0, 0, null); 704 c.restore(); 705 706 images.add(outline); 707 } 708 } 709 }, 710 new AsyncTaskCallback() { 711 @Override 712 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { 713 mRunningTasks.remove(task); 714 if (task.isCancelled()) return; 715 if (task.page > getPageCount()) return; 716 if (task.pageContentType != mContentType) return; 717 onHolographicPageItemsLoaded(data); 718 } 719 }); 720 721 // Ensure that the outline task always runs in the background, serially 722 AppsCustomizeAsyncTask t = 723 new AppsCustomizeAsyncTask(page, mContentType, 724 AsyncTaskPageData.Type.LoadHolographicIconsData); 725 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 726 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData); 727 mRunningTasks.add(t); 728 } 729 730 /* 731 * Widgets PagedView implementation 732 */ 733 private void setupPage(PagedViewGridLayout layout) { 734 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, 735 mPageLayoutPaddingRight, mPageLayoutPaddingBottom); 736 737 // Note: We force a measure here to get around the fact that when we do layout calculations 738 // immediately after syncing, we don't have a proper width. 739 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); 740 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); 741 layout.setMinimumWidth(getPageContentWidth()); 742 layout.measure(widthSpec, heightSpec); 743 } 744 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h, 745 float scaleX, float scaleY) { 746 if (bitmap != null) { 747 Canvas c = new Canvas(bitmap); 748 c.scale(scaleX, scaleY); 749 Rect oldBounds = d.copyBounds(); 750 d.setBounds(x, y, x + w, y + h); 751 d.draw(c); 752 d.setBounds(oldBounds); // Restore the bounds 753 } 754 } 755 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) { 756 // Render the icon 757 Bitmap preview = Bitmap.createBitmap(cellWidth, mAppIconSize, Config.ARGB_8888); 758 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager); 759 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize, 1f, 1f); 760 return preview; 761 } 762 private Bitmap getWidgetPreview(AppWidgetProviderInfo info, 763 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) { 764 765 // Calculate the size of the drawable 766 cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan)); 767 cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan)); 768 int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan); 769 int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan); 770 771 // Scale down the bitmap to fit the space 772 float widgetPreviewScale = (float) cellWidth / expectedWidth; 773 expectedWidth = (int) (widgetPreviewScale * expectedWidth); 774 expectedHeight = (int) (widgetPreviewScale * expectedHeight); 775 776 // Load the preview image if possible 777 String packageName = info.provider.getPackageName(); 778 Drawable drawable = null; 779 Bitmap preview = null; 780 if (info.previewImage != 0) { 781 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null); 782 if (drawable == null) { 783 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon) 784 + " for provider: " + info.provider); 785 } else { 786 // Scale down the preview to the dimensions we want 787 int imageWidth = drawable.getIntrinsicWidth(); 788 int imageHeight = drawable.getIntrinsicHeight(); 789 float aspect = (float) imageWidth / imageHeight; 790 int newWidth = imageWidth; 791 int newHeight = imageHeight; 792 if (aspect > 1f) { 793 newWidth = expectedWidth; 794 newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth)); 795 } else { 796 newHeight = expectedHeight; 797 newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight)); 798 } 799 800 preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888); 801 renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f); 802 } 803 } 804 805 // Generate a preview image if we couldn't load one 806 if (drawable == null) { 807 Resources resources = mLauncher.getResources(); 808 int bitmapWidth; 809 int bitmapHeight; 810 811 // Specify the dimensions of the bitmap (since we are using a default preview bg with 812 // the full icon, we only imply the aspect ratio of the widget) 813 if (cellHSpan == cellVSpan) { 814 bitmapWidth = bitmapHeight = cellWidth; 815 expectedWidth = expectedHeight = mWidgetPreviewIconPaddedDimension; 816 } else if (cellHSpan >= cellVSpan) { 817 bitmapWidth = expectedWidth = cellWidth; 818 bitmapHeight = expectedHeight = mWidgetPreviewIconPaddedDimension; 819 } else { 820 // Note that in vertical widgets, we might not have enough space due to the text 821 // label, so be conservative and use the width as a height bound 822 bitmapWidth = expectedWidth = mWidgetPreviewIconPaddedDimension; 823 bitmapHeight = expectedHeight = cellWidth; 824 } 825 826 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888); 827 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth, 828 expectedHeight, 1f,1f); 829 830 // Draw the icon in the top left corner 831 try { 832 Drawable icon = null; 833 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null); 834 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application); 835 836 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage); 837 renderDrawableToBitmap(icon, preview, offset, offset, 838 mAppIconSize, mAppIconSize, 1f, 1f); 839 } catch (Resources.NotFoundException e) {} 840 } 841 return preview; 842 } 843 public void syncWidgetPages() { 844 // Ensure that we have the right number of pages 845 Context context = getContext(); 846 int[] countX = { mWidgetCountX, mShortcutCountX }; 847 int[] countY = { mWidgetCountY, mShortcutCountY }; 848 Object[] collection = { mWidgets, mShortcuts }; 849 for (int i = 0; i < 2; ++i) { 850 ArrayList<Object> list = (ArrayList<Object>) collection[i]; 851 int numItemsPerPage = countX[i] * countY[i]; 852 int numItemPages = (int) Math.ceil(list.size() / (float) numItemsPerPage); 853 for (int j = 0; j < numItemPages; ++j) { 854 PagedViewGridLayout layout = new PagedViewGridLayout(context, countX[i], 855 countY[i]); 856 setupPage(layout); 857 addView(layout); 858 } 859 } 860 } 861 public void syncWidgetPageItems(int page) { 862 int[] countX = { mWidgetCountX, mShortcutCountX }; 863 int[] countY = { mWidgetCountY, mShortcutCountY }; 864 int[] widthGap = { mWidgetWidthGap, mWidgetWidthGap }; 865 int[] heightGap = { mWidgetHeightGap, mWidgetHeightGap }; 866 int[] numItemsPerPage = { mWidgetCountX * mWidgetCountY, 867 mShortcutCountX * mShortcutCountY }; 868 Object[] collection = { mWidgets, mShortcuts }; 869 int contentWidth = mWidgetSpacingLayout.getContentWidth(); 870 int contentHeight = mWidgetSpacingLayout.getContentHeight(); 871 int numWidgetPages = (int) Math.ceil(mWidgets.size() / (float) numItemsPerPage[0]); 872 int[] offsets = { page * numItemsPerPage[0], (page - numWidgetPages) * numItemsPerPage[1] }; 873 int index = (page < numWidgetPages ? 0 : 1); 874 875 // Calculate the dimensions of each cell we are giving to each widget 876 ArrayList<Object> list = (ArrayList<Object>) collection[index]; 877 ArrayList<Object> items = new ArrayList<Object>(); 878 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight 879 - ((countX[index] - 1) * widthGap[index])) / countX[index]); 880 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom 881 - ((countY[index] - 1) * heightGap[index])) / countY[index]); 882 883 int offset = offsets[index]; 884 for (int i = offset; i < Math.min(offset + numItemsPerPage[index], list.size()); ++i) { 885 items.add(list.get(i)); 886 } 887 888 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, countX[index]); 889 } 890 private void onSyncWidgetPageItems(AsyncTaskPageData data) { 891 int page = data.page; 892 PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(page); 893 // Only set the column count once we have items 894 layout.setColumnCount(layout.getCellCountX()); 895 896 ArrayList<Object> items = data.items; 897 int count = items.size(); 898 int cellWidth = data.cellWidth; 899 int cellHeight = data.cellHeight; 900 int cellCountX = data.cellCountX; 901 for (int i = 0; i < count; ++i) { 902 Object rawInfo = items.get(i); 903 PendingAddItemInfo createItemInfo = null; 904 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate( 905 R.layout.apps_customize_widget, layout, false); 906 if (rawInfo instanceof AppWidgetProviderInfo) { 907 // Fill in the widget information 908 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo; 909 createItemInfo = new PendingAddWidgetInfo(info, null, null); 910 int[] cellSpans = CellLayout.rectToCell(getResources(), 911 info.minWidth, info.minHeight, null); 912 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i)); 913 widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans, 914 mHolographicOutlineHelper); 915 widget.setTag(createItemInfo); 916 } else if (rawInfo instanceof ResolveInfo) { 917 // Fill in the shortcuts information 918 ResolveInfo info = (ResolveInfo) rawInfo; 919 createItemInfo = new PendingAddItemInfo(); 920 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; 921 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName, 922 info.activityInfo.name); 923 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i)); 924 widget.applyFromResolveInfo(mPackageManager, info, preview, 925 mHolographicOutlineHelper); 926 widget.setTag(createItemInfo); 927 } 928 widget.setOnClickListener(this); 929 widget.setOnLongClickListener(this); 930 widget.setOnTouchListener(this); 931 932 // Layout each widget 933 int ix = i % cellCountX; 934 int iy = i / cellCountX; 935 GridLayout.LayoutParams lp = new GridLayout.LayoutParams( 936 GridLayout.spec(iy, GridLayout.LEFT, GridLayout.CAN_STRETCH), 937 GridLayout.spec(ix, GridLayout.TOP, GridLayout.CAN_STRETCH)); 938 lp.width = cellWidth; 939 lp.height = cellHeight; 940 if (ix > 0) lp.leftMargin = mWidgetWidthGap; 941 if (iy > 0) lp.topMargin = mWidgetHeightGap; 942 layout.addView(widget, lp); 943 } 944 945 invalidate(); 946 forceUpdateAdjacentPagesAlpha(); 947 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages); 948 } 949 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) { 950 // Invalidate early to short-circuit children invalidates 951 invalidate(); 952 953 int page = data.page; 954 ViewGroup layout = (ViewGroup) getChildAt(page); 955 if (layout instanceof PagedViewCellLayout) { 956 PagedViewCellLayout cl = (PagedViewCellLayout) layout; 957 int count = cl.getPageChildCount(); 958 if (count != data.generatedImages.size()) return; 959 for (int i = 0; i < count; ++i) { 960 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i); 961 icon.setHolographicOutline(data.generatedImages.get(i)); 962 } 963 } else { 964 int count = layout.getChildCount(); 965 if (count != data.generatedImages.size()) return; 966 for (int i = 0; i < count; ++i) { 967 View v = layout.getChildAt(i); 968 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i)); 969 } 970 } 971 } 972 973 @Override 974 public void syncPages() { 975 removeAllViews(); 976 977 // Remove all background asyc tasks if we are loading content anew 978 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); 979 while (iter.hasNext()) { 980 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); 981 task.cancel(false); 982 iter.remove(); 983 } 984 985 switch (mContentType) { 986 case Applications: 987 syncAppsPages(); 988 break; 989 case Widgets: 990 syncWidgetPages(); 991 break; 992 } 993 } 994 @Override 995 public void syncPageItems(int page) { 996 switch (mContentType) { 997 case Applications: 998 syncAppsPageItems(page); 999 break; 1000 case Widgets: 1001 syncWidgetPageItems(page); 1002 break; 1003 } 1004 } 1005 1006 /** 1007 * Used by the parent to get the content width to set the tab bar to 1008 * @return 1009 */ 1010 public int getPageContentWidth() { 1011 return mContentWidth; 1012 } 1013 1014 @Override 1015 protected void onPageBeginMoving() { 1016 /* TO BE ENABLED LATER 1017 setChildrenDrawnWithCacheEnabled(true); 1018 for (int i = 0; i < getChildCount(); ++i) { 1019 View v = getChildAt(i); 1020 if (v instanceof PagedViewCellLayout) { 1021 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true); 1022 } 1023 } 1024 */ 1025 super.onPageBeginMoving(); 1026 } 1027 1028 @Override 1029 protected void onPageEndMoving() { 1030 /* TO BE ENABLED LATER 1031 for (int i = 0; i < getChildCount(); ++i) { 1032 View v = getChildAt(i); 1033 if (v instanceof PagedViewCellLayout) { 1034 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false); 1035 } 1036 } 1037 setChildrenDrawnWithCacheEnabled(false); 1038 */ 1039 super.onPageEndMoving(); 1040 } 1041 1042 /* 1043 * AllAppsView implementation 1044 */ 1045 @Override 1046 public void setup(Launcher launcher, DragController dragController) { 1047 mLauncher = launcher; 1048 mDragController = dragController; 1049 } 1050 @Override 1051 public void zoom(float zoom, boolean animate) { 1052 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed() 1053 } 1054 @Override 1055 public boolean isVisible() { 1056 return (getVisibility() == VISIBLE); 1057 } 1058 @Override 1059 public boolean isAnimating() { 1060 return false; 1061 } 1062 @Override 1063 public void setApps(ArrayList<ApplicationInfo> list) { 1064 mApps = list; 1065 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); 1066 1067 // The next layout pass will trigger data-ready if both widgets and apps are set, so 1068 // request a layout to do this test and invalidate the page data when ready. 1069 if (testDataReady()) requestLayout(); 1070 } 1071 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { 1072 // We add it in place, in alphabetical order 1073 int count = list.size(); 1074 for (int i = 0; i < count; ++i) { 1075 ApplicationInfo info = list.get(i); 1076 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); 1077 if (index < 0) { 1078 mApps.add(-(index + 1), info); 1079 } 1080 } 1081 } 1082 @Override 1083 public void addApps(ArrayList<ApplicationInfo> list) { 1084 addAppsWithoutInvalidate(list); 1085 invalidatePageData(); 1086 } 1087 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) { 1088 ComponentName removeComponent = item.intent.getComponent(); 1089 int length = list.size(); 1090 for (int i = 0; i < length; ++i) { 1091 ApplicationInfo info = list.get(i); 1092 if (info.intent.getComponent().equals(removeComponent)) { 1093 return i; 1094 } 1095 } 1096 return -1; 1097 } 1098 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { 1099 // loop through all the apps and remove apps that have the same component 1100 int length = list.size(); 1101 for (int i = 0; i < length; ++i) { 1102 ApplicationInfo info = list.get(i); 1103 int removeIndex = findAppByComponent(mApps, info); 1104 if (removeIndex > -1) { 1105 mApps.remove(removeIndex); 1106 } 1107 } 1108 } 1109 @Override 1110 public void removeApps(ArrayList<ApplicationInfo> list) { 1111 removeAppsWithoutInvalidate(list); 1112 invalidatePageData(); 1113 } 1114 @Override 1115 public void updateApps(ArrayList<ApplicationInfo> list) { 1116 // We remove and re-add the updated applications list because it's properties may have 1117 // changed (ie. the title), and this will ensure that the items will be in their proper 1118 // place in the list. 1119 removeAppsWithoutInvalidate(list); 1120 addAppsWithoutInvalidate(list); 1121 invalidatePageData(); 1122 } 1123 1124 @Override 1125 public void reset() { 1126 if (mContentType != ContentType.Applications) { 1127 // Reset to the first page of the Apps pane 1128 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost) 1129 mLauncher.findViewById(R.id.apps_customize_pane); 1130 tabs.setCurrentTabByTag(tabs.getTabTagForContentType(ContentType.Applications)); 1131 } else if (getCurrentPage() != 0) { 1132 setCurrentPage(0); 1133 invalidatePageData(); 1134 } 1135 } 1136 @Override 1137 public void dumpState() { 1138 // TODO: Dump information related to current list of Applications, Widgets, etc. 1139 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps); 1140 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets); 1141 dumpAppWidgetProviderInfoList(LOG_TAG, "mShortcuts", mShortcuts); 1142 } 1143 private void dumpAppWidgetProviderInfoList(String tag, String label, 1144 ArrayList<Object> list) { 1145 Log.d(tag, label + " size=" + list.size()); 1146 for (Object i: list) { 1147 if (i instanceof AppWidgetProviderInfo) { 1148 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i; 1149 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage 1150 + " resizeMode=" + info.resizeMode + " configure=" + info.configure 1151 + " initialLayout=" + info.initialLayout 1152 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight); 1153 } else if (i instanceof ResolveInfo) { 1154 ResolveInfo info = (ResolveInfo) i; 1155 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon=" 1156 + info.icon); 1157 } 1158 } 1159 } 1160 @Override 1161 public void surrender() { 1162 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we 1163 // should stop this now. 1164 } 1165 1166 /* 1167 * We load an extra page on each side to prevent flashes from scrolling and loading of the 1168 * widget previews in the background with the AsyncTasks. 1169 */ 1170 protected int getAssociatedLowerPageBound(int page) { 1171 return Math.max(0, page - 2); 1172 } 1173 protected int getAssociatedUpperPageBound(int page) { 1174 final int count = getChildCount(); 1175 return Math.min(page + 2, count - 1); 1176 } 1177 1178 @Override 1179 protected String getCurrentPageDescription() { 1180 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; 1181 int stringId = R.string.default_scroll_format; 1182 switch (mContentType) { 1183 case Applications: 1184 stringId = R.string.apps_customize_apps_scroll_format; 1185 break; 1186 case Widgets: 1187 stringId = R.string.apps_customize_widgets_scroll_format; 1188 break; 1189 } 1190 return String.format(mContext.getString(stringId), page + 1, getChildCount()); 1191 } 1192} 1193