The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame^] | 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 | |
| 17 | package com.android.launcher; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.app.Application; |
| 22 | import android.app.Dialog; |
| 23 | import android.app.SearchManager; |
| 24 | import android.app.StatusBarManager; |
| 25 | import android.content.ActivityNotFoundException; |
| 26 | import android.content.BroadcastReceiver; |
| 27 | import android.content.ComponentName; |
| 28 | import android.content.ContentResolver; |
| 29 | import android.content.Context; |
| 30 | import android.content.DialogInterface; |
| 31 | import android.content.Intent; |
| 32 | import android.content.IntentFilter; |
| 33 | import android.content.SharedPreferences; |
| 34 | import android.content.pm.ActivityInfo; |
| 35 | import android.content.pm.PackageManager; |
| 36 | import android.content.pm.PackageManager.NameNotFoundException; |
| 37 | import android.content.res.Resources; |
| 38 | import android.content.res.Configuration; |
| 39 | import android.database.ContentObserver; |
| 40 | import android.gadget.GadgetProviderInfo; |
| 41 | import android.gadget.GadgetManager; |
| 42 | import android.graphics.Bitmap; |
| 43 | import android.graphics.Rect; |
| 44 | import android.graphics.drawable.BitmapDrawable; |
| 45 | import android.graphics.drawable.Drawable; |
| 46 | import android.graphics.drawable.TransitionDrawable; |
| 47 | import android.net.Uri; |
| 48 | import android.os.Bundle; |
| 49 | import android.os.Handler; |
| 50 | import android.os.IBinder; |
| 51 | import android.os.Parcelable; |
| 52 | import android.os.RemoteException; |
| 53 | import android.os.ServiceManager; |
| 54 | import android.os.Message; |
| 55 | import android.provider.*; |
| 56 | import android.telephony.PhoneNumberUtils; |
| 57 | import android.text.Selection; |
| 58 | import android.text.SpannableStringBuilder; |
| 59 | import android.text.TextUtils; |
| 60 | import android.text.method.TextKeyListener; |
| 61 | import android.util.Log; |
| 62 | import android.view.Display; |
| 63 | import android.view.Gravity; |
| 64 | import android.view.KeyEvent; |
| 65 | import android.view.LayoutInflater; |
| 66 | import android.view.Menu; |
| 67 | import android.view.MenuItem; |
| 68 | import android.view.View; |
| 69 | import android.view.ViewGroup; |
| 70 | import android.view.WindowManager; |
| 71 | import android.view.View.OnLongClickListener; |
| 72 | import android.view.inputmethod.InputMethodManager; |
| 73 | import android.widget.AdapterView; |
| 74 | import android.widget.EditText; |
| 75 | import android.widget.ListView; |
| 76 | import android.widget.TextView; |
| 77 | import android.widget.Toast; |
| 78 | import android.widget.GridView; |
| 79 | import android.widget.SlidingDrawer; |
| 80 | import android.app.IWallpaperService; |
| 81 | |
| 82 | import java.lang.ref.WeakReference; |
| 83 | import java.util.ArrayList; |
| 84 | |
| 85 | /** |
| 86 | * Default launcher application. |
| 87 | */ |
| 88 | public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener { |
| 89 | static final String LOG_TAG = "Launcher"; |
| 90 | static final boolean LOGD = false; |
| 91 | |
| 92 | private static final boolean PROFILE_STARTUP = false; |
| 93 | private 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_GADGET = 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_GADGET = 9; |
| 111 | |
| 112 | static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate"; |
| 113 | |
| 114 | static final int SCREEN_COUNT = 3; |
| 115 | static final int DEFAULT_SCREN = 1; |
| 116 | static final int NUMBER_CELLS_X = 4; |
| 117 | static final int NUMBER_CELLS_Y = 4; |
| 118 | |
| 119 | private static final int DIALOG_CREATE_SHORTCUT = 1; |
| 120 | static final int DIALOG_RENAME_FOLDER = 2; |
| 121 | |
| 122 | private static final String PREFERENCES = "launcher"; |
| 123 | private static final String KEY_LOCALE = "locale"; |
| 124 | private static final String KEY_MCC = "mcc"; |
| 125 | private static final String KEY_MNC = "mnc"; |
| 126 | |
| 127 | // Type: int |
| 128 | private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen"; |
| 129 | // Type: boolean |
| 130 | private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder"; |
| 131 | // Type: long |
| 132 | private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder"; |
| 133 | // Type: int |
| 134 | private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen"; |
| 135 | // Type: int |
| 136 | private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX"; |
| 137 | // Type: int |
| 138 | private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY"; |
| 139 | // Type: int |
| 140 | private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX"; |
| 141 | // Type: int |
| 142 | private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY"; |
| 143 | // Type: int |
| 144 | private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX"; |
| 145 | // Type: int |
| 146 | private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY"; |
| 147 | // Type: int[] |
| 148 | private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells"; |
| 149 | // Type: boolean |
| 150 | private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder"; |
| 151 | // Type: long |
| 152 | private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id"; |
| 153 | |
| 154 | private static LauncherModel sModel; |
| 155 | |
| 156 | private static Bitmap sWallpaper; |
| 157 | |
| 158 | private static final Object sLock = new Object(); |
| 159 | private static int sScreen = DEFAULT_SCREN; |
| 160 | |
| 161 | private static WallpaperIntentReceiver sWallpaperReceiver; |
| 162 | |
| 163 | private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver(); |
| 164 | private final ContentObserver mObserver = new FavoritesChangeObserver(); |
| 165 | |
| 166 | private LayoutInflater mInflater; |
| 167 | |
| 168 | private DragLayer mDragLayer; |
| 169 | private Workspace mWorkspace; |
| 170 | |
| 171 | private GadgetManager mGadgetManager; |
| 172 | private LauncherGadgetHost mGadgetHost; |
| 173 | |
| 174 | static final int GADGET_HOST_ID = 1024; |
| 175 | |
| 176 | private CellLayout.CellInfo mAddItemCellInfo; |
| 177 | private CellLayout.CellInfo mMenuAddInfo; |
| 178 | private final int[] mCellCoordinates = new int[2]; |
| 179 | private FolderInfo mFolderInfo; |
| 180 | |
| 181 | private SlidingDrawer mDrawer; |
| 182 | private TransitionDrawable mHandleIcon; |
| 183 | private AllAppsGridView mAllAppsGrid; |
| 184 | |
| 185 | private boolean mDesktopLocked = true; |
| 186 | private Bundle mSavedState; |
| 187 | |
| 188 | private SpannableStringBuilder mDefaultKeySsb = null; |
| 189 | |
| 190 | private boolean mDestroyed; |
| 191 | |
| 192 | private boolean mRestoring; |
| 193 | private boolean mWaitingForResult; |
| 194 | private boolean mLocaleChanged; |
| 195 | |
| 196 | private Bundle mSavedInstanceState; |
| 197 | |
| 198 | @Override |
| 199 | protected void onCreate(Bundle savedInstanceState) { |
| 200 | super.onCreate(savedInstanceState); |
| 201 | mInflater = getLayoutInflater(); |
| 202 | |
| 203 | mGadgetManager = GadgetManager.getInstance(this); |
| 204 | |
| 205 | mGadgetHost = new LauncherGadgetHost(this, GADGET_HOST_ID); |
| 206 | mGadgetHost.startListening(); |
| 207 | |
| 208 | if (PROFILE_STARTUP) { |
| 209 | android.os.Debug.startMethodTracing("/sdcard/launcher"); |
| 210 | } |
| 211 | |
| 212 | checkForLocaleChange(); |
| 213 | setWallpaperDimension(); |
| 214 | |
| 215 | if (sModel == null) { |
| 216 | sModel = new LauncherModel(); |
| 217 | } |
| 218 | |
| 219 | setContentView(R.layout.launcher); |
| 220 | setupViews(); |
| 221 | |
| 222 | registerIntentReceivers(); |
| 223 | registerContentObservers(); |
| 224 | |
| 225 | mSavedState = savedInstanceState; |
| 226 | restoreState(mSavedState); |
| 227 | |
| 228 | if (PROFILE_STARTUP) { |
| 229 | android.os.Debug.stopMethodTracing(); |
| 230 | } |
| 231 | |
| 232 | if (!mRestoring) { |
| 233 | startLoaders(); |
| 234 | } |
| 235 | |
| 236 | // For handling default keys |
| 237 | mDefaultKeySsb = new SpannableStringBuilder(); |
| 238 | Selection.setSelection(mDefaultKeySsb, 0); |
| 239 | } |
| 240 | |
| 241 | private void checkForLocaleChange() { |
| 242 | final SharedPreferences preferences = getSharedPreferences(PREFERENCES, MODE_PRIVATE); |
| 243 | final Configuration configuration = getResources().getConfiguration(); |
| 244 | |
| 245 | final String previousLocale = preferences.getString(KEY_LOCALE, null); |
| 246 | final String locale = configuration.locale.toString(); |
| 247 | |
| 248 | final int previousMcc = preferences.getInt(KEY_MCC, -1); |
| 249 | final int mcc = configuration.mcc; |
| 250 | |
| 251 | final int previousMnc = preferences.getInt(KEY_MNC, -1); |
| 252 | final int mnc = configuration.mnc; |
| 253 | |
| 254 | mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc; |
| 255 | |
| 256 | if (mLocaleChanged) { |
| 257 | final SharedPreferences.Editor editor = preferences.edit(); |
| 258 | editor.putString(KEY_LOCALE, locale); |
| 259 | editor.putInt(KEY_MCC, mcc); |
| 260 | editor.putInt(KEY_MNC, mnc); |
| 261 | editor.commit(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | static int getScreen() { |
| 266 | synchronized (sLock) { |
| 267 | return sScreen; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static void setScreen(int screen) { |
| 272 | synchronized (sLock) { |
| 273 | sScreen = screen; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | private void startLoaders() { |
| 278 | sModel.loadApplications(true, this, mLocaleChanged); |
| 279 | sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, true); |
| 280 | mRestoring = false; |
| 281 | } |
| 282 | |
| 283 | private void setWallpaperDimension() { |
| 284 | IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE); |
| 285 | IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder); |
| 286 | |
| 287 | Display display = getWindowManager().getDefaultDisplay(); |
| 288 | boolean isPortrait = display.getWidth() < display.getHeight(); |
| 289 | |
| 290 | final int width = isPortrait ? display.getWidth() : display.getHeight(); |
| 291 | final int height = isPortrait ? display.getHeight() : display.getWidth(); |
| 292 | try { |
| 293 | wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height); |
| 294 | } catch (RemoteException e) { |
| 295 | // System is dead! |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 301 | // The pattern used here is that a user PICKs a specific application, |
| 302 | // which, depending on the target, might need to CREATE the actual target. |
| 303 | |
| 304 | // For example, the user would PICK_SHORTCUT for "Music playlist", and we |
| 305 | // launch over to the Music app to actually CREATE_SHORTCUT. |
| 306 | |
| 307 | if (resultCode == RESULT_OK && mAddItemCellInfo != null) { |
| 308 | switch (requestCode) { |
| 309 | case REQUEST_PICK_APPLICATION: |
| 310 | completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked); |
| 311 | break; |
| 312 | case REQUEST_PICK_SHORTCUT: |
| 313 | addShortcut(data); |
| 314 | break; |
| 315 | case REQUEST_CREATE_SHORTCUT: |
| 316 | completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked); |
| 317 | break; |
| 318 | case REQUEST_PICK_LIVE_FOLDER: |
| 319 | addLiveFolder(data); |
| 320 | break; |
| 321 | case REQUEST_CREATE_LIVE_FOLDER: |
| 322 | completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked); |
| 323 | break; |
| 324 | case REQUEST_PICK_GADGET: |
| 325 | addGadget(data); |
| 326 | break; |
| 327 | case REQUEST_CREATE_GADGET: |
| 328 | completeAddGadget(data, mAddItemCellInfo, !mDesktopLocked); |
| 329 | break; |
| 330 | } |
| 331 | } else if (requestCode == REQUEST_PICK_GADGET && |
| 332 | resultCode == RESULT_CANCELED && data != null) { |
| 333 | // Clean up the gadgetId if we canceled |
| 334 | int gadgetId = data.getIntExtra(GadgetManager.EXTRA_GADGET_ID, -1); |
| 335 | if (gadgetId != -1) { |
| 336 | mGadgetHost.deleteGadgetId(gadgetId); |
| 337 | } |
| 338 | } |
| 339 | mWaitingForResult = false; |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | protected void onResume() { |
| 344 | super.onResume(); |
| 345 | |
| 346 | if (mRestoring) { |
| 347 | startLoaders(); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 353 | boolean handled = super.onKeyUp(keyCode, event); |
| 354 | if (keyCode == KeyEvent.KEYCODE_SEARCH) { |
| 355 | handled = mWorkspace.snapToSearch(); |
| 356 | if (handled) closeDrawer(true); |
| 357 | } |
| 358 | return handled; |
| 359 | } |
| 360 | |
| 361 | private boolean acceptFilter() { |
| 362 | final InputMethodManager inputManager = (InputMethodManager) |
| 363 | getSystemService(Context.INPUT_METHOD_SERVICE); |
| 364 | return !inputManager.isFullscreenMode(); |
| 365 | } |
| 366 | |
| 367 | @Override |
| 368 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 369 | boolean handled = super.onKeyDown(keyCode, event); |
| 370 | if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) { |
| 371 | boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb, |
| 372 | keyCode, event); |
| 373 | if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) { |
| 374 | // something usable has been typed - dispatch it now. |
| 375 | final String str = mDefaultKeySsb.toString(); |
| 376 | |
| 377 | boolean isDialable = true; |
| 378 | final int count = str.length(); |
| 379 | for (int i = 0; i < count; i++) { |
| 380 | if (!PhoneNumberUtils.isReallyDialable(str.charAt(i))) { |
| 381 | isDialable = false; |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | Intent intent; |
| 386 | if (isDialable) { |
| 387 | intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", str, null)); |
| 388 | } else { |
| 389 | intent = new Intent(Contacts.Intents.UI.FILTER_CONTACTS_ACTION); |
| 390 | intent.putExtra(Contacts.Intents.UI.FILTER_TEXT_EXTRA_KEY, str); |
| 391 | } |
| 392 | |
| 393 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 394 | |
| 395 | try { |
| 396 | startActivity(intent); |
| 397 | } catch (android.content.ActivityNotFoundException ex) { |
| 398 | // Oh well... no one knows how to filter/dial. Life goes on. |
| 399 | } |
| 400 | |
| 401 | mDefaultKeySsb.clear(); |
| 402 | mDefaultKeySsb.clearSpans(); |
| 403 | Selection.setSelection(mDefaultKeySsb, 0); |
| 404 | |
| 405 | return true; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | return handled; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Restores the previous state, if it exists. |
| 414 | * |
| 415 | * @param savedState The previous state. |
| 416 | */ |
| 417 | private void restoreState(Bundle savedState) { |
| 418 | if (savedState == null) { |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1); |
| 423 | if (currentScreen > -1) { |
| 424 | mWorkspace.setCurrentScreen(currentScreen); |
| 425 | } |
| 426 | |
| 427 | final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1); |
| 428 | if (addScreen > -1) { |
| 429 | mAddItemCellInfo = new CellLayout.CellInfo(); |
| 430 | final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo; |
| 431 | addItemCellInfo.valid = true; |
| 432 | addItemCellInfo.screen = addScreen; |
| 433 | addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X); |
| 434 | addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y); |
| 435 | addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X); |
| 436 | addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y); |
| 437 | addItemCellInfo.findVacantCellsFromOccupied( |
| 438 | savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS), |
| 439 | savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X), |
| 440 | savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y)); |
| 441 | mRestoring = true; |
| 442 | } |
| 443 | |
| 444 | boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false); |
| 445 | if (renameFolder) { |
| 446 | long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID); |
| 447 | mFolderInfo = sModel.getFolderById(this, id); |
| 448 | mRestoring = true; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Finds all the views we need and configure them properly. |
| 454 | */ |
| 455 | private void setupViews() { |
| 456 | mDragLayer = (DragLayer) findViewById(R.id.drag_layer); |
| 457 | final DragLayer dragLayer = mDragLayer; |
| 458 | |
| 459 | mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace); |
| 460 | final Workspace workspace = mWorkspace; |
| 461 | |
| 462 | mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer); |
| 463 | final SlidingDrawer drawer = mDrawer; |
| 464 | |
| 465 | mAllAppsGrid = (AllAppsGridView) drawer.getContent(); |
| 466 | final AllAppsGridView grid = mAllAppsGrid; |
| 467 | |
| 468 | final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone); |
| 469 | |
| 470 | final HandleView handleIcon = (HandleView) drawer.findViewById(R.id.all_apps); |
| 471 | handleIcon.setLauncher(this); |
| 472 | mHandleIcon = (TransitionDrawable) handleIcon.getDrawable(); |
| 473 | mHandleIcon.setCrossFadeEnabled(true); |
| 474 | |
| 475 | drawer.lock(); |
| 476 | final DrawerManager drawerManager = new DrawerManager(); |
| 477 | drawer.setOnDrawerOpenListener(drawerManager); |
| 478 | drawer.setOnDrawerCloseListener(drawerManager); |
| 479 | drawer.setOnDrawerScrollListener(drawerManager); |
| 480 | |
| 481 | grid.setTextFilterEnabled(true); |
| 482 | grid.setDragger(dragLayer); |
| 483 | grid.setLauncher(this); |
| 484 | |
| 485 | workspace.setOnLongClickListener(this); |
| 486 | workspace.setDragger(dragLayer); |
| 487 | workspace.setLauncher(this); |
| 488 | loadWallpaper(); |
| 489 | |
| 490 | deleteZone.setLauncher(this); |
| 491 | deleteZone.setDragController(dragLayer); |
| 492 | deleteZone.setHandle(handleIcon); |
| 493 | |
| 494 | dragLayer.setIgnoredDropTarget(grid); |
| 495 | dragLayer.setDragScoller(workspace); |
| 496 | dragLayer.setDragListener(deleteZone); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Creates a view representing a shortcut. |
| 501 | * |
| 502 | * @param info The data structure describing the shortcut. |
| 503 | * |
| 504 | * @return A View inflated from R.layout.application. |
| 505 | */ |
| 506 | View createShortcut(ApplicationInfo info) { |
| 507 | return createShortcut(R.layout.application, |
| 508 | (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Creates a view representing a shortcut inflated from the specified resource. |
| 513 | * |
| 514 | * @param layoutResId The id of the XML layout used to create the shortcut. |
| 515 | * @param parent The group the shortcut belongs to. |
| 516 | * @param info The data structure describing the shortcut. |
| 517 | * |
| 518 | * @return A View inflated from layoutResId. |
| 519 | */ |
| 520 | View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) { |
| 521 | TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false); |
| 522 | |
| 523 | if (!info.filtered) { |
| 524 | info.icon = Utilities.createIconThumbnail(info.icon, this); |
| 525 | info.filtered = true; |
| 526 | } |
| 527 | |
| 528 | favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null); |
| 529 | favorite.setText(info.title); |
| 530 | favorite.setTag(info); |
| 531 | favorite.setOnClickListener(this); |
| 532 | |
| 533 | return favorite; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Add an application shortcut to the workspace. |
| 538 | * |
| 539 | * @param data The intent describing the application. |
| 540 | * @param cellInfo The position on screen where to create the shortcut. |
| 541 | */ |
| 542 | void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo, |
| 543 | boolean insertAtFirst) { |
| 544 | cellInfo.screen = mWorkspace.getCurrentScreen(); |
| 545 | if (!findSingleSlot(cellInfo)) return; |
| 546 | |
| 547 | // Find details for this application |
| 548 | ComponentName component = data.getComponent(); |
| 549 | PackageManager packageManager = context.getPackageManager(); |
| 550 | ActivityInfo activityInfo = null; |
| 551 | try { |
| 552 | activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */); |
| 553 | } catch (NameNotFoundException e) { |
| 554 | Log.e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e); |
| 555 | } |
| 556 | |
| 557 | if (activityInfo != null) { |
| 558 | ApplicationInfo itemInfo = new ApplicationInfo(); |
| 559 | |
| 560 | itemInfo.title = activityInfo.loadLabel(packageManager); |
| 561 | if (itemInfo.title == null) { |
| 562 | itemInfo.title = activityInfo.name; |
| 563 | } |
| 564 | |
| 565 | itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK | |
| 566 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 567 | itemInfo.icon = activityInfo.loadIcon(packageManager); |
| 568 | itemInfo.container = ItemInfo.NO_ID; |
| 569 | |
| 570 | mWorkspace.addApplicationShortcut(itemInfo, cellInfo, insertAtFirst); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Add a shortcut to the workspace. |
| 576 | * |
| 577 | * @param data The intent describing the shortcut. |
| 578 | * @param cellInfo The position on screen where to create the shortcut. |
| 579 | * @param insertAtFirst |
| 580 | */ |
| 581 | private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo, |
| 582 | boolean insertAtFirst) { |
| 583 | cellInfo.screen = mWorkspace.getCurrentScreen(); |
| 584 | if (!findSingleSlot(cellInfo)) return; |
| 585 | |
| 586 | final ApplicationInfo info = addShortcut(this, data, cellInfo, false); |
| 587 | |
| 588 | if (!mRestoring) { |
| 589 | sModel.addDesktopItem(info); |
| 590 | |
| 591 | final View view = createShortcut(info); |
| 592 | mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst); |
| 593 | } else if (sModel.isDesktopLoaded()) { |
| 594 | sModel.addDesktopItem(info); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | |
| 599 | /** |
| 600 | * Add a gadget to the workspace. |
| 601 | * |
| 602 | * @param data The intent describing the gadgetId. |
| 603 | * @param cellInfo The position on screen where to create the shortcut. |
| 604 | * @param insertAtFirst |
| 605 | */ |
| 606 | private void completeAddGadget(Intent data, CellLayout.CellInfo cellInfo, |
| 607 | boolean insertAtFirst) { |
| 608 | |
| 609 | Bundle extras = data.getExtras(); |
| 610 | int gadgetId = extras.getInt(GadgetManager.EXTRA_GADGET_ID, -1); |
| 611 | |
| 612 | Log.d(LOG_TAG, "dumping extras content="+extras.toString()); |
| 613 | |
| 614 | GadgetProviderInfo gadgetInfo = mGadgetManager.getGadgetInfo(gadgetId); |
| 615 | |
| 616 | // Calculate the grid spans needed to fit this gadget |
| 617 | CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen); |
| 618 | int[] spans = layout.rectToCell(gadgetInfo.minWidth, gadgetInfo.minHeight); |
| 619 | |
| 620 | // Try finding open space on Launcher screen |
| 621 | final int[] xy = mCellCoordinates; |
| 622 | if (!findSlot(cellInfo, xy, spans[0], spans[1])) return; |
| 623 | |
| 624 | // Build Launcher-specific Gadget info and save to database |
| 625 | LauncherGadgetInfo launcherInfo = new LauncherGadgetInfo(gadgetId); |
| 626 | launcherInfo.spanX = spans[0]; |
| 627 | launcherInfo.spanY = spans[1]; |
| 628 | |
| 629 | LauncherModel.addItemToDatabase(this, launcherInfo, |
| 630 | LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 631 | mWorkspace.getCurrentScreen(), xy[0], xy[1], false); |
| 632 | |
| 633 | if (!mRestoring) { |
| 634 | sModel.addDesktopGadget(launcherInfo); |
| 635 | |
| 636 | // Perform actual inflation because we're live |
| 637 | launcherInfo.hostView = mGadgetHost.createView(this, gadgetId, gadgetInfo); |
| 638 | |
| 639 | launcherInfo.hostView.setGadget(gadgetId, gadgetInfo); |
| 640 | launcherInfo.hostView.setTag(launcherInfo); |
| 641 | |
| 642 | mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1], |
| 643 | launcherInfo.spanX, launcherInfo.spanY, insertAtFirst); |
| 644 | } else if (sModel.isDesktopLoaded()) { |
| 645 | sModel.addDesktopGadget(launcherInfo); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | public LauncherGadgetHost getGadgetHost() { |
| 650 | return mGadgetHost; |
| 651 | } |
| 652 | |
| 653 | static ApplicationInfo addShortcut(Context context, Intent data, |
| 654 | CellLayout.CellInfo cellInfo, boolean notify) { |
| 655 | |
| 656 | Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); |
| 657 | String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); |
| 658 | Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); |
| 659 | |
| 660 | Drawable icon = null; |
| 661 | boolean filtered = false; |
| 662 | boolean customIcon = false; |
| 663 | Intent.ShortcutIconResource iconResource = null; |
| 664 | |
| 665 | if (bitmap != null) { |
| 666 | icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context)); |
| 667 | filtered = true; |
| 668 | customIcon = true; |
| 669 | } else { |
| 670 | Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE); |
| 671 | if (extra != null && extra instanceof Intent.ShortcutIconResource) { |
| 672 | try { |
| 673 | iconResource = (Intent.ShortcutIconResource) extra; |
| 674 | final PackageManager packageManager = context.getPackageManager(); |
| 675 | Resources resources = packageManager.getResourcesForApplication( |
| 676 | iconResource.packageName); |
| 677 | final int id = resources.getIdentifier(iconResource.resourceName, null, null); |
| 678 | icon = resources.getDrawable(id); |
| 679 | } catch (Exception e) { |
| 680 | Log.w(LOG_TAG, "Could not load shortcut icon: " + extra); |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | if (icon == null) { |
| 686 | icon = context.getPackageManager().getDefaultActivityIcon(); |
| 687 | } |
| 688 | |
| 689 | final ApplicationInfo info = new ApplicationInfo(); |
| 690 | info.icon = icon; |
| 691 | info.filtered = filtered; |
| 692 | info.title = name; |
| 693 | info.intent = intent; |
| 694 | info.customIcon = customIcon; |
| 695 | info.iconResource = iconResource; |
| 696 | |
| 697 | LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 698 | cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify); |
| 699 | return info; |
| 700 | } |
| 701 | |
| 702 | @Override |
| 703 | protected void onNewIntent(Intent intent) { |
| 704 | super.onNewIntent(intent); |
| 705 | |
| 706 | // Close the menu |
| 707 | if (Intent.ACTION_MAIN.equals(intent.getAction())) { |
| 708 | getWindow().closeAllPanels(); |
| 709 | |
| 710 | try { |
| 711 | dismissDialog(DIALOG_CREATE_SHORTCUT); |
| 712 | // Unlock the workspace if the dialog was showing |
| 713 | mWorkspace.unlock(); |
| 714 | } catch (Exception e) { |
| 715 | // An exception is thrown if the dialog is not visible, which is fine |
| 716 | } |
| 717 | |
| 718 | try { |
| 719 | dismissDialog(DIALOG_RENAME_FOLDER); |
| 720 | // Unlock the workspace if the dialog was showing |
| 721 | mWorkspace.unlock(); |
| 722 | } catch (Exception e) { |
| 723 | // An exception is thrown if the dialog is not visible, which is fine |
| 724 | } |
| 725 | |
| 726 | // If we are already in front we go back to the default screen, |
| 727 | // otherwise we don't |
| 728 | if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != |
| 729 | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) { |
| 730 | if (!mWorkspace.isDefaultScreenShowing()) { |
| 731 | mWorkspace.moveToDefaultScreen(); |
| 732 | } |
| 733 | closeDrawer(); |
| 734 | View v = getWindow().peekDecorView(); |
| 735 | if (v != null && v.getWindowToken() != null) { |
| 736 | InputMethodManager imm = (InputMethodManager)getSystemService( |
| 737 | INPUT_METHOD_SERVICE); |
| 738 | imm.hideSoftInputFromWindow(v.getWindowToken(), 0); |
| 739 | } |
| 740 | } else { |
| 741 | closeDrawer(false); |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | @Override |
| 747 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 748 | // Do not call super here |
| 749 | mSavedInstanceState = savedInstanceState; |
| 750 | } |
| 751 | |
| 752 | @Override |
| 753 | protected void onSaveInstanceState(Bundle outState) { |
| 754 | outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen()); |
| 755 | |
| 756 | final ArrayList<Folder> folders = mWorkspace.getOpenFolders(); |
| 757 | if (folders.size() > 0) { |
| 758 | final int count = folders.size(); |
| 759 | long[] ids = new long[count]; |
| 760 | for (int i = 0; i < count; i++) { |
| 761 | final FolderInfo info = folders.get(i).getInfo(); |
| 762 | ids[i] = info.id; |
| 763 | } |
| 764 | outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids); |
| 765 | } else { |
| 766 | super.onSaveInstanceState(outState); |
| 767 | } |
| 768 | |
| 769 | if (mDrawer.isOpened()) { |
| 770 | outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true); |
| 771 | } |
| 772 | |
| 773 | if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) { |
| 774 | final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo; |
| 775 | final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen); |
| 776 | |
| 777 | outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen); |
| 778 | outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX); |
| 779 | outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY); |
| 780 | outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX); |
| 781 | outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY); |
| 782 | outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX()); |
| 783 | outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY()); |
| 784 | outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS, |
| 785 | layout.getOccupiedCells()); |
| 786 | } |
| 787 | |
| 788 | if (mFolderInfo != null && mWaitingForResult) { |
| 789 | outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true); |
| 790 | outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | @Override |
| 795 | public void onDestroy() { |
| 796 | mDestroyed = true; |
| 797 | |
| 798 | super.onDestroy(); |
| 799 | |
| 800 | try { |
| 801 | mGadgetHost.stopListening(); |
| 802 | } catch (NullPointerException ex) { |
| 803 | Log.w(LOG_TAG, "problem while stopping GadgetHost during Launcher destruction", ex); |
| 804 | } |
| 805 | |
| 806 | TextKeyListener.getInstance().release(); |
| 807 | |
| 808 | mAllAppsGrid.clearTextFilter(); |
| 809 | mAllAppsGrid.setAdapter(null); |
| 810 | sModel.unbind(); |
| 811 | sModel.abortLoaders(); |
| 812 | |
| 813 | getContentResolver().unregisterContentObserver(mObserver); |
| 814 | unregisterReceiver(mApplicationsReceiver); |
| 815 | } |
| 816 | |
| 817 | @Override |
| 818 | public void startActivityForResult(Intent intent, int requestCode) { |
| 819 | mWaitingForResult = true; |
| 820 | super.startActivityForResult(intent, requestCode); |
| 821 | } |
| 822 | |
| 823 | @Override |
| 824 | public void startSearch(String initialQuery, boolean selectInitialQuery, |
| 825 | Bundle appSearchData, boolean globalSearch) { |
| 826 | if (appSearchData == null) { |
| 827 | appSearchData = new Bundle(); |
| 828 | appSearchData.putString(SearchManager.SOURCE, "launcher-search"); |
| 829 | } |
| 830 | super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch); |
| 831 | } |
| 832 | |
| 833 | @Override |
| 834 | public boolean onCreateOptionsMenu(Menu menu) { |
| 835 | if (mDesktopLocked) return false; |
| 836 | |
| 837 | super.onCreateOptionsMenu(menu); |
| 838 | menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add) |
| 839 | .setIcon(android.R.drawable.ic_menu_add) |
| 840 | .setAlphabeticShortcut('A'); |
| 841 | menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper) |
| 842 | .setIcon(android.R.drawable.ic_menu_gallery) |
| 843 | .setAlphabeticShortcut('W'); |
| 844 | menu.add(0, MENU_SEARCH, 0, R.string.menu_search) |
| 845 | .setIcon(android.R.drawable.ic_search_category_default) |
| 846 | .setAlphabeticShortcut(SearchManager.MENU_KEY); |
| 847 | menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications) |
| 848 | .setIcon(com.android.internal.R.drawable.ic_menu_notifications) |
| 849 | .setAlphabeticShortcut('N'); |
| 850 | |
| 851 | final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS); |
| 852 | settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 853 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 854 | |
| 855 | menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings) |
| 856 | .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P') |
| 857 | .setIntent(settings); |
| 858 | |
| 859 | return true; |
| 860 | } |
| 861 | |
| 862 | @Override |
| 863 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 864 | super.onPrepareOptionsMenu(menu); |
| 865 | |
| 866 | mMenuAddInfo = mWorkspace.findAllVacantCells(null); |
| 867 | menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid); |
| 868 | |
| 869 | return true; |
| 870 | } |
| 871 | |
| 872 | @Override |
| 873 | public boolean onOptionsItemSelected(MenuItem item) { |
| 874 | switch (item.getItemId()) { |
| 875 | case MENU_ADD: |
| 876 | addItems(); |
| 877 | return true; |
| 878 | case MENU_WALLPAPER_SETTINGS: |
| 879 | startWallpaper(); |
| 880 | return true; |
| 881 | case MENU_SEARCH: |
| 882 | if (mWorkspace.snapToSearch()) { |
| 883 | closeDrawer(true); // search gadget: get drawer out of the way |
| 884 | } else { |
| 885 | onSearchRequested(); // no search gadget: use system search UI |
| 886 | } |
| 887 | return true; |
| 888 | case MENU_NOTIFICATIONS: |
| 889 | showNotifications(); |
| 890 | return true; |
| 891 | } |
| 892 | |
| 893 | return super.onOptionsItemSelected(item); |
| 894 | } |
| 895 | |
| 896 | private void addItems() { |
| 897 | showAddDialog(mMenuAddInfo); |
| 898 | } |
| 899 | |
| 900 | private void removeShortcutsForPackage(String packageName) { |
| 901 | if (packageName != null && packageName.length() > 0) { |
| 902 | mWorkspace.removeShortcutsForPackage(packageName); |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | void addGadget(Intent data) { |
| 907 | // TODO: catch bad gadget exception when sent |
| 908 | int gadgetId = data.getIntExtra(GadgetManager.EXTRA_GADGET_ID, -1); |
| 909 | GadgetProviderInfo gadget = mGadgetManager.getGadgetInfo(gadgetId); |
| 910 | |
| 911 | if (gadget.configure != null) { |
| 912 | // Launch over to configure gadget, if needed |
| 913 | Intent intent = new Intent(GadgetManager.ACTION_GADGET_CONFIGURE); |
| 914 | intent.setComponent(gadget.configure); |
| 915 | intent.putExtra(GadgetManager.EXTRA_GADGET_ID, gadgetId); |
| 916 | |
| 917 | startActivityForResult(intent, REQUEST_CREATE_GADGET); |
| 918 | } else { |
| 919 | // Otherwise just add it |
| 920 | onActivityResult(REQUEST_CREATE_GADGET, Activity.RESULT_OK, data); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | void addSearch() { |
| 925 | final Widget info = Widget.makeSearch(); |
| 926 | final CellLayout.CellInfo cellInfo = mAddItemCellInfo; |
| 927 | |
| 928 | final int[] xy = mCellCoordinates; |
| 929 | final int spanX = info.spanX; |
| 930 | final int spanY = info.spanY; |
| 931 | |
| 932 | if (!findSlot(cellInfo, xy, spanX, spanY)) return; |
| 933 | |
| 934 | sModel.addDesktopItem(info); |
| 935 | LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 936 | mWorkspace.getCurrentScreen(), xy[0], xy[1], false); |
| 937 | |
| 938 | final View view = mInflater.inflate(info.layoutResource, null); |
| 939 | view.setTag(info); |
| 940 | |
| 941 | mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY); |
| 942 | } |
| 943 | |
| 944 | void addShortcut(Intent intent) { |
| 945 | startActivityForResult(intent, REQUEST_CREATE_SHORTCUT); |
| 946 | } |
| 947 | |
| 948 | void addLiveFolder(Intent intent) { |
| 949 | startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER); |
| 950 | } |
| 951 | |
| 952 | void addFolder(boolean insertAtFirst) { |
| 953 | UserFolderInfo folderInfo = new UserFolderInfo(); |
| 954 | folderInfo.title = getText(R.string.folder_name); |
| 955 | |
| 956 | CellLayout.CellInfo cellInfo = mAddItemCellInfo; |
| 957 | cellInfo.screen = mWorkspace.getCurrentScreen(); |
| 958 | if (!findSingleSlot(cellInfo)) return; |
| 959 | |
| 960 | // Update the model |
| 961 | LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 962 | mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false); |
| 963 | sModel.addDesktopItem(folderInfo); |
| 964 | sModel.addFolder(folderInfo); |
| 965 | |
| 966 | // Create the view |
| 967 | FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, |
| 968 | (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo); |
| 969 | mWorkspace.addInCurrentScreen(newFolder, |
| 970 | cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst); |
| 971 | } |
| 972 | |
| 973 | private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo, |
| 974 | boolean insertAtFirst) { |
| 975 | cellInfo.screen = mWorkspace.getCurrentScreen(); |
| 976 | if (!findSingleSlot(cellInfo)) return; |
| 977 | |
| 978 | final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false); |
| 979 | |
| 980 | if (!mRestoring) { |
| 981 | sModel.addDesktopItem(info); |
| 982 | |
| 983 | final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this, |
| 984 | (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); |
| 985 | mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst); |
| 986 | } else if (sModel.isDesktopLoaded()) { |
| 987 | sModel.addDesktopItem(info); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | static LiveFolderInfo addLiveFolder(Context context, Intent data, |
| 992 | CellLayout.CellInfo cellInfo, boolean notify) { |
| 993 | |
| 994 | Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT); |
| 995 | String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME); |
| 996 | |
| 997 | Drawable icon = null; |
| 998 | boolean filtered = false; |
| 999 | Intent.ShortcutIconResource iconResource = null; |
| 1000 | |
| 1001 | Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON); |
| 1002 | if (extra != null && extra instanceof Intent.ShortcutIconResource) { |
| 1003 | try { |
| 1004 | iconResource = (Intent.ShortcutIconResource) extra; |
| 1005 | final PackageManager packageManager = context.getPackageManager(); |
| 1006 | Resources resources = packageManager.getResourcesForApplication( |
| 1007 | iconResource.packageName); |
| 1008 | final int id = resources.getIdentifier(iconResource.resourceName, null, null); |
| 1009 | icon = resources.getDrawable(id); |
| 1010 | } catch (Exception e) { |
| 1011 | Log.w(LOG_TAG, "Could not load live folder icon: " + extra); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | if (icon == null) { |
| 1016 | icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder); |
| 1017 | } |
| 1018 | |
| 1019 | final LiveFolderInfo info = new LiveFolderInfo(); |
| 1020 | info.icon = icon; |
| 1021 | info.filtered = filtered; |
| 1022 | info.title = name; |
| 1023 | info.iconResource = iconResource; |
| 1024 | info.uri = data.getData(); |
| 1025 | info.baseIntent = baseIntent; |
| 1026 | info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, |
| 1027 | LiveFolders.DISPLAY_MODE_GRID); |
| 1028 | |
| 1029 | LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 1030 | cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify); |
| 1031 | sModel.addFolder(info); |
| 1032 | |
| 1033 | return info; |
| 1034 | } |
| 1035 | |
| 1036 | private boolean findSingleSlot(CellLayout.CellInfo cellInfo) { |
| 1037 | final int[] xy = new int[2]; |
| 1038 | if (findSlot(cellInfo, xy, 1, 1)) { |
| 1039 | cellInfo.cellX = xy[0]; |
| 1040 | cellInfo.cellY = xy[1]; |
| 1041 | return true; |
| 1042 | } |
| 1043 | return false; |
| 1044 | } |
| 1045 | |
| 1046 | private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) { |
| 1047 | if (!cellInfo.findCellForSpan(xy, spanX, spanY)) { |
| 1048 | boolean[] occupied = mSavedState != null ? |
| 1049 | mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null; |
| 1050 | cellInfo = mWorkspace.findAllVacantCells(occupied); |
| 1051 | if (!cellInfo.findCellForSpan(xy, spanX, spanY)) { |
| 1052 | Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show(); |
| 1053 | return false; |
| 1054 | } |
| 1055 | } |
| 1056 | return true; |
| 1057 | } |
| 1058 | |
| 1059 | private void showNotifications() { |
| 1060 | final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE); |
| 1061 | if (statusBar != null) { |
| 1062 | statusBar.expand(); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | private void startWallpaper() { |
| 1067 | final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER); |
| 1068 | startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper))); |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * Registers various intent receivers. The current implementation registers |
| 1073 | * only a wallpaper intent receiver to let other applications change the |
| 1074 | * wallpaper. |
| 1075 | */ |
| 1076 | private void registerIntentReceivers() { |
| 1077 | if (sWallpaperReceiver == null) { |
| 1078 | final Application application = getApplication(); |
| 1079 | |
| 1080 | sWallpaperReceiver = new WallpaperIntentReceiver(application, this); |
| 1081 | |
| 1082 | IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED); |
| 1083 | application.registerReceiver(sWallpaperReceiver, filter); |
| 1084 | } else { |
| 1085 | sWallpaperReceiver.setLauncher(this); |
| 1086 | } |
| 1087 | |
| 1088 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 1089 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 1090 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 1091 | filter.addDataScheme("package"); |
| 1092 | registerReceiver(mApplicationsReceiver, filter); |
| 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * Registers various content observers. The current implementation registers |
| 1097 | * only a favorites observer to keep track of the favorites applications. |
| 1098 | */ |
| 1099 | private void registerContentObservers() { |
| 1100 | ContentResolver resolver = getContentResolver(); |
| 1101 | resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver); |
| 1102 | } |
| 1103 | |
| 1104 | @Override |
| 1105 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 1106 | if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| 1107 | switch (event.getKeyCode()) { |
| 1108 | case KeyEvent.KEYCODE_BACK: |
| 1109 | mWorkspace.dispatchKeyEvent(event); |
| 1110 | closeFolder(); |
| 1111 | closeDrawer(); |
| 1112 | return true; |
| 1113 | case KeyEvent.KEYCODE_HOME: |
| 1114 | return true; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | return super.dispatchKeyEvent(event); |
| 1119 | } |
| 1120 | |
| 1121 | private void closeDrawer() { |
| 1122 | closeDrawer(true); |
| 1123 | } |
| 1124 | |
| 1125 | private void closeDrawer(boolean animated) { |
| 1126 | if (mDrawer.isOpened()) { |
| 1127 | if (animated) { |
| 1128 | mDrawer.animateClose(); |
| 1129 | } else { |
| 1130 | mDrawer.close(); |
| 1131 | } |
| 1132 | if (mDrawer.hasFocus()) { |
| 1133 | mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus(); |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | private void closeFolder() { |
| 1139 | Folder folder = mWorkspace.getOpenFolder(); |
| 1140 | if (folder != null) { |
| 1141 | closeFolder(folder); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | void closeFolder(Folder folder) { |
| 1146 | folder.getInfo().opened = false; |
| 1147 | ViewGroup parent = (ViewGroup) folder.getParent(); |
| 1148 | if (parent != null) { |
| 1149 | parent.removeView(folder); |
| 1150 | } |
| 1151 | folder.onClose(); |
| 1152 | } |
| 1153 | |
| 1154 | /** |
| 1155 | * When the notification that favorites have changed is received, requests |
| 1156 | * a favorites list refresh. |
| 1157 | */ |
| 1158 | private void onFavoritesChanged() { |
| 1159 | mDesktopLocked = true; |
| 1160 | mDrawer.lock(); |
| 1161 | sModel.loadUserItems(false, this, false, false); |
| 1162 | } |
| 1163 | |
| 1164 | void onDesktopItemsLoaded() { |
| 1165 | if (mDestroyed) return; |
| 1166 | |
| 1167 | mAllAppsGrid.setAdapter(Launcher.getModel().getApplicationsAdapter()); |
| 1168 | bindDesktopItems(); |
| 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * Refreshes the shortcuts shown on the workspace. |
| 1173 | */ |
| 1174 | private void bindDesktopItems() { |
| 1175 | final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems(); |
| 1176 | final ArrayList<LauncherGadgetInfo> gadgets = sModel.getDesktopGadgets(); |
| 1177 | if (shortcuts == null || gadgets == null) { |
| 1178 | return; |
| 1179 | } |
| 1180 | |
| 1181 | final Workspace workspace = mWorkspace; |
| 1182 | int count = workspace.getChildCount(); |
| 1183 | for (int i = 0; i < count; i++) { |
| 1184 | ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout(); |
| 1185 | } |
| 1186 | |
| 1187 | if (DEBUG_USER_INTERFACE) { |
| 1188 | android.widget.Button finishButton = new android.widget.Button(this); |
| 1189 | finishButton.setText("Finish"); |
| 1190 | workspace.addInScreen(finishButton, 1, 0, 0, 1, 1); |
| 1191 | |
| 1192 | finishButton.setOnClickListener(new android.widget.Button.OnClickListener() { |
| 1193 | public void onClick(View v) { |
| 1194 | finish(); |
| 1195 | } |
| 1196 | }); |
| 1197 | } |
| 1198 | |
| 1199 | final DesktopBinder binder = new DesktopBinder(this, shortcuts, gadgets); |
| 1200 | binder.startBindingItems(); |
| 1201 | } |
| 1202 | |
| 1203 | private void bindItems(Launcher.DesktopBinder binder, |
| 1204 | ArrayList<ItemInfo> shortcuts, int start, int count) { |
| 1205 | |
| 1206 | final Workspace workspace = mWorkspace; |
| 1207 | final boolean desktopLocked = mDesktopLocked; |
| 1208 | |
| 1209 | final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count); |
| 1210 | int i = start; |
| 1211 | |
| 1212 | for ( ; i < end; i++) { |
| 1213 | final ItemInfo item = shortcuts.get(i); |
| 1214 | switch (item.itemType) { |
| 1215 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 1216 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 1217 | final View shortcut = createShortcut((ApplicationInfo) item); |
| 1218 | workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1, |
| 1219 | !desktopLocked); |
| 1220 | break; |
| 1221 | case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: |
| 1222 | final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, |
| 1223 | (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()), |
| 1224 | (UserFolderInfo) item); |
| 1225 | workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1, |
| 1226 | !desktopLocked); |
| 1227 | break; |
| 1228 | case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER: |
| 1229 | final FolderIcon newLiveFolder = LiveFolderIcon.fromXml( |
| 1230 | R.layout.live_folder_icon, this, |
| 1231 | (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()), |
| 1232 | (LiveFolderInfo) item); |
| 1233 | workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1, |
| 1234 | !desktopLocked); |
| 1235 | break; |
| 1236 | case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH: |
| 1237 | final int screen = workspace.getCurrentScreen(); |
| 1238 | final View view = mInflater.inflate(R.layout.widget_search, |
| 1239 | (ViewGroup) workspace.getChildAt(screen), false); |
| 1240 | |
| 1241 | final Widget widget = (Widget) item; |
| 1242 | view.setTag(widget); |
| 1243 | |
| 1244 | workspace.addWidget(view, widget, !desktopLocked); |
| 1245 | break; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | workspace.requestLayout(); |
| 1250 | |
| 1251 | if (end >= count) { |
| 1252 | finishBindDesktopItems(); |
| 1253 | binder.startBindingGadgets(); |
| 1254 | } else { |
| 1255 | binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget(); |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | private void finishBindDesktopItems() { |
| 1260 | if (mSavedState != null) { |
| 1261 | if (!mWorkspace.hasFocus()) { |
| 1262 | mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus(); |
| 1263 | } |
| 1264 | |
| 1265 | final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS); |
| 1266 | if (userFolders != null) { |
| 1267 | for (long folderId : userFolders) { |
| 1268 | final FolderInfo info = sModel.findFolderById(folderId); |
| 1269 | if (info != null) { |
| 1270 | openFolder(info); |
| 1271 | } |
| 1272 | } |
| 1273 | final Folder openFolder = mWorkspace.getOpenFolder(); |
| 1274 | if (openFolder != null) { |
| 1275 | openFolder.requestFocus(); |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false); |
| 1280 | if (allApps) { |
| 1281 | mDrawer.open(); |
| 1282 | } |
| 1283 | |
| 1284 | mSavedState = null; |
| 1285 | } |
| 1286 | |
| 1287 | if (mSavedInstanceState != null) { |
| 1288 | super.onRestoreInstanceState(mSavedInstanceState); |
| 1289 | mSavedInstanceState = null; |
| 1290 | } |
| 1291 | |
| 1292 | if (mDrawer.isOpened() && !mDrawer.hasFocus()) { |
| 1293 | mDrawer.requestFocus(); |
| 1294 | } |
| 1295 | |
| 1296 | mDesktopLocked = false; |
| 1297 | mDrawer.unlock(); |
| 1298 | } |
| 1299 | |
| 1300 | private void bindGadgets(Launcher.DesktopBinder binder, |
| 1301 | ArrayList<LauncherGadgetInfo> gadgets, int start, int count) { |
| 1302 | |
| 1303 | final Workspace workspace = mWorkspace; |
| 1304 | final boolean desktopLocked = mDesktopLocked; |
| 1305 | |
| 1306 | final int end = Math.min(start + DesktopBinder.GADGETS_COUNT, count); |
| 1307 | int i = start; |
| 1308 | |
| 1309 | for ( ; i < end; i++) { |
| 1310 | final LauncherGadgetInfo item = gadgets.get(i); |
| 1311 | |
| 1312 | final int gadgetId = item.gadgetId; |
| 1313 | final GadgetProviderInfo gadgetInfo = mGadgetManager.getGadgetInfo(gadgetId); |
| 1314 | item.hostView = mGadgetHost.createView(this, gadgetId, gadgetInfo); |
| 1315 | |
| 1316 | if (LOGD) Log.d(LOG_TAG, String.format("about to setGadget for id=%d, info=%s", gadgetId, gadgetInfo)); |
| 1317 | |
| 1318 | item.hostView.setGadget(gadgetId, gadgetInfo); |
| 1319 | item.hostView.setTag(item); |
| 1320 | |
| 1321 | workspace.addInScreen(item.hostView, item.screen, item.cellX, |
| 1322 | item.cellY, item.spanX, item.spanY, !desktopLocked); |
| 1323 | } |
| 1324 | |
| 1325 | workspace.requestLayout(); |
| 1326 | |
| 1327 | if (end >= count) { |
| 1328 | finishBindDesktopGadgets(); |
| 1329 | } else { |
| 1330 | binder.obtainMessage(DesktopBinder.MESSAGE_BIND_GADGETS, i, count).sendToTarget(); |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | private void finishBindDesktopGadgets() { |
| 1335 | } |
| 1336 | |
| 1337 | DragController getDragController() { |
| 1338 | return mDragLayer; |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * Launches the intent referred by the clicked shortcut. |
| 1343 | * |
| 1344 | * @param v The view representing the clicked shortcut. |
| 1345 | */ |
| 1346 | public void onClick(View v) { |
| 1347 | Object tag = v.getTag(); |
| 1348 | if (tag instanceof ApplicationInfo) { |
| 1349 | // Open shortcut |
| 1350 | final Intent intent = ((ApplicationInfo) tag).intent; |
| 1351 | startActivitySafely(intent); |
| 1352 | } else if (tag instanceof FolderInfo) { |
| 1353 | handleFolderClick((FolderInfo) tag); |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | void startActivitySafely(Intent intent) { |
| 1358 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 1359 | try { |
| 1360 | startActivity(intent); |
| 1361 | } catch (ActivityNotFoundException e) { |
| 1362 | Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 1363 | } catch (SecurityException e) { |
| 1364 | Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 1365 | Log.e(LOG_TAG, "Launcher does not have the permission to launch " + intent + |
| 1366 | ". Make sure to create a MAIN intent-filter for the corresponding activity " + |
| 1367 | "or use the exported attribute for this activity.", e); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | private void handleFolderClick(FolderInfo folderInfo) { |
| 1372 | if (!folderInfo.opened) { |
| 1373 | // Close any open folder |
| 1374 | closeFolder(); |
| 1375 | // Open the requested folder |
| 1376 | openFolder(folderInfo); |
| 1377 | } else { |
| 1378 | // Find the open folder... |
| 1379 | Folder openFolder = mWorkspace.getFolderForTag(folderInfo); |
| 1380 | int folderScreen; |
| 1381 | if (openFolder != null) { |
| 1382 | folderScreen = mWorkspace.getScreenForView(openFolder); |
| 1383 | // .. and close it |
| 1384 | closeFolder(openFolder); |
| 1385 | if (folderScreen != mWorkspace.getCurrentScreen()) { |
| 1386 | // Close any folder open on the current screen |
| 1387 | closeFolder(); |
| 1388 | // Pull the folder onto this screen |
| 1389 | openFolder(folderInfo); |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | private void loadWallpaper() { |
| 1396 | // The first time the application is started, we load the wallpaper from |
| 1397 | // the ApplicationContext |
| 1398 | if (sWallpaper == null) { |
| 1399 | final Drawable drawable = getWallpaper(); |
| 1400 | if (drawable instanceof BitmapDrawable) { |
| 1401 | sWallpaper = ((BitmapDrawable) drawable).getBitmap(); |
| 1402 | } else { |
| 1403 | throw new IllegalStateException("The wallpaper must be a BitmapDrawable."); |
| 1404 | } |
| 1405 | } |
| 1406 | mWorkspace.loadWallpaper(sWallpaper); |
| 1407 | } |
| 1408 | |
| 1409 | /** |
| 1410 | * Opens the user fodler described by the specified tag. The opening of the folder |
| 1411 | * is animated relative to the specified View. If the View is null, no animation |
| 1412 | * is played. |
| 1413 | * |
| 1414 | * @param folderInfo The FolderInfo describing the folder to open. |
| 1415 | */ |
| 1416 | private void openFolder(FolderInfo folderInfo) { |
| 1417 | Folder openFolder; |
| 1418 | |
| 1419 | if (folderInfo instanceof UserFolderInfo) { |
| 1420 | openFolder = UserFolder.fromXml(this); |
| 1421 | } else if (folderInfo instanceof LiveFolderInfo) { |
| 1422 | openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo); |
| 1423 | } else { |
| 1424 | return; |
| 1425 | } |
| 1426 | |
| 1427 | openFolder.setDragger(mDragLayer); |
| 1428 | openFolder.setLauncher(this); |
| 1429 | |
| 1430 | openFolder.bind(folderInfo); |
| 1431 | folderInfo.opened = true; |
| 1432 | |
| 1433 | mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4); |
| 1434 | openFolder.onOpen(); |
| 1435 | } |
| 1436 | |
| 1437 | /** |
| 1438 | * Returns true if the workspace is being loaded. When the workspace is loading, |
| 1439 | * no user interaction should be allowed to avoid any conflict. |
| 1440 | * |
| 1441 | * @return True if the workspace is locked, false otherwise. |
| 1442 | */ |
| 1443 | boolean isWorkspaceLocked() { |
| 1444 | return mDesktopLocked; |
| 1445 | } |
| 1446 | |
| 1447 | public boolean onLongClick(View v) { |
| 1448 | if (mDesktopLocked) { |
| 1449 | return false; |
| 1450 | } |
| 1451 | |
| 1452 | if (!(v instanceof CellLayout)) { |
| 1453 | v = (View) v.getParent(); |
| 1454 | } |
| 1455 | |
| 1456 | CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag(); |
| 1457 | |
| 1458 | // This happens when long clicking an item with the dpad/trackball |
| 1459 | if (cellInfo == null) { |
| 1460 | return true; |
| 1461 | } |
| 1462 | |
| 1463 | if (mWorkspace.allowLongPress()) { |
| 1464 | if (cellInfo.cell == null) { |
| 1465 | if (cellInfo.valid) { |
| 1466 | // User long pressed on empty space |
| 1467 | showAddDialog(cellInfo); |
| 1468 | } |
| 1469 | } else { |
| 1470 | if (!(cellInfo.cell instanceof Folder)) { |
| 1471 | // User long pressed on an item |
| 1472 | mWorkspace.startDrag(cellInfo); |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | return true; |
| 1477 | } |
| 1478 | |
| 1479 | static LauncherModel getModel() { |
| 1480 | return sModel; |
| 1481 | } |
| 1482 | |
| 1483 | void closeAllApplications() { |
| 1484 | mDrawer.close(); |
| 1485 | } |
| 1486 | |
| 1487 | boolean isDrawerDown() { |
| 1488 | return !mDrawer.isMoving() && !mDrawer.isOpened(); |
| 1489 | } |
| 1490 | |
| 1491 | boolean isDrawerUp() { |
| 1492 | return mDrawer.isOpened() && !mDrawer.isMoving(); |
| 1493 | } |
| 1494 | |
| 1495 | Workspace getWorkspace() { |
| 1496 | return mWorkspace; |
| 1497 | } |
| 1498 | |
| 1499 | GridView getApplicationsGrid() { |
| 1500 | return mAllAppsGrid; |
| 1501 | } |
| 1502 | |
| 1503 | @Override |
| 1504 | protected Dialog onCreateDialog(int id) { |
| 1505 | switch (id) { |
| 1506 | case DIALOG_CREATE_SHORTCUT: |
| 1507 | return new CreateShortcut().createDialog(); |
| 1508 | case DIALOG_RENAME_FOLDER: |
| 1509 | return new RenameFolder().createDialog(); |
| 1510 | } |
| 1511 | |
| 1512 | return super.onCreateDialog(id); |
| 1513 | } |
| 1514 | |
| 1515 | @Override |
| 1516 | protected void onPrepareDialog(int id, Dialog dialog) { |
| 1517 | switch (id) { |
| 1518 | case DIALOG_CREATE_SHORTCUT: |
| 1519 | mWorkspace.lock(); |
| 1520 | break; |
| 1521 | case DIALOG_RENAME_FOLDER: |
| 1522 | mWorkspace.lock(); |
| 1523 | EditText input = (EditText) dialog.findViewById(R.id.folder_name); |
| 1524 | final CharSequence text = mFolderInfo.title; |
| 1525 | input.setText(text); |
| 1526 | input.setSelection(0, text.length()); |
| 1527 | break; |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | void showRenameDialog(FolderInfo info) { |
| 1532 | mFolderInfo = info; |
| 1533 | mWaitingForResult = true; |
| 1534 | showDialog(DIALOG_RENAME_FOLDER); |
| 1535 | } |
| 1536 | |
| 1537 | private void showAddDialog(CellLayout.CellInfo cellInfo) { |
| 1538 | mAddItemCellInfo = cellInfo; |
| 1539 | mWaitingForResult = true; |
| 1540 | showDialog(DIALOG_CREATE_SHORTCUT); |
| 1541 | } |
| 1542 | |
| 1543 | private class RenameFolder { |
| 1544 | private EditText mInput; |
| 1545 | |
| 1546 | Dialog createDialog() { |
| 1547 | mWaitingForResult = true; |
| 1548 | final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null); |
| 1549 | mInput = (EditText) layout.findViewById(R.id.folder_name); |
| 1550 | |
| 1551 | AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this); |
| 1552 | builder.setIcon(0); |
| 1553 | builder.setTitle(getString(R.string.rename_folder_title)); |
| 1554 | builder.setCancelable(true); |
| 1555 | builder.setOnCancelListener(new Dialog.OnCancelListener() { |
| 1556 | public void onCancel(DialogInterface dialog) { |
| 1557 | cleanup(); |
| 1558 | } |
| 1559 | }); |
| 1560 | builder.setNegativeButton(getString(R.string.cancel_action), |
| 1561 | new Dialog.OnClickListener() { |
| 1562 | public void onClick(DialogInterface dialog, int which) { |
| 1563 | cleanup(); |
| 1564 | } |
| 1565 | } |
| 1566 | ); |
| 1567 | builder.setPositiveButton(getString(R.string.rename_action), |
| 1568 | new Dialog.OnClickListener() { |
| 1569 | public void onClick(DialogInterface dialog, int which) { |
| 1570 | changeFolderName(); |
| 1571 | } |
| 1572 | } |
| 1573 | ); |
| 1574 | builder.setView(layout); |
| 1575 | return builder.create(); |
| 1576 | } |
| 1577 | |
| 1578 | private void changeFolderName() { |
| 1579 | final String name = mInput.getText().toString(); |
| 1580 | if (!TextUtils.isEmpty(name)) { |
| 1581 | // Make sure we have the right folder info |
| 1582 | mFolderInfo = sModel.findFolderById(mFolderInfo.id); |
| 1583 | mFolderInfo.title = name; |
| 1584 | LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo); |
| 1585 | |
| 1586 | if (mDesktopLocked) { |
| 1587 | mDrawer.lock(); |
| 1588 | sModel.loadUserItems(false, Launcher.this, false, false); |
| 1589 | } else { |
| 1590 | final FolderIcon folderIcon = (FolderIcon) |
| 1591 | mWorkspace.getViewForTag(mFolderInfo); |
| 1592 | if (folderIcon != null) { |
| 1593 | folderIcon.setText(name); |
| 1594 | getWorkspace().requestLayout(); |
| 1595 | } else { |
| 1596 | mDesktopLocked = true; |
| 1597 | mDrawer.lock(); |
| 1598 | sModel.loadUserItems(false, Launcher.this, false, false); |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | cleanup(); |
| 1603 | } |
| 1604 | |
| 1605 | private void cleanup() { |
| 1606 | mWorkspace.unlock(); |
| 1607 | dismissDialog(DIALOG_RENAME_FOLDER); |
| 1608 | mWaitingForResult = false; |
| 1609 | mFolderInfo = null; |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | /** |
| 1614 | * Displays the shortcut creation dialog and launches, if necessary, the |
| 1615 | * appropriate activity. |
| 1616 | */ |
| 1617 | private class CreateShortcut implements AdapterView.OnItemClickListener, |
| 1618 | DialogInterface.OnCancelListener { |
| 1619 | private AddAdapter mAdapter; |
| 1620 | private ListView mList; |
| 1621 | |
| 1622 | Dialog createDialog() { |
| 1623 | mWaitingForResult = true; |
| 1624 | |
| 1625 | mAdapter = new AddAdapter(Launcher.this); |
| 1626 | |
| 1627 | final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this); |
| 1628 | builder.setTitle(getString(R.string.menu_item_add_item)); |
| 1629 | builder.setIcon(0); |
| 1630 | |
| 1631 | mList = (ListView) |
| 1632 | View.inflate(Launcher.this, R.layout.create_shortcut_list, null); |
| 1633 | mList.setAdapter(mAdapter); |
| 1634 | mList.setOnItemClickListener(this); |
| 1635 | builder.setView(mList); |
| 1636 | builder.setInverseBackgroundForced(true); |
| 1637 | |
| 1638 | AlertDialog dialog = builder.create(); |
| 1639 | dialog.setOnCancelListener(this); |
| 1640 | |
| 1641 | WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes(); |
| 1642 | attributes.gravity = Gravity.TOP; |
| 1643 | dialog.onWindowAttributesChanged(attributes); |
| 1644 | |
| 1645 | return dialog; |
| 1646 | } |
| 1647 | |
| 1648 | public void onCancel(DialogInterface dialog) { |
| 1649 | mWaitingForResult = false; |
| 1650 | cleanup(); |
| 1651 | } |
| 1652 | |
| 1653 | private void cleanup() { |
| 1654 | mWorkspace.unlock(); |
| 1655 | dismissDialog(DIALOG_CREATE_SHORTCUT); |
| 1656 | } |
| 1657 | |
| 1658 | public void onItemClick(AdapterView parent, View view, int position, long id) { |
| 1659 | // handle which item was clicked based on position |
| 1660 | // this will launch off pick intent |
| 1661 | |
| 1662 | Object tag = view.getTag(); |
| 1663 | if (tag instanceof AddAdapter.ListItem) { |
| 1664 | AddAdapter.ListItem item = (AddAdapter.ListItem) tag; |
| 1665 | cleanup(); |
| 1666 | switch (item.actionTag) { |
| 1667 | case AddAdapter.ITEM_APPLICATION: { |
| 1668 | Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); |
| 1669 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 1670 | |
| 1671 | Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); |
| 1672 | pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent); |
| 1673 | startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION); |
| 1674 | break; |
| 1675 | } |
| 1676 | |
| 1677 | case AddAdapter.ITEM_SHORTCUT: { |
| 1678 | Intent shortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); |
| 1679 | |
| 1680 | Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); |
| 1681 | pickIntent.putExtra(Intent.EXTRA_INTENT, shortcutIntent); |
| 1682 | pickIntent.putExtra(Intent.EXTRA_TITLE, |
| 1683 | getText(R.string.title_select_shortcut)); |
| 1684 | startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT); |
| 1685 | break; |
| 1686 | } |
| 1687 | |
| 1688 | case AddAdapter.ITEM_SEARCH: { |
| 1689 | addSearch(); |
| 1690 | break; |
| 1691 | } |
| 1692 | |
| 1693 | case AddAdapter.ITEM_GADGET: { |
| 1694 | int gadgetId = Launcher.this.mGadgetHost.allocateGadgetId(); |
| 1695 | |
| 1696 | Intent pickIntent = new Intent(GadgetManager.ACTION_GADGET_PICK); |
| 1697 | pickIntent.putExtra(GadgetManager.EXTRA_GADGET_ID, gadgetId); |
| 1698 | startActivityForResult(pickIntent, REQUEST_PICK_GADGET); |
| 1699 | break; |
| 1700 | } |
| 1701 | |
| 1702 | case AddAdapter.ITEM_LIVE_FOLDER: { |
| 1703 | Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER); |
| 1704 | |
| 1705 | Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); |
| 1706 | pickIntent.putExtra(Intent.EXTRA_INTENT, liveFolderIntent); |
| 1707 | pickIntent.putExtra(Intent.EXTRA_TITLE, |
| 1708 | getText(R.string.title_select_live_folder)); |
| 1709 | startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER); |
| 1710 | break; |
| 1711 | } |
| 1712 | |
| 1713 | case AddAdapter.ITEM_FOLDER: { |
| 1714 | addFolder(!mDesktopLocked); |
| 1715 | dismissDialog(DIALOG_CREATE_SHORTCUT); |
| 1716 | break; |
| 1717 | } |
| 1718 | |
| 1719 | case AddAdapter.ITEM_WALLPAPER: { |
| 1720 | startWallpaper(); |
| 1721 | break; |
| 1722 | } |
| 1723 | |
| 1724 | } |
| 1725 | |
| 1726 | } |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | /** |
| 1731 | * Receives notifications when applications are added/removed. |
| 1732 | */ |
| 1733 | private class ApplicationsIntentReceiver extends BroadcastReceiver { |
| 1734 | @Override |
| 1735 | public void onReceive(Context context, Intent intent) { |
| 1736 | boolean reloadWorkspace = false; |
| 1737 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
| 1738 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 1739 | removeShortcutsForPackage(intent.getData().getSchemeSpecificPart()); |
| 1740 | } else { |
| 1741 | reloadWorkspace = true; |
| 1742 | } |
| 1743 | } |
| 1744 | removeDialog(DIALOG_CREATE_SHORTCUT); |
| 1745 | if (!reloadWorkspace) { |
| 1746 | sModel.loadApplications(false, Launcher.this, false); |
| 1747 | } else { |
| 1748 | sModel.loadUserItems(false, Launcher.this, false, true); |
| 1749 | } |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | /** |
| 1754 | * Receives notifications whenever the user favorites have changed. |
| 1755 | */ |
| 1756 | private class FavoritesChangeObserver extends ContentObserver { |
| 1757 | public FavoritesChangeObserver() { |
| 1758 | super(new Handler()); |
| 1759 | } |
| 1760 | |
| 1761 | @Override |
| 1762 | public void onChange(boolean selfChange) { |
| 1763 | onFavoritesChanged(); |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | /** |
| 1768 | * Receives intents from other applications to change the wallpaper. |
| 1769 | */ |
| 1770 | private static class WallpaperIntentReceiver extends BroadcastReceiver { |
| 1771 | private final Application mApplication; |
| 1772 | private WeakReference<Launcher> mLauncher; |
| 1773 | |
| 1774 | WallpaperIntentReceiver(Application application, Launcher launcher) { |
| 1775 | mApplication = application; |
| 1776 | setLauncher(launcher); |
| 1777 | } |
| 1778 | |
| 1779 | void setLauncher(Launcher launcher) { |
| 1780 | mLauncher = new WeakReference<Launcher>(launcher); |
| 1781 | } |
| 1782 | |
| 1783 | @Override |
| 1784 | public void onReceive(Context context, Intent intent) { |
| 1785 | // Load the wallpaper from the ApplicationContext and store it locally |
| 1786 | // until the Launcher Activity is ready to use it |
| 1787 | final Drawable drawable = mApplication.getWallpaper(); |
| 1788 | if (drawable instanceof BitmapDrawable) { |
| 1789 | sWallpaper = ((BitmapDrawable) drawable).getBitmap(); |
| 1790 | } else { |
| 1791 | throw new IllegalStateException("The wallpaper must be a BitmapDrawable."); |
| 1792 | } |
| 1793 | |
| 1794 | // If Launcher is alive, notify we have a new wallpaper |
| 1795 | if (mLauncher != null) { |
| 1796 | final Launcher launcher = mLauncher.get(); |
| 1797 | if (launcher != null) { |
| 1798 | launcher.loadWallpaper(); |
| 1799 | } |
| 1800 | } |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener, |
| 1805 | SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener { |
| 1806 | private boolean mOpen; |
| 1807 | |
| 1808 | public void onDrawerOpened() { |
| 1809 | if (!mOpen) { |
| 1810 | mHandleIcon.reverseTransition(150); |
| 1811 | final Rect bounds = mWorkspace.mDrawerBounds; |
| 1812 | |
| 1813 | View view = mAllAppsGrid; |
| 1814 | view.getDrawingRect(bounds); |
| 1815 | |
| 1816 | while (view != mDragLayer) { |
| 1817 | bounds.offset(view.getLeft(), view.getTop()); |
| 1818 | view = (View) view.getParent(); |
| 1819 | } |
| 1820 | |
| 1821 | mOpen = true; |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | public void onDrawerClosed() { |
| 1826 | if (mOpen) { |
| 1827 | mHandleIcon.reverseTransition(150); |
| 1828 | mWorkspace.mDrawerBounds.setEmpty(); |
| 1829 | mOpen = false; |
| 1830 | } |
| 1831 | mAllAppsGrid.setSelection(0); |
| 1832 | mAllAppsGrid.clearTextFilter(); |
| 1833 | } |
| 1834 | |
| 1835 | public void onScrollStarted() { |
| 1836 | } |
| 1837 | |
| 1838 | public void onScrollEnded() { |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | private static class DesktopBinder extends Handler { |
| 1843 | static final int MESSAGE_BIND_ITEMS = 0x1; |
| 1844 | static final int MESSAGE_BIND_GADGETS = 0x2; |
| 1845 | // Number of items to bind in every pass |
| 1846 | static final int ITEMS_COUNT = 6; |
| 1847 | static final int GADGETS_COUNT = 1; |
| 1848 | |
| 1849 | private final ArrayList<ItemInfo> mShortcuts; |
| 1850 | private final ArrayList<LauncherGadgetInfo> mGadgets; |
| 1851 | private final WeakReference<Launcher> mLauncher; |
| 1852 | |
| 1853 | DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts, |
| 1854 | ArrayList<LauncherGadgetInfo> gadgets) { |
| 1855 | |
| 1856 | mLauncher = new WeakReference<Launcher>(launcher); |
| 1857 | mShortcuts = shortcuts; |
| 1858 | mGadgets = gadgets; |
| 1859 | } |
| 1860 | |
| 1861 | public void startBindingItems() { |
| 1862 | obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget(); |
| 1863 | } |
| 1864 | |
| 1865 | public void startBindingGadgets() { |
| 1866 | obtainMessage(MESSAGE_BIND_GADGETS, 0, mGadgets.size()).sendToTarget(); |
| 1867 | } |
| 1868 | |
| 1869 | @Override |
| 1870 | public void handleMessage(Message msg) { |
| 1871 | Launcher launcher = mLauncher.get(); |
| 1872 | if (launcher == null) { |
| 1873 | return; |
| 1874 | } |
| 1875 | |
| 1876 | switch (msg.what) { |
| 1877 | case MESSAGE_BIND_ITEMS: { |
| 1878 | launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2); |
| 1879 | break; |
| 1880 | } |
| 1881 | case MESSAGE_BIND_GADGETS: { |
| 1882 | launcher.bindGadgets(this, mGadgets, msg.arg1, msg.arg2); |
| 1883 | break; |
| 1884 | } |
| 1885 | } |
| 1886 | } |
| 1887 | } |
| 1888 | } |