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