AppsCustomizePagedView.java revision 785d2eb2b8d7072c8124300dd9168ff51a91cf38
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 java.util.ArrayList; 20import java.util.Collections; 21import java.util.List; 22 23import android.appwidget.AppWidgetManager; 24import android.appwidget.AppWidgetProviderInfo; 25import android.content.ComponentName; 26import android.content.Context; 27import android.content.Intent; 28import android.content.pm.PackageManager; 29import android.content.pm.ResolveInfo; 30import android.content.res.Resources; 31import android.content.res.TypedArray; 32import android.graphics.Bitmap; 33import android.graphics.Bitmap.Config; 34import android.graphics.Canvas; 35import android.graphics.Rect; 36import android.graphics.drawable.Drawable; 37import android.util.AttributeSet; 38import android.util.Log; 39import android.view.Gravity; 40import android.view.LayoutInflater; 41import android.view.View; 42import android.widget.Checkable; 43import android.widget.ImageView; 44import android.widget.TextView; 45 46import com.android.launcher.R; 47 48public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements 49 AllAppsView, View.OnClickListener, DragSource { 50 static final String LOG_TAG = "AppsCustomizePagedView"; 51 52 /** 53 * The different content types that this paged view can show. 54 */ 55 public enum ContentType { 56 Applications, 57 Widgets 58 } 59 60 // Refs 61 private Launcher mLauncher; 62 private DragController mDragController; 63 private final LayoutInflater mLayoutInflater; 64 private final PackageManager mPackageManager; 65 66 // Content 67 private ContentType mContentType; 68 private ArrayList<ApplicationInfo> mApps; 69 private List<AppWidgetProviderInfo> mWidgets; 70 private List<ResolveInfo> mShortcuts; 71 72 // Dimens 73 private int mContentWidth; 74 private float mWidgetScale; 75 private PagedViewCellLayout mWidgetSpacingLayout; 76 77 public AppsCustomizePagedView(Context context, AttributeSet attrs) { 78 super(context, attrs); 79 mLayoutInflater = LayoutInflater.from(context); 80 mPackageManager = context.getPackageManager(); 81 mContentType = ContentType.Applications; 82 mApps = new ArrayList<ApplicationInfo>(); 83 84 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0); 85 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6); 86 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4); 87 a.recycle(); 88 89 // Create a dummy page and set it up to find out the content width (used by our parent) 90 PagedViewCellLayout layout = new PagedViewCellLayout(context); 91 setupPage(layout); 92 mContentWidth = layout.getContentWidth(); 93 94 // Create a dummy page that we can use to approximate the cell dimensions of widgets 95 mWidgetSpacingLayout = new PagedViewCellLayout(context); 96 mWidgetSpacingLayout.setCellCount(mCellCountX, mCellCountY); 97 } 98 99 @Override 100 protected void init() { 101 super.init(); 102 mCenterPagesVertically = false; 103 104 Context context = getContext(); 105 Resources r = context.getResources(); 106 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f); 107 } 108 109 public void onPackagesUpdated() { 110 // Get the list of widgets 111 mWidgets = AppWidgetManager.getInstance(mLauncher).getInstalledProviders(); 112 Collections.sort(mWidgets, LauncherModel.WIDGET_NAME_COMPARATOR); 113 114 // Get the list of shortcuts 115 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); 116 mShortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0); 117 Collections.sort(mShortcuts, new LauncherModel.ShortcutNameComparator(mPackageManager)); 118 } 119 120 @Override 121 public void onClick(View v) { 122 // Animate some feedback to the click 123 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag(); 124 animateClickFeedback(v, new Runnable() { 125 @Override 126 public void run() { 127 mLauncher.startActivitySafely(appInfo.intent, appInfo); 128 } 129 }); 130 } 131 132 /* 133 * PagedViewWithDraggableItems implementation 134 */ 135 @Override 136 protected void determineDraggingStart(android.view.MotionEvent ev) { 137 // Disable dragging by pulling an app down for now. 138 } 139 @Override 140 protected boolean beginDragging(View v) { 141 if (!super.beginDragging(v)) return false; 142 143 // Make a copy of the ApplicationInfo 144 ApplicationInfo appInfo = new ApplicationInfo((ApplicationInfo) v.getTag()); 145 146 // Show the uninstall button if the app is uninstallable. 147 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) { 148 DeleteZone allAppsDeleteZone = (DeleteZone) 149 mLauncher.findViewById(R.id.all_apps_delete_zone); 150 allAppsDeleteZone.setDragAndDropEnabled(true); 151 152 if ((appInfo.flags & ApplicationInfo.UPDATED_SYSTEM_APP_FLAG) != 0) { 153 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps_system_app); 154 } else { 155 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps); 156 } 157 } 158 159 // Show the info button 160 ApplicationInfoDropTarget allAppsInfoButton = 161 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target); 162 allAppsInfoButton.setDragAndDropEnabled(true); 163 164 // Compose the drag image (top compound drawable, index is 1) 165 final TextView tv = (TextView) v; 166 final Drawable icon = tv.getCompoundDrawables()[1]; 167 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), 168 Bitmap.Config.ARGB_8888); 169 Canvas c = new Canvas(b); 170 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop()); 171 icon.draw(c); 172 173 // Compose the visible rect of the drag image 174 Rect dragRect = null; 175 if (v instanceof TextView) { 176 int iconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size); 177 int top = v.getPaddingTop(); 178 int left = (b.getWidth() - iconSize) / 2; 179 int right = left + iconSize; 180 int bottom = top + iconSize; 181 dragRect = new Rect(left, top, right, bottom); 182 } 183 184 // Start the drag 185 mLauncher.lockScreenOrientation(); 186 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b); 187 mDragController.startDrag(v, b, this, appInfo, DragController.DRAG_ACTION_COPY, dragRect); 188 b.recycle(); 189 190 // Hide the pane so that the user can drop onto the workspace 191 mLauncher.showWorkspace(true); 192 return true; 193 } 194 private void endDragging(boolean success) { 195 post(new Runnable() { 196 // Once the drag operation has fully completed, hence the post, we want to disable the 197 // deleteZone and the appInfoButton in all apps, and re-enable the instance which 198 // live in the workspace 199 public void run() { 200 // if onDestroy was called on Launcher, we might have already deleted the 201 // all apps delete zone / info button, so check if they are null 202 DeleteZone allAppsDeleteZone = 203 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone); 204 ApplicationInfoDropTarget allAppsInfoButton = 205 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target); 206 207 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false); 208 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false); 209 } 210 }); 211 mLauncher.getWorkspace().onDragStopped(success); 212 mLauncher.unlockScreenOrientation(); 213 } 214 215 /* 216 * DragSource implementation 217 */ 218 @Override 219 public void onDragViewVisible() {} 220 @Override 221 public void onDropCompleted(View target, Object dragInfo, boolean success) { 222 endDragging(success); 223 } 224 225 public void setContentType(ContentType type) { 226 mContentType = type; 227 setCurrentPage(0); 228 invalidatePageData(); 229 } 230 231 /* 232 * Apps PagedView implementation 233 */ 234 private void setupPage(PagedViewCellLayout layout) { 235 layout.setCellCount(mCellCountX, mCellCountY); 236 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); 237 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, 238 mPageLayoutPaddingRight, mPageLayoutPaddingBottom); 239 240 // We force a measure here to get around the fact that when we do layout calculations 241 // immediately after syncing, we don't have a proper width. 242 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); 243 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); 244 layout.measure(widthSpec, heightSpec); 245 } 246 public void syncAppsPages() { 247 // Ensure that we have the right number of pages 248 Context context = getContext(); 249 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY)); 250 for (int i = 0; i < numPages; ++i) { 251 PagedViewCellLayout layout = new PagedViewCellLayout(context); 252 setupPage(layout); 253 addView(layout); 254 } 255 } 256 public void syncAppsPageItems(int page) { 257 // ensure that we have the right number of items on the pages 258 int numPages = getPageCount(); 259 int numCells = mCellCountX * mCellCountY; 260 int startIndex = page * numCells; 261 int endIndex = Math.min(startIndex + numCells, mApps.size()); 262 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); 263 layout.removeAllViewsOnPage(); 264 for (int i = startIndex; i < endIndex; ++i) { 265 ApplicationInfo info = mApps.get(i); 266 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate( 267 R.layout.apps_customize_application, layout, false); 268 icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1)); 269 icon.setOnClickListener(this); 270 icon.setOnLongClickListener(this); 271 icon.setOnTouchListener(this); 272 273 int index = i - startIndex; 274 int x = index % mCellCountX; 275 int y = index / mCellCountX; 276 setupPage(layout); 277 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); 278 } 279 } 280 /* 281 * Widgets PagedView implementation 282 */ 283 private void setupPage(PagedViewExtendedLayout layout) { 284 layout.setGravity(Gravity.LEFT); 285 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, 286 mPageLayoutPaddingRight, mPageLayoutPaddingBottom); 287 layout.setMinimumWidth(getPageContentWidth()); 288 } 289 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h, 290 float scaleX, float scaleY) { 291 Canvas c = new Canvas(); 292 if (bitmap != null) c.setBitmap(bitmap); 293 c.save(); 294 c.scale(scaleX, scaleY); 295 Rect oldBounds = d.copyBounds(); 296 d.setBounds(x, y, x + w, y + h); 297 d.draw(c); 298 d.setBounds(oldBounds); // Restore the bounds 299 c.restore(); 300 } 301 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) { 302 // See CustomizePagedView.getWidgetPreview() 303 return null; 304 } 305 public void syncWidgetPages() { 306 // Ensure that we have the right number of pages 307 Context context = getContext(); 308 int numPages = mWidgets.size(); 309 for (int i = 0; i < numPages; ++i) { 310 PagedViewExtendedLayout layout = new PagedViewExtendedLayout(context); 311 setupPage(layout); 312 addView(layout); 313 } 314 } 315 public void syncWidgetPageItems(int page) { 316 PagedViewExtendedLayout layout = (PagedViewExtendedLayout) getChildAt(page); 317 layout.removeAllViewsOnPage(); 318 for (int i = 0; i < 1; ++i) { 319 AppWidgetProviderInfo info = (AppWidgetProviderInfo) mWidgets.get(page); 320 FastBitmapDrawable icon = getWidgetPreview(info); 321 322 ImageView image = new ImageView(getContext()); 323 image.setBackgroundColor(0x99FF0000); 324 image.setImageDrawable(icon); 325 layout.addView(image, new PagedViewExtendedLayout.LayoutParams()); 326 } 327 } 328 @Override 329 public void syncPages() { 330 removeAllViews(); 331 switch (mContentType) { 332 case Applications: 333 syncAppsPages(); 334 break; 335 case Widgets: 336 syncWidgetPages(); 337 break; 338 } 339 } 340 @Override 341 public void syncPageItems(int page) { 342 switch (mContentType) { 343 case Applications: 344 syncAppsPageItems(page); 345 break; 346 case Widgets: 347 syncWidgetPageItems(page); 348 break; 349 } 350 } 351 352 /** 353 * Used by the parent to get the content width to set the tab bar to 354 * @return 355 */ 356 public int getPageContentWidth() { 357 return mContentWidth; 358 } 359 360 /* 361 * AllAppsView implementation 362 */ 363 @Override 364 public void setup(Launcher launcher, DragController dragController) { 365 mLauncher = launcher; 366 mDragController = dragController; 367 } 368 @Override 369 public void zoom(float zoom, boolean animate) { 370 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed() 371 } 372 @Override 373 public boolean isVisible() { 374 return (getVisibility() == VISIBLE); 375 } 376 @Override 377 public boolean isAnimating() { 378 return false; 379 } 380 @Override 381 public void setApps(ArrayList<ApplicationInfo> list) { 382 mApps = list; 383 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); 384 invalidatePageData(); 385 } 386 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { 387 // We add it in place, in alphabetical order 388 int count = list.size(); 389 for (int i = 0; i < count; ++i) { 390 ApplicationInfo info = list.get(i); 391 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); 392 if (index < 0) { 393 mApps.add(-(index + 1), info); 394 } 395 } 396 } 397 @Override 398 public void addApps(ArrayList<ApplicationInfo> list) { 399 addAppsWithoutInvalidate(list); 400 invalidatePageData(); 401 } 402 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) { 403 ComponentName removeComponent = item.intent.getComponent(); 404 int length = list.size(); 405 for (int i = 0; i < length; ++i) { 406 ApplicationInfo info = list.get(i); 407 if (info.intent.getComponent().equals(removeComponent)) { 408 return i; 409 } 410 } 411 return -1; 412 } 413 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { 414 // loop through all the apps and remove apps that have the same component 415 int length = list.size(); 416 for (int i = 0; i < length; ++i) { 417 ApplicationInfo info = list.get(i); 418 int removeIndex = findAppByComponent(mApps, info); 419 if (removeIndex > -1) { 420 mApps.remove(removeIndex); 421 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info)); 422 } 423 } 424 } 425 @Override 426 public void removeApps(ArrayList<ApplicationInfo> list) { 427 removeAppsWithoutInvalidate(list); 428 invalidatePageData(); 429 } 430 @Override 431 public void updateApps(ArrayList<ApplicationInfo> list) { 432 // We remove and re-add the updated applications list because it's properties may have 433 // changed (ie. the title), and this will ensure that the items will be in their proper 434 // place in the list. 435 removeAppsWithoutInvalidate(list); 436 addAppsWithoutInvalidate(list); 437 invalidatePageData(); 438 } 439 @Override 440 public void reset() { 441 setCurrentPage(0); 442 invalidatePageData(); 443 } 444 @Override 445 public void dumpState() { 446 // TODO: Dump information related to current list of Applications, Widgets, etc. 447 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps); 448 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets); 449 } 450 private void dumpAppWidgetProviderInfoList(String tag, String label, 451 List<AppWidgetProviderInfo> list) { 452 Log.d(tag, label + " size=" + list.size()); 453 for (AppWidgetProviderInfo info: list) { 454 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage 455 + " resizeMode=" + info.resizeMode + " configure=" + info.configure 456 + " initialLayout=" + info.initialLayout 457 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight); 458 } 459 } 460 @Override 461 public void surrender() { 462 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we 463 // should stop this now. 464 } 465} 466