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