Launcher.java revision fd22dac33e535779d1006fcfe22ef2b95152d916
1/* 2 * Copyright (C) 2008 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.app.Activity; 20import android.app.AlertDialog; 21import android.app.Dialog; 22import android.app.ISearchManager; 23import android.app.SearchManager; 24import android.app.StatusBarManager; 25import android.app.WallpaperInfo; 26import android.app.WallpaperManager; 27import android.content.ActivityNotFoundException; 28import android.content.ComponentName; 29import android.content.Context; 30import android.content.DialogInterface; 31import android.content.Intent; 32import android.content.Intent.ShortcutIconResource; 33import android.content.pm.ActivityInfo; 34import android.content.pm.LabeledIntent; 35import android.content.pm.PackageManager; 36import android.content.pm.PackageManager.NameNotFoundException; 37import android.content.res.Configuration; 38import android.content.res.Resources; 39import android.graphics.Bitmap; 40import android.graphics.drawable.Drawable; 41import android.graphics.drawable.TransitionDrawable; 42import android.os.Bundle; 43import android.os.Looper; 44import android.os.Message; 45import android.os.MessageQueue; 46import android.os.Parcelable; 47import android.os.RemoteException; 48import android.os.ServiceManager; 49import android.provider.LiveFolders; 50import android.text.Selection; 51import android.text.SpannableStringBuilder; 52import android.text.TextUtils; 53import android.text.method.TextKeyListener; 54import static android.util.Log.*; 55import android.util.Log; 56import android.view.Display; 57import android.view.KeyEvent; 58import android.view.LayoutInflater; 59import android.view.Menu; 60import android.view.MenuItem; 61import android.view.View; 62import android.view.ViewGroup; 63import android.view.WindowManager; 64import android.view.View.OnLongClickListener; 65import android.view.inputmethod.InputMethodManager; 66import android.widget.EditText; 67import android.widget.TextView; 68import android.widget.Toast; 69import android.appwidget.AppWidgetManager; 70import android.appwidget.AppWidgetProviderInfo; 71 72import java.lang.ref.WeakReference; 73import java.util.ArrayList; 74import java.util.HashMap; 75import java.util.LinkedList; 76import java.io.DataOutputStream; 77import java.io.FileNotFoundException; 78import java.io.IOException; 79import java.io.DataInputStream; 80 81/** 82 * Default launcher application. 83 */ 84public final class Launcher extends Activity 85 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, 86 SwipeController.SwipeListener { 87 static final String LOG_TAG = "Launcher"; 88 static final String TAG = LOG_TAG; 89 static final boolean LOGD = false; 90 91 static final boolean PROFILE_STARTUP = false; 92 static final boolean PROFILE_ROTATE = false; 93 static final boolean DEBUG_USER_INTERFACE = false; 94 95 private static final int WALLPAPER_SCREENS_SPAN = 2; 96 97 private static final int MENU_GROUP_ADD = 1; 98 private static final int MENU_ADD = Menu.FIRST + 1; 99 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1; 100 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1; 101 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1; 102 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1; 103 104 private static final int REQUEST_CREATE_SHORTCUT = 1; 105 private static final int REQUEST_CREATE_LIVE_FOLDER = 4; 106 private static final int REQUEST_CREATE_APPWIDGET = 5; 107 private static final int REQUEST_PICK_APPLICATION = 6; 108 private static final int REQUEST_PICK_SHORTCUT = 7; 109 private static final int REQUEST_PICK_LIVE_FOLDER = 8; 110 private static final int REQUEST_PICK_APPWIDGET = 9; 111 112 private static final int MODE_WORKSPACE = 0; 113 private static final int MODE_ALL_APPS = 1; 114 private static final int MODE_ALL_APPS_ZOOMED = 2; 115 116 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate"; 117 118 static final String EXTRA_CUSTOM_WIDGET = "custom_widget"; 119 static final String SEARCH_WIDGET = "search_widget"; 120 121 static final int SCREEN_COUNT = 3; 122 static final int DEFAULT_SCREN = 1; 123 static final int NUMBER_CELLS_X = 4; 124 static final int NUMBER_CELLS_Y = 4; 125 126 static final int DIALOG_CREATE_SHORTCUT = 1; 127 static final int DIALOG_RENAME_FOLDER = 2; 128 129 private static final String PREFERENCES = "launcher.preferences"; 130 131 // Type: int 132 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen"; 133 // Type: boolean 134 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder"; 135 // Type: long 136 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder"; 137 // Type: int 138 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen"; 139 // Type: int 140 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX"; 141 // Type: int 142 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY"; 143 // Type: int 144 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX"; 145 // Type: int 146 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY"; 147 // Type: int 148 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX"; 149 // Type: int 150 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY"; 151 // Type: int[] 152 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells"; 153 // Type: boolean 154 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder"; 155 // Type: long 156 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id"; 157 158 static final int APPWIDGET_HOST_ID = 1024; 159 160 private static final Object sLock = new Object(); 161 private static int sScreen = DEFAULT_SCREN; 162 163 private LayoutInflater mInflater; 164 165 private DragController mDragController; 166 private SwipeController mSwipeController; 167 private Workspace mWorkspace; 168 169 private AppWidgetManager mAppWidgetManager; 170 private LauncherAppWidgetHost mAppWidgetHost; 171 172 private CellLayout.CellInfo mAddItemCellInfo; 173 private CellLayout.CellInfo mMenuAddInfo; 174 private final int[] mCellCoordinates = new int[2]; 175 private FolderInfo mFolderInfo; 176 177 private DeleteZone mDeleteZone; 178 private HandleView mHandleView; 179 private AllAppsView mAllAppsGrid; 180 private int mMode = MODE_WORKSPACE; 181 182 private Bundle mSavedState; 183 184 private SpannableStringBuilder mDefaultKeySsb = null; 185 186 private boolean mIsNewIntent; 187 188 private boolean mWorkspaceLoading = true; 189 190 private boolean mRestoring; 191 private boolean mWaitingForResult; 192 private boolean mLocaleChanged; 193 194 private boolean mHomeDown; 195 private boolean mBackDown; 196 197 private Bundle mSavedInstanceState; 198 199 private LauncherModel mModel; 200 201 private ArrayList<ItemInfo> mDesktopItems = new ArrayList(); 202 private static HashMap<Long, FolderInfo> mFolders = new HashMap(); 203 private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList(); 204 205 @Override 206 protected void onCreate(Bundle savedInstanceState) { 207 super.onCreate(savedInstanceState); 208 209 mModel = ((LauncherApplication)getApplication()).setLauncher(this); 210 mInflater = getLayoutInflater(); 211 212 mAppWidgetManager = AppWidgetManager.getInstance(this); 213 214 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID); 215 mAppWidgetHost.startListening(); 216 217 if (PROFILE_STARTUP) { 218 android.os.Debug.startMethodTracing("/sdcard/launcher"); 219 } 220 221 checkForLocaleChange(); 222 setWallpaperDimension(); 223 224 setContentView(R.layout.launcher); 225 setupViews(); 226 227 lockAllApps(); 228 229 mSavedState = savedInstanceState; 230 restoreState(mSavedState); 231 232 if (PROFILE_STARTUP) { 233 android.os.Debug.stopMethodTracing(); 234 } 235 236 if (!mRestoring) { 237 mModel.startLoader(this, true); 238 } 239 240 // For handling default keys 241 mDefaultKeySsb = new SpannableStringBuilder(); 242 Selection.setSelection(mDefaultKeySsb, 0); 243 } 244 245 private void checkForLocaleChange() { 246 final LocaleConfiguration localeConfiguration = new LocaleConfiguration(); 247 readConfiguration(this, localeConfiguration); 248 249 final Configuration configuration = getResources().getConfiguration(); 250 251 final String previousLocale = localeConfiguration.locale; 252 final String locale = configuration.locale.toString(); 253 254 final int previousMcc = localeConfiguration.mcc; 255 final int mcc = configuration.mcc; 256 257 final int previousMnc = localeConfiguration.mnc; 258 final int mnc = configuration.mnc; 259 260 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc; 261 262 if (mLocaleChanged) { 263 localeConfiguration.locale = locale; 264 localeConfiguration.mcc = mcc; 265 localeConfiguration.mnc = mnc; 266 267 writeConfiguration(this, localeConfiguration); 268 AppInfoCache.flush(); 269 } 270 } 271 272 private static class LocaleConfiguration { 273 public String locale; 274 public int mcc = -1; 275 public int mnc = -1; 276 } 277 278 private static void readConfiguration(Context context, LocaleConfiguration configuration) { 279 DataInputStream in = null; 280 try { 281 in = new DataInputStream(context.openFileInput(PREFERENCES)); 282 configuration.locale = in.readUTF(); 283 configuration.mcc = in.readInt(); 284 configuration.mnc = in.readInt(); 285 } catch (FileNotFoundException e) { 286 // Ignore 287 } catch (IOException e) { 288 // Ignore 289 } finally { 290 if (in != null) { 291 try { 292 in.close(); 293 } catch (IOException e) { 294 // Ignore 295 } 296 } 297 } 298 } 299 300 private static void writeConfiguration(Context context, LocaleConfiguration configuration) { 301 DataOutputStream out = null; 302 try { 303 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE)); 304 out.writeUTF(configuration.locale); 305 out.writeInt(configuration.mcc); 306 out.writeInt(configuration.mnc); 307 out.flush(); 308 } catch (FileNotFoundException e) { 309 // Ignore 310 } catch (IOException e) { 311 //noinspection ResultOfMethodCallIgnored 312 context.getFileStreamPath(PREFERENCES).delete(); 313 } finally { 314 if (out != null) { 315 try { 316 out.close(); 317 } catch (IOException e) { 318 // Ignore 319 } 320 } 321 } 322 } 323 324 static int getScreen() { 325 synchronized (sLock) { 326 return sScreen; 327 } 328 } 329 330 static void setScreen(int screen) { 331 synchronized (sLock) { 332 sScreen = screen; 333 } 334 } 335 336 private void setWallpaperDimension() { 337 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE); 338 339 Display display = getWindowManager().getDefaultDisplay(); 340 boolean isPortrait = display.getWidth() < display.getHeight(); 341 342 final int width = isPortrait ? display.getWidth() : display.getHeight(); 343 final int height = isPortrait ? display.getHeight() : display.getWidth(); 344 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height); 345 } 346 347 @Override 348 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 349 mWaitingForResult = false; 350 351 // The pattern used here is that a user PICKs a specific application, 352 // which, depending on the target, might need to CREATE the actual target. 353 354 // For example, the user would PICK_SHORTCUT for "Music playlist", and we 355 // launch over to the Music app to actually CREATE_SHORTCUT. 356 357 if (resultCode == RESULT_OK && mAddItemCellInfo != null) { 358 switch (requestCode) { 359 case REQUEST_PICK_APPLICATION: 360 completeAddApplication(this, data, mAddItemCellInfo); 361 break; 362 case REQUEST_PICK_SHORTCUT: 363 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT); 364 break; 365 case REQUEST_CREATE_SHORTCUT: 366 completeAddShortcut(data, mAddItemCellInfo); 367 break; 368 case REQUEST_PICK_LIVE_FOLDER: 369 addLiveFolder(data); 370 break; 371 case REQUEST_CREATE_LIVE_FOLDER: 372 completeAddLiveFolder(data, mAddItemCellInfo); 373 break; 374 case REQUEST_PICK_APPWIDGET: 375 addAppWidget(data); 376 break; 377 case REQUEST_CREATE_APPWIDGET: 378 completeAddAppWidget(data, mAddItemCellInfo); 379 break; 380 } 381 } else if (requestCode == REQUEST_PICK_APPWIDGET && 382 resultCode == RESULT_CANCELED && data != null) { 383 // Clean up the appWidgetId if we canceled 384 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); 385 if (appWidgetId != -1) { 386 mAppWidgetHost.deleteAppWidgetId(appWidgetId); 387 } 388 } 389 } 390 391 @Override 392 protected void onResume() { 393 super.onResume(); 394 395 if (mRestoring) { 396 mWorkspaceLoading = true; 397 mModel.startLoader(this, true); 398 mRestoring = false; 399 } 400 401 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by 402 // onNewIntent), then close the search dialog if needed, because it probably 403 // came from the user pressing 'home' (rather than, for example, pressing 'back'). 404 if (mIsNewIntent) { 405 // Post to a handler so that this happens after the search dialog tries to open 406 // itself again. 407 mWorkspace.post(new Runnable() { 408 public void run() { 409 ISearchManager searchManagerService = ISearchManager.Stub.asInterface( 410 ServiceManager.getService(Context.SEARCH_SERVICE)); 411 try { 412 searchManagerService.stopSearch(); 413 } catch (RemoteException e) { 414 e(LOG_TAG, "error stopping search", e); 415 } 416 } 417 }); 418 } 419 420 mIsNewIntent = false; 421 } 422 423 @Override 424 protected void onPause() { 425 super.onPause(); 426 closeAllApps(); 427 } 428 429 @Override 430 public Object onRetainNonConfigurationInstance() { 431 // Flag the loader to stop early before switching 432 mModel.stopLoader(); 433 434 if (PROFILE_ROTATE) { 435 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate"); 436 } 437 return null; 438 } 439 440 private boolean acceptFilter() { 441 final InputMethodManager inputManager = (InputMethodManager) 442 getSystemService(Context.INPUT_METHOD_SERVICE); 443 return !inputManager.isFullscreenMode(); 444 } 445 446 @Override 447 public boolean onKeyDown(int keyCode, KeyEvent event) { 448 boolean handled = super.onKeyDown(keyCode, event); 449 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) { 450 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb, 451 keyCode, event); 452 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) { 453 // something usable has been typed - start a search 454 // the typed text will be retrieved and cleared by 455 // showSearchDialog() 456 // If there are multiple keystrokes before the search dialog takes focus, 457 // onSearchRequested() will be called for every keystroke, 458 // but it is idempotent, so it's fine. 459 return onSearchRequested(); 460 } 461 } 462 463 return handled; 464 } 465 466 private String getTypedText() { 467 return mDefaultKeySsb.toString(); 468 } 469 470 private void clearTypedText() { 471 mDefaultKeySsb.clear(); 472 mDefaultKeySsb.clearSpans(); 473 Selection.setSelection(mDefaultKeySsb, 0); 474 } 475 476 /** 477 * Restores the previous state, if it exists. 478 * 479 * @param savedState The previous state. 480 */ 481 private void restoreState(Bundle savedState) { 482 if (savedState == null) { 483 return; 484 } 485 486 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1); 487 if (currentScreen > -1) { 488 mWorkspace.setCurrentScreen(currentScreen); 489 } 490 491 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1); 492 if (addScreen > -1) { 493 mAddItemCellInfo = new CellLayout.CellInfo(); 494 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo; 495 addItemCellInfo.valid = true; 496 addItemCellInfo.screen = addScreen; 497 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X); 498 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y); 499 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X); 500 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y); 501 addItemCellInfo.findVacantCellsFromOccupied( 502 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS), 503 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X), 504 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y)); 505 mRestoring = true; 506 } 507 508 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false); 509 if (renameFolder) { 510 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID); 511 mFolderInfo = mModel.getFolderById(this, mFolders, id); 512 mRestoring = true; 513 } 514 } 515 516 /** 517 * Finds all the views we need and configure them properly. 518 */ 519 private void setupViews() { 520 mDragController = new DragController(this); 521 DragController dragController = mDragController; 522 mSwipeController = new SwipeController(this, this); 523 SwipeController swipeController = mSwipeController; 524 swipeController.setRange(-1, 0); 525 526 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer); 527 dragLayer.setDragController(dragController); 528 dragLayer.setSwipeController(swipeController); 529 530 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view); 531 mAllAppsGrid.setLauncher(this); 532 mAllAppsGrid.setDragController(dragController); 533 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window. 534 535 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace); 536 final Workspace workspace = mWorkspace; 537 538 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone); 539 mDeleteZone = deleteZone; 540 541 mHandleView = (HandleView) findViewById(R.id.all_apps_button); 542 mHandleView.setLauncher(this); 543 mHandleView.setOnClickListener(this); 544 /* TODO 545 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable(); 546 handleIocon.setCrossFadeEnabled(true); 547 */ 548 549 workspace.setOnLongClickListener(this); 550 workspace.setDragController(dragController); 551 workspace.setLauncher(this); 552 553 deleteZone.setLauncher(this); 554 deleteZone.setDragController(dragController); 555 deleteZone.setHandle(mHandleView); 556 557 dragController.setDragScoller(workspace); 558 dragController.setDragListener(deleteZone); 559 dragController.setScrollView(dragLayer); 560 561 // The order here is bottom to top. 562 dragController.addDropTarget(workspace); 563 dragController.addDropTarget(deleteZone); 564 } 565 566 /** 567 * Creates a view representing a shortcut. 568 * 569 * @param info The data structure describing the shortcut. 570 * 571 * @return A View inflated from R.layout.application. 572 */ 573 View createShortcut(ApplicationInfo info) { 574 return createShortcut(R.layout.application, 575 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); 576 } 577 578 /** 579 * Creates a view representing a shortcut inflated from the specified resource. 580 * 581 * @param layoutResId The id of the XML layout used to create the shortcut. 582 * @param parent The group the shortcut belongs to. 583 * @param info The data structure describing the shortcut. 584 * 585 * @return A View inflated from layoutResId. 586 */ 587 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) { 588 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false); 589 590 if (info.icon == null) { 591 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info); 592 } 593 if (!info.filtered) { 594 info.icon = Utilities.createIconThumbnail(info.icon, this); 595 info.filtered = true; 596 } 597 598 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null); 599 favorite.setText(info.title); 600 favorite.setTag(info); 601 favorite.setOnClickListener(this); 602 603 return favorite; 604 } 605 606 /** 607 * Add an application shortcut to the workspace. 608 * 609 * @param data The intent describing the application. 610 * @param cellInfo The position on screen where to create the shortcut. 611 */ 612 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) { 613 cellInfo.screen = mWorkspace.getCurrentScreen(); 614 if (!findSingleSlot(cellInfo)) return; 615 616 final ApplicationInfo info = infoFromApplicationIntent(context, data); 617 if (info != null) { 618 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked()); 619 } 620 } 621 622 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) { 623 ComponentName component = data.getComponent(); 624 PackageManager packageManager = context.getPackageManager(); 625 ActivityInfo activityInfo = null; 626 try { 627 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */); 628 } catch (NameNotFoundException e) { 629 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e); 630 } 631 632 if (activityInfo != null) { 633 ApplicationInfo itemInfo = new ApplicationInfo(); 634 635 itemInfo.title = activityInfo.loadLabel(packageManager); 636 if (itemInfo.title == null) { 637 itemInfo.title = activityInfo.name; 638 } 639 640 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK | 641 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 642 itemInfo.icon = activityInfo.loadIcon(packageManager); 643 itemInfo.container = ItemInfo.NO_ID; 644 645 return itemInfo; 646 } 647 648 return null; 649 } 650 651 /** 652 * Add a shortcut to the workspace. 653 * 654 * @param data The intent describing the shortcut. 655 * @param cellInfo The position on screen where to create the shortcut. 656 */ 657 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) { 658 cellInfo.screen = mWorkspace.getCurrentScreen(); 659 if (!findSingleSlot(cellInfo)) return; 660 661 final ApplicationInfo info = addShortcut(this, data, cellInfo, false); 662 663 if (!mRestoring) { 664 final View view = createShortcut(info); 665 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, 666 isWorkspaceLocked()); 667 } 668 } 669 670 671 /** 672 * Add a widget to the workspace. 673 * 674 * @param data The intent describing the appWidgetId. 675 * @param cellInfo The position on screen where to create the widget. 676 */ 677 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) { 678 Bundle extras = data.getExtras(); 679 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); 680 681 d(LOG_TAG, "dumping extras content="+extras.toString()); 682 683 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); 684 685 // Calculate the grid spans needed to fit this widget 686 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen); 687 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight); 688 689 // Try finding open space on Launcher screen 690 final int[] xy = mCellCoordinates; 691 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return; 692 693 // Build Launcher-specific widget info and save to database 694 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId); 695 launcherInfo.spanX = spans[0]; 696 launcherInfo.spanY = spans[1]; 697 698 LauncherModel.addItemToDatabase(this, launcherInfo, 699 LauncherSettings.Favorites.CONTAINER_DESKTOP, 700 mWorkspace.getCurrentScreen(), xy[0], xy[1], false); 701 702 if (!mRestoring) { 703 mDesktopItems.add(launcherInfo); 704 705 // Perform actual inflation because we're live 706 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); 707 708 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo); 709 launcherInfo.hostView.setTag(launcherInfo); 710 711 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1], 712 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked()); 713 } 714 } 715 716 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) { 717 mDesktopItems.remove(launcherInfo); 718 launcherInfo.hostView = null; 719 } 720 721 public LauncherAppWidgetHost getAppWidgetHost() { 722 return mAppWidgetHost; 723 } 724 725 static ApplicationInfo addShortcut(Context context, Intent data, 726 CellLayout.CellInfo cellInfo, boolean notify) { 727 728 final ApplicationInfo info = infoFromShortcutIntent(context, data); 729 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, 730 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify); 731 732 return info; 733 } 734 735 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) { 736 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); 737 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); 738 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); 739 740 Drawable icon = null; 741 boolean filtered = false; 742 boolean customIcon = false; 743 ShortcutIconResource iconResource = null; 744 745 if (bitmap != null) { 746 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context)); 747 filtered = true; 748 customIcon = true; 749 } else { 750 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE); 751 if (extra != null && extra instanceof ShortcutIconResource) { 752 try { 753 iconResource = (ShortcutIconResource) extra; 754 final PackageManager packageManager = context.getPackageManager(); 755 Resources resources = packageManager.getResourcesForApplication( 756 iconResource.packageName); 757 final int id = resources.getIdentifier(iconResource.resourceName, null, null); 758 icon = resources.getDrawable(id); 759 } catch (Exception e) { 760 w(LOG_TAG, "Could not load shortcut icon: " + extra); 761 } 762 } 763 } 764 765 if (icon == null) { 766 icon = context.getPackageManager().getDefaultActivityIcon(); 767 } 768 769 final ApplicationInfo info = new ApplicationInfo(); 770 info.icon = icon; 771 info.filtered = filtered; 772 info.title = name; 773 info.intent = intent; 774 info.customIcon = customIcon; 775 info.iconResource = iconResource; 776 777 return info; 778 } 779 780 @Override 781 protected void onNewIntent(Intent intent) { 782 super.onNewIntent(intent); 783 784 // Close the menu 785 if (Intent.ACTION_MAIN.equals(intent.getAction())) { 786 getWindow().closeAllPanels(); 787 788 // Set this flag so that onResume knows to close the search dialog if it's open, 789 // because this was a new intent (thus a press of 'home' or some such) rather than 790 // for example onResume being called when the user pressed the 'back' button. 791 mIsNewIntent = true; 792 793 try { 794 dismissDialog(DIALOG_CREATE_SHORTCUT); 795 // Unlock the workspace if the dialog was showing 796 } catch (Exception e) { 797 // An exception is thrown if the dialog is not visible, which is fine 798 } 799 800 try { 801 dismissDialog(DIALOG_RENAME_FOLDER); 802 // Unlock the workspace if the dialog was showing 803 } catch (Exception e) { 804 // An exception is thrown if the dialog is not visible, which is fine 805 } 806 807 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 808 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) { 809 810 if (!mWorkspace.isDefaultScreenShowing()) { 811 mWorkspace.moveToDefaultScreen(); 812 } 813 814 closeAllApps(); 815 816 final View v = getWindow().peekDecorView(); 817 if (v != null && v.getWindowToken() != null) { 818 InputMethodManager imm = (InputMethodManager)getSystemService( 819 INPUT_METHOD_SERVICE); 820 imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 821 } 822 } else { 823 closeAllApps(); 824 } 825 } 826 } 827 828 @Override 829 protected void onRestoreInstanceState(Bundle savedInstanceState) { 830 // Do not call super here 831 mSavedInstanceState = savedInstanceState; 832 } 833 834 @Override 835 protected void onSaveInstanceState(Bundle outState) { 836 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen()); 837 838 final ArrayList<Folder> folders = mWorkspace.getOpenFolders(); 839 if (folders.size() > 0) { 840 final int count = folders.size(); 841 long[] ids = new long[count]; 842 for (int i = 0; i < count; i++) { 843 final FolderInfo info = folders.get(i).getInfo(); 844 ids[i] = info.id; 845 } 846 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids); 847 } else { 848 super.onSaveInstanceState(outState); 849 } 850 851 final boolean isConfigurationChange = getChangingConfigurations() != 0; 852 853 // When the drawer is opened and we are saving the state because of a 854 // configuration change 855 // TODO should not do this if the drawer is currently closing. 856 if (isAllAppsVisible() && isConfigurationChange) { 857 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true); 858 } 859 860 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) { 861 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo; 862 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen); 863 864 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen); 865 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX); 866 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY); 867 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX); 868 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY); 869 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX()); 870 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY()); 871 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS, 872 layout.getOccupiedCells()); 873 } 874 875 if (mFolderInfo != null && mWaitingForResult) { 876 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true); 877 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id); 878 } 879 } 880 881 @Override 882 public void onDestroy() { 883 super.onDestroy(); 884 885 try { 886 mAppWidgetHost.stopListening(); 887 } catch (NullPointerException ex) { 888 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex); 889 } 890 891 TextKeyListener.getInstance().release(); 892 893 mModel.stopLoader(); 894 895 unbindDesktopItems(); 896 AppInfoCache.unbindDrawables(); 897 } 898 899 @Override 900 public void startActivityForResult(Intent intent, int requestCode) { 901 if (requestCode >= 0) mWaitingForResult = true; 902 super.startActivityForResult(intent, requestCode); 903 } 904 905 @Override 906 public void startSearch(String initialQuery, boolean selectInitialQuery, 907 Bundle appSearchData, boolean globalSearch) { 908 909 closeAllApps(); 910 911 // Slide the search widget to the top, if it's on the current screen, 912 // otherwise show the search dialog immediately. 913 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen(); 914 if (searchWidget == null) { 915 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch); 916 } else { 917 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch); 918 // show the currently typed text in the search widget while sliding 919 searchWidget.setQuery(getTypedText()); 920 } 921 } 922 923 /** 924 * Show the search dialog immediately, without changing the search widget. 925 * 926 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean) 927 */ 928 void showSearchDialog(String initialQuery, boolean selectInitialQuery, 929 Bundle appSearchData, boolean globalSearch) { 930 931 if (initialQuery == null) { 932 // Use any text typed in the launcher as the initial query 933 initialQuery = getTypedText(); 934 clearTypedText(); 935 } 936 if (appSearchData == null) { 937 appSearchData = new Bundle(); 938 appSearchData.putString(SearchManager.SOURCE, "launcher-search"); 939 } 940 941 final SearchManager searchManager = 942 (SearchManager) getSystemService(Context.SEARCH_SERVICE); 943 944 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen(); 945 if (searchWidget != null) { 946 // This gets called when the user leaves the search dialog to go back to 947 // the Launcher. 948 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() { 949 public void onCancel() { 950 searchManager.setOnCancelListener(null); 951 stopSearch(); 952 } 953 }); 954 } 955 956 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(), 957 appSearchData, globalSearch); 958 } 959 960 /** 961 * Cancel search dialog if it is open. 962 */ 963 void stopSearch() { 964 // Close search dialog 965 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 966 searchManager.stopSearch(); 967 // Restore search widget to its normal position 968 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen(); 969 if (searchWidget != null) { 970 searchWidget.stopSearch(false); 971 } 972 } 973 974 @Override 975 public boolean onCreateOptionsMenu(Menu menu) { 976 if (isWorkspaceLocked()) { 977 return false; 978 } 979 980 super.onCreateOptionsMenu(menu); 981 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add) 982 .setIcon(android.R.drawable.ic_menu_add) 983 .setAlphabeticShortcut('A'); 984 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper) 985 .setIcon(android.R.drawable.ic_menu_gallery) 986 .setAlphabeticShortcut('W'); 987 menu.add(0, MENU_SEARCH, 0, R.string.menu_search) 988 .setIcon(android.R.drawable.ic_search_category_default) 989 .setAlphabeticShortcut(SearchManager.MENU_KEY); 990 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications) 991 .setIcon(com.android.internal.R.drawable.ic_menu_notifications) 992 .setAlphabeticShortcut('N'); 993 994 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS); 995 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 996 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 997 998 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings) 999 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P') 1000 .setIntent(settings); 1001 1002 return true; 1003 } 1004 1005 @Override 1006 public boolean onPrepareOptionsMenu(Menu menu) { 1007 super.onPrepareOptionsMenu(menu); 1008 1009 mMenuAddInfo = mWorkspace.findAllVacantCells(null); 1010 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid); 1011 1012 return true; 1013 } 1014 1015 @Override 1016 public boolean onOptionsItemSelected(MenuItem item) { 1017 switch (item.getItemId()) { 1018 case MENU_ADD: 1019 addItems(); 1020 return true; 1021 case MENU_WALLPAPER_SETTINGS: 1022 startWallpaper(); 1023 return true; 1024 case MENU_SEARCH: 1025 onSearchRequested(); 1026 return true; 1027 case MENU_NOTIFICATIONS: 1028 showNotifications(); 1029 return true; 1030 } 1031 1032 return super.onOptionsItemSelected(item); 1033 } 1034 1035 /** 1036 * Indicates that we want global search for this activity by setting the globalSearch 1037 * argument for {@link #startSearch} to true. 1038 */ 1039 1040 @Override 1041 public boolean onSearchRequested() { 1042 startSearch(null, false, null, true); 1043 return true; 1044 } 1045 1046 public boolean isWorkspaceLocked() { 1047 return mWorkspaceLoading || mWaitingForResult; 1048 } 1049 1050 private void addItems() { 1051 showAddDialog(mMenuAddInfo); 1052 } 1053 1054 void addAppWidget(Intent data) { 1055 // TODO: catch bad widget exception when sent 1056 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); 1057 1058 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET); 1059 if (SEARCH_WIDGET.equals(customWidget)) { 1060 // We don't need this any more, since this isn't a real app widget. 1061 mAppWidgetHost.deleteAppWidgetId(appWidgetId); 1062 // add the search widget 1063 addSearch(); 1064 } else { 1065 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId); 1066 1067 if (appWidget.configure != null) { 1068 // Launch over to configure widget, if needed 1069 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); 1070 intent.setComponent(appWidget.configure); 1071 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 1072 1073 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET); 1074 } else { 1075 // Otherwise just add it 1076 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data); 1077 } 1078 } 1079 } 1080 1081 void addSearch() { 1082 final Widget info = Widget.makeSearch(); 1083 final CellLayout.CellInfo cellInfo = mAddItemCellInfo; 1084 1085 final int[] xy = mCellCoordinates; 1086 final int spanX = info.spanX; 1087 final int spanY = info.spanY; 1088 1089 if (!findSlot(cellInfo, xy, spanX, spanY)) return; 1090 1091 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, 1092 mWorkspace.getCurrentScreen(), xy[0], xy[1], false); 1093 1094 final View view = mInflater.inflate(info.layoutResource, null); 1095 view.setTag(info); 1096 Search search = (Search) view.findViewById(R.id.widget_search); 1097 search.setLauncher(this); 1098 1099 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY); 1100 } 1101 1102 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) { 1103 // Handle case where user selected "Applications" 1104 String applicationName = getResources().getString(R.string.group_applications); 1105 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); 1106 1107 if (applicationName != null && applicationName.equals(shortcutName)) { 1108 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 1109 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 1110 1111 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); 1112 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent); 1113 startActivityForResult(pickIntent, requestCodeApplication); 1114 } else { 1115 startActivityForResult(intent, requestCodeShortcut); 1116 } 1117 } 1118 1119 void addLiveFolder(Intent intent) { 1120 // Handle case where user selected "Folder" 1121 String folderName = getResources().getString(R.string.group_folder); 1122 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); 1123 1124 if (folderName != null && folderName.equals(shortcutName)) { 1125 addFolder(); 1126 } else { 1127 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER); 1128 } 1129 } 1130 1131 void addFolder() { 1132 UserFolderInfo folderInfo = new UserFolderInfo(); 1133 folderInfo.title = getText(R.string.folder_name); 1134 1135 CellLayout.CellInfo cellInfo = mAddItemCellInfo; 1136 cellInfo.screen = mWorkspace.getCurrentScreen(); 1137 if (!findSingleSlot(cellInfo)) return; 1138 1139 // Update the model 1140 LauncherModel.addItemToDatabase(this, folderInfo, 1141 LauncherSettings.Favorites.CONTAINER_DESKTOP, 1142 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false); 1143 mFolders.put(folderInfo.id, folderInfo); 1144 1145 // Create the view 1146 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, 1147 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo); 1148 mWorkspace.addInCurrentScreen(newFolder, 1149 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked()); 1150 } 1151 1152 void removeFolder(FolderInfo folder) { 1153 mFolders.remove(folder.id); 1154 } 1155 1156 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) { 1157 cellInfo.screen = mWorkspace.getCurrentScreen(); 1158 if (!findSingleSlot(cellInfo)) return; 1159 1160 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false); 1161 1162 if (!mRestoring) { 1163 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this, 1164 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); 1165 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, 1166 isWorkspaceLocked()); 1167 } 1168 } 1169 1170 static LiveFolderInfo addLiveFolder(Context context, Intent data, 1171 CellLayout.CellInfo cellInfo, boolean notify) { 1172 1173 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT); 1174 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME); 1175 1176 Drawable icon = null; 1177 boolean filtered = false; 1178 Intent.ShortcutIconResource iconResource = null; 1179 1180 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON); 1181 if (extra != null && extra instanceof Intent.ShortcutIconResource) { 1182 try { 1183 iconResource = (Intent.ShortcutIconResource) extra; 1184 final PackageManager packageManager = context.getPackageManager(); 1185 Resources resources = packageManager.getResourcesForApplication( 1186 iconResource.packageName); 1187 final int id = resources.getIdentifier(iconResource.resourceName, null, null); 1188 icon = resources.getDrawable(id); 1189 } catch (Exception e) { 1190 w(LOG_TAG, "Could not load live folder icon: " + extra); 1191 } 1192 } 1193 1194 if (icon == null) { 1195 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder); 1196 } 1197 1198 final LiveFolderInfo info = new LiveFolderInfo(); 1199 info.icon = icon; 1200 info.filtered = filtered; 1201 info.title = name; 1202 info.iconResource = iconResource; 1203 info.uri = data.getData(); 1204 info.baseIntent = baseIntent; 1205 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, 1206 LiveFolders.DISPLAY_MODE_GRID); 1207 1208 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, 1209 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify); 1210 mFolders.put(info.id, info); 1211 1212 return info; 1213 } 1214 1215 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) { 1216 final int[] xy = new int[2]; 1217 if (findSlot(cellInfo, xy, 1, 1)) { 1218 cellInfo.cellX = xy[0]; 1219 cellInfo.cellY = xy[1]; 1220 return true; 1221 } 1222 return false; 1223 } 1224 1225 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) { 1226 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) { 1227 boolean[] occupied = mSavedState != null ? 1228 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null; 1229 cellInfo = mWorkspace.findAllVacantCells(occupied); 1230 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) { 1231 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show(); 1232 return false; 1233 } 1234 } 1235 return true; 1236 } 1237 1238 private void showNotifications() { 1239 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE); 1240 if (statusBar != null) { 1241 statusBar.expand(); 1242 } 1243 } 1244 1245 private void startWallpaper() { 1246 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER); 1247 Intent chooser = Intent.createChooser(pickWallpaper, 1248 getText(R.string.chooser_wallpaper)); 1249 WallpaperManager wm = (WallpaperManager) 1250 getSystemService(Context.WALLPAPER_SERVICE); 1251 WallpaperInfo wi = wm.getWallpaperInfo(); 1252 if (wi != null && wi.getSettingsActivity() != null) { 1253 LabeledIntent li = new LabeledIntent(getPackageName(), 1254 R.string.configure_wallpaper, 0); 1255 li.setClassName(wi.getPackageName(), wi.getSettingsActivity()); 1256 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li }); 1257 } 1258 startActivity(chooser); 1259 } 1260 1261 @Override 1262 public void onWindowFocusChanged(boolean hasFocus) { 1263 super.onWindowFocusChanged(hasFocus); 1264 if (!hasFocus) { 1265 mBackDown = mHomeDown = false; 1266 } 1267 } 1268 1269 @Override 1270 public boolean dispatchKeyEvent(KeyEvent event) { 1271 if (event.getAction() == KeyEvent.ACTION_DOWN) { 1272 switch (event.getKeyCode()) { 1273 case KeyEvent.KEYCODE_BACK: 1274 mBackDown = true; 1275 return true; 1276 case KeyEvent.KEYCODE_HOME: 1277 mHomeDown = true; 1278 return true; 1279 } 1280 } else if (event.getAction() == KeyEvent.ACTION_UP) { 1281 switch (event.getKeyCode()) { 1282 case KeyEvent.KEYCODE_BACK: 1283 if (!event.isCanceled()) { 1284 mWorkspace.dispatchKeyEvent(event); 1285 if (isAllAppsVisible()) { 1286 closeAllApps(); 1287 } else { 1288 closeFolder(); 1289 } 1290 } 1291 mBackDown = false; 1292 return true; 1293 case KeyEvent.KEYCODE_HOME: 1294 mHomeDown = false; 1295 return true; 1296 } 1297 } 1298 1299 return super.dispatchKeyEvent(event); 1300 } 1301 1302 private void closeFolder() { 1303 Folder folder = mWorkspace.getOpenFolder(); 1304 if (folder != null) { 1305 closeFolder(folder); 1306 } 1307 } 1308 1309 void closeFolder(Folder folder) { 1310 folder.getInfo().opened = false; 1311 ViewGroup parent = (ViewGroup) folder.getParent(); 1312 if (parent != null) { 1313 parent.removeView(folder); 1314 // TODO: this line crashes. 1315 //mDragController.removeDropTarget((DropTarget)folder); 1316 } 1317 folder.onClose(); 1318 } 1319 1320 /** 1321 * Go through the and disconnect any of the callbacks in the drawables and the views or we 1322 * leak the previous Home screen on orientation change. 1323 */ 1324 private void unbindDesktopItems() { 1325 for (ItemInfo item: mDesktopItems) { 1326 item.unbind(); 1327 } 1328 } 1329 1330 /** 1331 * Launches the intent referred by the clicked shortcut. 1332 * 1333 * @param v The view representing the clicked shortcut. 1334 */ 1335 public void onClick(View v) { 1336 Object tag = v.getTag(); 1337 if (tag instanceof ApplicationInfo) { 1338 // Open shortcut 1339 final Intent intent = ((ApplicationInfo) tag).intent; 1340 startActivitySafely(intent); 1341 } else if (tag instanceof FolderInfo) { 1342 handleFolderClick((FolderInfo) tag); 1343 } else if (v == mHandleView) { 1344 Log.d(TAG, "onClick"); 1345 if (isAllAppsVisible()) { 1346 closeAllApps(); 1347 } else { 1348 showAllApps(); 1349 } 1350 } 1351 } 1352 1353 void startActivitySafely(Intent intent) { 1354 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 1355 try { 1356 startActivity(intent); 1357 } catch (ActivityNotFoundException e) { 1358 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); 1359 } catch (SecurityException e) { 1360 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); 1361 e(LOG_TAG, "Launcher does not have the permission to launch " + intent + 1362 ". Make sure to create a MAIN intent-filter for the corresponding activity " + 1363 "or use the exported attribute for this activity.", e); 1364 } 1365 } 1366 1367 private void handleFolderClick(FolderInfo folderInfo) { 1368 if (!folderInfo.opened) { 1369 // Close any open folder 1370 closeFolder(); 1371 // Open the requested folder 1372 openFolder(folderInfo); 1373 } else { 1374 // Find the open folder... 1375 Folder openFolder = mWorkspace.getFolderForTag(folderInfo); 1376 int folderScreen; 1377 if (openFolder != null) { 1378 folderScreen = mWorkspace.getScreenForView(openFolder); 1379 // .. and close it 1380 closeFolder(openFolder); 1381 if (folderScreen != mWorkspace.getCurrentScreen()) { 1382 // Close any folder open on the current screen 1383 closeFolder(); 1384 // Pull the folder onto this screen 1385 openFolder(folderInfo); 1386 } 1387 } 1388 } 1389 } 1390 1391 /** 1392 * Opens the user fodler described by the specified tag. The opening of the folder 1393 * is animated relative to the specified View. If the View is null, no animation 1394 * is played. 1395 * 1396 * @param folderInfo The FolderInfo describing the folder to open. 1397 */ 1398 private void openFolder(FolderInfo folderInfo) { 1399 Folder openFolder; 1400 1401 if (folderInfo instanceof UserFolderInfo) { 1402 openFolder = UserFolder.fromXml(this); 1403 } else if (folderInfo instanceof LiveFolderInfo) { 1404 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo); 1405 } else { 1406 return; 1407 } 1408 1409 openFolder.setDragController(mDragController); 1410 openFolder.setLauncher(this); 1411 1412 openFolder.bind(folderInfo); 1413 folderInfo.opened = true; 1414 1415 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4); 1416 openFolder.onOpen(); 1417 } 1418 1419 public boolean onLongClick(View v) { 1420 if (isWorkspaceLocked()) { 1421 return false; 1422 } 1423 1424 if (!(v instanceof CellLayout)) { 1425 v = (View) v.getParent(); 1426 } 1427 1428 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag(); 1429 1430 // This happens when long clicking an item with the dpad/trackball 1431 if (cellInfo == null) { 1432 return true; 1433 } 1434 1435 if (mWorkspace.allowLongPress()) { 1436 Log.d(TAG, "there"); 1437 mSwipeController.cancelSwipe(); 1438 if (cellInfo.cell == null) { 1439 if (cellInfo.valid) { 1440 // User long pressed on empty space 1441 mWorkspace.setAllowLongPress(false); 1442 showAddDialog(cellInfo); 1443 } 1444 } else { 1445 if (!(cellInfo.cell instanceof Folder)) { 1446 // User long pressed on an item 1447 mWorkspace.startDrag(cellInfo); 1448 } 1449 } 1450 } 1451 return true; 1452 } 1453 1454 View getDrawerHandle() { 1455 return mHandleView; 1456 } 1457 1458 Workspace getWorkspace() { 1459 return mWorkspace; 1460 } 1461 1462 /* TODO 1463 GridView getApplicationsGrid() { 1464 return mAllAppsGrid; 1465 } 1466 */ 1467 1468 @Override 1469 protected Dialog onCreateDialog(int id) { 1470 switch (id) { 1471 case DIALOG_CREATE_SHORTCUT: 1472 return new CreateShortcut().createDialog(); 1473 case DIALOG_RENAME_FOLDER: 1474 return new RenameFolder().createDialog(); 1475 } 1476 1477 return super.onCreateDialog(id); 1478 } 1479 1480 @Override 1481 protected void onPrepareDialog(int id, Dialog dialog) { 1482 switch (id) { 1483 case DIALOG_CREATE_SHORTCUT: 1484 break; 1485 case DIALOG_RENAME_FOLDER: 1486 if (mFolderInfo != null) { 1487 EditText input = (EditText) dialog.findViewById(R.id.folder_name); 1488 final CharSequence text = mFolderInfo.title; 1489 input.setText(text); 1490 input.setSelection(0, text.length()); 1491 } 1492 break; 1493 } 1494 } 1495 1496 void showRenameDialog(FolderInfo info) { 1497 mFolderInfo = info; 1498 mWaitingForResult = true; 1499 showDialog(DIALOG_RENAME_FOLDER); 1500 } 1501 1502 private void showAddDialog(CellLayout.CellInfo cellInfo) { 1503 mAddItemCellInfo = cellInfo; 1504 mWaitingForResult = true; 1505 showDialog(DIALOG_CREATE_SHORTCUT); 1506 } 1507 1508 private void pickShortcut(int requestCode, int title) { 1509 Bundle bundle = new Bundle(); 1510 1511 ArrayList<String> shortcutNames = new ArrayList<String>(); 1512 shortcutNames.add(getString(R.string.group_applications)); 1513 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); 1514 1515 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>(); 1516 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this, 1517 R.drawable.ic_launcher_application)); 1518 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons); 1519 1520 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); 1521 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT)); 1522 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title)); 1523 pickIntent.putExtras(bundle); 1524 1525 startActivityForResult(pickIntent, requestCode); 1526 } 1527 1528 private class RenameFolder { 1529 private EditText mInput; 1530 1531 Dialog createDialog() { 1532 mWaitingForResult = true; 1533 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null); 1534 mInput = (EditText) layout.findViewById(R.id.folder_name); 1535 1536 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this); 1537 builder.setIcon(0); 1538 builder.setTitle(getString(R.string.rename_folder_title)); 1539 builder.setCancelable(true); 1540 builder.setOnCancelListener(new Dialog.OnCancelListener() { 1541 public void onCancel(DialogInterface dialog) { 1542 cleanup(); 1543 } 1544 }); 1545 builder.setNegativeButton(getString(R.string.cancel_action), 1546 new Dialog.OnClickListener() { 1547 public void onClick(DialogInterface dialog, int which) { 1548 cleanup(); 1549 } 1550 } 1551 ); 1552 builder.setPositiveButton(getString(R.string.rename_action), 1553 new Dialog.OnClickListener() { 1554 public void onClick(DialogInterface dialog, int which) { 1555 changeFolderName(); 1556 } 1557 } 1558 ); 1559 builder.setView(layout); 1560 1561 final AlertDialog dialog = builder.create(); 1562 dialog.setOnShowListener(new DialogInterface.OnShowListener() { 1563 public void onShow(DialogInterface dialog) { 1564 } 1565 }); 1566 1567 return dialog; 1568 } 1569 1570 private void changeFolderName() { 1571 final String name = mInput.getText().toString(); 1572 if (!TextUtils.isEmpty(name)) { 1573 // Make sure we have the right folder info 1574 mFolderInfo = mFolders.get(mFolderInfo.id); 1575 mFolderInfo.title = name; 1576 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo); 1577 1578 if (mWorkspaceLoading) { 1579 lockAllApps(); 1580 mModel.setWorkspaceDirty(); 1581 mModel.startLoader(Launcher.this, false); 1582 } else { 1583 final FolderIcon folderIcon = (FolderIcon) 1584 mWorkspace.getViewForTag(mFolderInfo); 1585 if (folderIcon != null) { 1586 folderIcon.setText(name); 1587 getWorkspace().requestLayout(); 1588 } else { 1589 lockAllApps(); 1590 mModel.setWorkspaceDirty(); 1591 mWorkspaceLoading = true; 1592 mModel.startLoader(Launcher.this, false); 1593 } 1594 } 1595 } 1596 cleanup(); 1597 } 1598 1599 private void cleanup() { 1600 dismissDialog(DIALOG_RENAME_FOLDER); 1601 mWaitingForResult = false; 1602 mFolderInfo = null; 1603 } 1604 } 1605 1606 boolean isAllAppsVisible() { 1607 return mAllAppsGrid.isZooming() || mAllAppsGrid.isVisible(); 1608 } 1609 1610 void showAllApps() { 1611 if (mMode == MODE_ALL_APPS) { 1612 return; 1613 } 1614 1615 mSwipeController.setRange(-1, 0); 1616 mSwipeController.setImmediate(-1); 1617 mWorkspace.hide(); 1618 1619 // TODO: fade these two too 1620 mDeleteZone.setVisibility(View.GONE); 1621 //mHandleView.setVisibility(View.GONE); 1622 } 1623 1624 void closeAllApps() { 1625 if (mAllAppsGrid.isVisible()) { 1626 mSwipeController.setRange(0, 1); 1627 mSwipeController.setImmediate(1); 1628 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus(); 1629 1630 // TODO: fade these two too 1631 /* 1632 mDeleteZone.setVisibility(View.VISIBLE); 1633 mHandleView.setVisibility(View.VISIBLE); 1634 */ 1635 } 1636 } 1637 1638 void lockAllApps() { 1639 // TODO 1640 } 1641 1642 void unlockAllApps() { 1643 // TODO 1644 } 1645 1646 /** 1647 * Displays the shortcut creation dialog and launches, if necessary, the 1648 * appropriate activity. 1649 */ 1650 private class CreateShortcut implements DialogInterface.OnClickListener, 1651 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener, 1652 DialogInterface.OnShowListener { 1653 1654 private AddAdapter mAdapter; 1655 1656 Dialog createDialog() { 1657 mWaitingForResult = true; 1658 1659 mAdapter = new AddAdapter(Launcher.this); 1660 1661 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this); 1662 builder.setTitle(getString(R.string.menu_item_add_item)); 1663 builder.setAdapter(mAdapter, this); 1664 1665 builder.setInverseBackgroundForced(true); 1666 1667 AlertDialog dialog = builder.create(); 1668 dialog.setOnCancelListener(this); 1669 dialog.setOnDismissListener(this); 1670 dialog.setOnShowListener(this); 1671 1672 return dialog; 1673 } 1674 1675 public void onCancel(DialogInterface dialog) { 1676 mWaitingForResult = false; 1677 cleanup(); 1678 } 1679 1680 public void onDismiss(DialogInterface dialog) { 1681 } 1682 1683 private void cleanup() { 1684 dismissDialog(DIALOG_CREATE_SHORTCUT); 1685 } 1686 1687 /** 1688 * Handle the action clicked in the "Add to home" dialog. 1689 */ 1690 public void onClick(DialogInterface dialog, int which) { 1691 Resources res = getResources(); 1692 cleanup(); 1693 1694 switch (which) { 1695 case AddAdapter.ITEM_SHORTCUT: { 1696 // Insert extra item to handle picking application 1697 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut); 1698 break; 1699 } 1700 1701 case AddAdapter.ITEM_APPWIDGET: { 1702 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId(); 1703 1704 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK); 1705 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 1706 // add the search widget 1707 ArrayList<AppWidgetProviderInfo> customInfo = 1708 new ArrayList<AppWidgetProviderInfo>(); 1709 AppWidgetProviderInfo info = new AppWidgetProviderInfo(); 1710 info.provider = new ComponentName(getPackageName(), "XXX.YYY"); 1711 info.label = getString(R.string.group_search); 1712 info.icon = R.drawable.ic_search_widget; 1713 customInfo.add(info); 1714 pickIntent.putParcelableArrayListExtra( 1715 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo); 1716 ArrayList<Bundle> customExtras = new ArrayList<Bundle>(); 1717 Bundle b = new Bundle(); 1718 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET); 1719 customExtras.add(b); 1720 pickIntent.putParcelableArrayListExtra( 1721 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras); 1722 // start the pick activity 1723 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET); 1724 break; 1725 } 1726 1727 case AddAdapter.ITEM_LIVE_FOLDER: { 1728 // Insert extra item to handle inserting folder 1729 Bundle bundle = new Bundle(); 1730 1731 ArrayList<String> shortcutNames = new ArrayList<String>(); 1732 shortcutNames.add(res.getString(R.string.group_folder)); 1733 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); 1734 1735 ArrayList<ShortcutIconResource> shortcutIcons = 1736 new ArrayList<ShortcutIconResource>(); 1737 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this, 1738 R.drawable.ic_launcher_folder)); 1739 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons); 1740 1741 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); 1742 pickIntent.putExtra(Intent.EXTRA_INTENT, 1743 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER)); 1744 pickIntent.putExtra(Intent.EXTRA_TITLE, 1745 getText(R.string.title_select_live_folder)); 1746 pickIntent.putExtras(bundle); 1747 1748 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER); 1749 break; 1750 } 1751 1752 case AddAdapter.ITEM_WALLPAPER: { 1753 startWallpaper(); 1754 break; 1755 } 1756 } 1757 } 1758 1759 public void onShow(DialogInterface dialog) { 1760 } 1761 } 1762 1763 /** 1764 * Implementation of the method from SwipeController.SwipeListener. 1765 */ 1766 public void onStartSwipe() { 1767 switch (mMode) { 1768 case MODE_WORKSPACE: 1769 mWorkspace.enableChildrenCache(); 1770 break; 1771 case MODE_ALL_APPS: 1772 break; 1773 case MODE_ALL_APPS_ZOOMED: 1774 break; 1775 } 1776 } 1777 1778 /** 1779 * Implementation of the method from SwipeController.SwipeListener. 1780 * 1781 * @param amount The final value of the swipe (-1, 0 or 1) 1782 */ 1783 public void onFinishSwipe(int amount) { 1784 switch (mMode) { 1785 case MODE_WORKSPACE: 1786 if (amount == -1) { 1787 setWorkspaceAndAllAppsScale(-amount); 1788 mWorkspace.clearChildrenCache(); 1789 mMode = MODE_ALL_APPS; 1790 mSwipeController.setRange(0, 1); 1791 } 1792 break; 1793 case MODE_ALL_APPS: 1794 if (amount == 1) { 1795 setWorkspaceAndAllAppsScale(1-amount); 1796 mWorkspace.clearChildrenCache(); 1797 mMode = MODE_WORKSPACE; 1798 mSwipeController.setRange(-1, 0); 1799 } 1800 break; 1801 case MODE_ALL_APPS_ZOOMED: 1802 break; 1803 } 1804 } 1805 1806 /** 1807 * Implementation of the method from SwipeController.SwipeListener. 1808 */ 1809 public void onSwipe(float amount) { 1810 switch (mMode) { 1811 case MODE_WORKSPACE: 1812 // We can open the all apps view. 1813 // 0 == workspace is showing 1814 // -1 == all apps is showing 1815 setWorkspaceAndAllAppsScale(-amount); 1816 break; 1817 case MODE_ALL_APPS: 1818 // We can close it, or (someday) zoom it further 1819 // 0 == all apps showing 1820 // 1 == workspace is showing 1821 setWorkspaceAndAllAppsScale(1-amount); 1822 break; 1823 } 1824 } 1825 1826 /** 1827 * Set the scale factor for the workspace and the all apps grid. 1828 * 1829 * @param amount A float between 0 and 1, where: 1830 * 0 == workspace is showing and 1831 * 1 == the all apps grid is showing. 1832 */ 1833 private void setWorkspaceAndAllAppsScale(float amount) { 1834 //Log.d("setWorkspaceAndAllAppsScale", "setWorkspaceAndAllAppsScale amount=" + amount); 1835 if (amount < 0.001f) { 1836 amount = 0.0f; 1837 } 1838 if (amount > 0.999f) { 1839 amount = 1.0f; 1840 mWorkspace.setVisibility(View.INVISIBLE); 1841 } else { 1842 mWorkspace.setVisibility(View.VISIBLE); 1843 } 1844 //mWorkspace.setScale(1-amount); 1845 mAllAppsGrid.setScale(amount); 1846 } 1847 1848 /** 1849 * Implementation of the method from LauncherModel.Callbacks. 1850 */ 1851 public int getCurrentWorkspaceScreen() { 1852 return mWorkspace.getCurrentScreen(); 1853 } 1854 1855 /** 1856 * Refreshes the shortcuts shown on the workspace. 1857 * 1858 * Implementation of the method from LauncherModel.Callbacks. 1859 */ 1860 public void startBinding() { 1861 final Workspace workspace = mWorkspace; 1862 int count = workspace.getChildCount(); 1863 for (int i = 0; i < count; i++) { 1864 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout(); 1865 } 1866 1867 if (DEBUG_USER_INTERFACE) { 1868 android.widget.Button finishButton = new android.widget.Button(this); 1869 finishButton.setText("Finish"); 1870 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1); 1871 1872 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() { 1873 public void onClick(View v) { 1874 finish(); 1875 } 1876 }); 1877 } 1878 } 1879 1880 /** 1881 * Bind the items start-end from the list. 1882 * 1883 * Implementation of the method from LauncherModel.Callbacks. 1884 */ 1885 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) { 1886 1887 final Workspace workspace = mWorkspace; 1888 1889 for (int i=start; i<end; i++) { 1890 final ItemInfo item = shortcuts.get(i); 1891 mDesktopItems.add(item); 1892 switch (item.itemType) { 1893 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: 1894 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: 1895 final View shortcut = createShortcut((ApplicationInfo) item); 1896 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1, 1897 false); 1898 break; 1899 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: 1900 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, 1901 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()), 1902 (UserFolderInfo) item); 1903 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1, 1904 false); 1905 break; 1906 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER: 1907 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml( 1908 R.layout.live_folder_icon, this, 1909 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()), 1910 (LiveFolderInfo) item); 1911 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1, 1912 false); 1913 break; 1914 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH: 1915 final int screen = workspace.getCurrentScreen(); 1916 final View view = mInflater.inflate(R.layout.widget_search, 1917 (ViewGroup) workspace.getChildAt(screen), false); 1918 1919 Search search = (Search) view.findViewById(R.id.widget_search); 1920 search.setLauncher(this); 1921 1922 final Widget widget = (Widget) item; 1923 view.setTag(widget); 1924 1925 workspace.addWidget(view, widget, false); 1926 break; 1927 } 1928 } 1929 1930 workspace.requestLayout(); 1931 } 1932 1933 /** 1934 * Implementation of the method from LauncherModel.Callbacks. 1935 */ 1936 void bindFolders(HashMap<Long, FolderInfo> folders) { 1937 mFolders.putAll(folders); 1938 } 1939 1940 /** 1941 * Add the views for a widget to the workspace. 1942 * 1943 * Implementation of the method from LauncherModel.Callbacks. 1944 */ 1945 public void bindAppWidget(LauncherAppWidgetInfo item) { 1946 final Workspace workspace = mWorkspace; 1947 1948 final int appWidgetId = item.appWidgetId; 1949 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); 1950 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); 1951 1952 if (true) { 1953 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", 1954 appWidgetId, appWidgetInfo)); 1955 } 1956 1957 item.hostView.setAppWidget(appWidgetId, appWidgetInfo); 1958 item.hostView.setTag(item); 1959 1960 workspace.addInScreen(item.hostView, item.screen, item.cellX, 1961 item.cellY, item.spanX, item.spanY, false); 1962 1963 workspace.requestLayout(); 1964 1965 mDesktopItems.add(item); 1966 } 1967 1968 /** 1969 * Callback saying that there aren't any more items to bind. 1970 * 1971 * Implementation of the method from LauncherModel.Callbacks. 1972 */ 1973 public void finishBindingItems() { 1974 if (mSavedState != null) { 1975 if (!mWorkspace.hasFocus()) { 1976 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus(); 1977 } 1978 1979 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS); 1980 if (userFolders != null) { 1981 for (long folderId : userFolders) { 1982 final FolderInfo info = mFolders.get(folderId); 1983 if (info != null) { 1984 openFolder(info); 1985 } 1986 } 1987 final Folder openFolder = mWorkspace.getOpenFolder(); 1988 if (openFolder != null) { 1989 openFolder.requestFocus(); 1990 } 1991 } 1992 1993 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false); 1994 if (allApps) { 1995 showAllApps(); 1996 } 1997 1998 mSavedState = null; 1999 } 2000 2001 if (mSavedInstanceState != null) { 2002 super.onRestoreInstanceState(mSavedInstanceState); 2003 mSavedInstanceState = null; 2004 } 2005 2006 /* TODO 2007 if (mAllAppsVisible && !mDrawer.hasFocus()) { 2008 mDrawer.requestFocus(); 2009 } 2010 */ 2011 2012 Log.d(TAG, "finishBindingItems done"); 2013 mWorkspaceLoading = false; 2014 } 2015 2016 /** 2017 * Add the icons for all apps. 2018 * 2019 * Implementation of the method from LauncherModel.Callbacks. 2020 */ 2021 public void bindAllApplications(ArrayList<ApplicationInfo> apps) { 2022 Log.d(LOG_TAG, "got info for " + apps.size() + " apps"); 2023 mAllAppsList = apps; 2024 mAllAppsGrid.setApps(mAllAppsList); 2025 } 2026 2027 /** 2028 * A package was installed. 2029 * 2030 * Implementation of the method from LauncherModel.Callbacks. 2031 */ 2032 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) { 2033 removeDialog(DIALOG_CREATE_SHORTCUT); 2034 } 2035 2036 /** 2037 * A package was updated. 2038 * 2039 * Implementation of the method from LauncherModel.Callbacks. 2040 */ 2041 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) { 2042 removeDialog(DIALOG_CREATE_SHORTCUT); 2043 mWorkspace.updateShortcutsForPackage(packageName); 2044 } 2045 2046 /** 2047 * A package was uninstalled. 2048 * 2049 * Implementation of the method from LauncherModel.Callbacks. 2050 */ 2051 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) { 2052 removeDialog(DIALOG_CREATE_SHORTCUT); 2053 mWorkspace.removeShortcutsForPackage(packageName); 2054 } 2055 2056} 2057