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