blob: fbb1e5f7c85fb0f31cfb3880135bb3d9ca83a0fe [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;
174 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700175
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700176 private AppWidgetManager mAppWidgetManager;
177 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700178
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700179 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private CellLayout.CellInfo mAddItemCellInfo;
182 private CellLayout.CellInfo mMenuAddInfo;
183 private final int[] mCellCoordinates = new int[2];
184 private FolderInfo mFolderInfo;
185
Joe Onorato7404ee42009-07-31 11:54:44 -0700186 private AllAppsDialog mAllAppsDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700188 private HandleView mHandleView;
Joe Onorato7404ee42009-07-31 11:54:44 -0700189 private AllAppsGridView mAllAppsGrid; // TODO: put this into AllAppsDialog
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
191 private boolean mDesktopLocked = true;
192 private Bundle mSavedState;
193
194 private SpannableStringBuilder mDefaultKeySsb = null;
195
196 private boolean mDestroyed;
Mike LeBeau736cf282009-07-02 17:46:59 -0700197
198 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
200 private boolean mRestoring;
201 private boolean mWaitingForResult;
202 private boolean mLocaleChanged;
203
Dianne Hackborn67800862009-07-24 17:15:20 -0700204 private boolean mHomeDown;
205 private boolean mBackDown;
206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private Bundle mSavedInstanceState;
208
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700209 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 @Override
212 protected void onCreate(Bundle savedInstanceState) {
213 super.onCreate(savedInstanceState);
214 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700215
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700216 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700217
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700218 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
219 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 if (PROFILE_STARTUP) {
222 android.os.Debug.startMethodTracing("/sdcard/launcher");
223 }
224
225 checkForLocaleChange();
226 setWallpaperDimension();
227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 setContentView(R.layout.launcher);
229 setupViews();
230
Joe Onorato7404ee42009-07-31 11:54:44 -0700231 mAllAppsDialog = new AllAppsDialog(this);
232 mAllAppsDialog.lock();
233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 registerIntentReceivers();
235 registerContentObservers();
236
237 mSavedState = savedInstanceState;
238 restoreState(mSavedState);
239
240 if (PROFILE_STARTUP) {
241 android.os.Debug.stopMethodTracing();
242 }
243
244 if (!mRestoring) {
245 startLoaders();
246 }
247
248 // For handling default keys
249 mDefaultKeySsb = new SpannableStringBuilder();
250 Selection.setSelection(mDefaultKeySsb, 0);
251 }
Romain Guycbb89e42009-06-08 15:52:54 -0700252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700254 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
255 readConfiguration(this, localeConfiguration);
256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 final Configuration configuration = getResources().getConfiguration();
258
Romain Guy98d01652009-06-30 16:21:04 -0700259 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 final String locale = configuration.locale.toString();
261
Romain Guy98d01652009-06-30 16:21:04 -0700262 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 final int mcc = configuration.mcc;
264
Romain Guy98d01652009-06-30 16:21:04 -0700265 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 final int mnc = configuration.mnc;
267
268 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
269
270 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700271 localeConfiguration.locale = locale;
272 localeConfiguration.mcc = mcc;
273 localeConfiguration.mnc = mnc;
274
275 writeConfiguration(this, localeConfiguration);
276 }
277 }
278
279 private static class LocaleConfiguration {
280 public String locale;
281 public int mcc = -1;
282 public int mnc = -1;
283 }
284
285 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
286 DataInputStream in = null;
287 try {
288 in = new DataInputStream(context.openFileInput(PREFERENCES));
289 configuration.locale = in.readUTF();
290 configuration.mcc = in.readInt();
291 configuration.mnc = in.readInt();
292 } catch (FileNotFoundException e) {
293 // Ignore
294 } catch (IOException e) {
295 // Ignore
296 } finally {
297 if (in != null) {
298 try {
299 in.close();
300 } catch (IOException e) {
301 // Ignore
302 }
303 }
304 }
305 }
306
307 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
308 DataOutputStream out = null;
309 try {
310 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
311 out.writeUTF(configuration.locale);
312 out.writeInt(configuration.mcc);
313 out.writeInt(configuration.mnc);
314 out.flush();
315 } catch (FileNotFoundException e) {
316 // Ignore
317 } catch (IOException e) {
318 //noinspection ResultOfMethodCallIgnored
319 context.getFileStreamPath(PREFERENCES).delete();
320 } finally {
321 if (out != null) {
322 try {
323 out.close();
324 } catch (IOException e) {
325 // Ignore
326 }
327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 }
329 }
330
331 static int getScreen() {
332 synchronized (sLock) {
333 return sScreen;
334 }
335 }
336
337 static void setScreen(int screen) {
338 synchronized (sLock) {
339 sScreen = screen;
340 }
341 }
342
343 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700344 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
345 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
346
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 mRestoring = false;
348 }
349
350 private void setWallpaperDimension() {
351 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
352 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
353
354 Display display = getWindowManager().getDefaultDisplay();
355 boolean isPortrait = display.getWidth() < display.getHeight();
356
357 final int width = isPortrait ? display.getWidth() : display.getHeight();
358 final int height = isPortrait ? display.getHeight() : display.getWidth();
359 try {
360 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
361 } catch (RemoteException e) {
362 // System is dead!
363 }
364 }
365
366 @Override
367 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700368 mWaitingForResult = false;
369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 // The pattern used here is that a user PICKs a specific application,
371 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
374 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700375
Romain Guy94dabf12009-07-21 10:55:43 -0700376 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 switch (requestCode) {
378 case REQUEST_PICK_APPLICATION:
379 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
380 break;
381 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700382 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 break;
384 case REQUEST_CREATE_SHORTCUT:
385 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
386 break;
387 case REQUEST_PICK_LIVE_FOLDER:
388 addLiveFolder(data);
389 break;
390 case REQUEST_CREATE_LIVE_FOLDER:
391 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
392 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700393 case REQUEST_PICK_APPWIDGET:
394 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700396 case REQUEST_CREATE_APPWIDGET:
397 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 break;
399 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700400 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700402 // Clean up the appWidgetId if we canceled
403 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
404 if (appWidgetId != -1) {
405 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 }
407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 }
409
410 @Override
411 protected void onResume() {
412 super.onResume();
413
414 if (mRestoring) {
415 startLoaders();
416 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700417
418 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
419 // onNewIntent), then close the search dialog if needed, because it probably
420 // came from the user pressing 'home' (rather than, for example, pressing 'back').
421 if (mIsNewIntent) {
422 // Post to a handler so that this happens after the search dialog tries to open
423 // itself again.
424 mWorkspace.post(new Runnable() {
425 public void run() {
426 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
427 ServiceManager.getService(Context.SEARCH_SERVICE));
428 try {
429 searchManagerService.stopSearch();
430 } catch (RemoteException e) {
431 e(LOG_TAG, "error stopping search", e);
432 }
433 }
434 });
435 }
436
437 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 }
439
440 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700441 protected void onPause() {
442 super.onPause();
Romain Guycbb89e42009-06-08 15:52:54 -0700443 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700444 }
Romain Guycbb89e42009-06-08 15:52:54 -0700445
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700446 @Override
447 public Object onRetainNonConfigurationInstance() {
448 // Flag any binder to stop early before switching
449 if (mBinder != null) {
450 mBinder.mTerminate = true;
451 }
Romain Guycbb89e42009-06-08 15:52:54 -0700452
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700453 if (PROFILE_ROTATE) {
454 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
455 }
456 return null;
457 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700458
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 private boolean acceptFilter() {
460 final InputMethodManager inputManager = (InputMethodManager)
461 getSystemService(Context.INPUT_METHOD_SERVICE);
462 return !inputManager.isFullscreenMode();
463 }
464
465 @Override
466 public boolean onKeyDown(int keyCode, KeyEvent event) {
467 boolean handled = super.onKeyDown(keyCode, event);
468 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
469 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
470 keyCode, event);
471 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700472 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700473 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700474 // showSearchDialog()
475 // If there are multiple keystrokes before the search dialog takes focus,
476 // onSearchRequested() will be called for every keystroke,
477 // but it is idempotent, so it's fine.
478 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 }
480 }
481
482 return handled;
483 }
484
Karl Rosaen138a0412009-04-23 19:00:21 -0700485 private String getTypedText() {
486 return mDefaultKeySsb.toString();
487 }
488
489 private void clearTypedText() {
490 mDefaultKeySsb.clear();
491 mDefaultKeySsb.clearSpans();
492 Selection.setSelection(mDefaultKeySsb, 0);
493 }
494
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 /**
496 * Restores the previous state, if it exists.
497 *
498 * @param savedState The previous state.
499 */
500 private void restoreState(Bundle savedState) {
501 if (savedState == null) {
502 return;
503 }
504
505 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
506 if (currentScreen > -1) {
507 mWorkspace.setCurrentScreen(currentScreen);
508 }
509
510 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
511 if (addScreen > -1) {
512 mAddItemCellInfo = new CellLayout.CellInfo();
513 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
514 addItemCellInfo.valid = true;
515 addItemCellInfo.screen = addScreen;
516 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
517 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
518 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
519 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
520 addItemCellInfo.findVacantCellsFromOccupied(
521 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
522 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
523 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
524 mRestoring = true;
525 }
526
527 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
528 if (renameFolder) {
529 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
530 mFolderInfo = sModel.getFolderById(this, id);
531 mRestoring = true;
532 }
533 }
534
535 /**
536 * Finds all the views we need and configure them properly.
537 */
538 private void setupViews() {
539 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
540 final DragLayer dragLayer = mDragLayer;
541
542 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
543 final Workspace workspace = mWorkspace;
544
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
546
Joe Onorato7404ee42009-07-31 11:54:44 -0700547 mHandleView = (HandleView) findViewById(R.id.all_apps);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700548 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700549 mHandleView.setOnClickListener(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700550 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 mHandleIcon.setCrossFadeEnabled(true);
552
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 workspace.setOnLongClickListener(this);
554 workspace.setDragger(dragLayer);
555 workspace.setLauncher(this);
556 loadWallpaper();
557
558 deleteZone.setLauncher(this);
559 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700560 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561
Joe Onorato7404ee42009-07-31 11:54:44 -0700562 // TODO dragLayer.setIgnoredDropTarget(grid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 dragLayer.setDragScoller(workspace);
564 dragLayer.setDragListener(deleteZone);
565 }
566
567 /**
568 * Creates a view representing a shortcut.
569 *
570 * @param info The data structure describing the shortcut.
571 *
572 * @return A View inflated from R.layout.application.
573 */
574 View createShortcut(ApplicationInfo info) {
575 return createShortcut(R.layout.application,
576 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
577 }
578
579 /**
580 * Creates a view representing a shortcut inflated from the specified resource.
581 *
582 * @param layoutResId The id of the XML layout used to create the shortcut.
583 * @param parent The group the shortcut belongs to.
584 * @param info The data structure describing the shortcut.
585 *
586 * @return A View inflated from layoutResId.
587 */
588 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
589 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
590
591 if (!info.filtered) {
592 info.icon = Utilities.createIconThumbnail(info.icon, this);
593 info.filtered = true;
594 }
595
596 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
597 favorite.setText(info.title);
598 favorite.setTag(info);
599 favorite.setOnClickListener(this);
600
601 return favorite;
602 }
603
604 /**
605 * Add an application shortcut to the workspace.
606 *
607 * @param data The intent describing the application.
608 * @param cellInfo The position on screen where to create the shortcut.
609 */
610 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
611 boolean insertAtFirst) {
612 cellInfo.screen = mWorkspace.getCurrentScreen();
613 if (!findSingleSlot(cellInfo)) return;
614
Romain Guy73b979d2009-06-09 12:57:21 -0700615 final ApplicationInfo info = infoFromApplicationIntent(context, data);
616 if (info != null) {
617 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
618 }
619 }
620
621 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 ComponentName component = data.getComponent();
623 PackageManager packageManager = context.getPackageManager();
624 ActivityInfo activityInfo = null;
625 try {
626 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
627 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700628 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 }
Romain Guycbb89e42009-06-08 15:52:54 -0700630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 if (activityInfo != null) {
632 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 itemInfo.title = activityInfo.loadLabel(packageManager);
635 if (itemInfo.title == null) {
636 itemInfo.title = activityInfo.name;
637 }
Romain Guycbb89e42009-06-08 15:52:54 -0700638
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
640 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
641 itemInfo.icon = activityInfo.loadIcon(packageManager);
642 itemInfo.container = ItemInfo.NO_ID;
643
Romain Guy73b979d2009-06-09 12:57:21 -0700644 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 }
Romain Guy73b979d2009-06-09 12:57:21 -0700646
647 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
Romain Guycbb89e42009-06-08 15:52:54 -0700649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 /**
651 * Add a shortcut to the workspace.
652 *
653 * @param data The intent describing the shortcut.
654 * @param cellInfo The position on screen where to create the shortcut.
655 * @param insertAtFirst
656 */
657 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
658 boolean insertAtFirst) {
659 cellInfo.screen = mWorkspace.getCurrentScreen();
660 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
663
664 if (!mRestoring) {
665 sModel.addDesktopItem(info);
666
667 final View view = createShortcut(info);
668 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
669 } else if (sModel.isDesktopLoaded()) {
670 sModel.addDesktopItem(info);
671 }
672 }
673
Romain Guycbb89e42009-06-08 15:52:54 -0700674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700678 * @param data The intent describing the appWidgetId.
679 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700681 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 boolean insertAtFirst) {
683
684 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700685 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700686
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700687 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700688
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700689 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700690
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700693 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 // Try finding open space on Launcher screen
696 final int[] xy = mCellCoordinates;
697 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
698
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700699 // Build Launcher-specific widget info and save to database
700 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 launcherInfo.spanX = spans[0];
702 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 LauncherModel.addItemToDatabase(this, launcherInfo,
705 LauncherSettings.Favorites.CONTAINER_DESKTOP,
706 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
707
708 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700709 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700714 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700716
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
718 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
719 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700720 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 }
722 }
Romain Guycbb89e42009-06-08 15:52:54 -0700723
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700724 public LauncherAppWidgetHost getAppWidgetHost() {
725 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 }
Romain Guycbb89e42009-06-08 15:52:54 -0700727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 static ApplicationInfo addShortcut(Context context, Intent data,
729 CellLayout.CellInfo cellInfo, boolean notify) {
730
Romain Guy73b979d2009-06-09 12:57:21 -0700731 final ApplicationInfo info = infoFromShortcutIntent(context, data);
732 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
733 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
734
735 return info;
736 }
737
738 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
740 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
741 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
742
743 Drawable icon = null;
744 boolean filtered = false;
745 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700746 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747
748 if (bitmap != null) {
749 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
750 filtered = true;
751 customIcon = true;
752 } else {
753 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700754 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700756 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 final PackageManager packageManager = context.getPackageManager();
758 Resources resources = packageManager.getResourcesForApplication(
759 iconResource.packageName);
760 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
761 icon = resources.getDrawable(id);
762 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700763 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 }
765 }
766 }
767
768 if (icon == null) {
769 icon = context.getPackageManager().getDefaultActivityIcon();
770 }
771
772 final ApplicationInfo info = new ApplicationInfo();
773 info.icon = icon;
774 info.filtered = filtered;
775 info.title = name;
776 info.intent = intent;
777 info.customIcon = customIcon;
778 info.iconResource = iconResource;
779
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 return info;
781 }
782
783 @Override
784 protected void onNewIntent(Intent intent) {
785 super.onNewIntent(intent);
786
787 // Close the menu
788 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
789 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700790
791 // Set this flag so that onResume knows to close the search dialog if it's open,
792 // because this was a new intent (thus a press of 'home' or some such) rather than
793 // for example onResume being called when the user pressed the 'back' button.
794 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795
796 try {
797 dismissDialog(DIALOG_CREATE_SHORTCUT);
798 // Unlock the workspace if the dialog was showing
799 mWorkspace.unlock();
800 } catch (Exception e) {
801 // An exception is thrown if the dialog is not visible, which is fine
802 }
803
804 try {
805 dismissDialog(DIALOG_RENAME_FOLDER);
806 // Unlock the workspace if the dialog was showing
807 mWorkspace.unlock();
808 } catch (Exception e) {
809 // An exception is thrown if the dialog is not visible, which is fine
810 }
811
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
813 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700814
Romain Guy94dabf12009-07-21 10:55:43 -0700815 if (!mWorkspace.isDefaultScreenShowing()) {
816 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700818
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700820
821 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 if (v != null && v.getWindowToken() != null) {
823 InputMethodManager imm = (InputMethodManager)getSystemService(
824 INPUT_METHOD_SERVICE);
825 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
826 }
827 } else {
828 closeDrawer(false);
829 }
830 }
831 }
832
833 @Override
834 protected void onRestoreInstanceState(Bundle savedInstanceState) {
835 // Do not call super here
836 mSavedInstanceState = savedInstanceState;
837 }
838
839 @Override
840 protected void onSaveInstanceState(Bundle outState) {
841 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
842
843 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
844 if (folders.size() > 0) {
845 final int count = folders.size();
846 long[] ids = new long[count];
847 for (int i = 0; i < count; i++) {
848 final FolderInfo info = folders.get(i).getInfo();
849 ids[i] = info.id;
850 }
851 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
852 } else {
853 super.onSaveInstanceState(outState);
854 }
855
Romain Guy3cf604f2009-06-16 13:12:53 -0700856 final boolean isConfigurationChange = getChangingConfigurations() != 0;
857
Romain Guy5a941392009-04-28 15:18:25 -0700858 // When the drawer is opened and we are saving the state because of a
859 // configuration change
Joe Onorato7404ee42009-07-31 11:54:44 -0700860 if (mAllAppsDialog.isOpen && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863
864 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
865 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
866 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
867
868 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
869 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
870 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
871 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
872 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
873 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
874 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
875 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
876 layout.getOccupiedCells());
877 }
878
879 if (mFolderInfo != null && mWaitingForResult) {
880 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
881 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
882 }
883 }
884
885 @Override
886 public void onDestroy() {
887 mDestroyed = true;
888
889 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700890
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700892 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700894 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
896
897 TextKeyListener.getInstance().release();
898
899 mAllAppsGrid.clearTextFilter();
900 mAllAppsGrid.setAdapter(null);
901 sModel.unbind();
902 sModel.abortLoaders();
903
904 getContentResolver().unregisterContentObserver(mObserver);
905 unregisterReceiver(mApplicationsReceiver);
906 }
907
908 @Override
909 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700910 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 super.startActivityForResult(intent, requestCode);
912 }
913
914 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700915 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700917
918 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700919
Karl Rosaen138a0412009-04-23 19:00:21 -0700920 // Slide the search widget to the top, if it's on the current screen,
921 // otherwise show the search dialog immediately.
922 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
923 if (searchWidget == null) {
924 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
925 } else {
926 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
927 // show the currently typed text in the search widget while sliding
928 searchWidget.setQuery(getTypedText());
929 }
930 }
Romain Guycbb89e42009-06-08 15:52:54 -0700931
Karl Rosaen138a0412009-04-23 19:00:21 -0700932 /**
933 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700934 *
935 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700936 */
Romain Guycbb89e42009-06-08 15:52:54 -0700937 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700938 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700939
Karl Rosaen138a0412009-04-23 19:00:21 -0700940 if (initialQuery == null) {
941 // Use any text typed in the launcher as the initial query
942 initialQuery = getTypedText();
943 clearTypedText();
944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 if (appSearchData == null) {
946 appSearchData = new Bundle();
947 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
948 }
Romain Guycbb89e42009-06-08 15:52:54 -0700949
Karl Rosaen138a0412009-04-23 19:00:21 -0700950 final SearchManager searchManager =
951 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
952
953 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
954 if (searchWidget != null) {
955 // This gets called when the user leaves the search dialog to go back to
956 // the Launcher.
957 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
958 public void onCancel() {
959 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700960 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700961 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 });
963 }
Romain Guycbb89e42009-06-08 15:52:54 -0700964
Karl Rosaen138a0412009-04-23 19:00:21 -0700965 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700966 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 /**
970 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 */
Romain Guy5a941392009-04-28 15:18:25 -0700972 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700973 // Close search dialog
974 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100975 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700976 // Restore search widget to its normal position
977 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
978 if (searchWidget != null) {
979 searchWidget.stopSearch(false);
980 }
981 }
Romain Guycbb89e42009-06-08 15:52:54 -0700982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 @Override
984 public boolean onCreateOptionsMenu(Menu menu) {
985 if (mDesktopLocked) return false;
986
987 super.onCreateOptionsMenu(menu);
988 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
989 .setIcon(android.R.drawable.ic_menu_add)
990 .setAlphabeticShortcut('A');
991 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
992 .setIcon(android.R.drawable.ic_menu_gallery)
993 .setAlphabeticShortcut('W');
994 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
995 .setIcon(android.R.drawable.ic_search_category_default)
996 .setAlphabeticShortcut(SearchManager.MENU_KEY);
997 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
998 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
999 .setAlphabeticShortcut('N');
1000
1001 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001002 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1003 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004
1005 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1006 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1007 .setIntent(settings);
1008
1009 return true;
1010 }
1011
1012 @Override
1013 public boolean onPrepareOptionsMenu(Menu menu) {
1014 super.onPrepareOptionsMenu(menu);
1015
1016 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1017 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1018
1019 return true;
1020 }
1021
1022 @Override
1023 public boolean onOptionsItemSelected(MenuItem item) {
1024 switch (item.getItemId()) {
1025 case MENU_ADD:
1026 addItems();
1027 return true;
1028 case MENU_WALLPAPER_SETTINGS:
1029 startWallpaper();
1030 return true;
1031 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001032 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 return true;
1034 case MENU_NOTIFICATIONS:
1035 showNotifications();
1036 return true;
1037 }
1038
1039 return super.onOptionsItemSelected(item);
1040 }
Romain Guycbb89e42009-06-08 15:52:54 -07001041
Karl Rosaen138a0412009-04-23 19:00:21 -07001042 /**
1043 * Indicates that we want global search for this activity by setting the globalSearch
1044 * argument for {@link #startSearch} to true.
1045 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001047 @Override
1048 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001049 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001050 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001051 }
1052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 private void addItems() {
1054 showAddDialog(mMenuAddInfo);
1055 }
1056
1057 private void removeShortcutsForPackage(String packageName) {
1058 if (packageName != null && packageName.length() > 0) {
1059 mWorkspace.removeShortcutsForPackage(packageName);
1060 }
1061 }
Romain Guycbb89e42009-06-08 15:52:54 -07001062
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001063 private void updateShortcutsForPackage(String packageName) {
1064 if (packageName != null && packageName.length() > 0) {
1065 mWorkspace.updateShortcutsForPackage(packageName);
1066 }
1067 }
1068
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001069 void addAppWidget(Intent data) {
1070 // TODO: catch bad widget exception when sent
1071 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001073 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1074 if (SEARCH_WIDGET.equals(customWidget)) {
1075 // We don't need this any more, since this isn't a real app widget.
1076 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1077 // add the search widget
1078 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001080 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1081
1082 if (appWidget.configure != null) {
1083 // Launch over to configure widget, if needed
1084 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1085 intent.setComponent(appWidget.configure);
1086 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1087
1088 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1089 } else {
1090 // Otherwise just add it
1091 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1092 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
1094 }
Romain Guycbb89e42009-06-08 15:52:54 -07001095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 void addSearch() {
1097 final Widget info = Widget.makeSearch();
1098 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 final int[] xy = mCellCoordinates;
1101 final int spanX = info.spanX;
1102 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 sModel.addDesktopItem(info);
1107 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1108 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 final View view = mInflater.inflate(info.layoutResource, null);
1111 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001112 Search search = (Search) view.findViewById(R.id.widget_search);
1113 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1116 }
1117
Romain Guy73b979d2009-06-09 12:57:21 -07001118 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001119 // Handle case where user selected "Applications"
1120 String applicationName = getResources().getString(R.string.group_applications);
1121 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001122
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001123 if (applicationName != null && applicationName.equals(shortcutName)) {
1124 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1125 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001126
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001127 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1128 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001129 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001130 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001131 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 }
1134
1135 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001136 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001137 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001138 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001139
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001140 if (folderName != null && folderName.equals(shortcutName)) {
1141 addFolder(!mDesktopLocked);
1142 } else {
1143 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 }
1146
1147 void addFolder(boolean insertAtFirst) {
1148 UserFolderInfo folderInfo = new UserFolderInfo();
1149 folderInfo.title = getText(R.string.folder_name);
1150
1151 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1152 cellInfo.screen = mWorkspace.getCurrentScreen();
1153 if (!findSingleSlot(cellInfo)) return;
1154
1155 // Update the model
1156 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1157 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1158 sModel.addDesktopItem(folderInfo);
1159 sModel.addFolder(folderInfo);
1160
1161 // Create the view
1162 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1163 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1164 mWorkspace.addInCurrentScreen(newFolder,
1165 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1166 }
Romain Guycbb89e42009-06-08 15:52:54 -07001167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1169 boolean insertAtFirst) {
1170 cellInfo.screen = mWorkspace.getCurrentScreen();
1171 if (!findSingleSlot(cellInfo)) return;
1172
1173 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1174
1175 if (!mRestoring) {
1176 sModel.addDesktopItem(info);
1177
1178 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1179 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1180 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1181 } else if (sModel.isDesktopLoaded()) {
1182 sModel.addDesktopItem(info);
1183 }
1184 }
1185
1186 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1187 CellLayout.CellInfo cellInfo, boolean notify) {
1188
1189 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1190 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1191
1192 Drawable icon = null;
1193 boolean filtered = false;
1194 Intent.ShortcutIconResource iconResource = null;
1195
1196 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1197 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1198 try {
1199 iconResource = (Intent.ShortcutIconResource) extra;
1200 final PackageManager packageManager = context.getPackageManager();
1201 Resources resources = packageManager.getResourcesForApplication(
1202 iconResource.packageName);
1203 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1204 icon = resources.getDrawable(id);
1205 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001206 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 }
1208 }
1209
1210 if (icon == null) {
1211 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1212 }
1213
1214 final LiveFolderInfo info = new LiveFolderInfo();
1215 info.icon = icon;
1216 info.filtered = filtered;
1217 info.title = name;
1218 info.iconResource = iconResource;
1219 info.uri = data.getData();
1220 info.baseIntent = baseIntent;
1221 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1222 LiveFolders.DISPLAY_MODE_GRID);
1223
1224 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1225 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1226 sModel.addFolder(info);
1227
1228 return info;
1229 }
1230
1231 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1232 final int[] xy = new int[2];
1233 if (findSlot(cellInfo, xy, 1, 1)) {
1234 cellInfo.cellX = xy[0];
1235 cellInfo.cellY = xy[1];
1236 return true;
1237 }
1238 return false;
1239 }
1240
1241 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1242 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1243 boolean[] occupied = mSavedState != null ?
1244 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1245 cellInfo = mWorkspace.findAllVacantCells(occupied);
1246 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1247 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1248 return false;
1249 }
1250 }
1251 return true;
1252 }
1253
1254 private void showNotifications() {
1255 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1256 if (statusBar != null) {
1257 statusBar.expand();
1258 }
1259 }
1260
1261 private void startWallpaper() {
1262 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1263 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1264 }
1265
1266 /**
1267 * Registers various intent receivers. The current implementation registers
1268 * only a wallpaper intent receiver to let other applications change the
1269 * wallpaper.
1270 */
1271 private void registerIntentReceivers() {
1272 if (sWallpaperReceiver == null) {
1273 final Application application = getApplication();
1274
1275 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1276
1277 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1278 application.registerReceiver(sWallpaperReceiver, filter);
1279 } else {
1280 sWallpaperReceiver.setLauncher(this);
1281 }
1282
1283 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1284 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1285 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1286 filter.addDataScheme("package");
1287 registerReceiver(mApplicationsReceiver, filter);
1288 }
1289
1290 /**
1291 * Registers various content observers. The current implementation registers
1292 * only a favorites observer to keep track of the favorites applications.
1293 */
1294 private void registerContentObservers() {
1295 ContentResolver resolver = getContentResolver();
1296 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1297 }
1298
1299 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001300 public void onWindowFocusChanged(boolean hasFocus) {
1301 super.onWindowFocusChanged(hasFocus);
1302 if (!hasFocus) {
1303 mBackDown = mHomeDown = false;
1304 }
1305 }
1306
1307 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 public boolean dispatchKeyEvent(KeyEvent event) {
1309 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1310 switch (event.getKeyCode()) {
1311 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001312 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 return true;
1314 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001315 mHomeDown = true;
1316 return true;
1317 }
1318 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1319 switch (event.getKeyCode()) {
1320 case KeyEvent.KEYCODE_BACK:
1321 if (!event.isCanceled()) {
1322 mWorkspace.dispatchKeyEvent(event);
Joe Onorato7404ee42009-07-31 11:54:44 -07001323 if (mAllAppsDialog.isOpen) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001324 closeDrawer();
1325 } else {
1326 closeFolder();
1327 }
1328 }
1329 mBackDown = false;
1330 return true;
1331 case KeyEvent.KEYCODE_HOME:
1332 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 return true;
1334 }
1335 }
1336
1337 return super.dispatchKeyEvent(event);
1338 }
1339
1340 private void closeDrawer() {
1341 closeDrawer(true);
1342 }
1343
1344 private void closeDrawer(boolean animated) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001345 if (mAllAppsDialog.isOpen) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 if (animated) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001347 // TODO mDrawer.animateClose();
1348 mAllAppsDialog.dismiss();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 } else {
Joe Onorato7404ee42009-07-31 11:54:44 -07001350 mAllAppsDialog.dismiss();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001352 if (false /* TODO mDrawer.hasFocus() */) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1354 }
1355 }
1356 }
1357
1358 private void closeFolder() {
1359 Folder folder = mWorkspace.getOpenFolder();
1360 if (folder != null) {
1361 closeFolder(folder);
1362 }
1363 }
1364
1365 void closeFolder(Folder folder) {
1366 folder.getInfo().opened = false;
1367 ViewGroup parent = (ViewGroup) folder.getParent();
1368 if (parent != null) {
1369 parent.removeView(folder);
1370 }
1371 folder.onClose();
1372 }
1373
1374 /**
1375 * When the notification that favorites have changed is received, requests
1376 * a favorites list refresh.
1377 */
1378 private void onFavoritesChanged() {
1379 mDesktopLocked = true;
Joe Onorato7404ee42009-07-31 11:54:44 -07001380 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 sModel.loadUserItems(false, this, false, false);
1382 }
1383
1384 void onDesktopItemsLoaded() {
1385 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 bindDesktopItems();
1387 }
Romain Guycbb89e42009-06-08 15:52:54 -07001388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 /**
1390 * Refreshes the shortcuts shown on the workspace.
1391 */
1392 private void bindDesktopItems() {
1393 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001394 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001395 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1396 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 return;
1398 }
1399
1400 final Workspace workspace = mWorkspace;
1401 int count = workspace.getChildCount();
1402 for (int i = 0; i < count; i++) {
1403 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1404 }
Romain Guycbb89e42009-06-08 15:52:54 -07001405
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 if (DEBUG_USER_INTERFACE) {
1407 android.widget.Button finishButton = new android.widget.Button(this);
1408 finishButton.setText("Finish");
1409 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1410
1411 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1412 public void onClick(View v) {
1413 finish();
1414 }
1415 });
1416 }
Romain Guycbb89e42009-06-08 15:52:54 -07001417
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001418 // Flag any old binder to terminate early
1419 if (mBinder != null) {
1420 mBinder.mTerminate = true;
1421 }
Romain Guycbb89e42009-06-08 15:52:54 -07001422
Karl Rosaen138a0412009-04-23 19:00:21 -07001423 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001424 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
1426
1427 private void bindItems(Launcher.DesktopBinder binder,
1428 ArrayList<ItemInfo> shortcuts, int start, int count) {
1429
1430 final Workspace workspace = mWorkspace;
1431 final boolean desktopLocked = mDesktopLocked;
1432
1433 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1434 int i = start;
1435
1436 for ( ; i < end; i++) {
1437 final ItemInfo item = shortcuts.get(i);
1438 switch (item.itemType) {
1439 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1440 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1441 final View shortcut = createShortcut((ApplicationInfo) item);
1442 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1443 !desktopLocked);
1444 break;
1445 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1446 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1447 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1448 (UserFolderInfo) item);
1449 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1450 !desktopLocked);
1451 break;
1452 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1453 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1454 R.layout.live_folder_icon, this,
1455 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1456 (LiveFolderInfo) item);
1457 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1458 !desktopLocked);
1459 break;
1460 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1461 final int screen = workspace.getCurrentScreen();
1462 final View view = mInflater.inflate(R.layout.widget_search,
1463 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001464
Karl Rosaen138a0412009-04-23 19:00:21 -07001465 Search search = (Search) view.findViewById(R.id.widget_search);
1466 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 final Widget widget = (Widget) item;
1469 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001470
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 workspace.addWidget(view, widget, !desktopLocked);
1472 break;
1473 }
1474 }
1475
1476 workspace.requestLayout();
1477
1478 if (end >= count) {
1479 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001480 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 } else {
1482 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1483 }
1484 }
1485
1486 private void finishBindDesktopItems() {
1487 if (mSavedState != null) {
1488 if (!mWorkspace.hasFocus()) {
1489 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1490 }
1491
1492 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1493 if (userFolders != null) {
1494 for (long folderId : userFolders) {
1495 final FolderInfo info = sModel.findFolderById(folderId);
1496 if (info != null) {
1497 openFolder(info);
1498 }
1499 }
1500 final Folder openFolder = mWorkspace.getOpenFolder();
1501 if (openFolder != null) {
1502 openFolder.requestFocus();
1503 }
1504 }
1505
1506 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1507 if (allApps) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001508 showDialog(DIALOG_ALL_APPS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 }
1510
1511 mSavedState = null;
1512 }
1513
1514 if (mSavedInstanceState != null) {
1515 super.onRestoreInstanceState(mSavedInstanceState);
1516 mSavedInstanceState = null;
1517 }
1518
Joe Onorato7404ee42009-07-31 11:54:44 -07001519 /* TODO
1520 if (mAllAppsDialog.isOpen && !mDrawer.hasFocus()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 mDrawer.requestFocus();
1522 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001523 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524
1525 mDesktopLocked = false;
Joe Onorato7404ee42009-07-31 11:54:44 -07001526 mAllAppsDialog.unlock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 }
Romain Guycbb89e42009-06-08 15:52:54 -07001528
Karl Rosaen138a0412009-04-23 19:00:21 -07001529 private void bindDrawer(Launcher.DesktopBinder binder,
1530 ApplicationsAdapter drawerAdapter) {
1531 mAllAppsGrid.setAdapter(drawerAdapter);
1532 binder.startBindingAppWidgetsWhenIdle();
1533 }
Romain Guycbb89e42009-06-08 15:52:54 -07001534
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001535 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001536 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001537
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 final Workspace workspace = mWorkspace;
1539 final boolean desktopLocked = mDesktopLocked;
1540
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001541 if (!appWidgets.isEmpty()) {
1542 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001543
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001544 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001545 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001546 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001547
Karl Rosaen138a0412009-04-23 19:00:21 -07001548 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001549
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001550 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1551 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001552
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001553 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1554 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001556 workspace.requestLayout();
1557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001559 if (appWidgets.isEmpty()) {
1560 if (PROFILE_ROTATE) {
1561 android.os.Debug.stopMethodTracing();
1562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001564 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
1566 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001567
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 DragController getDragController() {
1569 return mDragLayer;
1570 }
1571
1572 /**
1573 * Launches the intent referred by the clicked shortcut.
1574 *
1575 * @param v The view representing the clicked shortcut.
1576 */
1577 public void onClick(View v) {
1578 Object tag = v.getTag();
1579 if (tag instanceof ApplicationInfo) {
1580 // Open shortcut
1581 final Intent intent = ((ApplicationInfo) tag).intent;
1582 startActivitySafely(intent);
1583 } else if (tag instanceof FolderInfo) {
1584 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001585 } else if (v == mHandleView) {
1586 if (mAllAppsDialog.isOpen) {
1587 // TODO how can we be here?
1588 } else {
1589 showDialog(DIALOG_ALL_APPS);
1590 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 }
1592 }
1593
1594 void startActivitySafely(Intent intent) {
1595 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1596 try {
1597 startActivity(intent);
1598 } catch (ActivityNotFoundException e) {
1599 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1600 } catch (SecurityException e) {
1601 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001602 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1604 "or use the exported attribute for this activity.", e);
1605 }
1606 }
1607
1608 private void handleFolderClick(FolderInfo folderInfo) {
1609 if (!folderInfo.opened) {
1610 // Close any open folder
1611 closeFolder();
1612 // Open the requested folder
1613 openFolder(folderInfo);
1614 } else {
1615 // Find the open folder...
1616 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1617 int folderScreen;
1618 if (openFolder != null) {
1619 folderScreen = mWorkspace.getScreenForView(openFolder);
1620 // .. and close it
1621 closeFolder(openFolder);
1622 if (folderScreen != mWorkspace.getCurrentScreen()) {
1623 // Close any folder open on the current screen
1624 closeFolder();
1625 // Pull the folder onto this screen
1626 openFolder(folderInfo);
1627 }
1628 }
1629 }
1630 }
1631
1632 private void loadWallpaper() {
1633 // The first time the application is started, we load the wallpaper from
1634 // the ApplicationContext
1635 if (sWallpaper == null) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001636 final Drawable drawable;
1637 if (false) {
1638 drawable = getWallpaper();
1639 } else {
1640 drawable = getResources().getDrawable(R.drawable.wallpaper_path);
1641 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 if (drawable instanceof BitmapDrawable) {
1643 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1644 } else {
1645 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1646 }
1647 }
1648 mWorkspace.loadWallpaper(sWallpaper);
1649 }
1650
1651 /**
1652 * Opens the user fodler described by the specified tag. The opening of the folder
1653 * is animated relative to the specified View. If the View is null, no animation
1654 * is played.
1655 *
1656 * @param folderInfo The FolderInfo describing the folder to open.
1657 */
1658 private void openFolder(FolderInfo folderInfo) {
1659 Folder openFolder;
1660
1661 if (folderInfo instanceof UserFolderInfo) {
1662 openFolder = UserFolder.fromXml(this);
1663 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001664 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 } else {
1666 return;
1667 }
1668
1669 openFolder.setDragger(mDragLayer);
1670 openFolder.setLauncher(this);
1671
1672 openFolder.bind(folderInfo);
1673 folderInfo.opened = true;
1674
1675 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1676 openFolder.onOpen();
1677 }
1678
1679 /**
1680 * Returns true if the workspace is being loaded. When the workspace is loading,
1681 * no user interaction should be allowed to avoid any conflict.
1682 *
1683 * @return True if the workspace is locked, false otherwise.
1684 */
1685 boolean isWorkspaceLocked() {
1686 return mDesktopLocked;
1687 }
1688
1689 public boolean onLongClick(View v) {
1690 if (mDesktopLocked) {
1691 return false;
1692 }
1693
1694 if (!(v instanceof CellLayout)) {
1695 v = (View) v.getParent();
1696 }
1697
1698 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1699
1700 // This happens when long clicking an item with the dpad/trackball
1701 if (cellInfo == null) {
1702 return true;
1703 }
1704
1705 if (mWorkspace.allowLongPress()) {
1706 if (cellInfo.cell == null) {
1707 if (cellInfo.valid) {
1708 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001709 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 showAddDialog(cellInfo);
1711 }
1712 } else {
1713 if (!(cellInfo.cell instanceof Folder)) {
1714 // User long pressed on an item
1715 mWorkspace.startDrag(cellInfo);
1716 }
1717 }
1718 }
1719 return true;
1720 }
1721
1722 static LauncherModel getModel() {
1723 return sModel;
1724 }
1725
1726 void closeAllApplications() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001727 // TODO mDrawer.close();
1728 mAllAppsDialog.dismiss();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 }
1730
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001731 View getDrawerHandle() {
1732 return mHandleView;
1733 }
1734
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 boolean isDrawerDown() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001736 return /* TODO !mDrawer.isMoving() && */ !mAllAppsDialog.isOpen;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 }
1738
1739 boolean isDrawerUp() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001740 return mAllAppsDialog.isOpen; /* TODO && !mDrawer.isMoving();*/
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 }
1742
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001743 boolean isDrawerMoving() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001744 return false; // TODO mDrawer.isMoving();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001745 }
1746
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 Workspace getWorkspace() {
1748 return mWorkspace;
1749 }
1750
Joe Onorato7404ee42009-07-31 11:54:44 -07001751 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 GridView getApplicationsGrid() {
1753 return mAllAppsGrid;
1754 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001755 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756
1757 @Override
1758 protected Dialog onCreateDialog(int id) {
1759 switch (id) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001760 case DIALOG_ALL_APPS:
1761 return mAllAppsDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 case DIALOG_CREATE_SHORTCUT:
1763 return new CreateShortcut().createDialog();
1764 case DIALOG_RENAME_FOLDER:
1765 return new RenameFolder().createDialog();
1766 }
1767
1768 return super.onCreateDialog(id);
1769 }
1770
1771 @Override
1772 protected void onPrepareDialog(int id, Dialog dialog) {
1773 switch (id) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001774 case DIALOG_ALL_APPS:
1775 mAllAppsDialog.isOpen = true;
1776 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 break;
1779 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001780 if (mFolderInfo != null) {
1781 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1782 final CharSequence text = mFolderInfo.title;
1783 input.setText(text);
1784 input.setSelection(0, text.length());
1785 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 break;
1787 }
1788 }
1789
1790 void showRenameDialog(FolderInfo info) {
1791 mFolderInfo = info;
1792 mWaitingForResult = true;
1793 showDialog(DIALOG_RENAME_FOLDER);
1794 }
1795
1796 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1797 mAddItemCellInfo = cellInfo;
1798 mWaitingForResult = true;
1799 showDialog(DIALOG_CREATE_SHORTCUT);
1800 }
1801
Romain Guy73b979d2009-06-09 12:57:21 -07001802 private void pickShortcut(int requestCode, int title) {
1803 Bundle bundle = new Bundle();
1804
1805 ArrayList<String> shortcutNames = new ArrayList<String>();
1806 shortcutNames.add(getString(R.string.group_applications));
1807 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1808
1809 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1810 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1811 R.drawable.ic_launcher_application));
1812 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1813
1814 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1815 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1816 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1817 pickIntent.putExtras(bundle);
1818
1819 startActivityForResult(pickIntent, requestCode);
1820 }
1821
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 private class RenameFolder {
1823 private EditText mInput;
1824
1825 Dialog createDialog() {
1826 mWaitingForResult = true;
1827 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1828 mInput = (EditText) layout.findViewById(R.id.folder_name);
1829
1830 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1831 builder.setIcon(0);
1832 builder.setTitle(getString(R.string.rename_folder_title));
1833 builder.setCancelable(true);
1834 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1835 public void onCancel(DialogInterface dialog) {
1836 cleanup();
1837 }
1838 });
1839 builder.setNegativeButton(getString(R.string.cancel_action),
1840 new Dialog.OnClickListener() {
1841 public void onClick(DialogInterface dialog, int which) {
1842 cleanup();
1843 }
1844 }
1845 );
1846 builder.setPositiveButton(getString(R.string.rename_action),
1847 new Dialog.OnClickListener() {
1848 public void onClick(DialogInterface dialog, int which) {
1849 changeFolderName();
1850 }
1851 }
1852 );
1853 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001854
1855 final AlertDialog dialog = builder.create();
1856 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1857 public void onShow(DialogInterface dialog) {
1858 mWorkspace.lock();
1859 }
1860 });
1861
1862 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
1864
1865 private void changeFolderName() {
1866 final String name = mInput.getText().toString();
1867 if (!TextUtils.isEmpty(name)) {
1868 // Make sure we have the right folder info
1869 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1870 mFolderInfo.title = name;
1871 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1872
1873 if (mDesktopLocked) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001874 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 sModel.loadUserItems(false, Launcher.this, false, false);
1876 } else {
1877 final FolderIcon folderIcon = (FolderIcon)
1878 mWorkspace.getViewForTag(mFolderInfo);
1879 if (folderIcon != null) {
1880 folderIcon.setText(name);
1881 getWorkspace().requestLayout();
1882 } else {
1883 mDesktopLocked = true;
Joe Onorato7404ee42009-07-31 11:54:44 -07001884 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 sModel.loadUserItems(false, Launcher.this, false, false);
1886 }
1887 }
1888 }
1889 cleanup();
1890 }
1891
1892 private void cleanup() {
1893 mWorkspace.unlock();
1894 dismissDialog(DIALOG_RENAME_FOLDER);
1895 mWaitingForResult = false;
1896 mFolderInfo = null;
1897 }
1898 }
1899
1900 /**
Joe Onorato7404ee42009-07-31 11:54:44 -07001901 * Holds the 3d all apps view.
1902 */
1903 private class AllAppsDialog extends Dialog implements DialogInterface.OnCancelListener,
1904 DialogInterface.OnDismissListener, DialogInterface.OnShowListener {
1905
1906 boolean isOpen;
1907
1908 AllAppsDialog(Context context) {
1909 super(context, android.R.style.Theme_Translucent_NoTitleBar);
1910
1911 setOnCancelListener(this);
1912 setOnDismissListener(this);
1913 setOnShowListener(this);
1914
1915 setContentView(R.layout.all_apps);
1916 AllAppsGridView grid = mAllAppsGrid = (AllAppsGridView)findViewById(R.id.all_apps);
1917
1918 grid.setTextFilterEnabled(false);
1919 // TODO grid.setDragger(dragLayer);
1920 grid.setLauncher(Launcher.this);
1921 }
1922
1923 public void onCancel(DialogInterface dialog) {
1924 onDestroy();
1925 }
1926
1927 public void onDismiss(DialogInterface dialog) {
1928 onDestroy();
1929 }
1930
1931 public void onShow(DialogInterface dialog) {
1932 }
1933
1934 private void onDestroy() {
1935 this.isOpen = false;
1936 }
1937
1938 void lock() {
1939 // TODO
1940 }
1941
1942 void unlock() {
1943 // TODO
1944 }
1945 }
1946
1947 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 * Displays the shortcut creation dialog and launches, if necessary, the
1949 * appropriate activity.
1950 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001951 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001952 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1953 DialogInterface.OnShowListener {
1954
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001956
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 Dialog createDialog() {
1958 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001961
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1963 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001964 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 builder.setInverseBackgroundForced(true);
1967
1968 AlertDialog dialog = builder.create();
1969 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001970 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001971 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 return dialog;
1974 }
1975
1976 public void onCancel(DialogInterface dialog) {
1977 mWaitingForResult = false;
1978 cleanup();
1979 }
1980
Romain Guycbb89e42009-06-08 15:52:54 -07001981 public void onDismiss(DialogInterface dialog) {
1982 mWorkspace.unlock();
1983 }
1984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 private void cleanup() {
1986 mWorkspace.unlock();
1987 dismissDialog(DIALOG_CREATE_SHORTCUT);
1988 }
1989
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001990 /**
1991 * Handle the action clicked in the "Add to home" dialog.
1992 */
1993 public void onClick(DialogInterface dialog, int which) {
1994 Resources res = getResources();
1995 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001996
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001997 switch (which) {
1998 case AddAdapter.ITEM_SHORTCUT: {
1999 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002000 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002001 break;
2002 }
Romain Guycbb89e42009-06-08 15:52:54 -07002003
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002004 case AddAdapter.ITEM_APPWIDGET: {
2005 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002006
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002007 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2008 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2009 // add the search widget
2010 ArrayList<AppWidgetProviderInfo> customInfo =
2011 new ArrayList<AppWidgetProviderInfo>();
2012 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2013 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2014 info.label = getString(R.string.group_search);
2015 info.icon = R.drawable.ic_search_widget;
2016 customInfo.add(info);
2017 pickIntent.putParcelableArrayListExtra(
2018 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2019 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2020 Bundle b = new Bundle();
2021 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2022 customExtras.add(b);
2023 pickIntent.putParcelableArrayListExtra(
2024 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2025 // start the pick activity
2026 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2027 break;
2028 }
Romain Guycbb89e42009-06-08 15:52:54 -07002029
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002030 case AddAdapter.ITEM_LIVE_FOLDER: {
2031 // Insert extra item to handle inserting folder
2032 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002033
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002034 ArrayList<String> shortcutNames = new ArrayList<String>();
2035 shortcutNames.add(res.getString(R.string.group_folder));
2036 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002037
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002038 ArrayList<ShortcutIconResource> shortcutIcons =
2039 new ArrayList<ShortcutIconResource>();
2040 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2041 R.drawable.ic_launcher_folder));
2042 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2043
2044 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2045 pickIntent.putExtra(Intent.EXTRA_INTENT,
2046 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2047 pickIntent.putExtra(Intent.EXTRA_TITLE,
2048 getText(R.string.title_select_live_folder));
2049 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002050
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002051 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2052 break;
2053 }
2054
2055 case AddAdapter.ITEM_WALLPAPER: {
2056 startWallpaper();
2057 break;
2058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
2060 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002061
2062 public void onShow(DialogInterface dialog) {
2063 mWorkspace.lock();
2064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 }
2066
2067 /**
2068 * Receives notifications when applications are added/removed.
2069 */
2070 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2071 @Override
2072 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002073 final String action = intent.getAction();
2074 final String packageName = intent.getData().getSchemeSpecificPart();
2075 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2076
2077 if (LauncherModel.DEBUG_LOADERS) {
2078 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2079 ", replacing=" + replacing);
2080 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002082
2083 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2084 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2085 if (!replacing) {
2086 removeShortcutsForPackage(packageName);
2087 if (LauncherModel.DEBUG_LOADERS) {
2088 d(LauncherModel.LOG_TAG, " --> remove package");
2089 }
2090 sModel.removePackage(Launcher.this, packageName);
2091 }
2092 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2093 // later, we will update the package at this time
2094 } else {
2095 if (!replacing) {
2096 if (LauncherModel.DEBUG_LOADERS) {
2097 d(LauncherModel.LOG_TAG, " --> add package");
2098 }
2099 sModel.addPackage(Launcher.this, packageName);
2100 } else {
2101 if (LauncherModel.DEBUG_LOADERS) {
2102 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2103 }
2104 sModel.updatePackage(Launcher.this, packageName);
2105 updateShortcutsForPackage(packageName);
2106 }
2107 }
2108 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002110 if (LauncherModel.DEBUG_LOADERS) {
2111 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2112 }
2113 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 }
2115 }
2116 }
2117
2118 /**
2119 * Receives notifications whenever the user favorites have changed.
2120 */
2121 private class FavoritesChangeObserver extends ContentObserver {
2122 public FavoritesChangeObserver() {
2123 super(new Handler());
2124 }
2125
2126 @Override
2127 public void onChange(boolean selfChange) {
2128 onFavoritesChanged();
2129 }
2130 }
2131
2132 /**
2133 * Receives intents from other applications to change the wallpaper.
2134 */
2135 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2136 private final Application mApplication;
2137 private WeakReference<Launcher> mLauncher;
2138
2139 WallpaperIntentReceiver(Application application, Launcher launcher) {
2140 mApplication = application;
2141 setLauncher(launcher);
2142 }
2143
2144 void setLauncher(Launcher launcher) {
2145 mLauncher = new WeakReference<Launcher>(launcher);
2146 }
2147
2148 @Override
2149 public void onReceive(Context context, Intent intent) {
2150 // Load the wallpaper from the ApplicationContext and store it locally
2151 // until the Launcher Activity is ready to use it
2152 final Drawable drawable = mApplication.getWallpaper();
2153 if (drawable instanceof BitmapDrawable) {
2154 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2155 } else {
2156 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2157 }
2158
2159 // If Launcher is alive, notify we have a new wallpaper
2160 if (mLauncher != null) {
2161 final Launcher launcher = mLauncher.get();
2162 if (launcher != null) {
2163 launcher.loadWallpaper();
2164 }
2165 }
2166 }
2167 }
2168
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002169 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002171 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002172 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 // Number of items to bind in every pass
2175 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176
2177 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002178 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002179 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002181
Karl Rosaen138a0412009-04-23 19:00:21 -07002182 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183
2184 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002185 ArrayList<LauncherAppWidgetInfo> appWidgets,
2186 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187
2188 mLauncher = new WeakReference<Launcher>(launcher);
2189 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002190 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002191
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002192 // Sort widgets so active workspace is bound first
2193 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2194 final int size = appWidgets.size();
2195 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002196
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002197 for (int i = 0; i < size; i++) {
2198 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2199 if (appWidgetInfo.screen == currentScreen) {
2200 mAppWidgets.addFirst(appWidgetInfo);
2201 } else {
2202 mAppWidgets.addLast(appWidgetInfo);
2203 }
2204 }
2205 }
Romain Guycbb89e42009-06-08 15:52:54 -07002206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 public void startBindingItems() {
2208 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2209 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002210
2211 public void startBindingDrawer() {
2212 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2213 }
Romain Guycbb89e42009-06-08 15:52:54 -07002214
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002215 public void startBindingAppWidgetsWhenIdle() {
2216 // Ask for notification when message queue becomes idle
2217 final MessageQueue messageQueue = Looper.myQueue();
2218 messageQueue.addIdleHandler(this);
2219 }
Romain Guycbb89e42009-06-08 15:52:54 -07002220
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002221 public boolean queueIdle() {
2222 // Queue is idle, so start binding items
2223 startBindingAppWidgets();
2224 return false;
2225 }
2226
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002227 public void startBindingAppWidgets() {
2228 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2229 }
2230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 @Override
2232 public void handleMessage(Message msg) {
2233 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002234 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 return;
2236 }
Romain Guycbb89e42009-06-08 15:52:54 -07002237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 switch (msg.what) {
2239 case MESSAGE_BIND_ITEMS: {
2240 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2241 break;
2242 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002243 case MESSAGE_BIND_DRAWER: {
2244 launcher.bindDrawer(this, mDrawerAdapter);
2245 break;
2246 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002247 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002248 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 break;
2250 }
2251 }
2252 }
2253 }
2254}