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