blob: b4437d41d86542fc4b2e26757a30af59755d2679 [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;
Karl Rosaen138a0412009-04-23 19:00:21 -070074import android.widget.SlidingDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.widget.TextView;
76import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070077import android.appwidget.AppWidgetManager;
78import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
80import java.lang.ref.WeakReference;
81import java.util.ArrayList;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070082import java.util.LinkedList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
84/**
85 * Default launcher application.
86 */
87public 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 Project7376fae2009-03-11 12:11:58 -070092 private static final boolean PROFILE_DRAWER = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070093 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 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 Project7376fae2009-03-11 12:11:58 -0700107 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 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 Project7376fae2009-03-11 12:11:58 -0700111 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
113 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
114
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700115 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
116 static final String SEARCH_WIDGET = "search_widget";
117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 static final int SCREEN_COUNT = 3;
119 static final int DEFAULT_SCREN = 1;
120 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700121 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
123 private static final int DIALOG_CREATE_SHORTCUT = 1;
Romain Guycbb89e42009-06-08 15:52:54 -0700124 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
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 Projectbc219c32009-03-09 11:52:14 -0700158 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
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;
Romain Guycbb89e42009-06-08 15:52:54 -0700174
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700175 private AppWidgetManager mAppWidgetManager;
176 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700177
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 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 Project7376fae2009-03-11 12:11:58 -0700187 private HandleView mHandleView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 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 Sharkey99c87582009-03-24 17:59:43 -0700203 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 @Override
206 protected void onCreate(Bundle savedInstanceState) {
207 super.onCreate(savedInstanceState);
208 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700209
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700211
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
213 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700214
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 if (PROFILE_STARTUP) {
216 android.os.Debug.startMethodTracing("/sdcard/launcher");
217 }
218
219 checkForLocaleChange();
220 setWallpaperDimension();
221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 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 }
Romain Guycbb89e42009-06-08 15:52:54 -0700243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 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 Projectca9475f2009-03-13 13:04:24 -0700281 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
282 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 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.
Romain Guycbb89e42009-06-08 15:52:54 -0700307
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
309 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700310
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 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 Project7376fae2009-03-11 12:11:58 -0700328 case REQUEST_PICK_APPWIDGET:
329 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700331 case REQUEST_CREATE_APPWIDGET:
332 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 break;
334 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700335 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700337 // 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 Project31dd5032009-03-03 19:32:27 -0800341 }
342 }
343 mWaitingForResult = false;
344 }
345
346 @Override
347 protected void onResume() {
348 super.onResume();
349
350 if (mRestoring) {
351 startLoaders();
352 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700353
354 // Make sure that the search gadget (if any) is in its normal place.
Romain Guy5a941392009-04-28 15:18:25 -0700355 stopSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 }
357
358 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700359 protected void onPause() {
360 super.onPause();
Romain Guycbb89e42009-06-08 15:52:54 -0700361 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700362 }
Romain Guycbb89e42009-06-08 15:52:54 -0700363
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700364 @Override
365 public Object onRetainNonConfigurationInstance() {
366 // Flag any binder to stop early before switching
367 if (mBinder != null) {
368 mBinder.mTerminate = true;
369 }
Romain Guycbb89e42009-06-08 15:52:54 -0700370
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700371 if (PROFILE_ROTATE) {
372 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
373 }
374 return null;
375 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700376
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 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 Rosaen138a0412009-04-23 19:00:21 -0700390 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700391 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700392 // 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 Project31dd5032009-03-03 19:32:27 -0800397 }
398 }
399
400 return handled;
401 }
402
Karl Rosaen138a0412009-04-23 19:00:21 -0700403 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 Project31dd5032009-03-03 19:32:27 -0800413 /**
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 Project7376fae2009-03-11 12:11:58 -0700471 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
472 mHandleView.setLauncher(this);
473 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 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 Rosaen138a0412009-04-23 19:00:21 -0700482 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 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 Project7376fae2009-03-11 12:11:58 -0700493 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494
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 }
Romain Guycbb89e42009-06-08 15:52:54 -0700557
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 if (activityInfo != null) {
559 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700560
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 itemInfo.title = activityInfo.loadLabel(packageManager);
562 if (itemInfo.title == null) {
563 itemInfo.title = activityInfo.name;
564 }
Romain Guycbb89e42009-06-08 15:52:54 -0700565
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 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 }
Romain Guycbb89e42009-06-08 15:52:54 -0700574
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 /**
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;
Romain Guycbb89e42009-06-08 15:52:54 -0700586
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 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
Romain Guycbb89e42009-06-08 15:52:54 -0700599
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700601 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700603 * @param data The intent describing the appWidgetId.
604 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700606 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 boolean insertAtFirst) {
608
609 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700610 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700611
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700612 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700613
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700614 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700615
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700616 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700618 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700619
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 // 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 Project7376fae2009-03-11 12:11:58 -0700624 // Build Launcher-specific widget info and save to database
625 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 launcherInfo.spanX = spans[0];
627 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 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 Project7376fae2009-03-11 12:11:58 -0700634 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700637 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700638
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700639 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
643 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
644 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700645 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
647 }
Romain Guycbb89e42009-06-08 15:52:54 -0700648
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700649 public LauncherAppWidgetHost getAppWidgetHost() {
650 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
Romain Guycbb89e42009-06-08 15:52:54 -0700652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 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 Projectf96811c2009-03-18 17:39:48 -0700680 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
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 Guy5a941392009-04-28 15:18:25 -0700769 // 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 Project31dd5032009-03-03 19:32:27 -0800772 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774
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();
Romain Guycbb89e42009-06-08 15:52:54 -0700801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700803 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700805 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 }
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
Romain Guycbb89e42009-06-08 15:52:54 -0700826 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700828
829 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700830
Karl Rosaen138a0412009-04-23 19:00:21 -0700831 // 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 }
Romain Guycbb89e42009-06-08 15:52:54 -0700842
Karl Rosaen138a0412009-04-23 19:00:21 -0700843 /**
844 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700845 *
846 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700847 */
Romain Guycbb89e42009-06-08 15:52:54 -0700848 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700849 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700850
Karl Rosaen138a0412009-04-23 19:00:21 -0700851 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 Project31dd5032009-03-03 19:32:27 -0800856 if (appSearchData == null) {
857 appSearchData = new Bundle();
858 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
859 }
Romain Guycbb89e42009-06-08 15:52:54 -0700860
Karl Rosaen138a0412009-04-23 19:00:21 -0700861 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 Guy5a941392009-04-28 15:18:25 -0700871 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700872 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700873 });
874 }
Romain Guycbb89e42009-06-08 15:52:54 -0700875
Karl Rosaen138a0412009-04-23 19:00:21 -0700876 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700877 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 }
879
Karl Rosaen138a0412009-04-23 19:00:21 -0700880 /**
881 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700882 */
Romain Guy5a941392009-04-28 15:18:25 -0700883 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700884 // 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 }
Romain Guycbb89e42009-06-08 15:52:54 -0700895
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 @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 Guy5a941392009-04-28 15:18:25 -0700915 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
916 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917
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 Projectca9475f2009-03-13 13:04:24 -0700945 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 return true;
947 case MENU_NOTIFICATIONS:
948 showNotifications();
949 return true;
950 }
951
952 return super.onOptionsItemSelected(item);
953 }
Romain Guycbb89e42009-06-08 15:52:54 -0700954
Karl Rosaen138a0412009-04-23 19:00:21 -0700955 /**
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 Project31dd5032009-03-03 19:32:27 -0800959
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700960 @Override
961 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -0700962 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700964 }
965
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 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 }
Romain Guycbb89e42009-06-08 15:52:54 -0700975
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700976 private void updateShortcutsForPackage(String packageName) {
977 if (packageName != null && packageName.length() > 0) {
978 mWorkspace.updateShortcutsForPackage(packageName);
979 }
980 }
981
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700982 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 Project31dd5032009-03-03 19:32:27 -0800985
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700986 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 Project31dd5032009-03-03 19:32:27 -0800992 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700993 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 Project31dd5032009-03-03 19:32:27 -08001006 }
1007 }
Romain Guycbb89e42009-06-08 15:52:54 -07001008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 void addSearch() {
1010 final Widget info = Widget.makeSearch();
1011 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 final int[] xy = mCellCoordinates;
1014 final int spanX = info.spanX;
1015 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001016
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001018
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 sModel.addDesktopItem(info);
1020 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1021 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 final View view = mInflater.inflate(info.layoutResource, null);
1024 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001025 Search search = (Search) view.findViewById(R.id.widget_search);
1026 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1029 }
1030
1031 void addShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001032 // Handle case where user selected "Applications"
1033 String applicationName = getResources().getString(R.string.group_applications);
1034 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001035
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001036 if (applicationName != null && applicationName.equals(shortcutName)) {
1037 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1038 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001039
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001040 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 Project31dd5032009-03-03 19:32:27 -08001046 }
1047
1048 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001049 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001050 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001051 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001052
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001053 if (folderName != null && folderName.equals(shortcutName)) {
1054 addFolder(!mDesktopLocked);
1055 } else {
1056 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1057 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 }
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 }
Romain Guycbb89e42009-06-08 15:52:54 -07001080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 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 Projectf96811c2009-03-18 17:39:48 -07001119 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 }
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:
Romain Guycbb89e42009-06-08 15:52:54 -07001217 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001218 if (mDrawer.isOpened()) {
1219 closeDrawer();
1220 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001221 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 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 Project31dd5032009-03-03 19:32:27 -08001277 bindDesktopItems();
1278 }
Romain Guycbb89e42009-06-08 15:52:54 -07001279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 /**
1281 * Refreshes the shortcuts shown on the workspace.
1282 */
1283 private void bindDesktopItems() {
1284 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001285 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001286 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1287 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 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 }
Romain Guycbb89e42009-06-08 15:52:54 -07001296
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 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 }
Romain Guycbb89e42009-06-08 15:52:54 -07001308
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001309 // Flag any old binder to terminate early
1310 if (mBinder != null) {
1311 mBinder.mTerminate = true;
1312 }
Romain Guycbb89e42009-06-08 15:52:54 -07001313
Karl Rosaen138a0412009-04-23 19:00:21 -07001314 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001315 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
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);
Romain Guycbb89e42009-06-08 15:52:54 -07001355
Karl Rosaen138a0412009-04-23 19:00:21 -07001356 Search search = (Search) view.findViewById(R.id.widget_search);
1357 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001358
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 final Widget widget = (Widget) item;
1360 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001361
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 workspace.addWidget(view, widget, !desktopLocked);
1363 break;
1364 }
1365 }
1366
1367 workspace.requestLayout();
1368
1369 if (end >= count) {
1370 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001371 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 } 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 }
Romain Guycbb89e42009-06-08 15:52:54 -07001417
Karl Rosaen138a0412009-04-23 19:00:21 -07001418 private void bindDrawer(Launcher.DesktopBinder binder,
1419 ApplicationsAdapter drawerAdapter) {
1420 mAllAppsGrid.setAdapter(drawerAdapter);
1421 binder.startBindingAppWidgetsWhenIdle();
1422 }
Romain Guycbb89e42009-06-08 15:52:54 -07001423
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001424 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001425 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 final Workspace workspace = mWorkspace;
1428 final boolean desktopLocked = mDesktopLocked;
1429
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001430 if (!appWidgets.isEmpty()) {
1431 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001432
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001433 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001434 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001435 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001436
Karl Rosaen138a0412009-04-23 19:00:21 -07001437 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001438
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001439 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1440 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001441
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001442 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1443 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001444
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001445 workspace.requestLayout();
1446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001448 if (appWidgets.isEmpty()) {
1449 if (PROFILE_ROTATE) {
1450 android.os.Debug.stopMethodTracing();
1451 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001453 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 }
1455 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 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 Projectca9475f2009-03-13 13:04:24 -07001587 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 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 Project7376fae2009-03-11 12:11:58 -07001608 View getDrawerHandle() {
1609 return mHandleView;
1610 }
1611
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 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 Project7376fae2009-03-11 12:11:58 -07001620 boolean isDrawerMoving() {
1621 return mDrawer.isMoving();
1622 }
1623
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 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);
Romain Guycbb89e42009-06-08 15:52:54 -07001655 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 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 Sharkeyc7fdae12009-03-24 20:41:22 -07001746 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07001747 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001749
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 Dialog createDialog() {
1751 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001752
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001754
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1756 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001757 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001758
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 builder.setInverseBackgroundForced(true);
1760
1761 AlertDialog dialog = builder.create();
1762 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001763 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764
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
Romain Guycbb89e42009-06-08 15:52:54 -07001773 public void onDismiss(DialogInterface dialog) {
1774 mWorkspace.unlock();
1775 }
1776
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 private void cleanup() {
1778 mWorkspace.unlock();
1779 dismissDialog(DIALOG_CREATE_SHORTCUT);
1780 }
1781
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001782 /**
1783 * Handle the action clicked in the "Add to home" dialog.
1784 */
1785 public void onClick(DialogInterface dialog, int which) {
1786 Resources res = getResources();
1787 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001788
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001789 switch (which) {
1790 case AddAdapter.ITEM_SHORTCUT: {
1791 // Insert extra item to handle picking application
1792 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001793
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001794 ArrayList<String> shortcutNames = new ArrayList<String>();
1795 shortcutNames.add(res.getString(R.string.group_applications));
1796 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001797
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001798 ArrayList<ShortcutIconResource> shortcutIcons =
1799 new ArrayList<ShortcutIconResource>();
1800 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1801 R.drawable.ic_launcher_application));
1802 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
Romain Guycbb89e42009-06-08 15:52:54 -07001803
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001804 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1805 pickIntent.putExtra(Intent.EXTRA_INTENT,
1806 new Intent(Intent.ACTION_CREATE_SHORTCUT));
1807 pickIntent.putExtra(Intent.EXTRA_TITLE,
1808 getText(R.string.title_select_shortcut));
1809 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001810
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001811 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
1812 break;
1813 }
Romain Guycbb89e42009-06-08 15:52:54 -07001814
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001815 case AddAdapter.ITEM_APPWIDGET: {
1816 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001817
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001818 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1819 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1820 // add the search widget
1821 ArrayList<AppWidgetProviderInfo> customInfo =
1822 new ArrayList<AppWidgetProviderInfo>();
1823 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1824 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1825 info.label = getString(R.string.group_search);
1826 info.icon = R.drawable.ic_search_widget;
1827 customInfo.add(info);
1828 pickIntent.putParcelableArrayListExtra(
1829 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1830 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1831 Bundle b = new Bundle();
1832 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1833 customExtras.add(b);
1834 pickIntent.putParcelableArrayListExtra(
1835 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1836 // start the pick activity
1837 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1838 break;
1839 }
Romain Guycbb89e42009-06-08 15:52:54 -07001840
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001841 case AddAdapter.ITEM_LIVE_FOLDER: {
1842 // Insert extra item to handle inserting folder
1843 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001844
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001845 ArrayList<String> shortcutNames = new ArrayList<String>();
1846 shortcutNames.add(res.getString(R.string.group_folder));
1847 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001848
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001849 ArrayList<ShortcutIconResource> shortcutIcons =
1850 new ArrayList<ShortcutIconResource>();
1851 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1852 R.drawable.ic_launcher_folder));
1853 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1854
1855 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1856 pickIntent.putExtra(Intent.EXTRA_INTENT,
1857 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1858 pickIntent.putExtra(Intent.EXTRA_TITLE,
1859 getText(R.string.title_select_live_folder));
1860 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001861
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001862 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1863 break;
1864 }
1865
1866 case AddAdapter.ITEM_WALLPAPER: {
1867 startWallpaper();
1868 break;
1869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 }
1871 }
1872 }
1873
1874 /**
1875 * Receives notifications when applications are added/removed.
1876 */
1877 private class ApplicationsIntentReceiver extends BroadcastReceiver {
1878 @Override
1879 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001880 final String action = intent.getAction();
1881 final String packageName = intent.getData().getSchemeSpecificPart();
1882 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1883
1884 if (LauncherModel.DEBUG_LOADERS) {
1885 d(LauncherModel.LOG_TAG, "application intent received: " + action +
1886 ", replacing=" + replacing);
1887 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001889
1890 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1891 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1892 if (!replacing) {
1893 removeShortcutsForPackage(packageName);
1894 if (LauncherModel.DEBUG_LOADERS) {
1895 d(LauncherModel.LOG_TAG, " --> remove package");
1896 }
1897 sModel.removePackage(Launcher.this, packageName);
1898 }
1899 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1900 // later, we will update the package at this time
1901 } else {
1902 if (!replacing) {
1903 if (LauncherModel.DEBUG_LOADERS) {
1904 d(LauncherModel.LOG_TAG, " --> add package");
1905 }
1906 sModel.addPackage(Launcher.this, packageName);
1907 } else {
1908 if (LauncherModel.DEBUG_LOADERS) {
1909 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
1910 }
1911 sModel.updatePackage(Launcher.this, packageName);
1912 updateShortcutsForPackage(packageName);
1913 }
1914 }
1915 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001917 if (LauncherModel.DEBUG_LOADERS) {
1918 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
1919 }
1920 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 }
1922 }
1923 }
1924
1925 /**
1926 * Receives notifications whenever the user favorites have changed.
1927 */
1928 private class FavoritesChangeObserver extends ContentObserver {
1929 public FavoritesChangeObserver() {
1930 super(new Handler());
1931 }
1932
1933 @Override
1934 public void onChange(boolean selfChange) {
1935 onFavoritesChanged();
1936 }
1937 }
1938
1939 /**
1940 * Receives intents from other applications to change the wallpaper.
1941 */
1942 private static class WallpaperIntentReceiver extends BroadcastReceiver {
1943 private final Application mApplication;
1944 private WeakReference<Launcher> mLauncher;
1945
1946 WallpaperIntentReceiver(Application application, Launcher launcher) {
1947 mApplication = application;
1948 setLauncher(launcher);
1949 }
1950
1951 void setLauncher(Launcher launcher) {
1952 mLauncher = new WeakReference<Launcher>(launcher);
1953 }
1954
1955 @Override
1956 public void onReceive(Context context, Intent intent) {
1957 // Load the wallpaper from the ApplicationContext and store it locally
1958 // until the Launcher Activity is ready to use it
1959 final Drawable drawable = mApplication.getWallpaper();
1960 if (drawable instanceof BitmapDrawable) {
1961 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1962 } else {
1963 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1964 }
1965
1966 // If Launcher is alive, notify we have a new wallpaper
1967 if (mLauncher != null) {
1968 final Launcher launcher = mLauncher.get();
1969 if (launcher != null) {
1970 launcher.loadWallpaper();
1971 }
1972 }
1973 }
1974 }
1975
1976 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
1977 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
1978 private boolean mOpen;
1979
1980 public void onDrawerOpened() {
1981 if (!mOpen) {
1982 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001983
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001985 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986
1987 mOpen = true;
1988 }
1989 }
1990
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001991 private void offsetBoundsToDragLayer(Rect bounds, View view) {
1992 view.getDrawingRect(bounds);
1993
1994 while (view != mDragLayer) {
1995 bounds.offset(view.getLeft(), view.getTop());
1996 view = (View) view.getParent();
1997 }
1998 }
1999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 public void onDrawerClosed() {
2001 if (mOpen) {
2002 mHandleIcon.reverseTransition(150);
2003 mWorkspace.mDrawerBounds.setEmpty();
2004 mOpen = false;
2005 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 mAllAppsGrid.setSelection(0);
2008 mAllAppsGrid.clearTextFilter();
2009 }
2010
2011 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002012 if (PROFILE_DRAWER) {
2013 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2014 }
2015
2016 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2017 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 }
2019
2020 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002021 if (PROFILE_DRAWER) {
2022 android.os.Debug.stopMethodTracing();
2023 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 }
2025 }
2026
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002027 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002029 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002030 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 // Number of items to bind in every pass
2033 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034
2035 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002036 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002037 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002039
Karl Rosaen138a0412009-04-23 19:00:21 -07002040 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041
2042 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002043 ArrayList<LauncherAppWidgetInfo> appWidgets,
2044 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045
2046 mLauncher = new WeakReference<Launcher>(launcher);
2047 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002048 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002049
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002050 // Sort widgets so active workspace is bound first
2051 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2052 final int size = appWidgets.size();
2053 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002054
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002055 for (int i = 0; i < size; i++) {
2056 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2057 if (appWidgetInfo.screen == currentScreen) {
2058 mAppWidgets.addFirst(appWidgetInfo);
2059 } else {
2060 mAppWidgets.addLast(appWidgetInfo);
2061 }
2062 }
2063 }
Romain Guycbb89e42009-06-08 15:52:54 -07002064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 public void startBindingItems() {
2066 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2067 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002068
2069 public void startBindingDrawer() {
2070 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2071 }
Romain Guycbb89e42009-06-08 15:52:54 -07002072
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002073 public void startBindingAppWidgetsWhenIdle() {
2074 // Ask for notification when message queue becomes idle
2075 final MessageQueue messageQueue = Looper.myQueue();
2076 messageQueue.addIdleHandler(this);
2077 }
Romain Guycbb89e42009-06-08 15:52:54 -07002078
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002079 public boolean queueIdle() {
2080 // Queue is idle, so start binding items
2081 startBindingAppWidgets();
2082 return false;
2083 }
2084
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002085 public void startBindingAppWidgets() {
2086 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2087 }
2088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 @Override
2090 public void handleMessage(Message msg) {
2091 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002092 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 return;
2094 }
Romain Guycbb89e42009-06-08 15:52:54 -07002095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 switch (msg.what) {
2097 case MESSAGE_BIND_ITEMS: {
2098 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2099 break;
2100 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002101 case MESSAGE_BIND_DRAWER: {
2102 launcher.bindDrawer(this, mDrawerAdapter);
2103 break;
2104 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002105 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002106 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 break;
2108 }
2109 }
2110 }
2111 }
2112}
Karl Rosaen138a0412009-04-23 19:00:21 -07002113