blob: 293abe808a10427f0d4ac0f361d3183ac1675ba8 [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 Project15a88802009-02-10 15:44:05 -080040import android.gadget.GadgetInfo;
41import 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);
200 mGadgetHost = new LauncherGadgetHost(this, GADGET_HOST_ID);
201
202 // TODO: figure out if this is first launch and correctly clear GadgetHost database
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700203
204 if (PROFILE_STARTUP) {
205 android.os.Debug.startMethodTracing("/sdcard/launcher");
206 }
207
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800208 checkForLocaleChange();
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700209 setWallpaperDimension();
210
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700211 if (sModel == null) {
212 sModel = new LauncherModel();
213 }
214
215 setContentView(R.layout.launcher);
216 setupViews();
217
218 registerIntentReceivers();
219 registerContentObservers();
220
221 mSavedState = savedInstanceState;
222 restoreState(mSavedState);
223
224 if (PROFILE_STARTUP) {
225 android.os.Debug.stopMethodTracing();
226 }
227
228 if (!mRestoring) {
229 startLoaders();
230 }
231
232 // For handling default keys
233 mDefaultKeySsb = new SpannableStringBuilder();
234 Selection.setSelection(mDefaultKeySsb, 0);
235 }
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800236
The Android Open Source Project15a88802009-02-10 15:44:05 -0800237 @Override
238 protected void onStart() {
239 super.onStart();
240 mGadgetHost.startListening();
241 }
242
243 @Override
244 protected void onStop() {
245 super.onStop();
246 mGadgetHost.stopListening();
247 }
248
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800249 private void checkForLocaleChange() {
250 final SharedPreferences preferences = getSharedPreferences(PREFERENCES, MODE_PRIVATE);
251 final Configuration configuration = getResources().getConfiguration();
252
253 final String previousLocale = preferences.getString(KEY_LOCALE, null);
254 final String locale = configuration.locale.toString();
255
256 final int previousMcc = preferences.getInt(KEY_MCC, -1);
257 final int mcc = configuration.mcc;
258
259 final int previousMnc = preferences.getInt(KEY_MNC, -1);
260 final int mnc = configuration.mnc;
261
262 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
263
264 if (mLocaleChanged) {
265 final SharedPreferences.Editor editor = preferences.edit();
266 editor.putString(KEY_LOCALE, locale);
267 editor.putInt(KEY_MCC, mcc);
268 editor.putInt(KEY_MNC, mnc);
269 editor.commit();
270 }
271 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700272
273 static int getScreen() {
274 synchronized (sLock) {
275 return sScreen;
276 }
277 }
278
279 static void setScreen(int screen) {
280 synchronized (sLock) {
281 sScreen = screen;
282 }
283 }
284
285 private void startLoaders() {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800286 sModel.loadApplications(true, this, mLocaleChanged);
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800287 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, true);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700288 mRestoring = false;
289 }
290
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700291 private void setWallpaperDimension() {
292 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
293 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
294
295 Display display = getWindowManager().getDefaultDisplay();
296 boolean isPortrait = display.getWidth() < display.getHeight();
297
298 final int width = isPortrait ? display.getWidth() : display.getHeight();
299 final int height = isPortrait ? display.getHeight() : display.getWidth();
300 try {
301 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
302 } catch (RemoteException e) {
303 // System is dead!
304 }
305 }
306
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700307 @Override
308 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project15a88802009-02-10 15:44:05 -0800309 // The pattern used here is that a user PICKs a specific application,
310 // which, depending on the target, might need to CREATE the actual target.
311
312 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
313 // launch over to the Music app to actually CREATE_SHORTCUT.
314
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700315 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
316 switch (requestCode) {
The Android Open Source Project15a88802009-02-10 15:44:05 -0800317 case REQUEST_PICK_APPLICATION:
318 completeAddApplication(this, data, mAddItemCellInfo);
319 break;
320 case REQUEST_PICK_SHORTCUT:
321 addShortcut(data);
322 break;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700323 case REQUEST_CREATE_SHORTCUT:
324 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
325 break;
The Android Open Source Project15a88802009-02-10 15:44:05 -0800326 case REQUEST_PICK_LIVE_FOLDER:
327 addLiveFolder(data);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700328 break;
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800329 case REQUEST_CREATE_LIVE_FOLDER:
330 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
331 break;
The Android Open Source Project15a88802009-02-10 15:44:05 -0800332 case REQUEST_PICK_GADGET:
333 addGadget(data);
334 break;
335 case REQUEST_CREATE_GADGET:
336 completeAddGadget(data, mAddItemCellInfo, !mDesktopLocked);
337 break;
338 }
339 } else if (requestCode == REQUEST_PICK_GADGET &&
340 resultCode == RESULT_CANCELED && data != null) {
341 // Clean up the gadgetId if we canceled
342 int gadgetId = data.getIntExtra(GadgetManager.EXTRA_GADGET_ID, -1);
343 if (gadgetId != -1) {
344 mGadgetHost.deleteGadgetId(gadgetId);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700345 }
346 }
347 mWaitingForResult = false;
348 }
349
350 @Override
351 protected void onResume() {
352 super.onResume();
353
354 if (mRestoring) {
355 startLoaders();
356 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700357 }
358
359 @Override
360 public boolean onKeyUp(int keyCode, KeyEvent event) {
361 boolean handled = super.onKeyUp(keyCode, event);
362 if (keyCode == KeyEvent.KEYCODE_SEARCH) {
363 handled = mWorkspace.snapToSearch();
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800364 if (handled) closeDrawer(true);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700365 }
366 return handled;
367 }
368
369 @Override
370 public boolean onKeyDown(int keyCode, KeyEvent event) {
371 boolean handled = super.onKeyDown(keyCode, event);
372 if (!handled && keyCode != KeyEvent.KEYCODE_ENTER) {
373 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 */
544 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
545 cellInfo.screen = mWorkspace.getCurrentScreen();
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700546
The Android Open Source Project15a88802009-02-10 15:44:05 -0800547 // Find details for this application
548 ComponentName component = data.getComponent();
549 PackageManager packageManager = context.getPackageManager();
550 ActivityInfo activityInfo = null;
551 try {
552 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
553 } catch (NameNotFoundException e) {
554 Log.e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
555 }
556
557 if (activityInfo != null) {
558 ApplicationInfo itemInfo = new ApplicationInfo();
559
560 itemInfo.title = activityInfo.loadLabel(packageManager);
561 if (itemInfo.title == null) {
562 itemInfo.title = activityInfo.name;
563 }
564
565 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
566 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
567 itemInfo.icon = activityInfo.loadIcon(packageManager);
568 itemInfo.container = ItemInfo.NO_ID;
569
570 mWorkspace.addApplicationShortcut(itemInfo, mAddItemCellInfo);
571 }
572 }
573
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700574 /**
575 * Add a shortcut to the workspace.
576 *
577 * @param data The intent describing the shortcut.
578 * @param cellInfo The position on screen where to create the shortcut.
579 * @param insertAtFirst
580 */
581 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
582 boolean insertAtFirst) {
583
584 cellInfo.screen = mWorkspace.getCurrentScreen();
585 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
586
587 if (!mRestoring) {
588 sModel.addDesktopItem(info);
589
590 final View view = createShortcut(info);
591 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
592 } else if (sModel.isDesktopLoaded()) {
593 sModel.addDesktopItem(info);
594 }
595 }
596
The Android Open Source Project15a88802009-02-10 15:44:05 -0800597
598 /**
599 * Add a gadget to the workspace.
600 *
601 * @param data The intent describing the gadgetId.
602 * @param cellInfo The position on screen where to create the shortcut.
603 * @param insertAtFirst
604 */
605 private void completeAddGadget(Intent data, CellLayout.CellInfo cellInfo,
606 boolean insertAtFirst) {
607
608 Bundle extras = data.getExtras();
609 int gadgetId = extras.getInt(GadgetManager.EXTRA_GADGET_ID, -1);
610
611 Log.d(LOG_TAG, "dumping extras content="+extras.toString());
612
613 GadgetInfo gadgetInfo = mGadgetManager.getGadgetInfo(gadgetId);
614
615 // Calculate the grid spans needed to fit this gadget
616 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
617 layout.rectToCell(gadgetInfo.minWidth, gadgetInfo.minHeight, cellInfo);
618
619 // Try finding open space on Launcher screen
620 final int[] xy = mCellCoordinates;
621 if (!findSlot(cellInfo, xy, cellInfo.spanX, cellInfo.spanY)) return;
622
623 // Build Launcher-specific Gadget info and save to database
624 LauncherGadgetInfo launcherInfo = new LauncherGadgetInfo(gadgetId);
625 launcherInfo.spanX = cellInfo.spanX;
626 launcherInfo.spanY = cellInfo.spanY;
627
628 LauncherModel.addItemToDatabase(this, launcherInfo,
629 LauncherSettings.Favorites.CONTAINER_DESKTOP,
630 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
631
632 if (!mRestoring) {
633 sModel.addDesktopItem(launcherInfo);
634
635 // Perform actual inflation because we're live
636 launcherInfo.hostView = mGadgetHost.createView(this, gadgetId, gadgetInfo);
637
638 launcherInfo.hostView.setGadget(gadgetId, gadgetInfo);
639 launcherInfo.hostView.setTag(launcherInfo);
640
641 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
642 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
643 } else if (sModel.isDesktopLoaded()) {
644 sModel.addDesktopItem(launcherInfo);
645 }
646
647 // Request fresh update if we needed to config this gadget to
648 // remove the stale UPDATE from the initial bind
649 if (!extras.containsKey(SKIP_CONFIG) || true) {
650 // TODO: move this down into GadgetManager to prevent abuse? (anyone
651 // could force a specific gadget into the ground through flooding)
652 Log.d(LOG_TAG, "requesting new gadget update because we had a config step");
653 Intent intent = new Intent(GadgetManager.GADGET_UPDATE_ACTION);
654 intent.putExtra(GadgetManager.EXTRA_GADGET_IDS, new int[] { gadgetId });
655 intent.setComponent(gadgetInfo.provider);
656 sendBroadcast(intent);
657 }
658
659 }
660
661 public LauncherGadgetHost getGadgetHost() {
662 return mGadgetHost;
663 }
664
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700665 static ApplicationInfo addShortcut(Context context, Intent data,
666 CellLayout.CellInfo cellInfo, boolean notify) {
667
668 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
669 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
670 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
671
672 Drawable icon = null;
673 boolean filtered = false;
674 boolean customIcon = false;
675 Intent.ShortcutIconResource iconResource = null;
676
677 if (bitmap != null) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800678 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700679 filtered = true;
680 customIcon = true;
681 } else {
682 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
683 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
684 try {
685 iconResource = (Intent.ShortcutIconResource) extra;
686 final PackageManager packageManager = context.getPackageManager();
687 Resources resources = packageManager.getResourcesForApplication(
688 iconResource.packageName);
689 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
690 icon = resources.getDrawable(id);
691 } catch (Exception e) {
692 Log.w(LOG_TAG, "Could not load shortcut icon: " + extra);
693 }
694 }
695 }
696
697 if (icon == null) {
698 icon = context.getPackageManager().getDefaultActivityIcon();
699 }
700
701 final ApplicationInfo info = new ApplicationInfo();
702 info.icon = icon;
703 info.filtered = filtered;
704 info.title = name;
705 info.intent = intent;
706 info.customIcon = customIcon;
707 info.iconResource = iconResource;
708
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800709 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700710 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
711 return info;
712 }
713
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700714 @Override
715 protected void onNewIntent(Intent intent) {
716 super.onNewIntent(intent);
717
718 // Close the menu
719 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
720 getWindow().closeAllPanels();
721
722 try {
723 dismissDialog(DIALOG_CREATE_SHORTCUT);
724 // Unlock the workspace if the dialog was showing
725 mWorkspace.unlock();
726 } catch (Exception e) {
727 // An exception is thrown if the dialog is not visible, which is fine
728 }
729
730 try {
731 dismissDialog(DIALOG_RENAME_FOLDER);
732 // Unlock the workspace if the dialog was showing
733 mWorkspace.unlock();
734 } catch (Exception e) {
735 // An exception is thrown if the dialog is not visible, which is fine
736 }
737
738 // If we are already in front we go back to the default screen,
739 // otherwise we don't
740 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
741 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
742 if (!mWorkspace.isDefaultScreenShowing()) {
743 mWorkspace.moveToDefaultScreen();
744 }
745 closeDrawer();
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800746 View v = getWindow().peekDecorView();
747 if (v != null && v.getWindowToken() != null) {
748 InputMethodManager imm = (InputMethodManager)getSystemService(
749 INPUT_METHOD_SERVICE);
The Android Open Source Project946cd912009-01-15 16:12:13 -0800750 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800751 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700752 } else {
753 closeDrawer(false);
754 }
755 }
756 }
757
758 @Override
759 protected void onSaveInstanceState(Bundle outState) {
760 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
761
762 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
763 if (folders.size() > 0) {
764 final int count = folders.size();
765 long[] ids = new long[count];
766 for (int i = 0; i < count; i++) {
767 final FolderInfo info = folders.get(i).getInfo();
768 ids[i] = info.id;
769 }
770 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
771 } else {
772 super.onSaveInstanceState(outState);
773 }
774
775 if (mDrawer.isOpened()) {
776 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
777 }
778
779 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
780 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
781 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
782
783 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
784 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
785 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
786 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
787 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
788 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
789 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
790 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
791 layout.getOccupiedCells());
792 }
793
794 if (mFolderInfo != null && mWaitingForResult) {
795 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
796 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
797 }
798 }
799
800 @Override
801 public void onDestroy() {
802 mDestroyed = true;
803
804 super.onDestroy();
805
806 TextKeyListener.getInstance().release();
807
808 mAllAppsGrid.clearTextFilter();
809 mAllAppsGrid.setAdapter(null);
810 sModel.unbind();
811 sModel.abortLoaders();
812
813 getContentResolver().unregisterContentObserver(mObserver);
814 unregisterReceiver(mApplicationsReceiver);
815 }
816
817 @Override
818 public void startActivityForResult(Intent intent, int requestCode) {
819 mWaitingForResult = true;
820 super.startActivityForResult(intent, requestCode);
821 }
822
823 @Override
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800824 public void startSearch(String initialQuery, boolean selectInitialQuery,
825 Bundle appSearchData, boolean globalSearch) {
826 if (appSearchData == null) {
827 appSearchData = new Bundle();
828 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
829 }
830 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
831 }
832
833 @Override
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700834 public boolean onCreateOptionsMenu(Menu menu) {
835 if (mDesktopLocked) return false;
836
837 super.onCreateOptionsMenu(menu);
838 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
839 .setIcon(android.R.drawable.ic_menu_add)
840 .setAlphabeticShortcut('A');
841 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800842 .setIcon(android.R.drawable.ic_menu_gallery)
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700843 .setAlphabeticShortcut('W');
844 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
845 .setIcon(android.R.drawable.ic_search_category_default)
846 .setAlphabeticShortcut(SearchManager.MENU_KEY);
847 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
The Android Open Source Project62b49bb2009-01-20 14:04:00 -0800848 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700849 .setAlphabeticShortcut('N');
850
851 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
852 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
853 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
854
855 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800856 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700857 .setIntent(settings);
858
859 return true;
860 }
861
862 @Override
863 public boolean onPrepareOptionsMenu(Menu menu) {
864 super.onPrepareOptionsMenu(menu);
865
866 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
867 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
868
869 return true;
870 }
871
872 @Override
873 public boolean onOptionsItemSelected(MenuItem item) {
874 switch (item.getItemId()) {
875 case MENU_ADD:
876 addItems();
877 return true;
878 case MENU_WALLPAPER_SETTINGS:
879 startWallpaper();
880 return true;
881 case MENU_SEARCH:
882 if (!mWorkspace.snapToSearch()) {
883 onSearchRequested();
884 }
885 return true;
886 case MENU_NOTIFICATIONS:
887 showNotifications();
888 return true;
889 }
890
891 return super.onOptionsItemSelected(item);
892 }
893
894 private void addItems() {
895 showAddDialog(mMenuAddInfo);
896 }
897
898 private void removeShortcutsForPackage(String packageName) {
899 if (packageName != null && packageName.length() > 0) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700900 mWorkspace.removeShortcutsForPackage(packageName);
901 }
902 }
The Android Open Source Project15a88802009-02-10 15:44:05 -0800903
904 static final String SKIP_CONFIG = "skip_config";
905
906 void addGadget(Intent data) {
907 int gadgetId = data.getIntExtra(GadgetManager.EXTRA_GADGET_ID, -1);
908 GadgetInfo gadget = mGadgetManager.getGadgetInfo(gadgetId);
909
910 if (gadget.configure != null) {
911 // Launch over to configure gadget, if needed
912 Intent intent = new Intent(GadgetManager.GADGET_CONFIGURE_ACTION);
913 intent.setComponent(gadget.configure);
914 intent.putExtra(GadgetManager.EXTRA_GADGET_ID, gadgetId);
915
916 startActivityForResult(intent, REQUEST_CREATE_GADGET);
917 } else {
918 // Otherwise just add it
919 Log.d(LOG_TAG, "dumping extras content="+data.getExtras().toString());
920 data.putExtra(SKIP_CONFIG, true);
921 Log.d(LOG_TAG, "dumping extras content="+data.getExtras().toString());
922 onActivityResult(REQUEST_CREATE_GADGET, Activity.RESULT_OK, data);
923 }
924 }
925
926 void addSearch() {
927 final Widget info = Widget.makeSearch();
928 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
929
930 final int[] xy = mCellCoordinates;
931 final int spanX = info.spanX;
932 final int spanY = info.spanY;
933
934 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
935
936 sModel.addDesktopItem(info);
937 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
938 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
939
940 final View view = mInflater.inflate(info.layoutResource, null);
941 view.setTag(info);
942
943 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
944 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700945
946 void addShortcut(Intent intent) {
947 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
948 }
949
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800950 void addLiveFolder(Intent intent) {
951 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
952 }
953
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700954 void addFolder() {
955 UserFolderInfo folderInfo = new UserFolderInfo();
956 folderInfo.title = getText(R.string.folder_name);
957 int cellX = mAddItemCellInfo.cellX;
958 int cellY = mAddItemCellInfo.cellY;
959
960 // Update the model
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800961 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700962 mWorkspace.getCurrentScreen(), cellX, cellY, false);
963 sModel.addDesktopItem(folderInfo);
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800964 sModel.addFolder(folderInfo);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700965
966 // Create the view
967 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
968 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
969 mWorkspace.addInCurrentScreen(newFolder, cellX, cellY, 1, 1);
970 }
971
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800972 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
973 boolean insertAtFirst) {
974
975 cellInfo.screen = mWorkspace.getCurrentScreen();
976 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
977
978 if (!mRestoring) {
979 sModel.addDesktopItem(info);
980
981 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
982 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
983 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
984 } else if (sModel.isDesktopLoaded()) {
985 sModel.addDesktopItem(info);
986 }
987 }
988
989 static LiveFolderInfo addLiveFolder(Context context, Intent data,
990 CellLayout.CellInfo cellInfo, boolean notify) {
991
992 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
993 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
994
995 Drawable icon = null;
996 boolean filtered = false;
997 Intent.ShortcutIconResource iconResource = null;
998
999 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1000 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1001 try {
1002 iconResource = (Intent.ShortcutIconResource) extra;
1003 final PackageManager packageManager = context.getPackageManager();
1004 Resources resources = packageManager.getResourcesForApplication(
1005 iconResource.packageName);
1006 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1007 icon = resources.getDrawable(id);
1008 } catch (Exception e) {
1009 Log.w(LOG_TAG, "Could not load live folder icon: " + extra);
1010 }
1011 }
1012
1013 if (icon == null) {
1014 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1015 }
1016
1017 final LiveFolderInfo info = new LiveFolderInfo();
1018 info.icon = icon;
1019 info.filtered = filtered;
1020 info.title = name;
1021 info.iconResource = iconResource;
1022 info.uri = data.getData();
1023 info.baseIntent = baseIntent;
1024 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1025 LiveFolders.DISPLAY_MODE_GRID);
1026
1027 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1028 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1029 sModel.addFolder(info);
1030
1031 return info;
1032 }
1033
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001034 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1035 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1036 boolean[] occupied = mSavedState != null ?
1037 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1038 cellInfo = mWorkspace.findAllVacantCells(occupied);
1039 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1040 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1041 return false;
1042 }
1043 }
1044 return true;
1045 }
1046
1047 private void showNotifications() {
1048 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1049 if (statusBar != null) {
1050 statusBar.expand();
1051 }
1052 }
1053
1054 private void startWallpaper() {
1055 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1056 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1057 }
1058
1059 /**
1060 * Registers various intent receivers. The current implementation registers
1061 * only a wallpaper intent receiver to let other applications change the
1062 * wallpaper.
1063 */
1064 private void registerIntentReceivers() {
1065 if (sWallpaperReceiver == null) {
1066 final Application application = getApplication();
1067
1068 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1069
1070 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1071 application.registerReceiver(sWallpaperReceiver, filter);
1072 } else {
1073 sWallpaperReceiver.setLauncher(this);
1074 }
1075
1076 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1077 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1078 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1079 filter.addDataScheme("package");
1080 registerReceiver(mApplicationsReceiver, filter);
1081 }
1082
1083 /**
1084 * Registers various content observers. The current implementation registers
1085 * only a favorites observer to keep track of the favorites applications.
1086 */
1087 private void registerContentObservers() {
1088 ContentResolver resolver = getContentResolver();
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001089 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001090 }
1091
1092 @Override
1093 public boolean dispatchKeyEvent(KeyEvent event) {
1094 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1095 switch (event.getKeyCode()) {
1096 case KeyEvent.KEYCODE_BACK:
1097 mWorkspace.dispatchKeyEvent(event);
1098 closeFolder();
1099 closeDrawer();
1100 return true;
1101 case KeyEvent.KEYCODE_HOME:
1102 return true;
1103 }
1104 }
1105
1106 return super.dispatchKeyEvent(event);
1107 }
1108
1109 private void closeDrawer() {
1110 closeDrawer(true);
1111 }
1112
1113 private void closeDrawer(boolean animated) {
1114 if (mDrawer.isOpened()) {
1115 if (animated) {
1116 mDrawer.animateClose();
1117 } else {
1118 mDrawer.close();
1119 }
1120 if (mDrawer.hasFocus()) {
1121 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1122 }
1123 }
1124 }
1125
1126 private void closeFolder() {
1127 Folder folder = mWorkspace.getOpenFolder();
1128 if (folder != null) {
1129 closeFolder(folder);
1130 }
1131 }
1132
1133 void closeFolder(Folder folder) {
1134 folder.getInfo().opened = false;
1135 ViewGroup parent = (ViewGroup) folder.getParent();
1136 if (parent != null) {
1137 parent.removeView(folder);
1138 }
1139 folder.onClose();
1140 }
1141
1142 /**
1143 * When the notification that favorites have changed is received, requests
1144 * a favorites list refresh.
1145 */
1146 private void onFavoritesChanged() {
1147 mDesktopLocked = true;
1148 mDrawer.lock();
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001149 sModel.loadUserItems(false, this, false, false);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001150 }
1151
1152 void onDesktopItemsLoaded() {
1153 if (mDestroyed) return;
1154
1155 bindDesktopItems();
1156 mAllAppsGrid.setAdapter(Launcher.getModel().getApplicationsAdapter());
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001157 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001158
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001159 /**
1160 * Refreshes the shortcuts shown on the workspace.
1161 */
1162 private void bindDesktopItems() {
1163 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
1164 if (shortcuts == null) {
1165 return;
1166 }
1167
1168 final Workspace workspace = mWorkspace;
1169 int count = workspace.getChildCount();
1170 for (int i = 0; i < count; i++) {
1171 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1172 }
The Android Open Source Project15a88802009-02-10 15:44:05 -08001173
1174 if (DEBUG_USER_INTERFACE) {
1175 android.widget.Button finishButton = new android.widget.Button(this);
1176 finishButton.setText("Finish");
1177 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1178
1179 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1180 public void onClick(View v) {
1181 finish();
1182 }
1183 });
1184 }
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001185
1186 count = shortcuts.size();
1187
1188 final DesktopItemsBinder binder = new DesktopItemsBinder(this, shortcuts);
1189 binder.obtainMessage(DesktopItemsBinder.MESSAGE_BIND_ITEMS, 0, count).sendToTarget();
1190 }
1191
1192 private void bindItems(Launcher.DesktopItemsBinder binder,
1193 ArrayList<ItemInfo> shortcuts, int start, int count) {
1194
1195 final Workspace workspace = mWorkspace;
1196 final boolean desktopLocked = mDesktopLocked;
1197
1198 final int end = Math.min(start + DesktopItemsBinder.ITEMS_COUNT, count);
1199 int i = start;
1200
1201 for ( ; i < end; i++) {
1202 final ItemInfo item = shortcuts.get(i);
1203 switch (item.itemType) {
1204 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1205 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1206 final View shortcut = createShortcut((ApplicationInfo) item);
1207 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1208 !desktopLocked);
1209 break;
1210 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1211 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1212 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1213 (UserFolderInfo) item);
1214 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1215 !desktopLocked);
1216 break;
1217 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1218 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1219 R.layout.live_folder_icon, this,
1220 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1221 (LiveFolderInfo) item);
1222 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1223 !desktopLocked);
1224 break;
The Android Open Source Project15a88802009-02-10 15:44:05 -08001225 case LauncherSettings.Favorites.ITEM_TYPE_GADGET:
1226 final LauncherGadgetInfo launcherInfo = (LauncherGadgetInfo) item;
1227
1228 final int gadgetId = launcherInfo.gadgetId;
1229 GadgetInfo gadgetInfo = mGadgetManager.getGadgetInfo(gadgetId);
1230 launcherInfo.hostView = mGadgetHost.createView(this, gadgetId, gadgetInfo);
1231
1232 Log.d(LOG_TAG, "about to setGadget during desktop bind");
1233 launcherInfo.hostView.setGadget(gadgetId, gadgetInfo);
1234 launcherInfo.hostView.setTag(launcherInfo);
1235
1236 workspace.addInScreen(launcherInfo.hostView, item.screen, item.cellX,
1237 item.cellY, item.spanX, item.spanY, !desktopLocked);
1238
1239 // Now that we've bound the item, request an update for it
1240 if (gadgetInfo != null) {
1241 if (gadgetInfo.provider != null) {
1242 Intent intent = new Intent(GadgetManager.GADGET_UPDATE_ACTION);
1243 intent.putExtra(GadgetManager.EXTRA_GADGET_IDS, new int[] { gadgetId });
1244 intent.setComponent(gadgetInfo.provider);
1245 sendBroadcast(intent);
1246 }
1247 }
1248
1249 break;
1250 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1251 final int screen = workspace.getCurrentScreen();
1252 final View view = mInflater.inflate(R.layout.widget_search,
1253 (ViewGroup) workspace.getChildAt(screen), false);
1254
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001255 final Widget widget = (Widget) item;
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001256 view.setTag(widget);
The Android Open Source Project15a88802009-02-10 15:44:05 -08001257
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001258 workspace.addWidget(view, widget, !desktopLocked);
The Android Open Source Project15a88802009-02-10 15:44:05 -08001259 break;
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001260 }
1261 }
1262
1263 workspace.requestLayout();
1264
1265 if (end >= count) {
1266 finishBindDesktopItems();
1267 } else {
1268 binder.obtainMessage(DesktopItemsBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1269 }
1270 }
1271
1272 private void finishBindDesktopItems() {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001273 if (mSavedState != null) {
1274 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1275
1276 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1277 if (userFolders != null) {
1278 for (long folderId : userFolders) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001279 final FolderInfo info = sModel.findFolderById(folderId);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001280 if (info != null) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001281 openFolder(info);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001282 }
1283 }
1284 final Folder openFolder = mWorkspace.getOpenFolder();
1285 if (openFolder != null) {
1286 openFolder.requestFocus();
1287 } else {
1288 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1289 }
1290 }
1291
1292 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1293 if (allApps) {
1294 mDrawer.open();
1295 mDrawer.requestFocus();
1296 }
1297
1298 mSavedState = null;
1299 }
1300
1301 mDesktopLocked = false;
1302 mDrawer.unlock();
1303 }
1304
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001305 DragController getDragController() {
1306 return mDragLayer;
1307 }
1308
1309 /**
1310 * Launches the intent referred by the clicked shortcut.
1311 *
1312 * @param v The view representing the clicked shortcut.
1313 */
1314 public void onClick(View v) {
1315 Object tag = v.getTag();
1316 if (tag instanceof ApplicationInfo) {
1317 // Open shortcut
1318 final Intent intent = ((ApplicationInfo) tag).intent;
1319 startActivitySafely(intent);
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001320 } else if (tag instanceof FolderInfo) {
1321 handleFolderClick((FolderInfo) tag);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001322 }
1323 }
1324
1325 void startActivitySafely(Intent intent) {
1326 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1327 try {
1328 startActivity(intent);
1329 } catch (ActivityNotFoundException e) {
1330 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
The Android Open Source Project15a88802009-02-10 15:44:05 -08001331 } catch (SecurityException e) {
1332 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1333 Log.e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
1334 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1335 "or use the exported attribute for this activity.", e);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001336 }
1337 }
1338
1339 private void handleFolderClick(FolderInfo folderInfo) {
1340 if (!folderInfo.opened) {
1341 // Close any open folder
1342 closeFolder();
1343 // Open the requested folder
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001344 openFolder(folderInfo);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001345 } else {
1346 // Find the open folder...
1347 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1348 int folderScreen;
1349 if (openFolder != null) {
1350 folderScreen = mWorkspace.getScreenForView(openFolder);
1351 // .. and close it
1352 closeFolder(openFolder);
1353 if (folderScreen != mWorkspace.getCurrentScreen()) {
1354 // Close any folder open on the current screen
1355 closeFolder();
1356 // Pull the folder onto this screen
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001357 openFolder(folderInfo);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001358 }
1359 }
1360 }
1361 }
1362
1363 private void loadWallpaper() {
1364 // The first time the application is started, we load the wallpaper from
1365 // the ApplicationContext
1366 if (sWallpaper == null) {
1367 final Drawable drawable = getWallpaper();
1368 if (drawable instanceof BitmapDrawable) {
1369 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1370 } else {
1371 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1372 }
1373 }
1374 mWorkspace.loadWallpaper(sWallpaper);
1375 }
1376
1377 /**
1378 * Opens the user fodler described by the specified tag. The opening of the folder
1379 * is animated relative to the specified View. If the View is null, no animation
1380 * is played.
1381 *
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001382 * @param folderInfo The FolderInfo describing the folder to open.
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001383 */
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001384 private void openFolder(FolderInfo folderInfo) {
1385 Folder openFolder;
1386
1387 if (folderInfo instanceof UserFolderInfo) {
1388 openFolder = UserFolder.fromXml(this);
1389 } else if (folderInfo instanceof LiveFolderInfo) {
1390 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1391 } else {
1392 return;
1393 }
1394
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001395 openFolder.setDragger(mDragLayer);
1396 openFolder.setLauncher(this);
1397
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001398 openFolder.bind(folderInfo);
1399 folderInfo.opened = true;
1400
1401 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1402 openFolder.onOpen();
1403 }
1404
1405 /**
1406 * Returns true if the workspace is being loaded. When the workspace is loading,
1407 * no user interaction should be allowed to avoid any conflict.
1408 *
1409 * @return True if the workspace is locked, false otherwise.
1410 */
1411 boolean isWorkspaceLocked() {
1412 return mDesktopLocked;
1413 }
1414
1415 public boolean onLongClick(View v) {
1416 if (mDesktopLocked) {
1417 return false;
1418 }
1419
1420 if (!(v instanceof CellLayout)) {
1421 v = (View) v.getParent();
1422 }
1423
1424 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1425
1426 // This happens when long clicking an item with the dpad/trackball
1427 if (cellInfo == null) {
1428 return false;
1429 }
1430
1431 if (mWorkspace.allowLongPress()) {
1432 if (cellInfo.cell == null) {
1433 if (cellInfo.valid) {
1434 // User long pressed on empty space
1435 showAddDialog(cellInfo);
1436 }
1437 } else {
1438 if (!(cellInfo.cell instanceof Folder)) {
1439 // User long pressed on an item
1440 mWorkspace.startDrag(cellInfo);
1441 }
1442 }
1443 }
1444 return true;
1445 }
1446
1447 static LauncherModel getModel() {
1448 return sModel;
1449 }
1450
1451 void closeAllApplications() {
1452 mDrawer.close();
1453 }
1454
1455 boolean isDrawerDown() {
1456 return !mDrawer.isMoving() && !mDrawer.isOpened();
1457 }
1458
1459 Workspace getWorkspace() {
1460 return mWorkspace;
1461 }
1462
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001463 GridView getApplicationsGrid() {
1464 return mAllAppsGrid;
1465 }
1466
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001467 @Override
1468 protected Dialog onCreateDialog(int id) {
1469 switch (id) {
1470 case DIALOG_CREATE_SHORTCUT:
1471 return new CreateShortcut().createDialog();
1472 case DIALOG_RENAME_FOLDER:
1473 return new RenameFolder().createDialog();
1474 }
1475
1476 return super.onCreateDialog(id);
1477 }
1478
1479 @Override
1480 protected void onPrepareDialog(int id, Dialog dialog) {
1481 switch (id) {
1482 case DIALOG_CREATE_SHORTCUT:
1483 mWorkspace.lock();
1484 break;
1485 case DIALOG_RENAME_FOLDER:
1486 mWorkspace.lock();
1487 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1488 final CharSequence text = mFolderInfo.title;
1489 input.setText(text);
1490 input.setSelection(0, text.length());
1491 break;
1492 }
1493 }
1494
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001495 void showRenameDialog(FolderInfo info) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001496 mFolderInfo = info;
1497 mWaitingForResult = true;
1498 showDialog(DIALOG_RENAME_FOLDER);
1499 }
1500
1501 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1502 mAddItemCellInfo = cellInfo;
1503 mWaitingForResult = true;
1504 showDialog(DIALOG_CREATE_SHORTCUT);
1505 }
1506
1507 private class RenameFolder {
1508 private EditText mInput;
1509
1510 Dialog createDialog() {
1511 mWaitingForResult = true;
1512 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1513 mInput = (EditText) layout.findViewById(R.id.folder_name);
1514
1515 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1516 builder.setIcon(0);
1517 builder.setTitle(getString(R.string.rename_folder_title));
1518 builder.setCancelable(true);
1519 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1520 public void onCancel(DialogInterface dialog) {
1521 cleanup();
1522 }
1523 });
1524 builder.setNegativeButton(getString(R.string.cancel_action),
1525 new Dialog.OnClickListener() {
1526 public void onClick(DialogInterface dialog, int which) {
1527 cleanup();
1528 }
1529 }
1530 );
1531 builder.setPositiveButton(getString(R.string.rename_action),
1532 new Dialog.OnClickListener() {
1533 public void onClick(DialogInterface dialog, int which) {
1534 changeFolderName();
1535 }
1536 }
1537 );
1538 builder.setView(layout);
1539 return builder.create();
1540 }
1541
1542 private void changeFolderName() {
1543 final String name = mInput.getText().toString();
1544 if (!TextUtils.isEmpty(name)) {
1545 // Make sure we have the right folder info
1546 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1547 mFolderInfo.title = name;
1548 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1549
1550 if (mDesktopLocked) {
1551 mDrawer.lock();
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001552 sModel.loadUserItems(false, Launcher.this, false, false);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001553 } else {
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001554 final FolderIcon folderIcon = (FolderIcon)
1555 mWorkspace.getViewForTag(mFolderInfo);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001556 if (folderIcon != null) {
1557 folderIcon.setText(name);
1558 getWorkspace().requestLayout();
1559 } else {
1560 mDesktopLocked = true;
1561 mDrawer.lock();
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001562 sModel.loadUserItems(false, Launcher.this, false, false);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001563 }
1564 }
1565 }
1566 cleanup();
1567 }
1568
1569 private void cleanup() {
1570 mWorkspace.unlock();
1571 dismissDialog(DIALOG_RENAME_FOLDER);
1572 mWaitingForResult = false;
1573 mFolderInfo = null;
1574 }
1575 }
1576
1577 /**
1578 * Displays the shortcut creation dialog and launches, if necessary, the
1579 * appropriate activity.
1580 */
The Android Open Source Project15a88802009-02-10 15:44:05 -08001581 private class CreateShortcut implements AdapterView.OnItemClickListener,
1582 DialogInterface.OnCancelListener {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001583 private AddAdapter mAdapter;
The Android Open Source Project15a88802009-02-10 15:44:05 -08001584 private ListView mList;
1585
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001586 Dialog createDialog() {
1587 mWaitingForResult = true;
The Android Open Source Project15a88802009-02-10 15:44:05 -08001588
1589 mAdapter = new AddAdapter(Launcher.this);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001590
1591 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1592 builder.setTitle(getString(R.string.menu_item_add_item));
1593 builder.setIcon(0);
1594
The Android Open Source Project15a88802009-02-10 15:44:05 -08001595 mList = (ListView)
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001596 View.inflate(Launcher.this, R.layout.create_shortcut_list, null);
1597 mList.setAdapter(mAdapter);
The Android Open Source Project15a88802009-02-10 15:44:05 -08001598 mList.setOnItemClickListener(this);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001599 builder.setView(mList);
1600 builder.setInverseBackgroundForced(true);
1601
1602 AlertDialog dialog = builder.create();
1603 dialog.setOnCancelListener(this);
1604
1605 WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
1606 attributes.gravity = Gravity.TOP;
1607 dialog.onWindowAttributesChanged(attributes);
1608
1609 return dialog;
1610 }
1611
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001612 public void onCancel(DialogInterface dialog) {
1613 mWaitingForResult = false;
1614 cleanup();
1615 }
1616
1617 private void cleanup() {
1618 mWorkspace.unlock();
1619 dismissDialog(DIALOG_CREATE_SHORTCUT);
1620 }
1621
The Android Open Source Project15a88802009-02-10 15:44:05 -08001622 public void onItemClick(AdapterView parent, View view, int position, long id) {
1623 // handle which item was clicked based on position
1624 // this will launch off pick intent
1625
1626 Object tag = view.getTag();
1627 if (tag instanceof AddAdapter.ListItem) {
1628 AddAdapter.ListItem item = (AddAdapter.ListItem) tag;
1629 cleanup();
1630 switch (item.actionTag) {
1631 case AddAdapter.ITEM_APPLICATION: {
1632 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1633 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1634
1635 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1636 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
1637 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
1638 break;
1639 }
1640
1641 case AddAdapter.ITEM_SHORTCUT: {
1642 Intent shortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1643
1644 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1645 pickIntent.putExtra(Intent.EXTRA_INTENT, shortcutIntent);
1646 pickIntent.putExtra(Intent.EXTRA_TITLE,
1647 getText(R.string.title_select_shortcut));
1648 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
1649 break;
1650 }
1651
1652 case AddAdapter.ITEM_SEARCH: {
1653 addSearch();
1654 break;
1655 }
1656
1657 case AddAdapter.ITEM_GADGET: {
1658 int gadgetId = Launcher.this.mGadgetHost.allocateGadgetId();
1659
1660 Intent pickIntent = new Intent(GadgetManager.GADGET_PICK_ACTION);
1661 pickIntent.putExtra(GadgetManager.EXTRA_HOST_ID, GADGET_HOST_ID);
1662 pickIntent.putExtra(GadgetManager.EXTRA_GADGET_ID, gadgetId);
1663 startActivityForResult(pickIntent, REQUEST_PICK_GADGET);
1664 break;
1665 }
1666
1667 case AddAdapter.ITEM_LIVE_FOLDER: {
1668 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1669
1670 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1671 pickIntent.putExtra(Intent.EXTRA_INTENT, liveFolderIntent);
1672 pickIntent.putExtra(Intent.EXTRA_TITLE,
1673 getText(R.string.title_select_live_folder));
1674 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1675 break;
1676 }
1677
1678 case AddAdapter.ITEM_FOLDER: {
1679 addFolder();
1680 dismissDialog(DIALOG_CREATE_SHORTCUT);
1681 break;
1682 }
1683
1684 case AddAdapter.ITEM_WALLPAPER: {
1685 startWallpaper();
1686 break;
1687 }
1688
1689 }
1690
1691 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001692 }
1693 }
1694
1695 /**
1696 * Receives notifications when applications are added/removed.
1697 */
1698 private class ApplicationsIntentReceiver extends BroadcastReceiver {
1699 @Override
1700 public void onReceive(Context context, Intent intent) {
The Android Open Source Project15a88802009-02-10 15:44:05 -08001701 boolean reloadWorkspace = false;
1702 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
1703 if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
1704 removeShortcutsForPackage(intent.getData().getSchemeSpecificPart());
1705 } else {
1706 reloadWorkspace = true;
1707 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001708 }
1709 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project15a88802009-02-10 15:44:05 -08001710 if (!reloadWorkspace) {
1711 sModel.loadApplications(false, Launcher.this, false);
1712 } else {
1713 sModel.loadUserItems(false, Launcher.this, false, true);
1714 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001715 }
1716 }
1717
1718 /**
1719 * Receives notifications whenever the user favorites have changed.
1720 */
1721 private class FavoritesChangeObserver extends ContentObserver {
1722 public FavoritesChangeObserver() {
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001723 super(new Handler());
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001724 }
1725
1726 @Override
1727 public void onChange(boolean selfChange) {
1728 onFavoritesChanged();
1729 }
1730 }
1731
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001732 /**
1733 * Receives intents from other applications to change the wallpaper.
1734 */
1735 private static class WallpaperIntentReceiver extends BroadcastReceiver {
1736 private final Application mApplication;
1737 private WeakReference<Launcher> mLauncher;
1738
1739 WallpaperIntentReceiver(Application application, Launcher launcher) {
1740 mApplication = application;
1741 setLauncher(launcher);
1742 }
1743
1744 void setLauncher(Launcher launcher) {
1745 mLauncher = new WeakReference<Launcher>(launcher);
1746 }
1747
1748 @Override
1749 public void onReceive(Context context, Intent intent) {
1750 // Load the wallpaper from the ApplicationContext and store it locally
1751 // until the Launcher Activity is ready to use it
1752 final Drawable drawable = mApplication.getWallpaper();
1753 if (drawable instanceof BitmapDrawable) {
1754 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1755 } else {
1756 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1757 }
1758
1759 // If Launcher is alive, notify we have a new wallpaper
1760 if (mLauncher != null) {
1761 final Launcher launcher = mLauncher.get();
1762 if (launcher != null) {
1763 launcher.loadWallpaper();
1764 }
1765 }
1766 }
1767 }
1768
1769 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
1770 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
1771 private boolean mOpen;
1772
1773 public void onDrawerOpened() {
1774 if (!mOpen) {
1775 mHandleIcon.reverseTransition(150);
1776 mOpen = true;
1777 }
1778 }
1779
1780 public void onDrawerClosed() {
1781 if (mOpen) {
1782 mHandleIcon.reverseTransition(150);
1783 mOpen = false;
1784 }
1785 mAllAppsGrid.setSelection(0);
1786 mAllAppsGrid.clearTextFilter();
1787 mWorkspace.clearChildrenCache();
1788 }
1789
1790 public void onScrollStarted() {
1791 mWorkspace.enableChildrenCache();
1792 }
1793
1794 public void onScrollEnded() {
1795 }
1796 }
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001797
1798 private static class DesktopItemsBinder extends Handler {
1799 static final int MESSAGE_BIND_ITEMS = 0x1;
1800 // Number of items to bind in every pass
1801 static final int ITEMS_COUNT = 6;
1802
1803 private final ArrayList<ItemInfo> mShortcuts;
1804 private final WeakReference<Launcher> mLauncher;
1805
1806 DesktopItemsBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts) {
1807 mLauncher = new WeakReference<Launcher>(launcher);
1808 mShortcuts = shortcuts;
1809 }
1810
1811 @Override
1812 public void handleMessage(Message msg) {
1813 switch (msg.what) {
1814 case MESSAGE_BIND_ITEMS:
1815 Launcher launcher = mLauncher.get();
1816 if (launcher != null) launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
1817 break;
1818 }
1819 }
1820 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001821}