blob: 3fd141d5a029e80ceb687697ba1a0bf0193c6094 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Application;
22import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070023import android.app.ISearchManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070024import android.app.IWallpaperService;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.app.SearchManager;
26import android.app.StatusBarManager;
27import android.content.ActivityNotFoundException;
28import android.content.BroadcastReceiver;
29import android.content.ComponentName;
30import android.content.ContentResolver;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070035import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.pm.ActivityInfo;
37import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070040import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.Bitmap;
43import android.graphics.Rect;
44import android.graphics.drawable.BitmapDrawable;
45import android.graphics.drawable.Drawable;
46import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.os.Bundle;
48import android.os.Handler;
49import android.os.IBinder;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070050import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070052import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.os.Parcelable;
54import android.os.RemoteException;
55import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070056import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.text.Selection;
58import android.text.SpannableStringBuilder;
59import android.text.TextUtils;
60import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070061import static android.util.Log.*;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.KeyEvent;
64import android.view.LayoutInflater;
65import android.view.Menu;
66import android.view.MenuItem;
67import android.view.View;
68import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.view.View.OnLongClickListener;
70import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.widget.EditText;
Karl Rosaen138a0412009-04-23 19:00:21 -070072import android.widget.GridView;
Karl Rosaen138a0412009-04-23 19:00:21 -070073import android.widget.SlidingDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.widget.TextView;
75import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070076import android.appwidget.AppWidgetManager;
77import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
79import java.lang.ref.WeakReference;
80import java.util.ArrayList;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070081import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070082import java.io.DataOutputStream;
83import java.io.FileNotFoundException;
84import java.io.IOException;
85import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
87/**
88 * Default launcher application.
89 */
90public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener {
91 static final String LOG_TAG = "Launcher";
92 static final boolean LOGD = false;
93
94 private static final boolean PROFILE_STARTUP = false;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070095 private static final boolean PROFILE_DRAWER = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070096 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 private static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070098
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 private static final int WALLPAPER_SCREENS_SPAN = 2;
100
101 private static final int MENU_GROUP_ADD = 1;
102 private static final int MENU_ADD = Menu.FIRST + 1;
103 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
104 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
105 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700106 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
108 private static final int REQUEST_CREATE_SHORTCUT = 1;
109 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700110 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 private static final int REQUEST_PICK_APPLICATION = 6;
112 private static final int REQUEST_PICK_SHORTCUT = 7;
113 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700114 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
116 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
117
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700118 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
119 static final String SEARCH_WIDGET = "search_widget";
120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 static final int SCREEN_COUNT = 3;
122 static final int DEFAULT_SCREN = 1;
123 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700124 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 private static final int DIALOG_CREATE_SHORTCUT = 1;
Romain Guycbb89e42009-06-08 15:52:54 -0700127 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Romain Guy98d01652009-06-30 16:21:04 -0700129 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
131 // Type: int
132 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
133 // Type: boolean
134 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
135 // Type: long
136 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
149 // Type: int
150 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
151 // Type: int[]
152 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
153 // Type: boolean
154 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
155 // Type: long
156 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
157
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700158 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
160 private static Bitmap sWallpaper;
161
162 private static final Object sLock = new Object();
163 private static int sScreen = DEFAULT_SCREN;
164
165 private static WallpaperIntentReceiver sWallpaperReceiver;
166
167 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
168 private final ContentObserver mObserver = new FavoritesChangeObserver();
169
170 private LayoutInflater mInflater;
171
172 private DragLayer mDragLayer;
173 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700174
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700175 private AppWidgetManager mAppWidgetManager;
176 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700177
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private CellLayout.CellInfo mAddItemCellInfo;
181 private CellLayout.CellInfo mMenuAddInfo;
182 private final int[] mCellCoordinates = new int[2];
183 private FolderInfo mFolderInfo;
184
185 private SlidingDrawer mDrawer;
186 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700187 private HandleView mHandleView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private AllAppsGridView mAllAppsGrid;
189
190 private boolean mDesktopLocked = true;
191 private Bundle mSavedState;
192
193 private SpannableStringBuilder mDefaultKeySsb = null;
194
195 private boolean mDestroyed;
Mike LeBeau736cf282009-07-02 17:46:59 -0700196
197 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
199 private boolean mRestoring;
200 private boolean mWaitingForResult;
201 private boolean mLocaleChanged;
202
203 private Bundle mSavedInstanceState;
204
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700205 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 @Override
208 protected void onCreate(Bundle savedInstanceState) {
209 super.onCreate(savedInstanceState);
210 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700211
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700213
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
215 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 if (PROFILE_STARTUP) {
218 android.os.Debug.startMethodTracing("/sdcard/launcher");
219 }
220
221 checkForLocaleChange();
222 setWallpaperDimension();
223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 setContentView(R.layout.launcher);
225 setupViews();
226
227 registerIntentReceivers();
228 registerContentObservers();
229
230 mSavedState = savedInstanceState;
231 restoreState(mSavedState);
232
233 if (PROFILE_STARTUP) {
234 android.os.Debug.stopMethodTracing();
235 }
236
237 if (!mRestoring) {
238 startLoaders();
239 }
240
241 // For handling default keys
242 mDefaultKeySsb = new SpannableStringBuilder();
243 Selection.setSelection(mDefaultKeySsb, 0);
244 }
Romain Guycbb89e42009-06-08 15:52:54 -0700245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700247 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
248 readConfiguration(this, localeConfiguration);
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 final Configuration configuration = getResources().getConfiguration();
251
Romain Guy98d01652009-06-30 16:21:04 -0700252 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 final String locale = configuration.locale.toString();
254
Romain Guy98d01652009-06-30 16:21:04 -0700255 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 final int mcc = configuration.mcc;
257
Romain Guy98d01652009-06-30 16:21:04 -0700258 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 final int mnc = configuration.mnc;
260
261 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
262
263 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700264 localeConfiguration.locale = locale;
265 localeConfiguration.mcc = mcc;
266 localeConfiguration.mnc = mnc;
267
268 writeConfiguration(this, localeConfiguration);
269 }
270 }
271
272 private static class LocaleConfiguration {
273 public String locale;
274 public int mcc = -1;
275 public int mnc = -1;
276 }
277
278 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
279 DataInputStream in = null;
280 try {
281 in = new DataInputStream(context.openFileInput(PREFERENCES));
282 configuration.locale = in.readUTF();
283 configuration.mcc = in.readInt();
284 configuration.mnc = in.readInt();
285 } catch (FileNotFoundException e) {
286 // Ignore
287 } catch (IOException e) {
288 // Ignore
289 } finally {
290 if (in != null) {
291 try {
292 in.close();
293 } catch (IOException e) {
294 // Ignore
295 }
296 }
297 }
298 }
299
300 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
301 DataOutputStream out = null;
302 try {
303 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
304 out.writeUTF(configuration.locale);
305 out.writeInt(configuration.mcc);
306 out.writeInt(configuration.mnc);
307 out.flush();
308 } catch (FileNotFoundException e) {
309 // Ignore
310 } catch (IOException e) {
311 //noinspection ResultOfMethodCallIgnored
312 context.getFileStreamPath(PREFERENCES).delete();
313 } finally {
314 if (out != null) {
315 try {
316 out.close();
317 } catch (IOException e) {
318 // Ignore
319 }
320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
322 }
323
324 static int getScreen() {
325 synchronized (sLock) {
326 return sScreen;
327 }
328 }
329
330 static void setScreen(int screen) {
331 synchronized (sLock) {
332 sScreen = screen;
333 }
334 }
335
336 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700337 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
338 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 mRestoring = false;
341 }
342
343 private void setWallpaperDimension() {
344 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
345 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
346
347 Display display = getWindowManager().getDefaultDisplay();
348 boolean isPortrait = display.getWidth() < display.getHeight();
349
350 final int width = isPortrait ? display.getWidth() : display.getHeight();
351 final int height = isPortrait ? display.getHeight() : display.getWidth();
352 try {
353 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
354 } catch (RemoteException e) {
355 // System is dead!
356 }
357 }
358
359 @Override
360 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700361 mWaitingForResult = false;
362
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 // The pattern used here is that a user PICKs a specific application,
364 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
367 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700368
Romain Guy94dabf12009-07-21 10:55:43 -0700369 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 switch (requestCode) {
371 case REQUEST_PICK_APPLICATION:
372 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
373 break;
374 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700375 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 break;
377 case REQUEST_CREATE_SHORTCUT:
378 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
379 break;
380 case REQUEST_PICK_LIVE_FOLDER:
381 addLiveFolder(data);
382 break;
383 case REQUEST_CREATE_LIVE_FOLDER:
384 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
385 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700386 case REQUEST_PICK_APPWIDGET:
387 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700389 case REQUEST_CREATE_APPWIDGET:
390 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 break;
392 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700393 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700395 // Clean up the appWidgetId if we canceled
396 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
397 if (appWidgetId != -1) {
398 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 }
400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
402
403 @Override
404 protected void onResume() {
405 super.onResume();
406
407 if (mRestoring) {
408 startLoaders();
409 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700410
411 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
412 // onNewIntent), then close the search dialog if needed, because it probably
413 // came from the user pressing 'home' (rather than, for example, pressing 'back').
414 if (mIsNewIntent) {
415 // Post to a handler so that this happens after the search dialog tries to open
416 // itself again.
417 mWorkspace.post(new Runnable() {
418 public void run() {
419 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
420 ServiceManager.getService(Context.SEARCH_SERVICE));
421 try {
422 searchManagerService.stopSearch();
423 } catch (RemoteException e) {
424 e(LOG_TAG, "error stopping search", e);
425 }
426 }
427 });
428 }
429
430 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
432
433 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700434 protected void onPause() {
435 super.onPause();
Romain Guycbb89e42009-06-08 15:52:54 -0700436 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700437 }
Romain Guycbb89e42009-06-08 15:52:54 -0700438
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700439 @Override
440 public Object onRetainNonConfigurationInstance() {
441 // Flag any binder to stop early before switching
442 if (mBinder != null) {
443 mBinder.mTerminate = true;
444 }
Romain Guycbb89e42009-06-08 15:52:54 -0700445
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700446 if (PROFILE_ROTATE) {
447 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
448 }
449 return null;
450 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 private boolean acceptFilter() {
453 final InputMethodManager inputManager = (InputMethodManager)
454 getSystemService(Context.INPUT_METHOD_SERVICE);
455 return !inputManager.isFullscreenMode();
456 }
457
458 @Override
459 public boolean onKeyDown(int keyCode, KeyEvent event) {
460 boolean handled = super.onKeyDown(keyCode, event);
461 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
462 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
463 keyCode, event);
464 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700465 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700466 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700467 // showSearchDialog()
468 // If there are multiple keystrokes before the search dialog takes focus,
469 // onSearchRequested() will be called for every keystroke,
470 // but it is idempotent, so it's fine.
471 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
473 }
474
475 return handled;
476 }
477
Karl Rosaen138a0412009-04-23 19:00:21 -0700478 private String getTypedText() {
479 return mDefaultKeySsb.toString();
480 }
481
482 private void clearTypedText() {
483 mDefaultKeySsb.clear();
484 mDefaultKeySsb.clearSpans();
485 Selection.setSelection(mDefaultKeySsb, 0);
486 }
487
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 /**
489 * Restores the previous state, if it exists.
490 *
491 * @param savedState The previous state.
492 */
493 private void restoreState(Bundle savedState) {
494 if (savedState == null) {
495 return;
496 }
497
498 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
499 if (currentScreen > -1) {
500 mWorkspace.setCurrentScreen(currentScreen);
501 }
502
503 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
504 if (addScreen > -1) {
505 mAddItemCellInfo = new CellLayout.CellInfo();
506 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
507 addItemCellInfo.valid = true;
508 addItemCellInfo.screen = addScreen;
509 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
510 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
511 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
512 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
513 addItemCellInfo.findVacantCellsFromOccupied(
514 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
515 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
516 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
517 mRestoring = true;
518 }
519
520 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
521 if (renameFolder) {
522 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
523 mFolderInfo = sModel.getFolderById(this, id);
524 mRestoring = true;
525 }
526 }
527
528 /**
529 * Finds all the views we need and configure them properly.
530 */
531 private void setupViews() {
532 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
533 final DragLayer dragLayer = mDragLayer;
534
535 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
536 final Workspace workspace = mWorkspace;
537
538 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
539 final SlidingDrawer drawer = mDrawer;
540
541 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
542 final AllAppsGridView grid = mAllAppsGrid;
543
544 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
545
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700546 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
547 mHandleView.setLauncher(this);
548 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 mHandleIcon.setCrossFadeEnabled(true);
550
551 drawer.lock();
552 final DrawerManager drawerManager = new DrawerManager();
553 drawer.setOnDrawerOpenListener(drawerManager);
554 drawer.setOnDrawerCloseListener(drawerManager);
555 drawer.setOnDrawerScrollListener(drawerManager);
556
Karl Rosaen138a0412009-04-23 19:00:21 -0700557 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 grid.setDragger(dragLayer);
559 grid.setLauncher(this);
560
561 workspace.setOnLongClickListener(this);
562 workspace.setDragger(dragLayer);
563 workspace.setLauncher(this);
564 loadWallpaper();
565
566 deleteZone.setLauncher(this);
567 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700568 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569
570 dragLayer.setIgnoredDropTarget(grid);
571 dragLayer.setDragScoller(workspace);
572 dragLayer.setDragListener(deleteZone);
573 }
574
575 /**
576 * Creates a view representing a shortcut.
577 *
578 * @param info The data structure describing the shortcut.
579 *
580 * @return A View inflated from R.layout.application.
581 */
582 View createShortcut(ApplicationInfo info) {
583 return createShortcut(R.layout.application,
584 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
585 }
586
587 /**
588 * Creates a view representing a shortcut inflated from the specified resource.
589 *
590 * @param layoutResId The id of the XML layout used to create the shortcut.
591 * @param parent The group the shortcut belongs to.
592 * @param info The data structure describing the shortcut.
593 *
594 * @return A View inflated from layoutResId.
595 */
596 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
597 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
598
599 if (!info.filtered) {
600 info.icon = Utilities.createIconThumbnail(info.icon, this);
601 info.filtered = true;
602 }
603
604 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
605 favorite.setText(info.title);
606 favorite.setTag(info);
607 favorite.setOnClickListener(this);
608
609 return favorite;
610 }
611
612 /**
613 * Add an application shortcut to the workspace.
614 *
615 * @param data The intent describing the application.
616 * @param cellInfo The position on screen where to create the shortcut.
617 */
618 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
619 boolean insertAtFirst) {
620 cellInfo.screen = mWorkspace.getCurrentScreen();
621 if (!findSingleSlot(cellInfo)) return;
622
Romain Guy73b979d2009-06-09 12:57:21 -0700623 final ApplicationInfo info = infoFromApplicationIntent(context, data);
624 if (info != null) {
625 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
626 }
627 }
628
629 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 ComponentName component = data.getComponent();
631 PackageManager packageManager = context.getPackageManager();
632 ActivityInfo activityInfo = null;
633 try {
634 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
635 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700636 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
Romain Guycbb89e42009-06-08 15:52:54 -0700638
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 if (activityInfo != null) {
640 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 itemInfo.title = activityInfo.loadLabel(packageManager);
643 if (itemInfo.title == null) {
644 itemInfo.title = activityInfo.name;
645 }
Romain Guycbb89e42009-06-08 15:52:54 -0700646
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
648 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
649 itemInfo.icon = activityInfo.loadIcon(packageManager);
650 itemInfo.container = ItemInfo.NO_ID;
651
Romain Guy73b979d2009-06-09 12:57:21 -0700652 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 }
Romain Guy73b979d2009-06-09 12:57:21 -0700654
655 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 }
Romain Guycbb89e42009-06-08 15:52:54 -0700657
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 /**
659 * Add a shortcut to the workspace.
660 *
661 * @param data The intent describing the shortcut.
662 * @param cellInfo The position on screen where to create the shortcut.
663 * @param insertAtFirst
664 */
665 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
666 boolean insertAtFirst) {
667 cellInfo.screen = mWorkspace.getCurrentScreen();
668 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
671
672 if (!mRestoring) {
673 sModel.addDesktopItem(info);
674
675 final View view = createShortcut(info);
676 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
677 } else if (sModel.isDesktopLoaded()) {
678 sModel.addDesktopItem(info);
679 }
680 }
681
Romain Guycbb89e42009-06-08 15:52:54 -0700682
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700684 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686 * @param data The intent describing the appWidgetId.
687 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700689 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 boolean insertAtFirst) {
691
692 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700693 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700694
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700695 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700696
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700698
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700699 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700701 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700702
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 // Try finding open space on Launcher screen
704 final int[] xy = mCellCoordinates;
705 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
706
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 // Build Launcher-specific widget info and save to database
708 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 launcherInfo.spanX = spans[0];
710 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 LauncherModel.addItemToDatabase(this, launcherInfo,
713 LauncherSettings.Favorites.CONTAINER_DESKTOP,
714 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
715
716 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700718
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700720 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700721
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700722 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700724
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
726 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
727 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 }
730 }
Romain Guycbb89e42009-06-08 15:52:54 -0700731
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732 public LauncherAppWidgetHost getAppWidgetHost() {
733 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 }
Romain Guycbb89e42009-06-08 15:52:54 -0700735
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 static ApplicationInfo addShortcut(Context context, Intent data,
737 CellLayout.CellInfo cellInfo, boolean notify) {
738
Romain Guy73b979d2009-06-09 12:57:21 -0700739 final ApplicationInfo info = infoFromShortcutIntent(context, data);
740 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
741 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
742
743 return info;
744 }
745
746 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
748 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
749 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
750
751 Drawable icon = null;
752 boolean filtered = false;
753 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700754 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755
756 if (bitmap != null) {
757 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
758 filtered = true;
759 customIcon = true;
760 } else {
761 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700762 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700764 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 final PackageManager packageManager = context.getPackageManager();
766 Resources resources = packageManager.getResourcesForApplication(
767 iconResource.packageName);
768 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
769 icon = resources.getDrawable(id);
770 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700771 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 }
773 }
774 }
775
776 if (icon == null) {
777 icon = context.getPackageManager().getDefaultActivityIcon();
778 }
779
780 final ApplicationInfo info = new ApplicationInfo();
781 info.icon = icon;
782 info.filtered = filtered;
783 info.title = name;
784 info.intent = intent;
785 info.customIcon = customIcon;
786 info.iconResource = iconResource;
787
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 return info;
789 }
790
791 @Override
792 protected void onNewIntent(Intent intent) {
793 super.onNewIntent(intent);
794
795 // Close the menu
796 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
797 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700798
799 // Set this flag so that onResume knows to close the search dialog if it's open,
800 // because this was a new intent (thus a press of 'home' or some such) rather than
801 // for example onResume being called when the user pressed the 'back' button.
802 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803
804 try {
805 dismissDialog(DIALOG_CREATE_SHORTCUT);
806 // Unlock the workspace if the dialog was showing
807 mWorkspace.unlock();
808 } catch (Exception e) {
809 // An exception is thrown if the dialog is not visible, which is fine
810 }
811
812 try {
813 dismissDialog(DIALOG_RENAME_FOLDER);
814 // Unlock the workspace if the dialog was showing
815 mWorkspace.unlock();
816 } catch (Exception e) {
817 // An exception is thrown if the dialog is not visible, which is fine
818 }
819
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
821 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700822
Romain Guy94dabf12009-07-21 10:55:43 -0700823 if (!mWorkspace.isDefaultScreenShowing()) {
824 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700826
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700828
829 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 if (v != null && v.getWindowToken() != null) {
831 InputMethodManager imm = (InputMethodManager)getSystemService(
832 INPUT_METHOD_SERVICE);
833 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
834 }
835 } else {
836 closeDrawer(false);
837 }
838 }
839 }
840
841 @Override
842 protected void onRestoreInstanceState(Bundle savedInstanceState) {
843 // Do not call super here
844 mSavedInstanceState = savedInstanceState;
845 }
846
847 @Override
848 protected void onSaveInstanceState(Bundle outState) {
849 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
850
851 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
852 if (folders.size() > 0) {
853 final int count = folders.size();
854 long[] ids = new long[count];
855 for (int i = 0; i < count; i++) {
856 final FolderInfo info = folders.get(i).getInfo();
857 ids[i] = info.id;
858 }
859 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
860 } else {
861 super.onSaveInstanceState(outState);
862 }
863
Romain Guy3cf604f2009-06-16 13:12:53 -0700864 final boolean isConfigurationChange = getChangingConfigurations() != 0;
865
Romain Guy5a941392009-04-28 15:18:25 -0700866 // When the drawer is opened and we are saving the state because of a
867 // configuration change
Romain Guy3cf604f2009-06-16 13:12:53 -0700868 if (mDrawer.isOpened() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871
872 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
873 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
874 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
875
876 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
877 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
878 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
879 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
880 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
881 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
882 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
883 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
884 layout.getOccupiedCells());
885 }
886
887 if (mFolderInfo != null && mWaitingForResult) {
888 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
889 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
890 }
891 }
892
893 @Override
894 public void onDestroy() {
895 mDestroyed = true;
896
897 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700898
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700900 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700902 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 }
904
905 TextKeyListener.getInstance().release();
906
907 mAllAppsGrid.clearTextFilter();
908 mAllAppsGrid.setAdapter(null);
909 sModel.unbind();
910 sModel.abortLoaders();
911
912 getContentResolver().unregisterContentObserver(mObserver);
913 unregisterReceiver(mApplicationsReceiver);
914 }
915
916 @Override
917 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700918 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 super.startActivityForResult(intent, requestCode);
920 }
921
922 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700923 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700925
926 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700927
Karl Rosaen138a0412009-04-23 19:00:21 -0700928 // Slide the search widget to the top, if it's on the current screen,
929 // otherwise show the search dialog immediately.
930 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
931 if (searchWidget == null) {
932 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
933 } else {
934 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
935 // show the currently typed text in the search widget while sliding
936 searchWidget.setQuery(getTypedText());
937 }
938 }
Romain Guycbb89e42009-06-08 15:52:54 -0700939
Karl Rosaen138a0412009-04-23 19:00:21 -0700940 /**
941 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700942 *
943 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700944 */
Romain Guycbb89e42009-06-08 15:52:54 -0700945 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700946 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700947
Karl Rosaen138a0412009-04-23 19:00:21 -0700948 if (initialQuery == null) {
949 // Use any text typed in the launcher as the initial query
950 initialQuery = getTypedText();
951 clearTypedText();
952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 if (appSearchData == null) {
954 appSearchData = new Bundle();
955 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
956 }
Romain Guycbb89e42009-06-08 15:52:54 -0700957
Karl Rosaen138a0412009-04-23 19:00:21 -0700958 final SearchManager searchManager =
959 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
960
961 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
962 if (searchWidget != null) {
963 // This gets called when the user leaves the search dialog to go back to
964 // the Launcher.
965 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
966 public void onCancel() {
967 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700968 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700969 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 });
971 }
Romain Guycbb89e42009-06-08 15:52:54 -0700972
Karl Rosaen138a0412009-04-23 19:00:21 -0700973 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700974 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
976
Karl Rosaen138a0412009-04-23 19:00:21 -0700977 /**
978 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700979 */
Romain Guy5a941392009-04-28 15:18:25 -0700980 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700981 // Close search dialog
982 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100983 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700984 // Restore search widget to its normal position
985 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
986 if (searchWidget != null) {
987 searchWidget.stopSearch(false);
988 }
989 }
Romain Guycbb89e42009-06-08 15:52:54 -0700990
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 @Override
992 public boolean onCreateOptionsMenu(Menu menu) {
993 if (mDesktopLocked) return false;
994
995 super.onCreateOptionsMenu(menu);
996 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
997 .setIcon(android.R.drawable.ic_menu_add)
998 .setAlphabeticShortcut('A');
999 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1000 .setIcon(android.R.drawable.ic_menu_gallery)
1001 .setAlphabeticShortcut('W');
1002 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1003 .setIcon(android.R.drawable.ic_search_category_default)
1004 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1005 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1006 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1007 .setAlphabeticShortcut('N');
1008
1009 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001010 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1011 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012
1013 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1014 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1015 .setIntent(settings);
1016
1017 return true;
1018 }
1019
1020 @Override
1021 public boolean onPrepareOptionsMenu(Menu menu) {
1022 super.onPrepareOptionsMenu(menu);
1023
1024 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1025 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1026
1027 return true;
1028 }
1029
1030 @Override
1031 public boolean onOptionsItemSelected(MenuItem item) {
1032 switch (item.getItemId()) {
1033 case MENU_ADD:
1034 addItems();
1035 return true;
1036 case MENU_WALLPAPER_SETTINGS:
1037 startWallpaper();
1038 return true;
1039 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001040 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 return true;
1042 case MENU_NOTIFICATIONS:
1043 showNotifications();
1044 return true;
1045 }
1046
1047 return super.onOptionsItemSelected(item);
1048 }
Romain Guycbb89e42009-06-08 15:52:54 -07001049
Karl Rosaen138a0412009-04-23 19:00:21 -07001050 /**
1051 * Indicates that we want global search for this activity by setting the globalSearch
1052 * argument for {@link #startSearch} to true.
1053 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001055 @Override
1056 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001057 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001058 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001059 }
1060
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 private void addItems() {
1062 showAddDialog(mMenuAddInfo);
1063 }
1064
1065 private void removeShortcutsForPackage(String packageName) {
1066 if (packageName != null && packageName.length() > 0) {
1067 mWorkspace.removeShortcutsForPackage(packageName);
1068 }
1069 }
Romain Guycbb89e42009-06-08 15:52:54 -07001070
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001071 private void updateShortcutsForPackage(String packageName) {
1072 if (packageName != null && packageName.length() > 0) {
1073 mWorkspace.updateShortcutsForPackage(packageName);
1074 }
1075 }
1076
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001077 void addAppWidget(Intent data) {
1078 // TODO: catch bad widget exception when sent
1079 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001081 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1082 if (SEARCH_WIDGET.equals(customWidget)) {
1083 // We don't need this any more, since this isn't a real app widget.
1084 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1085 // add the search widget
1086 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001088 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1089
1090 if (appWidget.configure != null) {
1091 // Launch over to configure widget, if needed
1092 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1093 intent.setComponent(appWidget.configure);
1094 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1095
1096 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1097 } else {
1098 // Otherwise just add it
1099 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 }
1102 }
Romain Guycbb89e42009-06-08 15:52:54 -07001103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 void addSearch() {
1105 final Widget info = Widget.makeSearch();
1106 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 final int[] xy = mCellCoordinates;
1109 final int spanX = info.spanX;
1110 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001111
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001113
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 sModel.addDesktopItem(info);
1115 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1116 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001117
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 final View view = mInflater.inflate(info.layoutResource, null);
1119 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001120 Search search = (Search) view.findViewById(R.id.widget_search);
1121 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1124 }
1125
Romain Guy73b979d2009-06-09 12:57:21 -07001126 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001127 // Handle case where user selected "Applications"
1128 String applicationName = getResources().getString(R.string.group_applications);
1129 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001130
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001131 if (applicationName != null && applicationName.equals(shortcutName)) {
1132 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1133 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001134
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001135 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1136 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001137 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001138 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001139 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 }
1142
1143 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001144 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001145 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001146 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001147
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001148 if (folderName != null && folderName.equals(shortcutName)) {
1149 addFolder(!mDesktopLocked);
1150 } else {
1151 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 }
1154
1155 void addFolder(boolean insertAtFirst) {
1156 UserFolderInfo folderInfo = new UserFolderInfo();
1157 folderInfo.title = getText(R.string.folder_name);
1158
1159 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1160 cellInfo.screen = mWorkspace.getCurrentScreen();
1161 if (!findSingleSlot(cellInfo)) return;
1162
1163 // Update the model
1164 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1165 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1166 sModel.addDesktopItem(folderInfo);
1167 sModel.addFolder(folderInfo);
1168
1169 // Create the view
1170 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1171 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1172 mWorkspace.addInCurrentScreen(newFolder,
1173 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1174 }
Romain Guycbb89e42009-06-08 15:52:54 -07001175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1177 boolean insertAtFirst) {
1178 cellInfo.screen = mWorkspace.getCurrentScreen();
1179 if (!findSingleSlot(cellInfo)) return;
1180
1181 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1182
1183 if (!mRestoring) {
1184 sModel.addDesktopItem(info);
1185
1186 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1187 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1188 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1189 } else if (sModel.isDesktopLoaded()) {
1190 sModel.addDesktopItem(info);
1191 }
1192 }
1193
1194 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1195 CellLayout.CellInfo cellInfo, boolean notify) {
1196
1197 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1198 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1199
1200 Drawable icon = null;
1201 boolean filtered = false;
1202 Intent.ShortcutIconResource iconResource = null;
1203
1204 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1205 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1206 try {
1207 iconResource = (Intent.ShortcutIconResource) extra;
1208 final PackageManager packageManager = context.getPackageManager();
1209 Resources resources = packageManager.getResourcesForApplication(
1210 iconResource.packageName);
1211 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1212 icon = resources.getDrawable(id);
1213 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001214 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
1216 }
1217
1218 if (icon == null) {
1219 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1220 }
1221
1222 final LiveFolderInfo info = new LiveFolderInfo();
1223 info.icon = icon;
1224 info.filtered = filtered;
1225 info.title = name;
1226 info.iconResource = iconResource;
1227 info.uri = data.getData();
1228 info.baseIntent = baseIntent;
1229 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1230 LiveFolders.DISPLAY_MODE_GRID);
1231
1232 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1233 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1234 sModel.addFolder(info);
1235
1236 return info;
1237 }
1238
1239 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1240 final int[] xy = new int[2];
1241 if (findSlot(cellInfo, xy, 1, 1)) {
1242 cellInfo.cellX = xy[0];
1243 cellInfo.cellY = xy[1];
1244 return true;
1245 }
1246 return false;
1247 }
1248
1249 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1250 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1251 boolean[] occupied = mSavedState != null ?
1252 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1253 cellInfo = mWorkspace.findAllVacantCells(occupied);
1254 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1255 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1256 return false;
1257 }
1258 }
1259 return true;
1260 }
1261
1262 private void showNotifications() {
1263 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1264 if (statusBar != null) {
1265 statusBar.expand();
1266 }
1267 }
1268
1269 private void startWallpaper() {
1270 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1271 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1272 }
1273
1274 /**
1275 * Registers various intent receivers. The current implementation registers
1276 * only a wallpaper intent receiver to let other applications change the
1277 * wallpaper.
1278 */
1279 private void registerIntentReceivers() {
1280 if (sWallpaperReceiver == null) {
1281 final Application application = getApplication();
1282
1283 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1284
1285 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1286 application.registerReceiver(sWallpaperReceiver, filter);
1287 } else {
1288 sWallpaperReceiver.setLauncher(this);
1289 }
1290
1291 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1292 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1293 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1294 filter.addDataScheme("package");
1295 registerReceiver(mApplicationsReceiver, filter);
1296 }
1297
1298 /**
1299 * Registers various content observers. The current implementation registers
1300 * only a favorites observer to keep track of the favorites applications.
1301 */
1302 private void registerContentObservers() {
1303 ContentResolver resolver = getContentResolver();
1304 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1305 }
1306
1307 @Override
1308 public boolean dispatchKeyEvent(KeyEvent event) {
1309 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1310 switch (event.getKeyCode()) {
1311 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001312 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001313 if (mDrawer.isOpened()) {
1314 closeDrawer();
1315 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001316 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 return true;
1319 case KeyEvent.KEYCODE_HOME:
1320 return true;
1321 }
1322 }
1323
1324 return super.dispatchKeyEvent(event);
1325 }
1326
1327 private void closeDrawer() {
1328 closeDrawer(true);
1329 }
1330
1331 private void closeDrawer(boolean animated) {
1332 if (mDrawer.isOpened()) {
1333 if (animated) {
1334 mDrawer.animateClose();
1335 } else {
1336 mDrawer.close();
1337 }
1338 if (mDrawer.hasFocus()) {
1339 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1340 }
1341 }
1342 }
1343
1344 private void closeFolder() {
1345 Folder folder = mWorkspace.getOpenFolder();
1346 if (folder != null) {
1347 closeFolder(folder);
1348 }
1349 }
1350
1351 void closeFolder(Folder folder) {
1352 folder.getInfo().opened = false;
1353 ViewGroup parent = (ViewGroup) folder.getParent();
1354 if (parent != null) {
1355 parent.removeView(folder);
1356 }
1357 folder.onClose();
1358 }
1359
1360 /**
1361 * When the notification that favorites have changed is received, requests
1362 * a favorites list refresh.
1363 */
1364 private void onFavoritesChanged() {
1365 mDesktopLocked = true;
1366 mDrawer.lock();
1367 sModel.loadUserItems(false, this, false, false);
1368 }
1369
1370 void onDesktopItemsLoaded() {
1371 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 bindDesktopItems();
1373 }
Romain Guycbb89e42009-06-08 15:52:54 -07001374
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 /**
1376 * Refreshes the shortcuts shown on the workspace.
1377 */
1378 private void bindDesktopItems() {
1379 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001380 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001381 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1382 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 return;
1384 }
1385
1386 final Workspace workspace = mWorkspace;
1387 int count = workspace.getChildCount();
1388 for (int i = 0; i < count; i++) {
1389 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1390 }
Romain Guycbb89e42009-06-08 15:52:54 -07001391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 if (DEBUG_USER_INTERFACE) {
1393 android.widget.Button finishButton = new android.widget.Button(this);
1394 finishButton.setText("Finish");
1395 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1396
1397 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1398 public void onClick(View v) {
1399 finish();
1400 }
1401 });
1402 }
Romain Guycbb89e42009-06-08 15:52:54 -07001403
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001404 // Flag any old binder to terminate early
1405 if (mBinder != null) {
1406 mBinder.mTerminate = true;
1407 }
Romain Guycbb89e42009-06-08 15:52:54 -07001408
Karl Rosaen138a0412009-04-23 19:00:21 -07001409 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001410 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 }
1412
1413 private void bindItems(Launcher.DesktopBinder binder,
1414 ArrayList<ItemInfo> shortcuts, int start, int count) {
1415
1416 final Workspace workspace = mWorkspace;
1417 final boolean desktopLocked = mDesktopLocked;
1418
1419 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1420 int i = start;
1421
1422 for ( ; i < end; i++) {
1423 final ItemInfo item = shortcuts.get(i);
1424 switch (item.itemType) {
1425 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1426 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1427 final View shortcut = createShortcut((ApplicationInfo) item);
1428 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1429 !desktopLocked);
1430 break;
1431 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1432 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1433 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1434 (UserFolderInfo) item);
1435 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1436 !desktopLocked);
1437 break;
1438 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1439 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1440 R.layout.live_folder_icon, this,
1441 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1442 (LiveFolderInfo) item);
1443 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1444 !desktopLocked);
1445 break;
1446 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1447 final int screen = workspace.getCurrentScreen();
1448 final View view = mInflater.inflate(R.layout.widget_search,
1449 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001450
Karl Rosaen138a0412009-04-23 19:00:21 -07001451 Search search = (Search) view.findViewById(R.id.widget_search);
1452 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 final Widget widget = (Widget) item;
1455 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 workspace.addWidget(view, widget, !desktopLocked);
1458 break;
1459 }
1460 }
1461
1462 workspace.requestLayout();
1463
1464 if (end >= count) {
1465 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001466 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 } else {
1468 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1469 }
1470 }
1471
1472 private void finishBindDesktopItems() {
1473 if (mSavedState != null) {
1474 if (!mWorkspace.hasFocus()) {
1475 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1476 }
1477
1478 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1479 if (userFolders != null) {
1480 for (long folderId : userFolders) {
1481 final FolderInfo info = sModel.findFolderById(folderId);
1482 if (info != null) {
1483 openFolder(info);
1484 }
1485 }
1486 final Folder openFolder = mWorkspace.getOpenFolder();
1487 if (openFolder != null) {
1488 openFolder.requestFocus();
1489 }
1490 }
1491
1492 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1493 if (allApps) {
1494 mDrawer.open();
1495 }
1496
1497 mSavedState = null;
1498 }
1499
1500 if (mSavedInstanceState != null) {
1501 super.onRestoreInstanceState(mSavedInstanceState);
1502 mSavedInstanceState = null;
1503 }
1504
1505 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1506 mDrawer.requestFocus();
1507 }
1508
1509 mDesktopLocked = false;
1510 mDrawer.unlock();
1511 }
Romain Guycbb89e42009-06-08 15:52:54 -07001512
Karl Rosaen138a0412009-04-23 19:00:21 -07001513 private void bindDrawer(Launcher.DesktopBinder binder,
1514 ApplicationsAdapter drawerAdapter) {
1515 mAllAppsGrid.setAdapter(drawerAdapter);
1516 binder.startBindingAppWidgetsWhenIdle();
1517 }
Romain Guycbb89e42009-06-08 15:52:54 -07001518
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001519 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001520 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 final Workspace workspace = mWorkspace;
1523 final boolean desktopLocked = mDesktopLocked;
1524
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001525 if (!appWidgets.isEmpty()) {
1526 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001527
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001528 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001529 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001530 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001531
Karl Rosaen138a0412009-04-23 19:00:21 -07001532 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001533
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001534 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1535 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001537 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1538 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001539
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001540 workspace.requestLayout();
1541 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001543 if (appWidgets.isEmpty()) {
1544 if (PROFILE_ROTATE) {
1545 android.os.Debug.stopMethodTracing();
1546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001548 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
1550 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001551
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 DragController getDragController() {
1553 return mDragLayer;
1554 }
1555
1556 /**
1557 * Launches the intent referred by the clicked shortcut.
1558 *
1559 * @param v The view representing the clicked shortcut.
1560 */
1561 public void onClick(View v) {
1562 Object tag = v.getTag();
1563 if (tag instanceof ApplicationInfo) {
1564 // Open shortcut
1565 final Intent intent = ((ApplicationInfo) tag).intent;
1566 startActivitySafely(intent);
1567 } else if (tag instanceof FolderInfo) {
1568 handleFolderClick((FolderInfo) tag);
1569 }
1570 }
1571
1572 void startActivitySafely(Intent intent) {
1573 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1574 try {
1575 startActivity(intent);
1576 } catch (ActivityNotFoundException e) {
1577 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1578 } catch (SecurityException e) {
1579 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001580 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1582 "or use the exported attribute for this activity.", e);
1583 }
1584 }
1585
1586 private void handleFolderClick(FolderInfo folderInfo) {
1587 if (!folderInfo.opened) {
1588 // Close any open folder
1589 closeFolder();
1590 // Open the requested folder
1591 openFolder(folderInfo);
1592 } else {
1593 // Find the open folder...
1594 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1595 int folderScreen;
1596 if (openFolder != null) {
1597 folderScreen = mWorkspace.getScreenForView(openFolder);
1598 // .. and close it
1599 closeFolder(openFolder);
1600 if (folderScreen != mWorkspace.getCurrentScreen()) {
1601 // Close any folder open on the current screen
1602 closeFolder();
1603 // Pull the folder onto this screen
1604 openFolder(folderInfo);
1605 }
1606 }
1607 }
1608 }
1609
1610 private void loadWallpaper() {
1611 // The first time the application is started, we load the wallpaper from
1612 // the ApplicationContext
1613 if (sWallpaper == null) {
1614 final Drawable drawable = getWallpaper();
1615 if (drawable instanceof BitmapDrawable) {
1616 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1617 } else {
1618 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1619 }
1620 }
1621 mWorkspace.loadWallpaper(sWallpaper);
1622 }
1623
1624 /**
1625 * Opens the user fodler described by the specified tag. The opening of the folder
1626 * is animated relative to the specified View. If the View is null, no animation
1627 * is played.
1628 *
1629 * @param folderInfo The FolderInfo describing the folder to open.
1630 */
1631 private void openFolder(FolderInfo folderInfo) {
1632 Folder openFolder;
1633
1634 if (folderInfo instanceof UserFolderInfo) {
1635 openFolder = UserFolder.fromXml(this);
1636 } else if (folderInfo instanceof LiveFolderInfo) {
1637 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1638 } else {
1639 return;
1640 }
1641
1642 openFolder.setDragger(mDragLayer);
1643 openFolder.setLauncher(this);
1644
1645 openFolder.bind(folderInfo);
1646 folderInfo.opened = true;
1647
1648 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1649 openFolder.onOpen();
1650 }
1651
1652 /**
1653 * Returns true if the workspace is being loaded. When the workspace is loading,
1654 * no user interaction should be allowed to avoid any conflict.
1655 *
1656 * @return True if the workspace is locked, false otherwise.
1657 */
1658 boolean isWorkspaceLocked() {
1659 return mDesktopLocked;
1660 }
1661
1662 public boolean onLongClick(View v) {
1663 if (mDesktopLocked) {
1664 return false;
1665 }
1666
1667 if (!(v instanceof CellLayout)) {
1668 v = (View) v.getParent();
1669 }
1670
1671 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1672
1673 // This happens when long clicking an item with the dpad/trackball
1674 if (cellInfo == null) {
1675 return true;
1676 }
1677
1678 if (mWorkspace.allowLongPress()) {
1679 if (cellInfo.cell == null) {
1680 if (cellInfo.valid) {
1681 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001682 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 showAddDialog(cellInfo);
1684 }
1685 } else {
1686 if (!(cellInfo.cell instanceof Folder)) {
1687 // User long pressed on an item
1688 mWorkspace.startDrag(cellInfo);
1689 }
1690 }
1691 }
1692 return true;
1693 }
1694
1695 static LauncherModel getModel() {
1696 return sModel;
1697 }
1698
1699 void closeAllApplications() {
1700 mDrawer.close();
1701 }
1702
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001703 View getDrawerHandle() {
1704 return mHandleView;
1705 }
1706
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 boolean isDrawerDown() {
1708 return !mDrawer.isMoving() && !mDrawer.isOpened();
1709 }
1710
1711 boolean isDrawerUp() {
1712 return mDrawer.isOpened() && !mDrawer.isMoving();
1713 }
1714
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001715 boolean isDrawerMoving() {
1716 return mDrawer.isMoving();
1717 }
1718
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 Workspace getWorkspace() {
1720 return mWorkspace;
1721 }
1722
1723 GridView getApplicationsGrid() {
1724 return mAllAppsGrid;
1725 }
1726
1727 @Override
1728 protected Dialog onCreateDialog(int id) {
1729 switch (id) {
1730 case DIALOG_CREATE_SHORTCUT:
1731 return new CreateShortcut().createDialog();
1732 case DIALOG_RENAME_FOLDER:
1733 return new RenameFolder().createDialog();
1734 }
1735
1736 return super.onCreateDialog(id);
1737 }
1738
1739 @Override
1740 protected void onPrepareDialog(int id, Dialog dialog) {
1741 switch (id) {
1742 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 break;
1744 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001745 if (mFolderInfo != null) {
1746 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1747 final CharSequence text = mFolderInfo.title;
1748 input.setText(text);
1749 input.setSelection(0, text.length());
1750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 break;
1752 }
1753 }
1754
1755 void showRenameDialog(FolderInfo info) {
1756 mFolderInfo = info;
1757 mWaitingForResult = true;
1758 showDialog(DIALOG_RENAME_FOLDER);
1759 }
1760
1761 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1762 mAddItemCellInfo = cellInfo;
1763 mWaitingForResult = true;
1764 showDialog(DIALOG_CREATE_SHORTCUT);
1765 }
1766
Romain Guy73b979d2009-06-09 12:57:21 -07001767 private void pickShortcut(int requestCode, int title) {
1768 Bundle bundle = new Bundle();
1769
1770 ArrayList<String> shortcutNames = new ArrayList<String>();
1771 shortcutNames.add(getString(R.string.group_applications));
1772 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1773
1774 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1775 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1776 R.drawable.ic_launcher_application));
1777 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1778
1779 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1780 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1781 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1782 pickIntent.putExtras(bundle);
1783
1784 startActivityForResult(pickIntent, requestCode);
1785 }
1786
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 private class RenameFolder {
1788 private EditText mInput;
1789
1790 Dialog createDialog() {
1791 mWaitingForResult = true;
1792 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1793 mInput = (EditText) layout.findViewById(R.id.folder_name);
1794
1795 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1796 builder.setIcon(0);
1797 builder.setTitle(getString(R.string.rename_folder_title));
1798 builder.setCancelable(true);
1799 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1800 public void onCancel(DialogInterface dialog) {
1801 cleanup();
1802 }
1803 });
1804 builder.setNegativeButton(getString(R.string.cancel_action),
1805 new Dialog.OnClickListener() {
1806 public void onClick(DialogInterface dialog, int which) {
1807 cleanup();
1808 }
1809 }
1810 );
1811 builder.setPositiveButton(getString(R.string.rename_action),
1812 new Dialog.OnClickListener() {
1813 public void onClick(DialogInterface dialog, int which) {
1814 changeFolderName();
1815 }
1816 }
1817 );
1818 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001819
1820 final AlertDialog dialog = builder.create();
1821 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1822 public void onShow(DialogInterface dialog) {
1823 mWorkspace.lock();
1824 }
1825 });
1826
1827 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 }
1829
1830 private void changeFolderName() {
1831 final String name = mInput.getText().toString();
1832 if (!TextUtils.isEmpty(name)) {
1833 // Make sure we have the right folder info
1834 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1835 mFolderInfo.title = name;
1836 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1837
1838 if (mDesktopLocked) {
1839 mDrawer.lock();
1840 sModel.loadUserItems(false, Launcher.this, false, false);
1841 } else {
1842 final FolderIcon folderIcon = (FolderIcon)
1843 mWorkspace.getViewForTag(mFolderInfo);
1844 if (folderIcon != null) {
1845 folderIcon.setText(name);
1846 getWorkspace().requestLayout();
1847 } else {
1848 mDesktopLocked = true;
1849 mDrawer.lock();
1850 sModel.loadUserItems(false, Launcher.this, false, false);
1851 }
1852 }
1853 }
1854 cleanup();
1855 }
1856
1857 private void cleanup() {
1858 mWorkspace.unlock();
1859 dismissDialog(DIALOG_RENAME_FOLDER);
1860 mWaitingForResult = false;
1861 mFolderInfo = null;
1862 }
1863 }
1864
1865 /**
1866 * Displays the shortcut creation dialog and launches, if necessary, the
1867 * appropriate activity.
1868 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001869 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001870 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1871 DialogInterface.OnShowListener {
1872
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001874
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 Dialog createDialog() {
1876 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001877
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001879
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1881 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001882 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001883
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 builder.setInverseBackgroundForced(true);
1885
1886 AlertDialog dialog = builder.create();
1887 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001888 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001889 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 return dialog;
1892 }
1893
1894 public void onCancel(DialogInterface dialog) {
1895 mWaitingForResult = false;
1896 cleanup();
1897 }
1898
Romain Guycbb89e42009-06-08 15:52:54 -07001899 public void onDismiss(DialogInterface dialog) {
1900 mWorkspace.unlock();
1901 }
1902
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 private void cleanup() {
1904 mWorkspace.unlock();
1905 dismissDialog(DIALOG_CREATE_SHORTCUT);
1906 }
1907
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001908 /**
1909 * Handle the action clicked in the "Add to home" dialog.
1910 */
1911 public void onClick(DialogInterface dialog, int which) {
1912 Resources res = getResources();
1913 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001914
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001915 switch (which) {
1916 case AddAdapter.ITEM_SHORTCUT: {
1917 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001918 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001919 break;
1920 }
Romain Guycbb89e42009-06-08 15:52:54 -07001921
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001922 case AddAdapter.ITEM_APPWIDGET: {
1923 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001924
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001925 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1926 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1927 // add the search widget
1928 ArrayList<AppWidgetProviderInfo> customInfo =
1929 new ArrayList<AppWidgetProviderInfo>();
1930 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1931 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1932 info.label = getString(R.string.group_search);
1933 info.icon = R.drawable.ic_search_widget;
1934 customInfo.add(info);
1935 pickIntent.putParcelableArrayListExtra(
1936 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1937 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1938 Bundle b = new Bundle();
1939 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1940 customExtras.add(b);
1941 pickIntent.putParcelableArrayListExtra(
1942 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1943 // start the pick activity
1944 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1945 break;
1946 }
Romain Guycbb89e42009-06-08 15:52:54 -07001947
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001948 case AddAdapter.ITEM_LIVE_FOLDER: {
1949 // Insert extra item to handle inserting folder
1950 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001951
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001952 ArrayList<String> shortcutNames = new ArrayList<String>();
1953 shortcutNames.add(res.getString(R.string.group_folder));
1954 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001955
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001956 ArrayList<ShortcutIconResource> shortcutIcons =
1957 new ArrayList<ShortcutIconResource>();
1958 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1959 R.drawable.ic_launcher_folder));
1960 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1961
1962 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1963 pickIntent.putExtra(Intent.EXTRA_INTENT,
1964 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1965 pickIntent.putExtra(Intent.EXTRA_TITLE,
1966 getText(R.string.title_select_live_folder));
1967 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001968
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001969 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1970 break;
1971 }
1972
1973 case AddAdapter.ITEM_WALLPAPER: {
1974 startWallpaper();
1975 break;
1976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
1978 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001979
1980 public void onShow(DialogInterface dialog) {
1981 mWorkspace.lock();
1982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
1984
1985 /**
1986 * Receives notifications when applications are added/removed.
1987 */
1988 private class ApplicationsIntentReceiver extends BroadcastReceiver {
1989 @Override
1990 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001991 final String action = intent.getAction();
1992 final String packageName = intent.getData().getSchemeSpecificPart();
1993 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1994
1995 if (LauncherModel.DEBUG_LOADERS) {
1996 d(LauncherModel.LOG_TAG, "application intent received: " + action +
1997 ", replacing=" + replacing);
1998 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002000
2001 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2002 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2003 if (!replacing) {
2004 removeShortcutsForPackage(packageName);
2005 if (LauncherModel.DEBUG_LOADERS) {
2006 d(LauncherModel.LOG_TAG, " --> remove package");
2007 }
2008 sModel.removePackage(Launcher.this, packageName);
2009 }
2010 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2011 // later, we will update the package at this time
2012 } else {
2013 if (!replacing) {
2014 if (LauncherModel.DEBUG_LOADERS) {
2015 d(LauncherModel.LOG_TAG, " --> add package");
2016 }
2017 sModel.addPackage(Launcher.this, packageName);
2018 } else {
2019 if (LauncherModel.DEBUG_LOADERS) {
2020 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2021 }
2022 sModel.updatePackage(Launcher.this, packageName);
2023 updateShortcutsForPackage(packageName);
2024 }
2025 }
2026 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002028 if (LauncherModel.DEBUG_LOADERS) {
2029 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2030 }
2031 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
2033 }
2034 }
2035
2036 /**
2037 * Receives notifications whenever the user favorites have changed.
2038 */
2039 private class FavoritesChangeObserver extends ContentObserver {
2040 public FavoritesChangeObserver() {
2041 super(new Handler());
2042 }
2043
2044 @Override
2045 public void onChange(boolean selfChange) {
2046 onFavoritesChanged();
2047 }
2048 }
2049
2050 /**
2051 * Receives intents from other applications to change the wallpaper.
2052 */
2053 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2054 private final Application mApplication;
2055 private WeakReference<Launcher> mLauncher;
2056
2057 WallpaperIntentReceiver(Application application, Launcher launcher) {
2058 mApplication = application;
2059 setLauncher(launcher);
2060 }
2061
2062 void setLauncher(Launcher launcher) {
2063 mLauncher = new WeakReference<Launcher>(launcher);
2064 }
2065
2066 @Override
2067 public void onReceive(Context context, Intent intent) {
2068 // Load the wallpaper from the ApplicationContext and store it locally
2069 // until the Launcher Activity is ready to use it
2070 final Drawable drawable = mApplication.getWallpaper();
2071 if (drawable instanceof BitmapDrawable) {
2072 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2073 } else {
2074 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2075 }
2076
2077 // If Launcher is alive, notify we have a new wallpaper
2078 if (mLauncher != null) {
2079 final Launcher launcher = mLauncher.get();
2080 if (launcher != null) {
2081 launcher.loadWallpaper();
2082 }
2083 }
2084 }
2085 }
2086
2087 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2088 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2089 private boolean mOpen;
2090
2091 public void onDrawerOpened() {
2092 if (!mOpen) {
2093 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002096 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097
2098 mOpen = true;
2099 }
2100 }
2101
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002102 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2103 view.getDrawingRect(bounds);
2104
2105 while (view != mDragLayer) {
2106 bounds.offset(view.getLeft(), view.getTop());
2107 view = (View) view.getParent();
2108 }
2109 }
2110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 public void onDrawerClosed() {
2112 if (mOpen) {
2113 mHandleIcon.reverseTransition(150);
2114 mWorkspace.mDrawerBounds.setEmpty();
2115 mOpen = false;
2116 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002117
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 mAllAppsGrid.setSelection(0);
2119 mAllAppsGrid.clearTextFilter();
2120 }
2121
2122 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002123 if (PROFILE_DRAWER) {
2124 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2125 }
2126
2127 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2128 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 }
2130
2131 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002132 if (PROFILE_DRAWER) {
2133 android.os.Debug.stopMethodTracing();
2134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 }
2136 }
2137
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002138 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002140 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002141 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 // Number of items to bind in every pass
2144 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002145
2146 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002147 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002148 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002150
Karl Rosaen138a0412009-04-23 19:00:21 -07002151 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152
2153 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002154 ArrayList<LauncherAppWidgetInfo> appWidgets,
2155 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156
2157 mLauncher = new WeakReference<Launcher>(launcher);
2158 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002159 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002160
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002161 // Sort widgets so active workspace is bound first
2162 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2163 final int size = appWidgets.size();
2164 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002165
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002166 for (int i = 0; i < size; i++) {
2167 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2168 if (appWidgetInfo.screen == currentScreen) {
2169 mAppWidgets.addFirst(appWidgetInfo);
2170 } else {
2171 mAppWidgets.addLast(appWidgetInfo);
2172 }
2173 }
2174 }
Romain Guycbb89e42009-06-08 15:52:54 -07002175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 public void startBindingItems() {
2177 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2178 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002179
2180 public void startBindingDrawer() {
2181 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2182 }
Romain Guycbb89e42009-06-08 15:52:54 -07002183
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002184 public void startBindingAppWidgetsWhenIdle() {
2185 // Ask for notification when message queue becomes idle
2186 final MessageQueue messageQueue = Looper.myQueue();
2187 messageQueue.addIdleHandler(this);
2188 }
Romain Guycbb89e42009-06-08 15:52:54 -07002189
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002190 public boolean queueIdle() {
2191 // Queue is idle, so start binding items
2192 startBindingAppWidgets();
2193 return false;
2194 }
2195
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002196 public void startBindingAppWidgets() {
2197 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2198 }
2199
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 @Override
2201 public void handleMessage(Message msg) {
2202 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002203 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 return;
2205 }
Romain Guycbb89e42009-06-08 15:52:54 -07002206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 switch (msg.what) {
2208 case MESSAGE_BIND_ITEMS: {
2209 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2210 break;
2211 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002212 case MESSAGE_BIND_DRAWER: {
2213 launcher.bindDrawer(this, mDrawerAdapter);
2214 break;
2215 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002216 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002217 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 break;
2219 }
2220 }
2221 }
2222 }
2223}