blob: 4252daecf67fd8a600a4969793aa17fdfd624298 [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 }
358
359 @Override
360 public boolean onKeyDown(int keyCode, KeyEvent event) {
361 boolean handled = super.onKeyDown(keyCode, event);
362 if (!handled && keyCode != KeyEvent.KEYCODE_ENTER) {
363 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
364 keyCode, event);
365 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
366 // something usable has been typed - dispatch it now.
367 final String str = mDefaultKeySsb.toString();
368
369 boolean isDialable = true;
370 final int count = str.length();
371 for (int i = 0; i < count; i++) {
372 if (!PhoneNumberUtils.isReallyDialable(str.charAt(i))) {
373 isDialable = false;
374 break;
375 }
376 }
377 Intent intent;
378 if (isDialable) {
379 intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", str, null));
380 } else {
381 intent = new Intent(Contacts.Intents.UI.FILTER_CONTACTS_ACTION);
382 intent.putExtra(Contacts.Intents.UI.FILTER_TEXT_EXTRA_KEY, str);
383 }
384
385 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
386
387 try {
388 startActivity(intent);
389 } catch (android.content.ActivityNotFoundException ex) {
390 // Oh well... no one knows how to filter/dial. Life goes on.
391 }
392
393 mDefaultKeySsb.clear();
394 mDefaultKeySsb.clearSpans();
395 Selection.setSelection(mDefaultKeySsb, 0);
396
397 return true;
398 }
399 }
400
401 return handled;
402 }
403
404 /**
405 * Restores the previous state, if it exists.
406 *
407 * @param savedState The previous state.
408 */
409 private void restoreState(Bundle savedState) {
410 if (savedState == null) {
411 return;
412 }
413
414 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
415 if (currentScreen > -1) {
416 mWorkspace.setCurrentScreen(currentScreen);
417 }
418
419 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
420 if (addScreen > -1) {
421 mAddItemCellInfo = new CellLayout.CellInfo();
422 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
423 addItemCellInfo.valid = true;
424 addItemCellInfo.screen = addScreen;
425 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
426 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
427 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
428 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
429 addItemCellInfo.findVacantCellsFromOccupied(
430 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
431 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
432 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
433 mRestoring = true;
434 }
435
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700436 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
437 if (renameFolder) {
438 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
439 mFolderInfo = sModel.getFolderById(this, id);
440 mRestoring = true;
441 }
442 }
443
444 /**
445 * Finds all the views we need and configure them properly.
446 */
447 private void setupViews() {
448 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
449 final DragLayer dragLayer = mDragLayer;
450
451 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
452 final Workspace workspace = mWorkspace;
453
454 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
455 final SlidingDrawer drawer = mDrawer;
456
457 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
458 final AllAppsGridView grid = mAllAppsGrid;
459
460 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
461
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800462 final HandleView handleIcon = (HandleView) drawer.findViewById(R.id.all_apps);
463 handleIcon.setLauncher(this);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700464 mHandleIcon = (TransitionDrawable) handleIcon.getDrawable();
465 mHandleIcon.setCrossFadeEnabled(true);
466
467 drawer.lock();
468 final DrawerManager drawerManager = new DrawerManager();
469 drawer.setOnDrawerOpenListener(drawerManager);
470 drawer.setOnDrawerCloseListener(drawerManager);
471 drawer.setOnDrawerScrollListener(drawerManager);
472
473 grid.setTextFilterEnabled(true);
474 grid.setDragger(dragLayer);
475 grid.setLauncher(this);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700476
477 workspace.setOnLongClickListener(this);
478 workspace.setDragger(dragLayer);
479 workspace.setLauncher(this);
480 loadWallpaper();
481
482 deleteZone.setLauncher(this);
483 deleteZone.setDragController(dragLayer);
484 deleteZone.setHandle(handleIcon);
485
486 dragLayer.setIgnoredDropTarget(grid);
487 dragLayer.setDragScoller(workspace);
488 dragLayer.setDragListener(deleteZone);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700489 }
490
491 /**
492 * Creates a view representing a shortcut.
493 *
494 * @param info The data structure describing the shortcut.
495 *
496 * @return A View inflated from R.layout.application.
497 */
498 View createShortcut(ApplicationInfo info) {
499 return createShortcut(R.layout.application,
500 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
501 }
502
503 /**
504 * Creates a view representing a shortcut inflated from the specified resource.
505 *
506 * @param layoutResId The id of the XML layout used to create the shortcut.
507 * @param parent The group the shortcut belongs to.
508 * @param info The data structure describing the shortcut.
509 *
510 * @return A View inflated from layoutResId.
511 */
512 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
513 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
514
515 if (!info.filtered) {
516 info.icon = Utilities.createIconThumbnail(info.icon, this);
517 info.filtered = true;
518 }
519
520 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
521 favorite.setText(info.title);
522 favorite.setTag(info);
523 favorite.setOnClickListener(this);
524
525 return favorite;
526 }
527
The Android Open Source Project15a88802009-02-10 15:44:05 -0800528 /**
529 * Add an application shortcut to the workspace.
530 *
531 * @param data The intent describing the application.
532 * @param cellInfo The position on screen where to create the shortcut.
533 */
The Android Open Source Project98baae62009-02-13 12:57:53 -0800534 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
535 boolean insertAtFirst) {
The Android Open Source Project15a88802009-02-10 15:44:05 -0800536 cellInfo.screen = mWorkspace.getCurrentScreen();
The Android Open Source Project98baae62009-02-13 12:57:53 -0800537 if (!findSingleSlot(cellInfo)) return;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700538
The Android Open Source Project15a88802009-02-10 15:44:05 -0800539 // Find details for this application
540 ComponentName component = data.getComponent();
541 PackageManager packageManager = context.getPackageManager();
542 ActivityInfo activityInfo = null;
543 try {
544 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
545 } catch (NameNotFoundException e) {
546 Log.e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
547 }
548
549 if (activityInfo != null) {
550 ApplicationInfo itemInfo = new ApplicationInfo();
551
552 itemInfo.title = activityInfo.loadLabel(packageManager);
553 if (itemInfo.title == null) {
554 itemInfo.title = activityInfo.name;
555 }
556
557 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
558 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
559 itemInfo.icon = activityInfo.loadIcon(packageManager);
560 itemInfo.container = ItemInfo.NO_ID;
561
The Android Open Source Project98baae62009-02-13 12:57:53 -0800562 mWorkspace.addApplicationShortcut(itemInfo, cellInfo, insertAtFirst);
The Android Open Source Project15a88802009-02-10 15:44:05 -0800563 }
564 }
565
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700566 /**
567 * Add a shortcut to the workspace.
568 *
569 * @param data The intent describing the shortcut.
570 * @param cellInfo The position on screen where to create the shortcut.
571 * @param insertAtFirst
572 */
573 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
574 boolean insertAtFirst) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700575 cellInfo.screen = mWorkspace.getCurrentScreen();
The Android Open Source Project98baae62009-02-13 12:57:53 -0800576 if (!findSingleSlot(cellInfo)) return;
577
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700578 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
579
580 if (!mRestoring) {
581 sModel.addDesktopItem(info);
582
583 final View view = createShortcut(info);
584 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
585 } else if (sModel.isDesktopLoaded()) {
586 sModel.addDesktopItem(info);
587 }
588 }
589
The Android Open Source Project15a88802009-02-10 15:44:05 -0800590
591 /**
592 * Add a gadget to the workspace.
593 *
594 * @param data The intent describing the gadgetId.
595 * @param cellInfo The position on screen where to create the shortcut.
596 * @param insertAtFirst
597 */
598 private void completeAddGadget(Intent data, CellLayout.CellInfo cellInfo,
599 boolean insertAtFirst) {
600
601 Bundle extras = data.getExtras();
602 int gadgetId = extras.getInt(GadgetManager.EXTRA_GADGET_ID, -1);
603
604 Log.d(LOG_TAG, "dumping extras content="+extras.toString());
605
The Android Open Source Project98baae62009-02-13 12:57:53 -0800606 GadgetProviderInfo gadgetInfo = mGadgetManager.getGadgetInfo(gadgetId);
The Android Open Source Project15a88802009-02-10 15:44:05 -0800607
608 // Calculate the grid spans needed to fit this gadget
609 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project98baae62009-02-13 12:57:53 -0800610 int[] spans = layout.rectToCell(gadgetInfo.minWidth, gadgetInfo.minHeight);
611
The Android Open Source Project15a88802009-02-10 15:44:05 -0800612 // Try finding open space on Launcher screen
613 final int[] xy = mCellCoordinates;
The Android Open Source Project98baae62009-02-13 12:57:53 -0800614 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
The Android Open Source Project15a88802009-02-10 15:44:05 -0800615
616 // Build Launcher-specific Gadget info and save to database
617 LauncherGadgetInfo launcherInfo = new LauncherGadgetInfo(gadgetId);
The Android Open Source Project98baae62009-02-13 12:57:53 -0800618 launcherInfo.spanX = spans[0];
619 launcherInfo.spanY = spans[1];
The Android Open Source Project15a88802009-02-10 15:44:05 -0800620
621 LauncherModel.addItemToDatabase(this, launcherInfo,
622 LauncherSettings.Favorites.CONTAINER_DESKTOP,
623 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
624
625 if (!mRestoring) {
626 sModel.addDesktopItem(launcherInfo);
627
628 // Perform actual inflation because we're live
629 launcherInfo.hostView = mGadgetHost.createView(this, gadgetId, gadgetInfo);
630
631 launcherInfo.hostView.setGadget(gadgetId, gadgetInfo);
632 launcherInfo.hostView.setTag(launcherInfo);
633
634 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
635 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
636 } else if (sModel.isDesktopLoaded()) {
637 sModel.addDesktopItem(launcherInfo);
638 }
The Android Open Source Project15a88802009-02-10 15:44:05 -0800639 }
640
641 public LauncherGadgetHost getGadgetHost() {
642 return mGadgetHost;
643 }
644
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700645 static ApplicationInfo addShortcut(Context context, Intent data,
646 CellLayout.CellInfo cellInfo, boolean notify) {
647
648 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
649 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
650 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
651
652 Drawable icon = null;
653 boolean filtered = false;
654 boolean customIcon = false;
655 Intent.ShortcutIconResource iconResource = null;
656
657 if (bitmap != null) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800658 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700659 filtered = true;
660 customIcon = true;
661 } else {
662 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
663 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
664 try {
665 iconResource = (Intent.ShortcutIconResource) extra;
666 final PackageManager packageManager = context.getPackageManager();
667 Resources resources = packageManager.getResourcesForApplication(
668 iconResource.packageName);
669 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
670 icon = resources.getDrawable(id);
671 } catch (Exception e) {
672 Log.w(LOG_TAG, "Could not load shortcut icon: " + extra);
673 }
674 }
675 }
676
677 if (icon == null) {
678 icon = context.getPackageManager().getDefaultActivityIcon();
679 }
680
681 final ApplicationInfo info = new ApplicationInfo();
682 info.icon = icon;
683 info.filtered = filtered;
684 info.title = name;
685 info.intent = intent;
686 info.customIcon = customIcon;
687 info.iconResource = iconResource;
688
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800689 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700690 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
691 return info;
692 }
693
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700694 @Override
695 protected void onNewIntent(Intent intent) {
696 super.onNewIntent(intent);
697
698 // Close the menu
699 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
700 getWindow().closeAllPanels();
701
702 try {
703 dismissDialog(DIALOG_CREATE_SHORTCUT);
704 // Unlock the workspace if the dialog was showing
705 mWorkspace.unlock();
706 } catch (Exception e) {
707 // An exception is thrown if the dialog is not visible, which is fine
708 }
709
710 try {
711 dismissDialog(DIALOG_RENAME_FOLDER);
712 // Unlock the workspace if the dialog was showing
713 mWorkspace.unlock();
714 } catch (Exception e) {
715 // An exception is thrown if the dialog is not visible, which is fine
716 }
717
718 // If we are already in front we go back to the default screen,
719 // otherwise we don't
720 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
721 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
722 if (!mWorkspace.isDefaultScreenShowing()) {
723 mWorkspace.moveToDefaultScreen();
724 }
725 closeDrawer();
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800726 View v = getWindow().peekDecorView();
727 if (v != null && v.getWindowToken() != null) {
728 InputMethodManager imm = (InputMethodManager)getSystemService(
729 INPUT_METHOD_SERVICE);
The Android Open Source Project946cd912009-01-15 16:12:13 -0800730 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800731 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700732 } else {
733 closeDrawer(false);
734 }
735 }
736 }
737
738 @Override
739 protected void onSaveInstanceState(Bundle outState) {
740 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
741
742 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
743 if (folders.size() > 0) {
744 final int count = folders.size();
745 long[] ids = new long[count];
746 for (int i = 0; i < count; i++) {
747 final FolderInfo info = folders.get(i).getInfo();
748 ids[i] = info.id;
749 }
750 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
751 } else {
752 super.onSaveInstanceState(outState);
753 }
754
755 if (mDrawer.isOpened()) {
756 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
757 }
758
759 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
760 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
761 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
762
763 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
764 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
765 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
766 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
767 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
768 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
769 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
770 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
771 layout.getOccupiedCells());
772 }
773
774 if (mFolderInfo != null && mWaitingForResult) {
775 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
776 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
777 }
778 }
779
780 @Override
781 public void onDestroy() {
782 mDestroyed = true;
783
784 super.onDestroy();
The Android Open Source Project98baae62009-02-13 12:57:53 -0800785
786 mGadgetHost.stopListening();
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700787
788 TextKeyListener.getInstance().release();
789
790 mAllAppsGrid.clearTextFilter();
791 mAllAppsGrid.setAdapter(null);
792 sModel.unbind();
793 sModel.abortLoaders();
794
795 getContentResolver().unregisterContentObserver(mObserver);
796 unregisterReceiver(mApplicationsReceiver);
797 }
798
799 @Override
800 public void startActivityForResult(Intent intent, int requestCode) {
801 mWaitingForResult = true;
802 super.startActivityForResult(intent, requestCode);
803 }
804
805 @Override
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800806 public void startSearch(String initialQuery, boolean selectInitialQuery,
807 Bundle appSearchData, boolean globalSearch) {
808 if (appSearchData == null) {
809 appSearchData = new Bundle();
810 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
811 }
812 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
813 }
814
815 @Override
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700816 public boolean onCreateOptionsMenu(Menu menu) {
817 if (mDesktopLocked) return false;
818
819 super.onCreateOptionsMenu(menu);
820 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
821 .setIcon(android.R.drawable.ic_menu_add)
822 .setAlphabeticShortcut('A');
823 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800824 .setIcon(android.R.drawable.ic_menu_gallery)
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700825 .setAlphabeticShortcut('W');
826 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
827 .setIcon(android.R.drawable.ic_search_category_default)
828 .setAlphabeticShortcut(SearchManager.MENU_KEY);
829 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
The Android Open Source Project62b49bb2009-01-20 14:04:00 -0800830 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700831 .setAlphabeticShortcut('N');
832
833 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
834 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
835 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
836
837 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800838 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700839 .setIntent(settings);
840
841 return true;
842 }
843
844 @Override
845 public boolean onPrepareOptionsMenu(Menu menu) {
846 super.onPrepareOptionsMenu(menu);
847
848 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
849 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
850
851 return true;
852 }
853
854 @Override
855 public boolean onOptionsItemSelected(MenuItem item) {
856 switch (item.getItemId()) {
857 case MENU_ADD:
858 addItems();
859 return true;
860 case MENU_WALLPAPER_SETTINGS:
861 startWallpaper();
862 return true;
863 case MENU_SEARCH:
864 if (!mWorkspace.snapToSearch()) {
865 onSearchRequested();
866 }
867 return true;
868 case MENU_NOTIFICATIONS:
869 showNotifications();
870 return true;
871 }
872
873 return super.onOptionsItemSelected(item);
874 }
875
876 private void addItems() {
877 showAddDialog(mMenuAddInfo);
878 }
879
880 private void removeShortcutsForPackage(String packageName) {
881 if (packageName != null && packageName.length() > 0) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700882 mWorkspace.removeShortcutsForPackage(packageName);
883 }
884 }
The Android Open Source Project15a88802009-02-10 15:44:05 -0800885
The Android Open Source Project15a88802009-02-10 15:44:05 -0800886 void addGadget(Intent data) {
The Android Open Source Project98baae62009-02-13 12:57:53 -0800887 // TODO: catch bad gadget exception when sent
The Android Open Source Project15a88802009-02-10 15:44:05 -0800888 int gadgetId = data.getIntExtra(GadgetManager.EXTRA_GADGET_ID, -1);
The Android Open Source Project98baae62009-02-13 12:57:53 -0800889 GadgetProviderInfo gadget = mGadgetManager.getGadgetInfo(gadgetId);
The Android Open Source Project15a88802009-02-10 15:44:05 -0800890
891 if (gadget.configure != null) {
892 // Launch over to configure gadget, if needed
The Android Open Source Project98baae62009-02-13 12:57:53 -0800893 Intent intent = new Intent(GadgetManager.ACTION_GADGET_CONFIGURE);
The Android Open Source Project15a88802009-02-10 15:44:05 -0800894 intent.setComponent(gadget.configure);
895 intent.putExtra(GadgetManager.EXTRA_GADGET_ID, gadgetId);
896
897 startActivityForResult(intent, REQUEST_CREATE_GADGET);
898 } else {
899 // Otherwise just add it
The Android Open Source Project15a88802009-02-10 15:44:05 -0800900 onActivityResult(REQUEST_CREATE_GADGET, Activity.RESULT_OK, data);
901 }
902 }
903
904 void addSearch() {
905 final Widget info = Widget.makeSearch();
906 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
907
908 final int[] xy = mCellCoordinates;
909 final int spanX = info.spanX;
910 final int spanY = info.spanY;
911
912 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
913
914 sModel.addDesktopItem(info);
915 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
916 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
917
918 final View view = mInflater.inflate(info.layoutResource, null);
919 view.setTag(info);
920
921 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
922 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700923
924 void addShortcut(Intent intent) {
925 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
926 }
927
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800928 void addLiveFolder(Intent intent) {
929 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
930 }
931
The Android Open Source Project98baae62009-02-13 12:57:53 -0800932 void addFolder(boolean insertAtFirst) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700933 UserFolderInfo folderInfo = new UserFolderInfo();
934 folderInfo.title = getText(R.string.folder_name);
The Android Open Source Project98baae62009-02-13 12:57:53 -0800935
936 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
937 cellInfo.screen = mWorkspace.getCurrentScreen();
938 if (!findSingleSlot(cellInfo)) return;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700939
940 // Update the model
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800941 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project98baae62009-02-13 12:57:53 -0800942 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700943 sModel.addDesktopItem(folderInfo);
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800944 sModel.addFolder(folderInfo);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700945
946 // Create the view
947 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
948 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
The Android Open Source Project98baae62009-02-13 12:57:53 -0800949 mWorkspace.addInCurrentScreen(newFolder,
950 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700951 }
The Android Open Source Project98baae62009-02-13 12:57:53 -0800952
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800953 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
954 boolean insertAtFirst) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800955 cellInfo.screen = mWorkspace.getCurrentScreen();
The Android Open Source Project98baae62009-02-13 12:57:53 -0800956 if (!findSingleSlot(cellInfo)) return;
957
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800958 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
959
960 if (!mRestoring) {
961 sModel.addDesktopItem(info);
962
963 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
964 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
965 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
966 } else if (sModel.isDesktopLoaded()) {
967 sModel.addDesktopItem(info);
968 }
969 }
970
971 static LiveFolderInfo addLiveFolder(Context context, Intent data,
972 CellLayout.CellInfo cellInfo, boolean notify) {
973
974 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
975 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
976
977 Drawable icon = null;
978 boolean filtered = false;
979 Intent.ShortcutIconResource iconResource = null;
980
981 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
982 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
983 try {
984 iconResource = (Intent.ShortcutIconResource) extra;
985 final PackageManager packageManager = context.getPackageManager();
986 Resources resources = packageManager.getResourcesForApplication(
987 iconResource.packageName);
988 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
989 icon = resources.getDrawable(id);
990 } catch (Exception e) {
991 Log.w(LOG_TAG, "Could not load live folder icon: " + extra);
992 }
993 }
994
995 if (icon == null) {
996 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
997 }
998
999 final LiveFolderInfo info = new LiveFolderInfo();
1000 info.icon = icon;
1001 info.filtered = filtered;
1002 info.title = name;
1003 info.iconResource = iconResource;
1004 info.uri = data.getData();
1005 info.baseIntent = baseIntent;
1006 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1007 LiveFolders.DISPLAY_MODE_GRID);
1008
1009 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1010 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1011 sModel.addFolder(info);
1012
1013 return info;
1014 }
1015
The Android Open Source Project98baae62009-02-13 12:57:53 -08001016 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1017 final int[] xy = new int[2];
1018 if (findSlot(cellInfo, xy, 1, 1)) {
1019 cellInfo.cellX = xy[0];
1020 cellInfo.cellY = xy[1];
1021 return true;
1022 }
1023 return false;
1024 }
1025
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001026 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1027 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1028 boolean[] occupied = mSavedState != null ?
1029 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1030 cellInfo = mWorkspace.findAllVacantCells(occupied);
1031 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1032 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1033 return false;
1034 }
1035 }
1036 return true;
1037 }
1038
1039 private void showNotifications() {
1040 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1041 if (statusBar != null) {
1042 statusBar.expand();
1043 }
1044 }
1045
1046 private void startWallpaper() {
1047 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1048 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1049 }
1050
1051 /**
1052 * Registers various intent receivers. The current implementation registers
1053 * only a wallpaper intent receiver to let other applications change the
1054 * wallpaper.
1055 */
1056 private void registerIntentReceivers() {
1057 if (sWallpaperReceiver == null) {
1058 final Application application = getApplication();
1059
1060 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1061
1062 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1063 application.registerReceiver(sWallpaperReceiver, filter);
1064 } else {
1065 sWallpaperReceiver.setLauncher(this);
1066 }
1067
1068 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1069 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1070 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1071 filter.addDataScheme("package");
1072 registerReceiver(mApplicationsReceiver, filter);
1073 }
1074
1075 /**
1076 * Registers various content observers. The current implementation registers
1077 * only a favorites observer to keep track of the favorites applications.
1078 */
1079 private void registerContentObservers() {
1080 ContentResolver resolver = getContentResolver();
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001081 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001082 }
1083
1084 @Override
1085 public boolean dispatchKeyEvent(KeyEvent event) {
1086 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1087 switch (event.getKeyCode()) {
1088 case KeyEvent.KEYCODE_BACK:
1089 mWorkspace.dispatchKeyEvent(event);
1090 closeFolder();
1091 closeDrawer();
1092 return true;
1093 case KeyEvent.KEYCODE_HOME:
1094 return true;
1095 }
1096 }
1097
1098 return super.dispatchKeyEvent(event);
1099 }
1100
1101 private void closeDrawer() {
1102 closeDrawer(true);
1103 }
1104
1105 private void closeDrawer(boolean animated) {
1106 if (mDrawer.isOpened()) {
1107 if (animated) {
1108 mDrawer.animateClose();
1109 } else {
1110 mDrawer.close();
1111 }
1112 if (mDrawer.hasFocus()) {
1113 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1114 }
1115 }
1116 }
1117
1118 private void closeFolder() {
1119 Folder folder = mWorkspace.getOpenFolder();
1120 if (folder != null) {
1121 closeFolder(folder);
1122 }
1123 }
1124
1125 void closeFolder(Folder folder) {
1126 folder.getInfo().opened = false;
1127 ViewGroup parent = (ViewGroup) folder.getParent();
1128 if (parent != null) {
1129 parent.removeView(folder);
1130 }
1131 folder.onClose();
1132 }
1133
1134 /**
1135 * When the notification that favorites have changed is received, requests
1136 * a favorites list refresh.
1137 */
1138 private void onFavoritesChanged() {
1139 mDesktopLocked = true;
1140 mDrawer.lock();
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001141 sModel.loadUserItems(false, this, false, false);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001142 }
1143
1144 void onDesktopItemsLoaded() {
1145 if (mDestroyed) return;
1146
1147 bindDesktopItems();
1148 mAllAppsGrid.setAdapter(Launcher.getModel().getApplicationsAdapter());
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001149 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001150
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001151 /**
1152 * Refreshes the shortcuts shown on the workspace.
1153 */
1154 private void bindDesktopItems() {
1155 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
1156 if (shortcuts == null) {
1157 return;
1158 }
1159
1160 final Workspace workspace = mWorkspace;
1161 int count = workspace.getChildCount();
1162 for (int i = 0; i < count; i++) {
1163 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1164 }
The Android Open Source Project15a88802009-02-10 15:44:05 -08001165
1166 if (DEBUG_USER_INTERFACE) {
1167 android.widget.Button finishButton = new android.widget.Button(this);
1168 finishButton.setText("Finish");
1169 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1170
1171 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1172 public void onClick(View v) {
1173 finish();
1174 }
1175 });
1176 }
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001177
1178 count = shortcuts.size();
The Android Open Source Project98baae62009-02-13 12:57:53 -08001179
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001180 final DesktopItemsBinder binder = new DesktopItemsBinder(this, shortcuts);
1181 binder.obtainMessage(DesktopItemsBinder.MESSAGE_BIND_ITEMS, 0, count).sendToTarget();
1182 }
1183
1184 private void bindItems(Launcher.DesktopItemsBinder binder,
1185 ArrayList<ItemInfo> shortcuts, int start, int count) {
1186
1187 final Workspace workspace = mWorkspace;
1188 final boolean desktopLocked = mDesktopLocked;
1189
1190 final int end = Math.min(start + DesktopItemsBinder.ITEMS_COUNT, count);
1191 int i = start;
1192
1193 for ( ; i < end; i++) {
1194 final ItemInfo item = shortcuts.get(i);
1195 switch (item.itemType) {
1196 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1197 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1198 final View shortcut = createShortcut((ApplicationInfo) item);
1199 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1200 !desktopLocked);
1201 break;
1202 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1203 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1204 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1205 (UserFolderInfo) item);
1206 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1207 !desktopLocked);
1208 break;
1209 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1210 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1211 R.layout.live_folder_icon, this,
1212 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1213 (LiveFolderInfo) item);
1214 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1215 !desktopLocked);
1216 break;
The Android Open Source Project15a88802009-02-10 15:44:05 -08001217 case LauncherSettings.Favorites.ITEM_TYPE_GADGET:
1218 final LauncherGadgetInfo launcherInfo = (LauncherGadgetInfo) item;
1219
1220 final int gadgetId = launcherInfo.gadgetId;
The Android Open Source Project98baae62009-02-13 12:57:53 -08001221 GadgetProviderInfo gadgetInfo = mGadgetManager.getGadgetInfo(gadgetId);
The Android Open Source Project15a88802009-02-10 15:44:05 -08001222 launcherInfo.hostView = mGadgetHost.createView(this, gadgetId, gadgetInfo);
The Android Open Source Project98baae62009-02-13 12:57:53 -08001223
1224 Log.d(LOG_TAG, "about to setGadget for id="+gadgetId+", info="+gadgetInfo);
The Android Open Source Project15a88802009-02-10 15:44:05 -08001225 launcherInfo.hostView.setGadget(gadgetId, gadgetInfo);
1226 launcherInfo.hostView.setTag(launcherInfo);
1227
1228 workspace.addInScreen(launcherInfo.hostView, item.screen, item.cellX,
1229 item.cellY, item.spanX, item.spanY, !desktopLocked);
1230
The Android Open Source Project15a88802009-02-10 15:44:05 -08001231 break;
1232 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1233 final int screen = workspace.getCurrentScreen();
1234 final View view = mInflater.inflate(R.layout.widget_search,
1235 (ViewGroup) workspace.getChildAt(screen), false);
1236
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001237 final Widget widget = (Widget) item;
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001238 view.setTag(widget);
The Android Open Source Project15a88802009-02-10 15:44:05 -08001239
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001240 workspace.addWidget(view, widget, !desktopLocked);
The Android Open Source Project15a88802009-02-10 15:44:05 -08001241 break;
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001242 }
1243 }
1244
1245 workspace.requestLayout();
1246
1247 if (end >= count) {
1248 finishBindDesktopItems();
1249 } else {
1250 binder.obtainMessage(DesktopItemsBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1251 }
1252 }
1253
1254 private void finishBindDesktopItems() {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001255 if (mSavedState != null) {
1256 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1257
1258 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1259 if (userFolders != null) {
1260 for (long folderId : userFolders) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001261 final FolderInfo info = sModel.findFolderById(folderId);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001262 if (info != null) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001263 openFolder(info);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001264 }
1265 }
1266 final Folder openFolder = mWorkspace.getOpenFolder();
1267 if (openFolder != null) {
1268 openFolder.requestFocus();
1269 } else {
1270 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1271 }
1272 }
1273
1274 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1275 if (allApps) {
1276 mDrawer.open();
1277 mDrawer.requestFocus();
1278 }
1279
1280 mSavedState = null;
1281 }
1282
1283 mDesktopLocked = false;
1284 mDrawer.unlock();
1285 }
1286
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001287 DragController getDragController() {
1288 return mDragLayer;
1289 }
1290
1291 /**
1292 * Launches the intent referred by the clicked shortcut.
1293 *
1294 * @param v The view representing the clicked shortcut.
1295 */
1296 public void onClick(View v) {
1297 Object tag = v.getTag();
1298 if (tag instanceof ApplicationInfo) {
1299 // Open shortcut
1300 final Intent intent = ((ApplicationInfo) tag).intent;
1301 startActivitySafely(intent);
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001302 } else if (tag instanceof FolderInfo) {
1303 handleFolderClick((FolderInfo) tag);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001304 }
1305 }
1306
1307 void startActivitySafely(Intent intent) {
1308 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1309 try {
1310 startActivity(intent);
1311 } catch (ActivityNotFoundException e) {
1312 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
The Android Open Source Project15a88802009-02-10 15:44:05 -08001313 } catch (SecurityException e) {
1314 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1315 Log.e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
1316 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1317 "or use the exported attribute for this activity.", e);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001318 }
1319 }
1320
1321 private void handleFolderClick(FolderInfo folderInfo) {
1322 if (!folderInfo.opened) {
1323 // Close any open folder
1324 closeFolder();
1325 // Open the requested folder
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001326 openFolder(folderInfo);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001327 } else {
1328 // Find the open folder...
1329 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1330 int folderScreen;
1331 if (openFolder != null) {
1332 folderScreen = mWorkspace.getScreenForView(openFolder);
1333 // .. and close it
1334 closeFolder(openFolder);
1335 if (folderScreen != mWorkspace.getCurrentScreen()) {
1336 // Close any folder open on the current screen
1337 closeFolder();
1338 // Pull the folder onto this screen
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001339 openFolder(folderInfo);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001340 }
1341 }
1342 }
1343 }
1344
1345 private void loadWallpaper() {
1346 // The first time the application is started, we load the wallpaper from
1347 // the ApplicationContext
1348 if (sWallpaper == null) {
1349 final Drawable drawable = getWallpaper();
1350 if (drawable instanceof BitmapDrawable) {
1351 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1352 } else {
1353 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1354 }
1355 }
1356 mWorkspace.loadWallpaper(sWallpaper);
1357 }
1358
1359 /**
1360 * Opens the user fodler described by the specified tag. The opening of the folder
1361 * is animated relative to the specified View. If the View is null, no animation
1362 * is played.
1363 *
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001364 * @param folderInfo The FolderInfo describing the folder to open.
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001365 */
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001366 private void openFolder(FolderInfo folderInfo) {
1367 Folder openFolder;
1368
1369 if (folderInfo instanceof UserFolderInfo) {
1370 openFolder = UserFolder.fromXml(this);
1371 } else if (folderInfo instanceof LiveFolderInfo) {
1372 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1373 } else {
1374 return;
1375 }
1376
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001377 openFolder.setDragger(mDragLayer);
1378 openFolder.setLauncher(this);
1379
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001380 openFolder.bind(folderInfo);
1381 folderInfo.opened = true;
1382
1383 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1384 openFolder.onOpen();
1385 }
1386
1387 /**
1388 * Returns true if the workspace is being loaded. When the workspace is loading,
1389 * no user interaction should be allowed to avoid any conflict.
1390 *
1391 * @return True if the workspace is locked, false otherwise.
1392 */
1393 boolean isWorkspaceLocked() {
1394 return mDesktopLocked;
1395 }
1396
1397 public boolean onLongClick(View v) {
1398 if (mDesktopLocked) {
1399 return false;
1400 }
1401
1402 if (!(v instanceof CellLayout)) {
1403 v = (View) v.getParent();
1404 }
1405
1406 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1407
1408 // This happens when long clicking an item with the dpad/trackball
1409 if (cellInfo == null) {
1410 return false;
1411 }
1412
1413 if (mWorkspace.allowLongPress()) {
1414 if (cellInfo.cell == null) {
1415 if (cellInfo.valid) {
1416 // User long pressed on empty space
1417 showAddDialog(cellInfo);
1418 }
1419 } else {
1420 if (!(cellInfo.cell instanceof Folder)) {
1421 // User long pressed on an item
1422 mWorkspace.startDrag(cellInfo);
1423 }
1424 }
1425 }
1426 return true;
1427 }
1428
1429 static LauncherModel getModel() {
1430 return sModel;
1431 }
1432
1433 void closeAllApplications() {
1434 mDrawer.close();
1435 }
1436
1437 boolean isDrawerDown() {
1438 return !mDrawer.isMoving() && !mDrawer.isOpened();
1439 }
1440
1441 Workspace getWorkspace() {
1442 return mWorkspace;
1443 }
1444
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001445 GridView getApplicationsGrid() {
1446 return mAllAppsGrid;
1447 }
1448
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001449 @Override
1450 protected Dialog onCreateDialog(int id) {
1451 switch (id) {
1452 case DIALOG_CREATE_SHORTCUT:
1453 return new CreateShortcut().createDialog();
1454 case DIALOG_RENAME_FOLDER:
1455 return new RenameFolder().createDialog();
1456 }
1457
1458 return super.onCreateDialog(id);
1459 }
1460
1461 @Override
1462 protected void onPrepareDialog(int id, Dialog dialog) {
1463 switch (id) {
1464 case DIALOG_CREATE_SHORTCUT:
1465 mWorkspace.lock();
1466 break;
1467 case DIALOG_RENAME_FOLDER:
1468 mWorkspace.lock();
1469 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1470 final CharSequence text = mFolderInfo.title;
1471 input.setText(text);
1472 input.setSelection(0, text.length());
1473 break;
1474 }
1475 }
1476
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001477 void showRenameDialog(FolderInfo info) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001478 mFolderInfo = info;
1479 mWaitingForResult = true;
1480 showDialog(DIALOG_RENAME_FOLDER);
1481 }
1482
1483 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1484 mAddItemCellInfo = cellInfo;
1485 mWaitingForResult = true;
1486 showDialog(DIALOG_CREATE_SHORTCUT);
1487 }
1488
1489 private class RenameFolder {
1490 private EditText mInput;
1491
1492 Dialog createDialog() {
1493 mWaitingForResult = true;
1494 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1495 mInput = (EditText) layout.findViewById(R.id.folder_name);
1496
1497 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1498 builder.setIcon(0);
1499 builder.setTitle(getString(R.string.rename_folder_title));
1500 builder.setCancelable(true);
1501 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1502 public void onCancel(DialogInterface dialog) {
1503 cleanup();
1504 }
1505 });
1506 builder.setNegativeButton(getString(R.string.cancel_action),
1507 new Dialog.OnClickListener() {
1508 public void onClick(DialogInterface dialog, int which) {
1509 cleanup();
1510 }
1511 }
1512 );
1513 builder.setPositiveButton(getString(R.string.rename_action),
1514 new Dialog.OnClickListener() {
1515 public void onClick(DialogInterface dialog, int which) {
1516 changeFolderName();
1517 }
1518 }
1519 );
1520 builder.setView(layout);
1521 return builder.create();
1522 }
1523
1524 private void changeFolderName() {
1525 final String name = mInput.getText().toString();
1526 if (!TextUtils.isEmpty(name)) {
1527 // Make sure we have the right folder info
1528 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1529 mFolderInfo.title = name;
1530 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1531
1532 if (mDesktopLocked) {
1533 mDrawer.lock();
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001534 sModel.loadUserItems(false, Launcher.this, false, false);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001535 } else {
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001536 final FolderIcon folderIcon = (FolderIcon)
1537 mWorkspace.getViewForTag(mFolderInfo);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001538 if (folderIcon != null) {
1539 folderIcon.setText(name);
1540 getWorkspace().requestLayout();
1541 } else {
1542 mDesktopLocked = true;
1543 mDrawer.lock();
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001544 sModel.loadUserItems(false, Launcher.this, false, false);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001545 }
1546 }
1547 }
1548 cleanup();
1549 }
1550
1551 private void cleanup() {
1552 mWorkspace.unlock();
1553 dismissDialog(DIALOG_RENAME_FOLDER);
1554 mWaitingForResult = false;
1555 mFolderInfo = null;
1556 }
1557 }
1558
1559 /**
1560 * Displays the shortcut creation dialog and launches, if necessary, the
1561 * appropriate activity.
1562 */
The Android Open Source Project15a88802009-02-10 15:44:05 -08001563 private class CreateShortcut implements AdapterView.OnItemClickListener,
1564 DialogInterface.OnCancelListener {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001565 private AddAdapter mAdapter;
The Android Open Source Project15a88802009-02-10 15:44:05 -08001566 private ListView mList;
1567
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001568 Dialog createDialog() {
1569 mWaitingForResult = true;
The Android Open Source Project15a88802009-02-10 15:44:05 -08001570
1571 mAdapter = new AddAdapter(Launcher.this);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001572
1573 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1574 builder.setTitle(getString(R.string.menu_item_add_item));
1575 builder.setIcon(0);
1576
The Android Open Source Project15a88802009-02-10 15:44:05 -08001577 mList = (ListView)
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001578 View.inflate(Launcher.this, R.layout.create_shortcut_list, null);
1579 mList.setAdapter(mAdapter);
The Android Open Source Project15a88802009-02-10 15:44:05 -08001580 mList.setOnItemClickListener(this);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001581 builder.setView(mList);
1582 builder.setInverseBackgroundForced(true);
1583
1584 AlertDialog dialog = builder.create();
1585 dialog.setOnCancelListener(this);
1586
1587 WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
1588 attributes.gravity = Gravity.TOP;
1589 dialog.onWindowAttributesChanged(attributes);
1590
1591 return dialog;
1592 }
1593
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001594 public void onCancel(DialogInterface dialog) {
1595 mWaitingForResult = false;
1596 cleanup();
1597 }
1598
1599 private void cleanup() {
1600 mWorkspace.unlock();
1601 dismissDialog(DIALOG_CREATE_SHORTCUT);
1602 }
1603
The Android Open Source Project15a88802009-02-10 15:44:05 -08001604 public void onItemClick(AdapterView parent, View view, int position, long id) {
1605 // handle which item was clicked based on position
1606 // this will launch off pick intent
1607
1608 Object tag = view.getTag();
1609 if (tag instanceof AddAdapter.ListItem) {
1610 AddAdapter.ListItem item = (AddAdapter.ListItem) tag;
1611 cleanup();
1612 switch (item.actionTag) {
1613 case AddAdapter.ITEM_APPLICATION: {
1614 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1615 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1616
1617 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1618 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
1619 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
1620 break;
1621 }
1622
1623 case AddAdapter.ITEM_SHORTCUT: {
1624 Intent shortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1625
1626 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1627 pickIntent.putExtra(Intent.EXTRA_INTENT, shortcutIntent);
1628 pickIntent.putExtra(Intent.EXTRA_TITLE,
1629 getText(R.string.title_select_shortcut));
1630 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
1631 break;
1632 }
1633
1634 case AddAdapter.ITEM_SEARCH: {
1635 addSearch();
1636 break;
1637 }
1638
1639 case AddAdapter.ITEM_GADGET: {
1640 int gadgetId = Launcher.this.mGadgetHost.allocateGadgetId();
1641
The Android Open Source Project98baae62009-02-13 12:57:53 -08001642 Intent pickIntent = new Intent(GadgetManager.ACTION_GADGET_PICK);
The Android Open Source Project15a88802009-02-10 15:44:05 -08001643 pickIntent.putExtra(GadgetManager.EXTRA_GADGET_ID, gadgetId);
1644 startActivityForResult(pickIntent, REQUEST_PICK_GADGET);
1645 break;
1646 }
1647
1648 case AddAdapter.ITEM_LIVE_FOLDER: {
1649 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1650
1651 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1652 pickIntent.putExtra(Intent.EXTRA_INTENT, liveFolderIntent);
1653 pickIntent.putExtra(Intent.EXTRA_TITLE,
1654 getText(R.string.title_select_live_folder));
1655 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1656 break;
1657 }
1658
1659 case AddAdapter.ITEM_FOLDER: {
The Android Open Source Project98baae62009-02-13 12:57:53 -08001660 addFolder(!mDesktopLocked);
The Android Open Source Project15a88802009-02-10 15:44:05 -08001661 dismissDialog(DIALOG_CREATE_SHORTCUT);
1662 break;
1663 }
1664
1665 case AddAdapter.ITEM_WALLPAPER: {
1666 startWallpaper();
1667 break;
1668 }
1669
1670 }
1671
1672 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001673 }
1674 }
1675
1676 /**
1677 * Receives notifications when applications are added/removed.
1678 */
1679 private class ApplicationsIntentReceiver extends BroadcastReceiver {
1680 @Override
1681 public void onReceive(Context context, Intent intent) {
The Android Open Source Project15a88802009-02-10 15:44:05 -08001682 boolean reloadWorkspace = false;
1683 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
1684 if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
1685 removeShortcutsForPackage(intent.getData().getSchemeSpecificPart());
1686 } else {
1687 reloadWorkspace = true;
1688 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001689 }
1690 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project15a88802009-02-10 15:44:05 -08001691 if (!reloadWorkspace) {
1692 sModel.loadApplications(false, Launcher.this, false);
1693 } else {
1694 sModel.loadUserItems(false, Launcher.this, false, true);
1695 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001696 }
1697 }
1698
1699 /**
1700 * Receives notifications whenever the user favorites have changed.
1701 */
1702 private class FavoritesChangeObserver extends ContentObserver {
1703 public FavoritesChangeObserver() {
The Android Open Source Projectd097a182008-12-17 18:05:58 -08001704 super(new Handler());
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001705 }
1706
1707 @Override
1708 public void onChange(boolean selfChange) {
1709 onFavoritesChanged();
1710 }
1711 }
1712
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001713 /**
1714 * Receives intents from other applications to change the wallpaper.
1715 */
1716 private static class WallpaperIntentReceiver extends BroadcastReceiver {
1717 private final Application mApplication;
1718 private WeakReference<Launcher> mLauncher;
1719
1720 WallpaperIntentReceiver(Application application, Launcher launcher) {
1721 mApplication = application;
1722 setLauncher(launcher);
1723 }
1724
1725 void setLauncher(Launcher launcher) {
1726 mLauncher = new WeakReference<Launcher>(launcher);
1727 }
1728
1729 @Override
1730 public void onReceive(Context context, Intent intent) {
1731 // Load the wallpaper from the ApplicationContext and store it locally
1732 // until the Launcher Activity is ready to use it
1733 final Drawable drawable = mApplication.getWallpaper();
1734 if (drawable instanceof BitmapDrawable) {
1735 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1736 } else {
1737 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1738 }
1739
1740 // If Launcher is alive, notify we have a new wallpaper
1741 if (mLauncher != null) {
1742 final Launcher launcher = mLauncher.get();
1743 if (launcher != null) {
1744 launcher.loadWallpaper();
1745 }
1746 }
1747 }
1748 }
1749
1750 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
1751 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
1752 private boolean mOpen;
1753
1754 public void onDrawerOpened() {
1755 if (!mOpen) {
1756 mHandleIcon.reverseTransition(150);
1757 mOpen = true;
1758 }
1759 }
1760
1761 public void onDrawerClosed() {
1762 if (mOpen) {
1763 mHandleIcon.reverseTransition(150);
1764 mOpen = false;
1765 }
1766 mAllAppsGrid.setSelection(0);
1767 mAllAppsGrid.clearTextFilter();
1768 mWorkspace.clearChildrenCache();
1769 }
1770
1771 public void onScrollStarted() {
1772 mWorkspace.enableChildrenCache();
1773 }
1774
1775 public void onScrollEnded() {
1776 }
1777 }
The Android Open Source Project0e320b22009-01-09 17:51:25 -08001778
1779 private static class DesktopItemsBinder extends Handler {
1780 static final int MESSAGE_BIND_ITEMS = 0x1;
1781 // Number of items to bind in every pass
1782 static final int ITEMS_COUNT = 6;
1783
1784 private final ArrayList<ItemInfo> mShortcuts;
1785 private final WeakReference<Launcher> mLauncher;
1786
1787 DesktopItemsBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts) {
1788 mLauncher = new WeakReference<Launcher>(launcher);
1789 mShortcuts = shortcuts;
1790 }
1791
1792 @Override
1793 public void handleMessage(Message msg) {
1794 switch (msg.what) {
1795 case MESSAGE_BIND_ITEMS:
1796 Launcher launcher = mLauncher.get();
1797 if (launcher != null) launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
1798 break;
1799 }
1800 }
1801 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001802}