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