blob: 7e42b8d986107c1870cf5cbce833c692c5b963fb [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();
Romain Guycbb89e42009-06-08 15:52:54 -0700444 closeDrawer(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
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 closeDrawer();
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 {
835 closeDrawer(false);
836 }
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
925 closeDrawer(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) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001331 closeDrawer();
1332 } 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
1347 private void closeDrawer() {
1348 closeDrawer(true);
1349 }
1350
1351 private void closeDrawer(boolean animated) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001352 if (mAllAppsDialog.isOpen) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 if (animated) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001354 // TODO mDrawer.animateClose();
1355 mAllAppsDialog.dismiss();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 } else {
Joe Onorato7404ee42009-07-31 11:54:44 -07001357 mAllAppsDialog.dismiss();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001359 if (false /* TODO mDrawer.hasFocus() */) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1361 }
1362 }
1363 }
1364
1365 private void closeFolder() {
1366 Folder folder = mWorkspace.getOpenFolder();
1367 if (folder != null) {
1368 closeFolder(folder);
1369 }
1370 }
1371
1372 void closeFolder(Folder folder) {
1373 folder.getInfo().opened = false;
1374 ViewGroup parent = (ViewGroup) folder.getParent();
1375 if (parent != null) {
1376 parent.removeView(folder);
1377 }
1378 folder.onClose();
1379 }
1380
1381 /**
1382 * When the notification that favorites have changed is received, requests
1383 * a favorites list refresh.
1384 */
1385 private void onFavoritesChanged() {
1386 mDesktopLocked = true;
Joe Onorato7404ee42009-07-31 11:54:44 -07001387 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 sModel.loadUserItems(false, this, false, false);
1389 }
1390
1391 void onDesktopItemsLoaded() {
1392 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 bindDesktopItems();
1394 }
Romain Guycbb89e42009-06-08 15:52:54 -07001395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 /**
1397 * Refreshes the shortcuts shown on the workspace.
1398 */
1399 private void bindDesktopItems() {
1400 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001401 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001402 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1403 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 return;
1405 }
1406
1407 final Workspace workspace = mWorkspace;
1408 int count = workspace.getChildCount();
1409 for (int i = 0; i < count; i++) {
1410 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1411 }
Romain Guycbb89e42009-06-08 15:52:54 -07001412
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 if (DEBUG_USER_INTERFACE) {
1414 android.widget.Button finishButton = new android.widget.Button(this);
1415 finishButton.setText("Finish");
1416 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1417
1418 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1419 public void onClick(View v) {
1420 finish();
1421 }
1422 });
1423 }
Romain Guycbb89e42009-06-08 15:52:54 -07001424
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001425 // Flag any old binder to terminate early
1426 if (mBinder != null) {
1427 mBinder.mTerminate = true;
1428 }
Romain Guycbb89e42009-06-08 15:52:54 -07001429
Karl Rosaen138a0412009-04-23 19:00:21 -07001430 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001431 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 }
1433
1434 private void bindItems(Launcher.DesktopBinder binder,
1435 ArrayList<ItemInfo> shortcuts, int start, int count) {
1436
1437 final Workspace workspace = mWorkspace;
1438 final boolean desktopLocked = mDesktopLocked;
1439
1440 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1441 int i = start;
1442
1443 for ( ; i < end; i++) {
1444 final ItemInfo item = shortcuts.get(i);
1445 switch (item.itemType) {
1446 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1447 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1448 final View shortcut = createShortcut((ApplicationInfo) item);
1449 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1450 !desktopLocked);
1451 break;
1452 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1453 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1454 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1455 (UserFolderInfo) item);
1456 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1457 !desktopLocked);
1458 break;
1459 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1460 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1461 R.layout.live_folder_icon, this,
1462 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1463 (LiveFolderInfo) item);
1464 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1465 !desktopLocked);
1466 break;
1467 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1468 final int screen = workspace.getCurrentScreen();
1469 final View view = mInflater.inflate(R.layout.widget_search,
1470 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001471
Karl Rosaen138a0412009-04-23 19:00:21 -07001472 Search search = (Search) view.findViewById(R.id.widget_search);
1473 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 final Widget widget = (Widget) item;
1476 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 workspace.addWidget(view, widget, !desktopLocked);
1479 break;
1480 }
1481 }
1482
1483 workspace.requestLayout();
1484
1485 if (end >= count) {
1486 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001487 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 } else {
1489 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1490 }
1491 }
1492
1493 private void finishBindDesktopItems() {
1494 if (mSavedState != null) {
1495 if (!mWorkspace.hasFocus()) {
1496 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1497 }
1498
1499 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1500 if (userFolders != null) {
1501 for (long folderId : userFolders) {
1502 final FolderInfo info = sModel.findFolderById(folderId);
1503 if (info != null) {
1504 openFolder(info);
1505 }
1506 }
1507 final Folder openFolder = mWorkspace.getOpenFolder();
1508 if (openFolder != null) {
1509 openFolder.requestFocus();
1510 }
1511 }
1512
1513 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1514 if (allApps) {
Joe Onorato45d43462009-07-31 14:36:51 -07001515 showAllAppsDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
1517
1518 mSavedState = null;
1519 }
1520
1521 if (mSavedInstanceState != null) {
1522 super.onRestoreInstanceState(mSavedInstanceState);
1523 mSavedInstanceState = null;
1524 }
1525
Joe Onorato7404ee42009-07-31 11:54:44 -07001526 /* TODO
1527 if (mAllAppsDialog.isOpen && !mDrawer.hasFocus()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 mDrawer.requestFocus();
1529 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001530 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531
1532 mDesktopLocked = false;
Joe Onorato7404ee42009-07-31 11:54:44 -07001533 mAllAppsDialog.unlock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
Romain Guycbb89e42009-06-08 15:52:54 -07001535
Karl Rosaen138a0412009-04-23 19:00:21 -07001536 private void bindDrawer(Launcher.DesktopBinder binder,
1537 ApplicationsAdapter drawerAdapter) {
1538 mAllAppsGrid.setAdapter(drawerAdapter);
1539 binder.startBindingAppWidgetsWhenIdle();
1540 }
Romain Guycbb89e42009-06-08 15:52:54 -07001541
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001542 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001543 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001544
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 final Workspace workspace = mWorkspace;
1546 final boolean desktopLocked = mDesktopLocked;
1547
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001548 if (!appWidgets.isEmpty()) {
1549 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001550
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001551 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001552 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001553 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001554
Karl Rosaen138a0412009-04-23 19:00:21 -07001555 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001556
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001557 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1558 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001559
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001560 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1561 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001562
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001563 workspace.requestLayout();
1564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001566 if (appWidgets.isEmpty()) {
1567 if (PROFILE_ROTATE) {
1568 android.os.Debug.stopMethodTracing();
1569 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001571 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
1573 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 DragController getDragController() {
1576 return mDragLayer;
1577 }
1578
1579 /**
1580 * Launches the intent referred by the clicked shortcut.
1581 *
1582 * @param v The view representing the clicked shortcut.
1583 */
1584 public void onClick(View v) {
1585 Object tag = v.getTag();
1586 if (tag instanceof ApplicationInfo) {
1587 // Open shortcut
1588 final Intent intent = ((ApplicationInfo) tag).intent;
1589 startActivitySafely(intent);
1590 } else if (tag instanceof FolderInfo) {
1591 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001592 } else if (v == mHandleView) {
1593 if (mAllAppsDialog.isOpen) {
1594 // TODO how can we be here?
1595 } else {
Joe Onorato45d43462009-07-31 14:36:51 -07001596 showAllAppsDialog();
Joe Onorato7404ee42009-07-31 11:54:44 -07001597 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
1599 }
1600
1601 void startActivitySafely(Intent intent) {
1602 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1603 try {
1604 startActivity(intent);
1605 } catch (ActivityNotFoundException e) {
1606 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1607 } catch (SecurityException e) {
1608 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001609 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1611 "or use the exported attribute for this activity.", e);
1612 }
1613 }
1614
1615 private void handleFolderClick(FolderInfo folderInfo) {
1616 if (!folderInfo.opened) {
1617 // Close any open folder
1618 closeFolder();
1619 // Open the requested folder
1620 openFolder(folderInfo);
1621 } else {
1622 // Find the open folder...
1623 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1624 int folderScreen;
1625 if (openFolder != null) {
1626 folderScreen = mWorkspace.getScreenForView(openFolder);
1627 // .. and close it
1628 closeFolder(openFolder);
1629 if (folderScreen != mWorkspace.getCurrentScreen()) {
1630 // Close any folder open on the current screen
1631 closeFolder();
1632 // Pull the folder onto this screen
1633 openFolder(folderInfo);
1634 }
1635 }
1636 }
1637 }
1638
1639 private void loadWallpaper() {
1640 // The first time the application is started, we load the wallpaper from
1641 // the ApplicationContext
1642 if (sWallpaper == null) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001643 final Drawable drawable;
1644 if (false) {
1645 drawable = getWallpaper();
1646 } else {
1647 drawable = getResources().getDrawable(R.drawable.wallpaper_path);
1648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 if (drawable instanceof BitmapDrawable) {
1650 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1651 } else {
1652 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1653 }
1654 }
Joe Onorato59791172009-07-31 16:21:40 -07001655 mWallpaperView.loadWallpaper(sWallpaper);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 }
1657
1658 /**
1659 * Opens the user fodler described by the specified tag. The opening of the folder
1660 * is animated relative to the specified View. If the View is null, no animation
1661 * is played.
1662 *
1663 * @param folderInfo The FolderInfo describing the folder to open.
1664 */
1665 private void openFolder(FolderInfo folderInfo) {
1666 Folder openFolder;
1667
1668 if (folderInfo instanceof UserFolderInfo) {
1669 openFolder = UserFolder.fromXml(this);
1670 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001671 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 } else {
1673 return;
1674 }
1675
1676 openFolder.setDragger(mDragLayer);
1677 openFolder.setLauncher(this);
1678
1679 openFolder.bind(folderInfo);
1680 folderInfo.opened = true;
1681
1682 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1683 openFolder.onOpen();
1684 }
1685
1686 /**
1687 * Returns true if the workspace is being loaded. When the workspace is loading,
1688 * no user interaction should be allowed to avoid any conflict.
1689 *
1690 * @return True if the workspace is locked, false otherwise.
1691 */
1692 boolean isWorkspaceLocked() {
1693 return mDesktopLocked;
1694 }
1695
1696 public boolean onLongClick(View v) {
1697 if (mDesktopLocked) {
1698 return false;
1699 }
1700
1701 if (!(v instanceof CellLayout)) {
1702 v = (View) v.getParent();
1703 }
1704
1705 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1706
1707 // This happens when long clicking an item with the dpad/trackball
1708 if (cellInfo == null) {
1709 return true;
1710 }
1711
1712 if (mWorkspace.allowLongPress()) {
1713 if (cellInfo.cell == null) {
1714 if (cellInfo.valid) {
1715 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001716 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 showAddDialog(cellInfo);
1718 }
1719 } else {
1720 if (!(cellInfo.cell instanceof Folder)) {
1721 // User long pressed on an item
1722 mWorkspace.startDrag(cellInfo);
1723 }
1724 }
1725 }
1726 return true;
1727 }
1728
1729 static LauncherModel getModel() {
1730 return sModel;
1731 }
1732
1733 void closeAllApplications() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001734 // TODO mDrawer.close();
1735 mAllAppsDialog.dismiss();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 }
1737
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001738 View getDrawerHandle() {
1739 return mHandleView;
1740 }
1741
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 boolean isDrawerDown() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001743 return /* TODO !mDrawer.isMoving() && */ !mAllAppsDialog.isOpen;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744 }
1745
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 Workspace getWorkspace() {
1747 return mWorkspace;
1748 }
1749
Joe Onorato7404ee42009-07-31 11:54:44 -07001750 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 GridView getApplicationsGrid() {
1752 return mAllAppsGrid;
1753 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001754 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755
1756 @Override
1757 protected Dialog onCreateDialog(int id) {
1758 switch (id) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001759 case DIALOG_ALL_APPS:
1760 return mAllAppsDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 case DIALOG_CREATE_SHORTCUT:
1762 return new CreateShortcut().createDialog();
1763 case DIALOG_RENAME_FOLDER:
1764 return new RenameFolder().createDialog();
1765 }
1766
1767 return super.onCreateDialog(id);
1768 }
1769
1770 @Override
1771 protected void onPrepareDialog(int id, Dialog dialog) {
1772 switch (id) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001773 case DIALOG_ALL_APPS:
Joe Onorato7404ee42009-07-31 11:54:44 -07001774 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 break;
1777 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001778 if (mFolderInfo != null) {
1779 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1780 final CharSequence text = mFolderInfo.title;
1781 input.setText(text);
1782 input.setSelection(0, text.length());
1783 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 break;
1785 }
1786 }
1787
Joe Onorato45d43462009-07-31 14:36:51 -07001788 void showAllAppsDialog() {
1789 mAllAppsDialog.isOpen = true;
1790 showDialog(DIALOG_ALL_APPS);
1791 }
1792
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 void showRenameDialog(FolderInfo info) {
1794 mFolderInfo = info;
1795 mWaitingForResult = true;
1796 showDialog(DIALOG_RENAME_FOLDER);
1797 }
1798
1799 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1800 mAddItemCellInfo = cellInfo;
1801 mWaitingForResult = true;
1802 showDialog(DIALOG_CREATE_SHORTCUT);
1803 }
1804
Romain Guy73b979d2009-06-09 12:57:21 -07001805 private void pickShortcut(int requestCode, int title) {
1806 Bundle bundle = new Bundle();
1807
1808 ArrayList<String> shortcutNames = new ArrayList<String>();
1809 shortcutNames.add(getString(R.string.group_applications));
1810 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1811
1812 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1813 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1814 R.drawable.ic_launcher_application));
1815 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1816
1817 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1818 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1819 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1820 pickIntent.putExtras(bundle);
1821
1822 startActivityForResult(pickIntent, requestCode);
1823 }
1824
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 private class RenameFolder {
1826 private EditText mInput;
1827
1828 Dialog createDialog() {
1829 mWaitingForResult = true;
1830 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1831 mInput = (EditText) layout.findViewById(R.id.folder_name);
1832
1833 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1834 builder.setIcon(0);
1835 builder.setTitle(getString(R.string.rename_folder_title));
1836 builder.setCancelable(true);
1837 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1838 public void onCancel(DialogInterface dialog) {
1839 cleanup();
1840 }
1841 });
1842 builder.setNegativeButton(getString(R.string.cancel_action),
1843 new Dialog.OnClickListener() {
1844 public void onClick(DialogInterface dialog, int which) {
1845 cleanup();
1846 }
1847 }
1848 );
1849 builder.setPositiveButton(getString(R.string.rename_action),
1850 new Dialog.OnClickListener() {
1851 public void onClick(DialogInterface dialog, int which) {
1852 changeFolderName();
1853 }
1854 }
1855 );
1856 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001857
1858 final AlertDialog dialog = builder.create();
1859 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1860 public void onShow(DialogInterface dialog) {
1861 mWorkspace.lock();
1862 }
1863 });
1864
1865 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
1867
1868 private void changeFolderName() {
1869 final String name = mInput.getText().toString();
1870 if (!TextUtils.isEmpty(name)) {
1871 // Make sure we have the right folder info
1872 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1873 mFolderInfo.title = name;
1874 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1875
1876 if (mDesktopLocked) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001877 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 sModel.loadUserItems(false, Launcher.this, false, false);
1879 } else {
1880 final FolderIcon folderIcon = (FolderIcon)
1881 mWorkspace.getViewForTag(mFolderInfo);
1882 if (folderIcon != null) {
1883 folderIcon.setText(name);
1884 getWorkspace().requestLayout();
1885 } else {
1886 mDesktopLocked = true;
Joe Onorato7404ee42009-07-31 11:54:44 -07001887 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 sModel.loadUserItems(false, Launcher.this, false, false);
1889 }
1890 }
1891 }
1892 cleanup();
1893 }
1894
1895 private void cleanup() {
1896 mWorkspace.unlock();
1897 dismissDialog(DIALOG_RENAME_FOLDER);
1898 mWaitingForResult = false;
1899 mFolderInfo = null;
1900 }
1901 }
1902
1903 /**
Joe Onorato7404ee42009-07-31 11:54:44 -07001904 * Holds the 3d all apps view.
1905 */
1906 private class AllAppsDialog extends Dialog implements DialogInterface.OnCancelListener,
1907 DialogInterface.OnDismissListener, DialogInterface.OnShowListener {
1908
1909 boolean isOpen;
1910
1911 AllAppsDialog(Context context) {
1912 super(context, android.R.style.Theme_Translucent_NoTitleBar);
1913
1914 setOnCancelListener(this);
1915 setOnDismissListener(this);
1916 setOnShowListener(this);
1917
1918 setContentView(R.layout.all_apps);
1919 AllAppsGridView grid = mAllAppsGrid = (AllAppsGridView)findViewById(R.id.all_apps);
1920
1921 grid.setTextFilterEnabled(false);
1922 // TODO grid.setDragger(dragLayer);
1923 grid.setLauncher(Launcher.this);
1924 }
1925
1926 public void onCancel(DialogInterface dialog) {
1927 onDestroy();
1928 }
1929
1930 public void onDismiss(DialogInterface dialog) {
1931 onDestroy();
1932 }
1933
1934 public void onShow(DialogInterface dialog) {
1935 }
1936
1937 private void onDestroy() {
1938 this.isOpen = false;
1939 }
1940
1941 void lock() {
1942 // TODO
1943 }
1944
1945 void unlock() {
1946 // TODO
1947 }
1948 }
1949
1950 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 * Displays the shortcut creation dialog and launches, if necessary, the
1952 * appropriate activity.
1953 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001954 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001955 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1956 DialogInterface.OnShowListener {
1957
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 Dialog createDialog() {
1961 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001962
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001964
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1966 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001967 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 builder.setInverseBackgroundForced(true);
1970
1971 AlertDialog dialog = builder.create();
1972 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001973 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001974 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 return dialog;
1977 }
1978
1979 public void onCancel(DialogInterface dialog) {
1980 mWaitingForResult = false;
1981 cleanup();
1982 }
1983
Romain Guycbb89e42009-06-08 15:52:54 -07001984 public void onDismiss(DialogInterface dialog) {
1985 mWorkspace.unlock();
1986 }
1987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 private void cleanup() {
1989 mWorkspace.unlock();
1990 dismissDialog(DIALOG_CREATE_SHORTCUT);
1991 }
1992
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001993 /**
1994 * Handle the action clicked in the "Add to home" dialog.
1995 */
1996 public void onClick(DialogInterface dialog, int which) {
1997 Resources res = getResources();
1998 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001999
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002000 switch (which) {
2001 case AddAdapter.ITEM_SHORTCUT: {
2002 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002003 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002004 break;
2005 }
Romain Guycbb89e42009-06-08 15:52:54 -07002006
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002007 case AddAdapter.ITEM_APPWIDGET: {
2008 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002009
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002010 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2011 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2012 // add the search widget
2013 ArrayList<AppWidgetProviderInfo> customInfo =
2014 new ArrayList<AppWidgetProviderInfo>();
2015 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2016 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2017 info.label = getString(R.string.group_search);
2018 info.icon = R.drawable.ic_search_widget;
2019 customInfo.add(info);
2020 pickIntent.putParcelableArrayListExtra(
2021 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2022 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2023 Bundle b = new Bundle();
2024 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2025 customExtras.add(b);
2026 pickIntent.putParcelableArrayListExtra(
2027 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2028 // start the pick activity
2029 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2030 break;
2031 }
Romain Guycbb89e42009-06-08 15:52:54 -07002032
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002033 case AddAdapter.ITEM_LIVE_FOLDER: {
2034 // Insert extra item to handle inserting folder
2035 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002036
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002037 ArrayList<String> shortcutNames = new ArrayList<String>();
2038 shortcutNames.add(res.getString(R.string.group_folder));
2039 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002040
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002041 ArrayList<ShortcutIconResource> shortcutIcons =
2042 new ArrayList<ShortcutIconResource>();
2043 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2044 R.drawable.ic_launcher_folder));
2045 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2046
2047 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2048 pickIntent.putExtra(Intent.EXTRA_INTENT,
2049 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2050 pickIntent.putExtra(Intent.EXTRA_TITLE,
2051 getText(R.string.title_select_live_folder));
2052 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002053
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002054 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2055 break;
2056 }
2057
2058 case AddAdapter.ITEM_WALLPAPER: {
2059 startWallpaper();
2060 break;
2061 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
2063 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002064
2065 public void onShow(DialogInterface dialog) {
2066 mWorkspace.lock();
2067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 }
2069
2070 /**
2071 * Receives notifications when applications are added/removed.
2072 */
2073 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2074 @Override
2075 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002076 final String action = intent.getAction();
2077 final String packageName = intent.getData().getSchemeSpecificPart();
2078 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2079
2080 if (LauncherModel.DEBUG_LOADERS) {
2081 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2082 ", replacing=" + replacing);
2083 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002085
2086 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2087 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2088 if (!replacing) {
2089 removeShortcutsForPackage(packageName);
2090 if (LauncherModel.DEBUG_LOADERS) {
2091 d(LauncherModel.LOG_TAG, " --> remove package");
2092 }
2093 sModel.removePackage(Launcher.this, packageName);
2094 }
2095 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2096 // later, we will update the package at this time
2097 } else {
2098 if (!replacing) {
2099 if (LauncherModel.DEBUG_LOADERS) {
2100 d(LauncherModel.LOG_TAG, " --> add package");
2101 }
2102 sModel.addPackage(Launcher.this, packageName);
2103 } else {
2104 if (LauncherModel.DEBUG_LOADERS) {
2105 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2106 }
2107 sModel.updatePackage(Launcher.this, packageName);
2108 updateShortcutsForPackage(packageName);
2109 }
2110 }
2111 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002113 if (LauncherModel.DEBUG_LOADERS) {
2114 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2115 }
2116 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 }
2118 }
2119 }
2120
2121 /**
2122 * Receives notifications whenever the user favorites have changed.
2123 */
2124 private class FavoritesChangeObserver extends ContentObserver {
2125 public FavoritesChangeObserver() {
2126 super(new Handler());
2127 }
2128
2129 @Override
2130 public void onChange(boolean selfChange) {
2131 onFavoritesChanged();
2132 }
2133 }
2134
2135 /**
2136 * Receives intents from other applications to change the wallpaper.
2137 */
2138 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2139 private final Application mApplication;
2140 private WeakReference<Launcher> mLauncher;
2141
2142 WallpaperIntentReceiver(Application application, Launcher launcher) {
2143 mApplication = application;
2144 setLauncher(launcher);
2145 }
2146
2147 void setLauncher(Launcher launcher) {
2148 mLauncher = new WeakReference<Launcher>(launcher);
2149 }
2150
2151 @Override
2152 public void onReceive(Context context, Intent intent) {
2153 // Load the wallpaper from the ApplicationContext and store it locally
2154 // until the Launcher Activity is ready to use it
2155 final Drawable drawable = mApplication.getWallpaper();
2156 if (drawable instanceof BitmapDrawable) {
2157 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2158 } else {
2159 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2160 }
2161
2162 // If Launcher is alive, notify we have a new wallpaper
2163 if (mLauncher != null) {
2164 final Launcher launcher = mLauncher.get();
2165 if (launcher != null) {
2166 launcher.loadWallpaper();
2167 }
2168 }
2169 }
2170 }
2171
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002172 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002174 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002175 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 // Number of items to bind in every pass
2178 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179
2180 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002181 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002182 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002184
Karl Rosaen138a0412009-04-23 19:00:21 -07002185 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186
2187 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002188 ArrayList<LauncherAppWidgetInfo> appWidgets,
2189 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190
2191 mLauncher = new WeakReference<Launcher>(launcher);
2192 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002193 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002194
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002195 // Sort widgets so active workspace is bound first
2196 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2197 final int size = appWidgets.size();
2198 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002199
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002200 for (int i = 0; i < size; i++) {
2201 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2202 if (appWidgetInfo.screen == currentScreen) {
2203 mAppWidgets.addFirst(appWidgetInfo);
2204 } else {
2205 mAppWidgets.addLast(appWidgetInfo);
2206 }
2207 }
2208 }
Romain Guycbb89e42009-06-08 15:52:54 -07002209
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 public void startBindingItems() {
2211 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2212 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002213
2214 public void startBindingDrawer() {
2215 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2216 }
Romain Guycbb89e42009-06-08 15:52:54 -07002217
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002218 public void startBindingAppWidgetsWhenIdle() {
2219 // Ask for notification when message queue becomes idle
2220 final MessageQueue messageQueue = Looper.myQueue();
2221 messageQueue.addIdleHandler(this);
2222 }
Romain Guycbb89e42009-06-08 15:52:54 -07002223
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002224 public boolean queueIdle() {
2225 // Queue is idle, so start binding items
2226 startBindingAppWidgets();
2227 return false;
2228 }
2229
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002230 public void startBindingAppWidgets() {
2231 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2232 }
2233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 @Override
2235 public void handleMessage(Message msg) {
2236 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002237 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 return;
2239 }
Romain Guycbb89e42009-06-08 15:52:54 -07002240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 switch (msg.what) {
2242 case MESSAGE_BIND_ITEMS: {
2243 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2244 break;
2245 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002246 case MESSAGE_BIND_DRAWER: {
2247 launcher.bindDrawer(this, mDrawerAdapter);
2248 break;
2249 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002250 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002251 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 break;
2253 }
2254 }
2255 }
2256 }
2257}