blob: c45acb3f66413c94940c04726fb10a59ce957890 [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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
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;
Joe Onorato7404ee42009-07-31 11:54:44 -070073import android.widget.ImageView;
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
Joe Onorato7404ee42009-07-31 11:54:44 -0700126 static final int DIALOG_ALL_APPS = 1;
127 static final int DIALOG_CREATE_SHORTCUT = 2;
128 static final int DIALOG_RENAME_FOLDER = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Romain Guy98d01652009-06-30 16:21:04 -0700130 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
132 // Type: int
133 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
134 // Type: boolean
135 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
136 // Type: long
137 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
148 // Type: int
149 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
150 // Type: int
151 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
152 // Type: int[]
153 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
154 // Type: boolean
155 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
156 // Type: long
157 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
158
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700159 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
161 private static Bitmap sWallpaper;
162
163 private static final Object sLock = new Object();
164 private static int sScreen = DEFAULT_SCREN;
165
166 private static WallpaperIntentReceiver sWallpaperReceiver;
167
168 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
169 private final ContentObserver mObserver = new FavoritesChangeObserver();
170
171 private LayoutInflater mInflater;
172
173 private DragLayer mDragLayer;
Joe Onorato59791172009-07-31 16:21:40 -0700174 private WallpaperView mWallpaperView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700176
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private AppWidgetManager mAppWidgetManager;
178 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700180 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private CellLayout.CellInfo mAddItemCellInfo;
183 private CellLayout.CellInfo mMenuAddInfo;
184 private final int[] mCellCoordinates = new int[2];
185 private FolderInfo mFolderInfo;
186
Joe Onorato7404ee42009-07-31 11:54:44 -0700187 private AllAppsDialog mAllAppsDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700189 private HandleView mHandleView;
Joe Onorato7404ee42009-07-31 11:54:44 -0700190 private AllAppsGridView mAllAppsGrid; // TODO: put this into AllAppsDialog
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
192 private boolean mDesktopLocked = true;
193 private Bundle mSavedState;
194
195 private SpannableStringBuilder mDefaultKeySsb = null;
196
197 private boolean mDestroyed;
Mike LeBeau736cf282009-07-02 17:46:59 -0700198
199 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
201 private boolean mRestoring;
202 private boolean mWaitingForResult;
203 private boolean mLocaleChanged;
204
Dianne Hackborn67800862009-07-24 17:15:20 -0700205 private boolean mHomeDown;
206 private boolean mBackDown;
207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private Bundle mSavedInstanceState;
209
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700210 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 @Override
213 protected void onCreate(Bundle savedInstanceState) {
214 super.onCreate(savedInstanceState);
215 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700216
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700217 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700218
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700219 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
220 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 if (PROFILE_STARTUP) {
223 android.os.Debug.startMethodTracing("/sdcard/launcher");
224 }
225
226 checkForLocaleChange();
227 setWallpaperDimension();
228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 setContentView(R.layout.launcher);
230 setupViews();
231
Joe Onorato7404ee42009-07-31 11:54:44 -0700232 mAllAppsDialog = new AllAppsDialog(this);
233 mAllAppsDialog.lock();
234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 registerIntentReceivers();
236 registerContentObservers();
237
238 mSavedState = savedInstanceState;
239 restoreState(mSavedState);
240
241 if (PROFILE_STARTUP) {
242 android.os.Debug.stopMethodTracing();
243 }
244
245 if (!mRestoring) {
246 startLoaders();
247 }
248
249 // For handling default keys
250 mDefaultKeySsb = new SpannableStringBuilder();
251 Selection.setSelection(mDefaultKeySsb, 0);
252 }
Romain Guycbb89e42009-06-08 15:52:54 -0700253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700255 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
256 readConfiguration(this, localeConfiguration);
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 final Configuration configuration = getResources().getConfiguration();
259
Romain Guy98d01652009-06-30 16:21:04 -0700260 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 final String locale = configuration.locale.toString();
262
Romain Guy98d01652009-06-30 16:21:04 -0700263 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 final int mcc = configuration.mcc;
265
Romain Guy98d01652009-06-30 16:21:04 -0700266 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 final int mnc = configuration.mnc;
268
269 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
270
271 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700272 localeConfiguration.locale = locale;
273 localeConfiguration.mcc = mcc;
274 localeConfiguration.mnc = mnc;
275
276 writeConfiguration(this, localeConfiguration);
277 }
278 }
279
280 private static class LocaleConfiguration {
281 public String locale;
282 public int mcc = -1;
283 public int mnc = -1;
284 }
285
286 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
287 DataInputStream in = null;
288 try {
289 in = new DataInputStream(context.openFileInput(PREFERENCES));
290 configuration.locale = in.readUTF();
291 configuration.mcc = in.readInt();
292 configuration.mnc = in.readInt();
293 } catch (FileNotFoundException e) {
294 // Ignore
295 } catch (IOException e) {
296 // Ignore
297 } finally {
298 if (in != null) {
299 try {
300 in.close();
301 } catch (IOException e) {
302 // Ignore
303 }
304 }
305 }
306 }
307
308 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
309 DataOutputStream out = null;
310 try {
311 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
312 out.writeUTF(configuration.locale);
313 out.writeInt(configuration.mcc);
314 out.writeInt(configuration.mnc);
315 out.flush();
316 } catch (FileNotFoundException e) {
317 // Ignore
318 } catch (IOException e) {
319 //noinspection ResultOfMethodCallIgnored
320 context.getFileStreamPath(PREFERENCES).delete();
321 } finally {
322 if (out != null) {
323 try {
324 out.close();
325 } catch (IOException e) {
326 // Ignore
327 }
328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 }
330 }
331
332 static int getScreen() {
333 synchronized (sLock) {
334 return sScreen;
335 }
336 }
337
338 static void setScreen(int screen) {
339 synchronized (sLock) {
340 sScreen = screen;
341 }
342 }
343
344 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700345 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
346 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 mRestoring = false;
349 }
350
351 private void setWallpaperDimension() {
352 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
353 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
354
355 Display display = getWindowManager().getDefaultDisplay();
356 boolean isPortrait = display.getWidth() < display.getHeight();
357
358 final int width = isPortrait ? display.getWidth() : display.getHeight();
359 final int height = isPortrait ? display.getHeight() : display.getWidth();
360 try {
361 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
362 } catch (RemoteException e) {
363 // System is dead!
364 }
365 }
366
367 @Override
368 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700369 mWaitingForResult = false;
370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 // The pattern used here is that a user PICKs a specific application,
372 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
375 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700376
Romain Guy94dabf12009-07-21 10:55:43 -0700377 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 switch (requestCode) {
379 case REQUEST_PICK_APPLICATION:
380 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
381 break;
382 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700383 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 break;
385 case REQUEST_CREATE_SHORTCUT:
386 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
387 break;
388 case REQUEST_PICK_LIVE_FOLDER:
389 addLiveFolder(data);
390 break;
391 case REQUEST_CREATE_LIVE_FOLDER:
392 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
393 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700394 case REQUEST_PICK_APPWIDGET:
395 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700397 case REQUEST_CREATE_APPWIDGET:
398 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 break;
400 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700401 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700403 // Clean up the appWidgetId if we canceled
404 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
405 if (appWidgetId != -1) {
406 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
410
411 @Override
412 protected void onResume() {
413 super.onResume();
414
415 if (mRestoring) {
416 startLoaders();
417 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700418
419 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
420 // onNewIntent), then close the search dialog if needed, because it probably
421 // came from the user pressing 'home' (rather than, for example, pressing 'back').
422 if (mIsNewIntent) {
423 // Post to a handler so that this happens after the search dialog tries to open
424 // itself again.
425 mWorkspace.post(new Runnable() {
426 public void run() {
427 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
428 ServiceManager.getService(Context.SEARCH_SERVICE));
429 try {
430 searchManagerService.stopSearch();
431 } catch (RemoteException e) {
432 e(LOG_TAG, "error stopping search", e);
433 }
434 }
435 });
436 }
437
438 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 }
440
441 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700442 protected void onPause() {
443 super.onPause();
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700444 closeAllAppsDialog(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700445 }
Romain Guycbb89e42009-06-08 15:52:54 -0700446
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700447 @Override
448 public Object onRetainNonConfigurationInstance() {
449 // Flag any binder to stop early before switching
450 if (mBinder != null) {
451 mBinder.mTerminate = true;
452 }
Romain Guycbb89e42009-06-08 15:52:54 -0700453
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700454 if (PROFILE_ROTATE) {
455 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
456 }
457 return null;
458 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 private boolean acceptFilter() {
461 final InputMethodManager inputManager = (InputMethodManager)
462 getSystemService(Context.INPUT_METHOD_SERVICE);
463 return !inputManager.isFullscreenMode();
464 }
465
466 @Override
467 public boolean onKeyDown(int keyCode, KeyEvent event) {
468 boolean handled = super.onKeyDown(keyCode, event);
469 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
470 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
471 keyCode, event);
472 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700473 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700474 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700475 // showSearchDialog()
476 // If there are multiple keystrokes before the search dialog takes focus,
477 // onSearchRequested() will be called for every keystroke,
478 // but it is idempotent, so it's fine.
479 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 }
481 }
482
483 return handled;
484 }
485
Karl Rosaen138a0412009-04-23 19:00:21 -0700486 private String getTypedText() {
487 return mDefaultKeySsb.toString();
488 }
489
490 private void clearTypedText() {
491 mDefaultKeySsb.clear();
492 mDefaultKeySsb.clearSpans();
493 Selection.setSelection(mDefaultKeySsb, 0);
494 }
495
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 /**
497 * Restores the previous state, if it exists.
498 *
499 * @param savedState The previous state.
500 */
501 private void restoreState(Bundle savedState) {
502 if (savedState == null) {
503 return;
504 }
505
506 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
507 if (currentScreen > -1) {
508 mWorkspace.setCurrentScreen(currentScreen);
509 }
510
511 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
512 if (addScreen > -1) {
513 mAddItemCellInfo = new CellLayout.CellInfo();
514 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
515 addItemCellInfo.valid = true;
516 addItemCellInfo.screen = addScreen;
517 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
518 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
519 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
520 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
521 addItemCellInfo.findVacantCellsFromOccupied(
522 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
523 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
524 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
525 mRestoring = true;
526 }
527
528 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
529 if (renameFolder) {
530 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
531 mFolderInfo = sModel.getFolderById(this, id);
532 mRestoring = true;
533 }
534 }
535
536 /**
537 * Finds all the views we need and configure them properly.
538 */
539 private void setupViews() {
540 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
541 final DragLayer dragLayer = mDragLayer;
542
Joe Onorato59791172009-07-31 16:21:40 -0700543 mWallpaperView = (WallpaperView) dragLayer.findViewById(R.id.wallpaper);
544 final WallpaperView wallpaper = mWallpaperView;
545
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
547 final Workspace workspace = mWorkspace;
548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
550
Joe Onorato7404ee42009-07-31 11:54:44 -0700551 mHandleView = (HandleView) findViewById(R.id.all_apps);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700552 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700553 mHandleView.setOnClickListener(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700554 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 mHandleIcon.setCrossFadeEnabled(true);
556
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 workspace.setOnLongClickListener(this);
558 workspace.setDragger(dragLayer);
559 workspace.setLauncher(this);
Joe Onorato59791172009-07-31 16:21:40 -0700560 workspace.setWallpaper(wallpaper);
561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 loadWallpaper();
Joe Onorato59791172009-07-31 16:21:40 -0700563 wallpaper.setScreenCount(workspace.getScreenCount());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564
565 deleteZone.setLauncher(this);
566 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700567 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568
Joe Onorato7404ee42009-07-31 11:54:44 -0700569 // TODO dragLayer.setIgnoredDropTarget(grid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 dragLayer.setDragScoller(workspace);
571 dragLayer.setDragListener(deleteZone);
572 }
573
574 /**
575 * Creates a view representing a shortcut.
576 *
577 * @param info The data structure describing the shortcut.
578 *
579 * @return A View inflated from R.layout.application.
580 */
581 View createShortcut(ApplicationInfo info) {
582 return createShortcut(R.layout.application,
583 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
584 }
585
586 /**
587 * Creates a view representing a shortcut inflated from the specified resource.
588 *
589 * @param layoutResId The id of the XML layout used to create the shortcut.
590 * @param parent The group the shortcut belongs to.
591 * @param info The data structure describing the shortcut.
592 *
593 * @return A View inflated from layoutResId.
594 */
595 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
596 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
597
598 if (!info.filtered) {
599 info.icon = Utilities.createIconThumbnail(info.icon, this);
600 info.filtered = true;
601 }
602
603 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
604 favorite.setText(info.title);
605 favorite.setTag(info);
606 favorite.setOnClickListener(this);
607
608 return favorite;
609 }
610
611 /**
612 * Add an application shortcut to the workspace.
613 *
614 * @param data The intent describing the application.
615 * @param cellInfo The position on screen where to create the shortcut.
616 */
617 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
618 boolean insertAtFirst) {
619 cellInfo.screen = mWorkspace.getCurrentScreen();
620 if (!findSingleSlot(cellInfo)) return;
621
Romain Guy73b979d2009-06-09 12:57:21 -0700622 final ApplicationInfo info = infoFromApplicationIntent(context, data);
623 if (info != null) {
624 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
625 }
626 }
627
628 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 ComponentName component = data.getComponent();
630 PackageManager packageManager = context.getPackageManager();
631 ActivityInfo activityInfo = null;
632 try {
633 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
634 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700635 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 }
Romain Guycbb89e42009-06-08 15:52:54 -0700637
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 if (activityInfo != null) {
639 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 itemInfo.title = activityInfo.loadLabel(packageManager);
642 if (itemInfo.title == null) {
643 itemInfo.title = activityInfo.name;
644 }
Romain Guycbb89e42009-06-08 15:52:54 -0700645
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
647 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
648 itemInfo.icon = activityInfo.loadIcon(packageManager);
649 itemInfo.container = ItemInfo.NO_ID;
650
Romain Guy73b979d2009-06-09 12:57:21 -0700651 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
Romain Guy73b979d2009-06-09 12:57:21 -0700653
654 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 }
Romain Guycbb89e42009-06-08 15:52:54 -0700656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 /**
658 * Add a shortcut to the workspace.
659 *
660 * @param data The intent describing the shortcut.
661 * @param cellInfo The position on screen where to create the shortcut.
662 * @param insertAtFirst
663 */
664 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
665 boolean insertAtFirst) {
666 cellInfo.screen = mWorkspace.getCurrentScreen();
667 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700668
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
670
671 if (!mRestoring) {
672 sModel.addDesktopItem(info);
673
674 final View view = createShortcut(info);
675 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
676 } else if (sModel.isDesktopLoaded()) {
677 sModel.addDesktopItem(info);
678 }
679 }
680
Romain Guycbb89e42009-06-08 15:52:54 -0700681
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700685 * @param data The intent describing the appWidgetId.
686 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 boolean insertAtFirst) {
690
691 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700692 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700693
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700694 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700695
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700696 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700697
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700698 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700700 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 // Try finding open space on Launcher screen
703 final int[] xy = mCellCoordinates;
704 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
705
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 // Build Launcher-specific widget info and save to database
707 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 launcherInfo.spanX = spans[0];
709 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 LauncherModel.addItemToDatabase(this, launcherInfo,
712 LauncherSettings.Favorites.CONTAINER_DESKTOP,
713 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
714
715 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700717
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700719 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700720
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700721 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700723
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
725 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
726 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700727 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 }
729 }
Romain Guycbb89e42009-06-08 15:52:54 -0700730
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700731 public LauncherAppWidgetHost getAppWidgetHost() {
732 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 }
Romain Guycbb89e42009-06-08 15:52:54 -0700734
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 static ApplicationInfo addShortcut(Context context, Intent data,
736 CellLayout.CellInfo cellInfo, boolean notify) {
737
Romain Guy73b979d2009-06-09 12:57:21 -0700738 final ApplicationInfo info = infoFromShortcutIntent(context, data);
739 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
740 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
741
742 return info;
743 }
744
745 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
747 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
748 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
749
750 Drawable icon = null;
751 boolean filtered = false;
752 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700753 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754
755 if (bitmap != null) {
756 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
757 filtered = true;
758 customIcon = true;
759 } else {
760 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700761 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700763 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 final PackageManager packageManager = context.getPackageManager();
765 Resources resources = packageManager.getResourcesForApplication(
766 iconResource.packageName);
767 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
768 icon = resources.getDrawable(id);
769 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700770 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 }
772 }
773 }
774
775 if (icon == null) {
776 icon = context.getPackageManager().getDefaultActivityIcon();
777 }
778
779 final ApplicationInfo info = new ApplicationInfo();
780 info.icon = icon;
781 info.filtered = filtered;
782 info.title = name;
783 info.intent = intent;
784 info.customIcon = customIcon;
785 info.iconResource = iconResource;
786
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 return info;
788 }
789
790 @Override
791 protected void onNewIntent(Intent intent) {
792 super.onNewIntent(intent);
793
794 // Close the menu
795 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
796 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700797
798 // Set this flag so that onResume knows to close the search dialog if it's open,
799 // because this was a new intent (thus a press of 'home' or some such) rather than
800 // for example onResume being called when the user pressed the 'back' button.
801 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802
803 try {
804 dismissDialog(DIALOG_CREATE_SHORTCUT);
805 // Unlock the workspace if the dialog was showing
806 mWorkspace.unlock();
807 } catch (Exception e) {
808 // An exception is thrown if the dialog is not visible, which is fine
809 }
810
811 try {
812 dismissDialog(DIALOG_RENAME_FOLDER);
813 // Unlock the workspace if the dialog was showing
814 mWorkspace.unlock();
815 } catch (Exception e) {
816 // An exception is thrown if the dialog is not visible, which is fine
817 }
818
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
820 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700821
Romain Guy94dabf12009-07-21 10:55:43 -0700822 if (!mWorkspace.isDefaultScreenShowing()) {
823 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700825
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700826 closeAllAppsDialog(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700827
828 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 if (v != null && v.getWindowToken() != null) {
830 InputMethodManager imm = (InputMethodManager)getSystemService(
831 INPUT_METHOD_SERVICE);
832 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
833 }
834 } else {
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700835 closeAllAppsDialog(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
837 }
838 }
839
840 @Override
841 protected void onRestoreInstanceState(Bundle savedInstanceState) {
842 // Do not call super here
843 mSavedInstanceState = savedInstanceState;
844 }
845
846 @Override
847 protected void onSaveInstanceState(Bundle outState) {
848 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
849
850 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
851 if (folders.size() > 0) {
852 final int count = folders.size();
853 long[] ids = new long[count];
854 for (int i = 0; i < count; i++) {
855 final FolderInfo info = folders.get(i).getInfo();
856 ids[i] = info.id;
857 }
858 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
859 } else {
860 super.onSaveInstanceState(outState);
861 }
862
Romain Guy3cf604f2009-06-16 13:12:53 -0700863 final boolean isConfigurationChange = getChangingConfigurations() != 0;
864
Romain Guy5a941392009-04-28 15:18:25 -0700865 // When the drawer is opened and we are saving the state because of a
866 // configuration change
Joe Onorato7404ee42009-07-31 11:54:44 -0700867 if (mAllAppsDialog.isOpen && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870
871 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
872 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
873 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
874
875 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
876 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
877 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
878 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
879 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
880 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
881 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
882 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
883 layout.getOccupiedCells());
884 }
885
886 if (mFolderInfo != null && mWaitingForResult) {
887 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
888 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
889 }
890 }
891
892 @Override
893 public void onDestroy() {
894 mDestroyed = true;
895
896 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700897
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700899 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700901 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 }
903
904 TextKeyListener.getInstance().release();
905
906 mAllAppsGrid.clearTextFilter();
907 mAllAppsGrid.setAdapter(null);
908 sModel.unbind();
909 sModel.abortLoaders();
910
911 getContentResolver().unregisterContentObserver(mObserver);
912 unregisterReceiver(mApplicationsReceiver);
913 }
914
915 @Override
916 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700917 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 super.startActivityForResult(intent, requestCode);
919 }
920
921 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700922 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700924
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700925 closeAllAppsDialog(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700926
Karl Rosaen138a0412009-04-23 19:00:21 -0700927 // Slide the search widget to the top, if it's on the current screen,
928 // otherwise show the search dialog immediately.
929 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
930 if (searchWidget == null) {
931 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
932 } else {
933 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
934 // show the currently typed text in the search widget while sliding
935 searchWidget.setQuery(getTypedText());
936 }
937 }
Romain Guycbb89e42009-06-08 15:52:54 -0700938
Karl Rosaen138a0412009-04-23 19:00:21 -0700939 /**
940 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700941 *
942 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700943 */
Romain Guycbb89e42009-06-08 15:52:54 -0700944 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700945 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700946
Karl Rosaen138a0412009-04-23 19:00:21 -0700947 if (initialQuery == null) {
948 // Use any text typed in the launcher as the initial query
949 initialQuery = getTypedText();
950 clearTypedText();
951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 if (appSearchData == null) {
953 appSearchData = new Bundle();
954 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
955 }
Romain Guycbb89e42009-06-08 15:52:54 -0700956
Karl Rosaen138a0412009-04-23 19:00:21 -0700957 final SearchManager searchManager =
958 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
959
960 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
961 if (searchWidget != null) {
962 // This gets called when the user leaves the search dialog to go back to
963 // the Launcher.
964 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
965 public void onCancel() {
966 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700967 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700968 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 });
970 }
Romain Guycbb89e42009-06-08 15:52:54 -0700971
Karl Rosaen138a0412009-04-23 19:00:21 -0700972 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700973 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 }
975
Karl Rosaen138a0412009-04-23 19:00:21 -0700976 /**
977 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700978 */
Romain Guy5a941392009-04-28 15:18:25 -0700979 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700980 // Close search dialog
981 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100982 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700983 // Restore search widget to its normal position
984 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
985 if (searchWidget != null) {
986 searchWidget.stopSearch(false);
987 }
988 }
Romain Guycbb89e42009-06-08 15:52:54 -0700989
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 @Override
991 public boolean onCreateOptionsMenu(Menu menu) {
992 if (mDesktopLocked) return false;
993
994 super.onCreateOptionsMenu(menu);
995 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
996 .setIcon(android.R.drawable.ic_menu_add)
997 .setAlphabeticShortcut('A');
998 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
999 .setIcon(android.R.drawable.ic_menu_gallery)
1000 .setAlphabeticShortcut('W');
1001 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1002 .setIcon(android.R.drawable.ic_search_category_default)
1003 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1004 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1005 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1006 .setAlphabeticShortcut('N');
1007
1008 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001009 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1010 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011
1012 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1013 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1014 .setIntent(settings);
1015
1016 return true;
1017 }
1018
1019 @Override
1020 public boolean onPrepareOptionsMenu(Menu menu) {
1021 super.onPrepareOptionsMenu(menu);
1022
1023 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1024 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1025
1026 return true;
1027 }
1028
1029 @Override
1030 public boolean onOptionsItemSelected(MenuItem item) {
1031 switch (item.getItemId()) {
1032 case MENU_ADD:
1033 addItems();
1034 return true;
1035 case MENU_WALLPAPER_SETTINGS:
1036 startWallpaper();
1037 return true;
1038 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001039 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 return true;
1041 case MENU_NOTIFICATIONS:
1042 showNotifications();
1043 return true;
1044 }
1045
1046 return super.onOptionsItemSelected(item);
1047 }
Romain Guycbb89e42009-06-08 15:52:54 -07001048
Karl Rosaen138a0412009-04-23 19:00:21 -07001049 /**
1050 * Indicates that we want global search for this activity by setting the globalSearch
1051 * argument for {@link #startSearch} to true.
1052 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001054 @Override
1055 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001056 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001057 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001058 }
1059
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 private void addItems() {
1061 showAddDialog(mMenuAddInfo);
1062 }
1063
1064 private void removeShortcutsForPackage(String packageName) {
1065 if (packageName != null && packageName.length() > 0) {
1066 mWorkspace.removeShortcutsForPackage(packageName);
1067 }
1068 }
Romain Guycbb89e42009-06-08 15:52:54 -07001069
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001070 private void updateShortcutsForPackage(String packageName) {
1071 if (packageName != null && packageName.length() > 0) {
1072 mWorkspace.updateShortcutsForPackage(packageName);
1073 }
1074 }
1075
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001076 void addAppWidget(Intent data) {
1077 // TODO: catch bad widget exception when sent
1078 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001080 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1081 if (SEARCH_WIDGET.equals(customWidget)) {
1082 // We don't need this any more, since this isn't a real app widget.
1083 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1084 // add the search widget
1085 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001087 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1088
1089 if (appWidget.configure != null) {
1090 // Launch over to configure widget, if needed
1091 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1092 intent.setComponent(appWidget.configure);
1093 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1094
1095 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1096 } else {
1097 // Otherwise just add it
1098 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1099 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 }
1101 }
Romain Guycbb89e42009-06-08 15:52:54 -07001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 void addSearch() {
1104 final Widget info = Widget.makeSearch();
1105 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 final int[] xy = mCellCoordinates;
1108 final int spanX = info.spanX;
1109 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 sModel.addDesktopItem(info);
1114 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1115 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 final View view = mInflater.inflate(info.layoutResource, null);
1118 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001119 Search search = (Search) view.findViewById(R.id.widget_search);
1120 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001121
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1123 }
1124
Romain Guy73b979d2009-06-09 12:57:21 -07001125 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001126 // Handle case where user selected "Applications"
1127 String applicationName = getResources().getString(R.string.group_applications);
1128 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001129
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001130 if (applicationName != null && applicationName.equals(shortcutName)) {
1131 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1132 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001133
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001134 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1135 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001136 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001137 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001138 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001139 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
1141
1142 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001143 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001144 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001145 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001146
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001147 if (folderName != null && folderName.equals(shortcutName)) {
1148 addFolder(!mDesktopLocked);
1149 } else {
1150 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 }
1153
1154 void addFolder(boolean insertAtFirst) {
1155 UserFolderInfo folderInfo = new UserFolderInfo();
1156 folderInfo.title = getText(R.string.folder_name);
1157
1158 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1159 cellInfo.screen = mWorkspace.getCurrentScreen();
1160 if (!findSingleSlot(cellInfo)) return;
1161
1162 // Update the model
1163 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1164 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1165 sModel.addDesktopItem(folderInfo);
1166 sModel.addFolder(folderInfo);
1167
1168 // Create the view
1169 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1170 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1171 mWorkspace.addInCurrentScreen(newFolder,
1172 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1173 }
Romain Guycbb89e42009-06-08 15:52:54 -07001174
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1176 boolean insertAtFirst) {
1177 cellInfo.screen = mWorkspace.getCurrentScreen();
1178 if (!findSingleSlot(cellInfo)) return;
1179
1180 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1181
1182 if (!mRestoring) {
1183 sModel.addDesktopItem(info);
1184
1185 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1186 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1187 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1188 } else if (sModel.isDesktopLoaded()) {
1189 sModel.addDesktopItem(info);
1190 }
1191 }
1192
1193 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1194 CellLayout.CellInfo cellInfo, boolean notify) {
1195
1196 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1197 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1198
1199 Drawable icon = null;
1200 boolean filtered = false;
1201 Intent.ShortcutIconResource iconResource = null;
1202
1203 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1204 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1205 try {
1206 iconResource = (Intent.ShortcutIconResource) extra;
1207 final PackageManager packageManager = context.getPackageManager();
1208 Resources resources = packageManager.getResourcesForApplication(
1209 iconResource.packageName);
1210 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1211 icon = resources.getDrawable(id);
1212 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001213 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 }
1215 }
1216
1217 if (icon == null) {
1218 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1219 }
1220
1221 final LiveFolderInfo info = new LiveFolderInfo();
1222 info.icon = icon;
1223 info.filtered = filtered;
1224 info.title = name;
1225 info.iconResource = iconResource;
1226 info.uri = data.getData();
1227 info.baseIntent = baseIntent;
1228 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1229 LiveFolders.DISPLAY_MODE_GRID);
1230
1231 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1232 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1233 sModel.addFolder(info);
1234
1235 return info;
1236 }
1237
1238 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1239 final int[] xy = new int[2];
1240 if (findSlot(cellInfo, xy, 1, 1)) {
1241 cellInfo.cellX = xy[0];
1242 cellInfo.cellY = xy[1];
1243 return true;
1244 }
1245 return false;
1246 }
1247
1248 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1249 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1250 boolean[] occupied = mSavedState != null ?
1251 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1252 cellInfo = mWorkspace.findAllVacantCells(occupied);
1253 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1254 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1255 return false;
1256 }
1257 }
1258 return true;
1259 }
1260
1261 private void showNotifications() {
1262 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1263 if (statusBar != null) {
1264 statusBar.expand();
1265 }
1266 }
1267
1268 private void startWallpaper() {
1269 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1270 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1271 }
1272
1273 /**
1274 * Registers various intent receivers. The current implementation registers
1275 * only a wallpaper intent receiver to let other applications change the
1276 * wallpaper.
1277 */
1278 private void registerIntentReceivers() {
1279 if (sWallpaperReceiver == null) {
1280 final Application application = getApplication();
1281
1282 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1283
1284 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1285 application.registerReceiver(sWallpaperReceiver, filter);
1286 } else {
1287 sWallpaperReceiver.setLauncher(this);
1288 }
1289
1290 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1291 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1292 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1293 filter.addDataScheme("package");
1294 registerReceiver(mApplicationsReceiver, filter);
1295 }
1296
1297 /**
1298 * Registers various content observers. The current implementation registers
1299 * only a favorites observer to keep track of the favorites applications.
1300 */
1301 private void registerContentObservers() {
1302 ContentResolver resolver = getContentResolver();
1303 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1304 }
1305
1306 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001307 public void onWindowFocusChanged(boolean hasFocus) {
1308 super.onWindowFocusChanged(hasFocus);
1309 if (!hasFocus) {
1310 mBackDown = mHomeDown = false;
1311 }
1312 }
1313
1314 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 public boolean dispatchKeyEvent(KeyEvent event) {
1316 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1317 switch (event.getKeyCode()) {
1318 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001319 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 return true;
1321 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001322 mHomeDown = true;
1323 return true;
1324 }
1325 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1326 switch (event.getKeyCode()) {
1327 case KeyEvent.KEYCODE_BACK:
1328 if (!event.isCanceled()) {
1329 mWorkspace.dispatchKeyEvent(event);
Joe Onorato7404ee42009-07-31 11:54:44 -07001330 if (mAllAppsDialog.isOpen) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001331 closeAllAppsDialog(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001332 } else {
1333 closeFolder();
1334 }
1335 }
1336 mBackDown = false;
1337 return true;
1338 case KeyEvent.KEYCODE_HOME:
1339 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 return true;
1341 }
1342 }
1343
1344 return super.dispatchKeyEvent(event);
1345 }
1346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 private void closeFolder() {
1348 Folder folder = mWorkspace.getOpenFolder();
1349 if (folder != null) {
1350 closeFolder(folder);
1351 }
1352 }
1353
1354 void closeFolder(Folder folder) {
1355 folder.getInfo().opened = false;
1356 ViewGroup parent = (ViewGroup) folder.getParent();
1357 if (parent != null) {
1358 parent.removeView(folder);
1359 }
1360 folder.onClose();
1361 }
1362
1363 /**
1364 * When the notification that favorites have changed is received, requests
1365 * a favorites list refresh.
1366 */
1367 private void onFavoritesChanged() {
1368 mDesktopLocked = true;
Joe Onorato7404ee42009-07-31 11:54:44 -07001369 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 sModel.loadUserItems(false, this, false, false);
1371 }
1372
1373 void onDesktopItemsLoaded() {
1374 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 bindDesktopItems();
1376 }
Romain Guycbb89e42009-06-08 15:52:54 -07001377
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 /**
1379 * Refreshes the shortcuts shown on the workspace.
1380 */
1381 private void bindDesktopItems() {
1382 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001383 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001384 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1385 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 return;
1387 }
1388
1389 final Workspace workspace = mWorkspace;
1390 int count = workspace.getChildCount();
1391 for (int i = 0; i < count; i++) {
1392 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1393 }
Romain Guycbb89e42009-06-08 15:52:54 -07001394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 if (DEBUG_USER_INTERFACE) {
1396 android.widget.Button finishButton = new android.widget.Button(this);
1397 finishButton.setText("Finish");
1398 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1399
1400 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1401 public void onClick(View v) {
1402 finish();
1403 }
1404 });
1405 }
Romain Guycbb89e42009-06-08 15:52:54 -07001406
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001407 // Flag any old binder to terminate early
1408 if (mBinder != null) {
1409 mBinder.mTerminate = true;
1410 }
Romain Guycbb89e42009-06-08 15:52:54 -07001411
Karl Rosaen138a0412009-04-23 19:00:21 -07001412 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001413 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415
1416 private void bindItems(Launcher.DesktopBinder binder,
1417 ArrayList<ItemInfo> shortcuts, int start, int count) {
1418
1419 final Workspace workspace = mWorkspace;
1420 final boolean desktopLocked = mDesktopLocked;
1421
1422 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1423 int i = start;
1424
1425 for ( ; i < end; i++) {
1426 final ItemInfo item = shortcuts.get(i);
1427 switch (item.itemType) {
1428 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1429 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1430 final View shortcut = createShortcut((ApplicationInfo) item);
1431 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1432 !desktopLocked);
1433 break;
1434 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1435 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1436 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1437 (UserFolderInfo) item);
1438 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1439 !desktopLocked);
1440 break;
1441 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1442 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1443 R.layout.live_folder_icon, this,
1444 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1445 (LiveFolderInfo) item);
1446 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1447 !desktopLocked);
1448 break;
1449 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1450 final int screen = workspace.getCurrentScreen();
1451 final View view = mInflater.inflate(R.layout.widget_search,
1452 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001453
Karl Rosaen138a0412009-04-23 19:00:21 -07001454 Search search = (Search) view.findViewById(R.id.widget_search);
1455 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 final Widget widget = (Widget) item;
1458 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001459
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 workspace.addWidget(view, widget, !desktopLocked);
1461 break;
1462 }
1463 }
1464
1465 workspace.requestLayout();
1466
1467 if (end >= count) {
1468 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001469 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 } else {
1471 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1472 }
1473 }
1474
1475 private void finishBindDesktopItems() {
1476 if (mSavedState != null) {
1477 if (!mWorkspace.hasFocus()) {
1478 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1479 }
1480
1481 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1482 if (userFolders != null) {
1483 for (long folderId : userFolders) {
1484 final FolderInfo info = sModel.findFolderById(folderId);
1485 if (info != null) {
1486 openFolder(info);
1487 }
1488 }
1489 final Folder openFolder = mWorkspace.getOpenFolder();
1490 if (openFolder != null) {
1491 openFolder.requestFocus();
1492 }
1493 }
1494
1495 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1496 if (allApps) {
Joe Onorato45d43462009-07-31 14:36:51 -07001497 showAllAppsDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 }
1499
1500 mSavedState = null;
1501 }
1502
1503 if (mSavedInstanceState != null) {
1504 super.onRestoreInstanceState(mSavedInstanceState);
1505 mSavedInstanceState = null;
1506 }
1507
Joe Onorato7404ee42009-07-31 11:54:44 -07001508 /* TODO
1509 if (mAllAppsDialog.isOpen && !mDrawer.hasFocus()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 mDrawer.requestFocus();
1511 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001512 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513
1514 mDesktopLocked = false;
Joe Onorato7404ee42009-07-31 11:54:44 -07001515 mAllAppsDialog.unlock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Karl Rosaen138a0412009-04-23 19:00:21 -07001518 private void bindDrawer(Launcher.DesktopBinder binder,
1519 ApplicationsAdapter drawerAdapter) {
1520 mAllAppsGrid.setAdapter(drawerAdapter);
1521 binder.startBindingAppWidgetsWhenIdle();
1522 }
Romain Guycbb89e42009-06-08 15:52:54 -07001523
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001524 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001525 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 final Workspace workspace = mWorkspace;
1528 final boolean desktopLocked = mDesktopLocked;
1529
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001530 if (!appWidgets.isEmpty()) {
1531 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001532
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001533 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001534 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001535 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Karl Rosaen138a0412009-04-23 19:00:21 -07001537 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001538
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001539 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1540 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001541
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001542 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1543 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001545 workspace.requestLayout();
1546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001548 if (appWidgets.isEmpty()) {
1549 if (PROFILE_ROTATE) {
1550 android.os.Debug.stopMethodTracing();
1551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001553 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
1555 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 DragController getDragController() {
1558 return mDragLayer;
1559 }
1560
1561 /**
1562 * Launches the intent referred by the clicked shortcut.
1563 *
1564 * @param v The view representing the clicked shortcut.
1565 */
1566 public void onClick(View v) {
1567 Object tag = v.getTag();
1568 if (tag instanceof ApplicationInfo) {
1569 // Open shortcut
1570 final Intent intent = ((ApplicationInfo) tag).intent;
1571 startActivitySafely(intent);
1572 } else if (tag instanceof FolderInfo) {
1573 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001574 } else if (v == mHandleView) {
1575 if (mAllAppsDialog.isOpen) {
1576 // TODO how can we be here?
1577 } else {
Joe Onorato45d43462009-07-31 14:36:51 -07001578 showAllAppsDialog();
Joe Onorato7404ee42009-07-31 11:54:44 -07001579 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
1581 }
1582
1583 void startActivitySafely(Intent intent) {
1584 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1585 try {
1586 startActivity(intent);
1587 } catch (ActivityNotFoundException e) {
1588 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1589 } catch (SecurityException e) {
1590 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001591 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1593 "or use the exported attribute for this activity.", e);
1594 }
1595 }
1596
1597 private void handleFolderClick(FolderInfo folderInfo) {
1598 if (!folderInfo.opened) {
1599 // Close any open folder
1600 closeFolder();
1601 // Open the requested folder
1602 openFolder(folderInfo);
1603 } else {
1604 // Find the open folder...
1605 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1606 int folderScreen;
1607 if (openFolder != null) {
1608 folderScreen = mWorkspace.getScreenForView(openFolder);
1609 // .. and close it
1610 closeFolder(openFolder);
1611 if (folderScreen != mWorkspace.getCurrentScreen()) {
1612 // Close any folder open on the current screen
1613 closeFolder();
1614 // Pull the folder onto this screen
1615 openFolder(folderInfo);
1616 }
1617 }
1618 }
1619 }
1620
1621 private void loadWallpaper() {
1622 // The first time the application is started, we load the wallpaper from
1623 // the ApplicationContext
1624 if (sWallpaper == null) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001625 final Drawable drawable;
1626 if (false) {
1627 drawable = getWallpaper();
1628 } else {
1629 drawable = getResources().getDrawable(R.drawable.wallpaper_path);
1630 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 if (drawable instanceof BitmapDrawable) {
1632 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1633 } else {
1634 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1635 }
1636 }
Joe Onorato59791172009-07-31 16:21:40 -07001637 mWallpaperView.loadWallpaper(sWallpaper);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 }
1639
1640 /**
1641 * Opens the user fodler described by the specified tag. The opening of the folder
1642 * is animated relative to the specified View. If the View is null, no animation
1643 * is played.
1644 *
1645 * @param folderInfo The FolderInfo describing the folder to open.
1646 */
1647 private void openFolder(FolderInfo folderInfo) {
1648 Folder openFolder;
1649
1650 if (folderInfo instanceof UserFolderInfo) {
1651 openFolder = UserFolder.fromXml(this);
1652 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001653 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 } else {
1655 return;
1656 }
1657
1658 openFolder.setDragger(mDragLayer);
1659 openFolder.setLauncher(this);
1660
1661 openFolder.bind(folderInfo);
1662 folderInfo.opened = true;
1663
1664 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1665 openFolder.onOpen();
1666 }
1667
1668 /**
1669 * Returns true if the workspace is being loaded. When the workspace is loading,
1670 * no user interaction should be allowed to avoid any conflict.
1671 *
1672 * @return True if the workspace is locked, false otherwise.
1673 */
1674 boolean isWorkspaceLocked() {
1675 return mDesktopLocked;
1676 }
1677
1678 public boolean onLongClick(View v) {
1679 if (mDesktopLocked) {
1680 return false;
1681 }
1682
1683 if (!(v instanceof CellLayout)) {
1684 v = (View) v.getParent();
1685 }
1686
1687 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1688
1689 // This happens when long clicking an item with the dpad/trackball
1690 if (cellInfo == null) {
1691 return true;
1692 }
1693
1694 if (mWorkspace.allowLongPress()) {
1695 if (cellInfo.cell == null) {
1696 if (cellInfo.valid) {
1697 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001698 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 showAddDialog(cellInfo);
1700 }
1701 } else {
1702 if (!(cellInfo.cell instanceof Folder)) {
1703 // User long pressed on an item
1704 mWorkspace.startDrag(cellInfo);
1705 }
1706 }
1707 }
1708 return true;
1709 }
1710
1711 static LauncherModel getModel() {
1712 return sModel;
1713 }
1714
1715 void closeAllApplications() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001716 // TODO mDrawer.close();
1717 mAllAppsDialog.dismiss();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 }
1719
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001720 View getDrawerHandle() {
1721 return mHandleView;
1722 }
1723
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 boolean isDrawerDown() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001725 return /* TODO !mDrawer.isMoving() && */ !mAllAppsDialog.isOpen;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 }
1727
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 Workspace getWorkspace() {
1729 return mWorkspace;
1730 }
1731
Joe Onorato7404ee42009-07-31 11:54:44 -07001732 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 GridView getApplicationsGrid() {
1734 return mAllAppsGrid;
1735 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001736 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737
1738 @Override
1739 protected Dialog onCreateDialog(int id) {
1740 switch (id) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001741 case DIALOG_ALL_APPS:
1742 return mAllAppsDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 case DIALOG_CREATE_SHORTCUT:
1744 return new CreateShortcut().createDialog();
1745 case DIALOG_RENAME_FOLDER:
1746 return new RenameFolder().createDialog();
1747 }
1748
1749 return super.onCreateDialog(id);
1750 }
1751
1752 @Override
1753 protected void onPrepareDialog(int id, Dialog dialog) {
1754 switch (id) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001755 case DIALOG_ALL_APPS:
Joe Onorato7404ee42009-07-31 11:54:44 -07001756 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 break;
1759 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001760 if (mFolderInfo != null) {
1761 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1762 final CharSequence text = mFolderInfo.title;
1763 input.setText(text);
1764 input.setSelection(0, text.length());
1765 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 break;
1767 }
1768 }
1769
1770 void showRenameDialog(FolderInfo info) {
1771 mFolderInfo = info;
1772 mWaitingForResult = true;
1773 showDialog(DIALOG_RENAME_FOLDER);
1774 }
1775
1776 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1777 mAddItemCellInfo = cellInfo;
1778 mWaitingForResult = true;
1779 showDialog(DIALOG_CREATE_SHORTCUT);
1780 }
1781
Romain Guy73b979d2009-06-09 12:57:21 -07001782 private void pickShortcut(int requestCode, int title) {
1783 Bundle bundle = new Bundle();
1784
1785 ArrayList<String> shortcutNames = new ArrayList<String>();
1786 shortcutNames.add(getString(R.string.group_applications));
1787 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1788
1789 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1790 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1791 R.drawable.ic_launcher_application));
1792 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1793
1794 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1795 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1796 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1797 pickIntent.putExtras(bundle);
1798
1799 startActivityForResult(pickIntent, requestCode);
1800 }
1801
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 private class RenameFolder {
1803 private EditText mInput;
1804
1805 Dialog createDialog() {
1806 mWaitingForResult = true;
1807 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1808 mInput = (EditText) layout.findViewById(R.id.folder_name);
1809
1810 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1811 builder.setIcon(0);
1812 builder.setTitle(getString(R.string.rename_folder_title));
1813 builder.setCancelable(true);
1814 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1815 public void onCancel(DialogInterface dialog) {
1816 cleanup();
1817 }
1818 });
1819 builder.setNegativeButton(getString(R.string.cancel_action),
1820 new Dialog.OnClickListener() {
1821 public void onClick(DialogInterface dialog, int which) {
1822 cleanup();
1823 }
1824 }
1825 );
1826 builder.setPositiveButton(getString(R.string.rename_action),
1827 new Dialog.OnClickListener() {
1828 public void onClick(DialogInterface dialog, int which) {
1829 changeFolderName();
1830 }
1831 }
1832 );
1833 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001834
1835 final AlertDialog dialog = builder.create();
1836 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1837 public void onShow(DialogInterface dialog) {
1838 mWorkspace.lock();
1839 }
1840 });
1841
1842 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 }
1844
1845 private void changeFolderName() {
1846 final String name = mInput.getText().toString();
1847 if (!TextUtils.isEmpty(name)) {
1848 // Make sure we have the right folder info
1849 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1850 mFolderInfo.title = name;
1851 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1852
1853 if (mDesktopLocked) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001854 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 sModel.loadUserItems(false, Launcher.this, false, false);
1856 } else {
1857 final FolderIcon folderIcon = (FolderIcon)
1858 mWorkspace.getViewForTag(mFolderInfo);
1859 if (folderIcon != null) {
1860 folderIcon.setText(name);
1861 getWorkspace().requestLayout();
1862 } else {
1863 mDesktopLocked = true;
Joe Onorato7404ee42009-07-31 11:54:44 -07001864 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 sModel.loadUserItems(false, Launcher.this, false, false);
1866 }
1867 }
1868 }
1869 cleanup();
1870 }
1871
1872 private void cleanup() {
1873 mWorkspace.unlock();
1874 dismissDialog(DIALOG_RENAME_FOLDER);
1875 mWaitingForResult = false;
1876 mFolderInfo = null;
1877 }
1878 }
1879
1880 /**
Joe Onorato7404ee42009-07-31 11:54:44 -07001881 * Holds the 3d all apps view.
1882 */
1883 private class AllAppsDialog extends Dialog implements DialogInterface.OnCancelListener,
1884 DialogInterface.OnDismissListener, DialogInterface.OnShowListener {
1885
1886 boolean isOpen;
1887
1888 AllAppsDialog(Context context) {
1889 super(context, android.R.style.Theme_Translucent_NoTitleBar);
1890
1891 setOnCancelListener(this);
1892 setOnDismissListener(this);
1893 setOnShowListener(this);
1894
1895 setContentView(R.layout.all_apps);
1896 AllAppsGridView grid = mAllAppsGrid = (AllAppsGridView)findViewById(R.id.all_apps);
1897
1898 grid.setTextFilterEnabled(false);
1899 // TODO grid.setDragger(dragLayer);
1900 grid.setLauncher(Launcher.this);
1901 }
1902
1903 public void onCancel(DialogInterface dialog) {
1904 onDestroy();
1905 }
1906
1907 public void onDismiss(DialogInterface dialog) {
1908 onDestroy();
1909 }
1910
1911 public void onShow(DialogInterface dialog) {
1912 }
1913
1914 private void onDestroy() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001915 }
1916
1917 void lock() {
1918 // TODO
1919 }
1920
1921 void unlock() {
1922 // TODO
1923 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001924
1925 @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
1926 switch (keyCode) {
1927 case KeyEvent.KEYCODE_BACK:
1928 closeAllAppsDialog(true);
1929 return true;
1930 default:
1931 return super.onKeyDown(keyCode, event);
1932 }
1933 }
1934 }
1935
1936 void showAllAppsDialog() {
1937 mAllAppsDialog.isOpen = true;
1938 showDialog(DIALOG_ALL_APPS);
1939 mWorkspace.hide();
1940 }
1941
1942 private void closeAllAppsDialog(boolean animated) {
1943 if (mAllAppsDialog.isOpen) {
1944 if (animated) {
1945 // TODO mDrawer.animateClose();
1946 mAllAppsDialog.dismiss();
1947 } else {
1948 mAllAppsDialog.dismiss();
1949 }
1950 mAllAppsDialog.isOpen = false;
1951 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1952 mWorkspace.show();
1953 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001954 }
1955
1956 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 * Displays the shortcut creation dialog and launches, if necessary, the
1958 * appropriate activity.
1959 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001960 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001961 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1962 DialogInterface.OnShowListener {
1963
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 Dialog createDialog() {
1967 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1972 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001973 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001974
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 builder.setInverseBackgroundForced(true);
1976
1977 AlertDialog dialog = builder.create();
1978 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001979 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001980 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 return dialog;
1983 }
1984
1985 public void onCancel(DialogInterface dialog) {
1986 mWaitingForResult = false;
1987 cleanup();
1988 }
1989
Romain Guycbb89e42009-06-08 15:52:54 -07001990 public void onDismiss(DialogInterface dialog) {
1991 mWorkspace.unlock();
1992 }
1993
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 private void cleanup() {
1995 mWorkspace.unlock();
1996 dismissDialog(DIALOG_CREATE_SHORTCUT);
1997 }
1998
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001999 /**
2000 * Handle the action clicked in the "Add to home" dialog.
2001 */
2002 public void onClick(DialogInterface dialog, int which) {
2003 Resources res = getResources();
2004 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002005
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002006 switch (which) {
2007 case AddAdapter.ITEM_SHORTCUT: {
2008 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002009 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002010 break;
2011 }
Romain Guycbb89e42009-06-08 15:52:54 -07002012
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002013 case AddAdapter.ITEM_APPWIDGET: {
2014 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002015
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002016 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2017 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2018 // add the search widget
2019 ArrayList<AppWidgetProviderInfo> customInfo =
2020 new ArrayList<AppWidgetProviderInfo>();
2021 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2022 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2023 info.label = getString(R.string.group_search);
2024 info.icon = R.drawable.ic_search_widget;
2025 customInfo.add(info);
2026 pickIntent.putParcelableArrayListExtra(
2027 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2028 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2029 Bundle b = new Bundle();
2030 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2031 customExtras.add(b);
2032 pickIntent.putParcelableArrayListExtra(
2033 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2034 // start the pick activity
2035 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2036 break;
2037 }
Romain Guycbb89e42009-06-08 15:52:54 -07002038
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002039 case AddAdapter.ITEM_LIVE_FOLDER: {
2040 // Insert extra item to handle inserting folder
2041 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002042
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002043 ArrayList<String> shortcutNames = new ArrayList<String>();
2044 shortcutNames.add(res.getString(R.string.group_folder));
2045 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002046
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002047 ArrayList<ShortcutIconResource> shortcutIcons =
2048 new ArrayList<ShortcutIconResource>();
2049 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2050 R.drawable.ic_launcher_folder));
2051 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2052
2053 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2054 pickIntent.putExtra(Intent.EXTRA_INTENT,
2055 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2056 pickIntent.putExtra(Intent.EXTRA_TITLE,
2057 getText(R.string.title_select_live_folder));
2058 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002059
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002060 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2061 break;
2062 }
2063
2064 case AddAdapter.ITEM_WALLPAPER: {
2065 startWallpaper();
2066 break;
2067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 }
2069 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002070
2071 public void onShow(DialogInterface dialog) {
2072 mWorkspace.lock();
2073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
2075
2076 /**
2077 * Receives notifications when applications are added/removed.
2078 */
2079 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2080 @Override
2081 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002082 final String action = intent.getAction();
2083 final String packageName = intent.getData().getSchemeSpecificPart();
2084 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2085
2086 if (LauncherModel.DEBUG_LOADERS) {
2087 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2088 ", replacing=" + replacing);
2089 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002091
2092 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2093 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2094 if (!replacing) {
2095 removeShortcutsForPackage(packageName);
2096 if (LauncherModel.DEBUG_LOADERS) {
2097 d(LauncherModel.LOG_TAG, " --> remove package");
2098 }
2099 sModel.removePackage(Launcher.this, packageName);
2100 }
2101 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2102 // later, we will update the package at this time
2103 } else {
2104 if (!replacing) {
2105 if (LauncherModel.DEBUG_LOADERS) {
2106 d(LauncherModel.LOG_TAG, " --> add package");
2107 }
2108 sModel.addPackage(Launcher.this, packageName);
2109 } else {
2110 if (LauncherModel.DEBUG_LOADERS) {
2111 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2112 }
2113 sModel.updatePackage(Launcher.this, packageName);
2114 updateShortcutsForPackage(packageName);
2115 }
2116 }
2117 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002119 if (LauncherModel.DEBUG_LOADERS) {
2120 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2121 }
2122 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 }
2124 }
2125 }
2126
2127 /**
2128 * Receives notifications whenever the user favorites have changed.
2129 */
2130 private class FavoritesChangeObserver extends ContentObserver {
2131 public FavoritesChangeObserver() {
2132 super(new Handler());
2133 }
2134
2135 @Override
2136 public void onChange(boolean selfChange) {
2137 onFavoritesChanged();
2138 }
2139 }
2140
2141 /**
2142 * Receives intents from other applications to change the wallpaper.
2143 */
2144 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2145 private final Application mApplication;
2146 private WeakReference<Launcher> mLauncher;
2147
2148 WallpaperIntentReceiver(Application application, Launcher launcher) {
2149 mApplication = application;
2150 setLauncher(launcher);
2151 }
2152
2153 void setLauncher(Launcher launcher) {
2154 mLauncher = new WeakReference<Launcher>(launcher);
2155 }
2156
2157 @Override
2158 public void onReceive(Context context, Intent intent) {
2159 // Load the wallpaper from the ApplicationContext and store it locally
2160 // until the Launcher Activity is ready to use it
2161 final Drawable drawable = mApplication.getWallpaper();
2162 if (drawable instanceof BitmapDrawable) {
2163 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2164 } else {
2165 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2166 }
2167
2168 // If Launcher is alive, notify we have a new wallpaper
2169 if (mLauncher != null) {
2170 final Launcher launcher = mLauncher.get();
2171 if (launcher != null) {
2172 launcher.loadWallpaper();
2173 }
2174 }
2175 }
2176 }
2177
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002178 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002180 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002181 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 // Number of items to bind in every pass
2184 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185
2186 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002187 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002188 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002190
Karl Rosaen138a0412009-04-23 19:00:21 -07002191 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192
2193 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002194 ArrayList<LauncherAppWidgetInfo> appWidgets,
2195 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196
2197 mLauncher = new WeakReference<Launcher>(launcher);
2198 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002199 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002200
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002201 // Sort widgets so active workspace is bound first
2202 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2203 final int size = appWidgets.size();
2204 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002205
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002206 for (int i = 0; i < size; i++) {
2207 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2208 if (appWidgetInfo.screen == currentScreen) {
2209 mAppWidgets.addFirst(appWidgetInfo);
2210 } else {
2211 mAppWidgets.addLast(appWidgetInfo);
2212 }
2213 }
2214 }
Romain Guycbb89e42009-06-08 15:52:54 -07002215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 public void startBindingItems() {
2217 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2218 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002219
2220 public void startBindingDrawer() {
2221 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2222 }
Romain Guycbb89e42009-06-08 15:52:54 -07002223
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002224 public void startBindingAppWidgetsWhenIdle() {
2225 // Ask for notification when message queue becomes idle
2226 final MessageQueue messageQueue = Looper.myQueue();
2227 messageQueue.addIdleHandler(this);
2228 }
Romain Guycbb89e42009-06-08 15:52:54 -07002229
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002230 public boolean queueIdle() {
2231 // Queue is idle, so start binding items
2232 startBindingAppWidgets();
2233 return false;
2234 }
2235
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002236 public void startBindingAppWidgets() {
2237 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2238 }
2239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 @Override
2241 public void handleMessage(Message msg) {
2242 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002243 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 return;
2245 }
Romain Guycbb89e42009-06-08 15:52:54 -07002246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247 switch (msg.what) {
2248 case MESSAGE_BIND_ITEMS: {
2249 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2250 break;
2251 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002252 case MESSAGE_BIND_DRAWER: {
2253 launcher.bindDrawer(this, mDrawerAdapter);
2254 break;
2255 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002256 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002257 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 break;
2259 }
2260 }
2261 }
2262 }
2263}