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