blob: b3702228b559c963e5a170fdb742202191101e92 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070022import android.app.ISearchManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.SearchManager;
24import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070025import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.ActivityNotFoundException;
27import android.content.BroadcastReceiver;
28import android.content.ComponentName;
29import android.content.ContentResolver;
30import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
33import android.content.IntentFilter;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070034import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.ActivityInfo;
36import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070039import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.drawable.Drawable;
43import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.os.Bundle;
45import android.os.Handler;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070046import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070047import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070048import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.os.Parcelable;
50import android.os.RemoteException;
51import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070052import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.text.Selection;
54import android.text.SpannableStringBuilder;
55import android.text.TextUtils;
56import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070057import static android.util.Log.*;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.view.KeyEvent;
60import android.view.LayoutInflater;
61import android.view.Menu;
62import android.view.MenuItem;
63import android.view.View;
64import android.view.ViewGroup;
Romain Guyb1b69f52009-08-10 15:10:15 -070065import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.view.View.OnLongClickListener;
67import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.widget.TextView;
70import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070071import android.appwidget.AppWidgetManager;
72import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073
74import java.lang.ref.WeakReference;
75import java.util.ArrayList;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070076import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070077import java.io.DataOutputStream;
78import java.io.FileNotFoundException;
79import java.io.IOException;
80import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081
82/**
83 * Default launcher application.
84 */
85public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener {
86 static final String LOG_TAG = "Launcher";
87 static final boolean LOGD = false;
88
89 private static final boolean PROFILE_STARTUP = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070090 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 private static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070092
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 private static final int WALLPAPER_SCREENS_SPAN = 2;
94
95 private static final int MENU_GROUP_ADD = 1;
96 private static final int MENU_ADD = Menu.FIRST + 1;
97 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
98 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
99 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700100 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
102 private static final int REQUEST_CREATE_SHORTCUT = 1;
103 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700104 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 private static final int REQUEST_PICK_APPLICATION = 6;
106 private static final int REQUEST_PICK_SHORTCUT = 7;
107 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700108 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
110 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
111
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700112 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
113 static final String SEARCH_WIDGET = "search_widget";
114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 static final int SCREEN_COUNT = 3;
116 static final int DEFAULT_SCREN = 1;
117 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700118 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato7404ee42009-07-31 11:54:44 -0700120 static final int DIALOG_ALL_APPS = 1;
121 static final int DIALOG_CREATE_SHORTCUT = 2;
122 static final int DIALOG_RENAME_FOLDER = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Romain Guy98d01652009-06-30 16:21:04 -0700124 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 // Type: int
127 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
128 // Type: boolean
129 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
130 // Type: long
131 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
146 // Type: int[]
147 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
148 // Type: boolean
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
150 // Type: long
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
152
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700153 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final Object sLock = new Object();
156 private static int sScreen = DEFAULT_SCREN;
157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
159 private final ContentObserver mObserver = new FavoritesChangeObserver();
160
161 private LayoutInflater mInflater;
162
Joe Onorato00acb122009-08-04 16:04:30 -0400163 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700165
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700166 private AppWidgetManager mAppWidgetManager;
167 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private CellLayout.CellInfo mAddItemCellInfo;
172 private CellLayout.CellInfo mMenuAddInfo;
173 private final int[] mCellCoordinates = new int[2];
174 private FolderInfo mFolderInfo;
175
Joe Onorato7404ee42009-07-31 11:54:44 -0700176 private AllAppsDialog mAllAppsDialog;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private HandleView mHandleView;
Joe Onorato93839052009-08-06 20:34:32 -0700178 private AllAppsView mAllAppsGrid; // TODO: put this into AllAppsDialog
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
180 private boolean mDesktopLocked = true;
181 private Bundle mSavedState;
182
183 private SpannableStringBuilder mDefaultKeySsb = null;
184
185 private boolean mDestroyed;
Mike LeBeau736cf282009-07-02 17:46:59 -0700186
187 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
189 private boolean mRestoring;
190 private boolean mWaitingForResult;
191 private boolean mLocaleChanged;
192
Dianne Hackborn67800862009-07-24 17:15:20 -0700193 private boolean mHomeDown;
194 private boolean mBackDown;
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private Bundle mSavedInstanceState;
197
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700198 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 @Override
201 protected void onCreate(Bundle savedInstanceState) {
202 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700206 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700207
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
209 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 if (PROFILE_STARTUP) {
212 android.os.Debug.startMethodTracing("/sdcard/launcher");
213 }
214
215 checkForLocaleChange();
216 setWallpaperDimension();
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 setContentView(R.layout.launcher);
219 setupViews();
220
Joe Onorato7404ee42009-07-31 11:54:44 -0700221 mAllAppsDialog = new AllAppsDialog(this);
222 mAllAppsDialog.lock();
223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 registerIntentReceivers();
225 registerContentObservers();
226
227 mSavedState = savedInstanceState;
228 restoreState(mSavedState);
229
230 if (PROFILE_STARTUP) {
231 android.os.Debug.stopMethodTracing();
232 }
233
234 if (!mRestoring) {
235 startLoaders();
236 }
237
238 // For handling default keys
239 mDefaultKeySsb = new SpannableStringBuilder();
240 Selection.setSelection(mDefaultKeySsb, 0);
241 }
Romain Guycbb89e42009-06-08 15:52:54 -0700242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700244 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
245 readConfiguration(this, localeConfiguration);
246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 final Configuration configuration = getResources().getConfiguration();
248
Romain Guy98d01652009-06-30 16:21:04 -0700249 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 final String locale = configuration.locale.toString();
251
Romain Guy98d01652009-06-30 16:21:04 -0700252 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 final int mcc = configuration.mcc;
254
Romain Guy98d01652009-06-30 16:21:04 -0700255 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 final int mnc = configuration.mnc;
257
258 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
259
260 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700261 localeConfiguration.locale = locale;
262 localeConfiguration.mcc = mcc;
263 localeConfiguration.mnc = mnc;
264
265 writeConfiguration(this, localeConfiguration);
266 }
267 }
268
269 private static class LocaleConfiguration {
270 public String locale;
271 public int mcc = -1;
272 public int mnc = -1;
273 }
274
275 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
276 DataInputStream in = null;
277 try {
278 in = new DataInputStream(context.openFileInput(PREFERENCES));
279 configuration.locale = in.readUTF();
280 configuration.mcc = in.readInt();
281 configuration.mnc = in.readInt();
282 } catch (FileNotFoundException e) {
283 // Ignore
284 } catch (IOException e) {
285 // Ignore
286 } finally {
287 if (in != null) {
288 try {
289 in.close();
290 } catch (IOException e) {
291 // Ignore
292 }
293 }
294 }
295 }
296
297 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
298 DataOutputStream out = null;
299 try {
300 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
301 out.writeUTF(configuration.locale);
302 out.writeInt(configuration.mcc);
303 out.writeInt(configuration.mnc);
304 out.flush();
305 } catch (FileNotFoundException e) {
306 // Ignore
307 } catch (IOException e) {
308 //noinspection ResultOfMethodCallIgnored
309 context.getFileStreamPath(PREFERENCES).delete();
310 } finally {
311 if (out != null) {
312 try {
313 out.close();
314 } catch (IOException e) {
315 // Ignore
316 }
317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 }
319 }
320
321 static int getScreen() {
322 synchronized (sLock) {
323 return sScreen;
324 }
325 }
326
327 static void setScreen(int screen) {
328 synchronized (sLock) {
329 sScreen = screen;
330 }
331 }
332
333 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700334 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
335 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 mRestoring = false;
338 }
339
340 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700341 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342
343 Display display = getWindowManager().getDefaultDisplay();
344 boolean isPortrait = display.getWidth() < display.getHeight();
345
346 final int width = isPortrait ? display.getWidth() : display.getHeight();
347 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700348 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 }
350
351 @Override
352 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700353 mWaitingForResult = false;
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 // The pattern used here is that a user PICKs a specific application,
356 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
359 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700360
Romain Guy94dabf12009-07-21 10:55:43 -0700361 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 switch (requestCode) {
363 case REQUEST_PICK_APPLICATION:
364 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
365 break;
366 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700367 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 break;
369 case REQUEST_CREATE_SHORTCUT:
370 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
371 break;
372 case REQUEST_PICK_LIVE_FOLDER:
373 addLiveFolder(data);
374 break;
375 case REQUEST_CREATE_LIVE_FOLDER:
376 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
377 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700378 case REQUEST_PICK_APPWIDGET:
379 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700381 case REQUEST_CREATE_APPWIDGET:
382 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 break;
384 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700385 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700387 // Clean up the appWidgetId if we canceled
388 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
389 if (appWidgetId != -1) {
390 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 }
392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
394
395 @Override
396 protected void onResume() {
397 super.onResume();
398
399 if (mRestoring) {
400 startLoaders();
401 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700402
403 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
404 // onNewIntent), then close the search dialog if needed, because it probably
405 // came from the user pressing 'home' (rather than, for example, pressing 'back').
406 if (mIsNewIntent) {
407 // Post to a handler so that this happens after the search dialog tries to open
408 // itself again.
409 mWorkspace.post(new Runnable() {
410 public void run() {
411 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
412 ServiceManager.getService(Context.SEARCH_SERVICE));
413 try {
414 searchManagerService.stopSearch();
415 } catch (RemoteException e) {
416 e(LOG_TAG, "error stopping search", e);
417 }
418 }
419 });
420 }
421
422 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
424
425 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700426 protected void onPause() {
427 super.onPause();
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700428 closeAllAppsDialog(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700429 }
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700431 @Override
432 public Object onRetainNonConfigurationInstance() {
433 // Flag any binder to stop early before switching
434 if (mBinder != null) {
435 mBinder.mTerminate = true;
436 }
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700438 if (PROFILE_ROTATE) {
439 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
440 }
441 return null;
442 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 private boolean acceptFilter() {
445 final InputMethodManager inputManager = (InputMethodManager)
446 getSystemService(Context.INPUT_METHOD_SERVICE);
447 return !inputManager.isFullscreenMode();
448 }
449
450 @Override
451 public boolean onKeyDown(int keyCode, KeyEvent event) {
452 boolean handled = super.onKeyDown(keyCode, event);
453 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
454 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
455 keyCode, event);
456 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700457 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700458 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700459 // showSearchDialog()
460 // If there are multiple keystrokes before the search dialog takes focus,
461 // onSearchRequested() will be called for every keystroke,
462 // but it is idempotent, so it's fine.
463 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 }
465 }
466
467 return handled;
468 }
469
Karl Rosaen138a0412009-04-23 19:00:21 -0700470 private String getTypedText() {
471 return mDefaultKeySsb.toString();
472 }
473
474 private void clearTypedText() {
475 mDefaultKeySsb.clear();
476 mDefaultKeySsb.clearSpans();
477 Selection.setSelection(mDefaultKeySsb, 0);
478 }
479
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 /**
481 * Restores the previous state, if it exists.
482 *
483 * @param savedState The previous state.
484 */
485 private void restoreState(Bundle savedState) {
486 if (savedState == null) {
487 return;
488 }
489
490 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
491 if (currentScreen > -1) {
492 mWorkspace.setCurrentScreen(currentScreen);
493 }
494
495 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
496 if (addScreen > -1) {
497 mAddItemCellInfo = new CellLayout.CellInfo();
498 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
499 addItemCellInfo.valid = true;
500 addItemCellInfo.screen = addScreen;
501 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
502 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
503 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
504 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
505 addItemCellInfo.findVacantCellsFromOccupied(
506 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
507 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
508 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
509 mRestoring = true;
510 }
511
512 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
513 if (renameFolder) {
514 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
515 mFolderInfo = sModel.getFolderById(this, id);
516 mRestoring = true;
517 }
518 }
519
520 /**
521 * Finds all the views we need and configure them properly.
522 */
523 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400524 mDragController = new DragController(this);
525 DragController dragController = mDragController;
526
Romain Guyb1b69f52009-08-10 15:10:15 -0700527 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400528 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529
530 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
531 final Workspace workspace = mWorkspace;
532
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
534
Joe Onorato7404ee42009-07-31 11:54:44 -0700535 mHandleView = (HandleView) findViewById(R.id.all_apps);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700536 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700537 mHandleView.setOnClickListener(this);
Romain Guyb1b69f52009-08-10 15:10:15 -0700538 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
539 handleIcon.setCrossFadeEnabled(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400542 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544
545 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400546 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700547 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548
Joe Onorato00acb122009-08-04 16:04:30 -0400549 dragController.setDragScoller(workspace);
550 dragController.setDragListener(deleteZone);
551 dragController.setScrollView(dragLayer);
552
553 // The order here is bottom to top.
554 dragController.addDropTarget(workspace);
555 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 }
557
558 /**
559 * Creates a view representing a shortcut.
560 *
561 * @param info The data structure describing the shortcut.
562 *
563 * @return A View inflated from R.layout.application.
564 */
565 View createShortcut(ApplicationInfo info) {
566 return createShortcut(R.layout.application,
567 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
568 }
569
570 /**
571 * Creates a view representing a shortcut inflated from the specified resource.
572 *
573 * @param layoutResId The id of the XML layout used to create the shortcut.
574 * @param parent The group the shortcut belongs to.
575 * @param info The data structure describing the shortcut.
576 *
577 * @return A View inflated from layoutResId.
578 */
579 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
580 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
581
582 if (!info.filtered) {
583 info.icon = Utilities.createIconThumbnail(info.icon, this);
584 info.filtered = true;
585 }
586
587 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
588 favorite.setText(info.title);
589 favorite.setTag(info);
590 favorite.setOnClickListener(this);
591
592 return favorite;
593 }
594
595 /**
596 * Add an application shortcut to the workspace.
597 *
598 * @param data The intent describing the application.
599 * @param cellInfo The position on screen where to create the shortcut.
600 */
601 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
602 boolean insertAtFirst) {
603 cellInfo.screen = mWorkspace.getCurrentScreen();
604 if (!findSingleSlot(cellInfo)) return;
605
Romain Guy73b979d2009-06-09 12:57:21 -0700606 final ApplicationInfo info = infoFromApplicationIntent(context, data);
607 if (info != null) {
608 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
609 }
610 }
611
612 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 ComponentName component = data.getComponent();
614 PackageManager packageManager = context.getPackageManager();
615 ActivityInfo activityInfo = null;
616 try {
617 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
618 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700619 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 }
Romain Guycbb89e42009-06-08 15:52:54 -0700621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 if (activityInfo != null) {
623 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 itemInfo.title = activityInfo.loadLabel(packageManager);
626 if (itemInfo.title == null) {
627 itemInfo.title = activityInfo.name;
628 }
Romain Guycbb89e42009-06-08 15:52:54 -0700629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
631 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
632 itemInfo.icon = activityInfo.loadIcon(packageManager);
633 itemInfo.container = ItemInfo.NO_ID;
634
Romain Guy73b979d2009-06-09 12:57:21 -0700635 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 }
Romain Guy73b979d2009-06-09 12:57:21 -0700637
638 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 }
Romain Guycbb89e42009-06-08 15:52:54 -0700640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 /**
642 * Add a shortcut to the workspace.
643 *
644 * @param data The intent describing the shortcut.
645 * @param cellInfo The position on screen where to create the shortcut.
646 * @param insertAtFirst
647 */
648 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
649 boolean insertAtFirst) {
650 cellInfo.screen = mWorkspace.getCurrentScreen();
651 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
654
655 if (!mRestoring) {
656 sModel.addDesktopItem(info);
657
658 final View view = createShortcut(info);
659 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
660 } else if (sModel.isDesktopLoaded()) {
661 sModel.addDesktopItem(info);
662 }
663 }
664
Romain Guycbb89e42009-06-08 15:52:54 -0700665
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700667 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700669 * @param data The intent describing the appWidgetId.
670 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700672 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 boolean insertAtFirst) {
674
675 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700677
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700678 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700679
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700680 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700681
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700682 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700684 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700685
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 // Try finding open space on Launcher screen
687 final int[] xy = mCellCoordinates;
688 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
689
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700690 // Build Launcher-specific widget info and save to database
691 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 launcherInfo.spanX = spans[0];
693 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 LauncherModel.addItemToDatabase(this, launcherInfo,
696 LauncherSettings.Favorites.CONTAINER_DESKTOP,
697 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
698
699 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700700 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700703 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700704
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700705 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
709 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
710 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700711 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
713 }
Romain Guycbb89e42009-06-08 15:52:54 -0700714
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700715 public LauncherAppWidgetHost getAppWidgetHost() {
716 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 }
Romain Guycbb89e42009-06-08 15:52:54 -0700718
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 static ApplicationInfo addShortcut(Context context, Intent data,
720 CellLayout.CellInfo cellInfo, boolean notify) {
721
Romain Guy73b979d2009-06-09 12:57:21 -0700722 final ApplicationInfo info = infoFromShortcutIntent(context, data);
723 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
724 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
725
726 return info;
727 }
728
729 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
731 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
732 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
733
734 Drawable icon = null;
735 boolean filtered = false;
736 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700737 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738
739 if (bitmap != null) {
740 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
741 filtered = true;
742 customIcon = true;
743 } else {
744 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700745 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700747 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 final PackageManager packageManager = context.getPackageManager();
749 Resources resources = packageManager.getResourcesForApplication(
750 iconResource.packageName);
751 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
752 icon = resources.getDrawable(id);
753 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700754 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 }
756 }
757 }
758
759 if (icon == null) {
760 icon = context.getPackageManager().getDefaultActivityIcon();
761 }
762
763 final ApplicationInfo info = new ApplicationInfo();
764 info.icon = icon;
765 info.filtered = filtered;
766 info.title = name;
767 info.intent = intent;
768 info.customIcon = customIcon;
769 info.iconResource = iconResource;
770
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 return info;
772 }
773
774 @Override
775 protected void onNewIntent(Intent intent) {
776 super.onNewIntent(intent);
777
778 // Close the menu
779 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
780 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700781
782 // Set this flag so that onResume knows to close the search dialog if it's open,
783 // because this was a new intent (thus a press of 'home' or some such) rather than
784 // for example onResume being called when the user pressed the 'back' button.
785 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786
787 try {
788 dismissDialog(DIALOG_CREATE_SHORTCUT);
789 // Unlock the workspace if the dialog was showing
790 mWorkspace.unlock();
791 } catch (Exception e) {
792 // An exception is thrown if the dialog is not visible, which is fine
793 }
794
795 try {
796 dismissDialog(DIALOG_RENAME_FOLDER);
797 // Unlock the workspace if the dialog was showing
798 mWorkspace.unlock();
799 } catch (Exception e) {
800 // An exception is thrown if the dialog is not visible, which is fine
801 }
802
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
804 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700805
Romain Guy94dabf12009-07-21 10:55:43 -0700806 if (!mWorkspace.isDefaultScreenShowing()) {
807 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700809
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700810 closeAllAppsDialog(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700811
812 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 if (v != null && v.getWindowToken() != null) {
814 InputMethodManager imm = (InputMethodManager)getSystemService(
815 INPUT_METHOD_SERVICE);
816 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
817 }
818 } else {
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700819 closeAllAppsDialog(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 }
821 }
822 }
823
824 @Override
825 protected void onRestoreInstanceState(Bundle savedInstanceState) {
826 // Do not call super here
827 mSavedInstanceState = savedInstanceState;
828 }
829
830 @Override
831 protected void onSaveInstanceState(Bundle outState) {
832 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
833
834 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
835 if (folders.size() > 0) {
836 final int count = folders.size();
837 long[] ids = new long[count];
838 for (int i = 0; i < count; i++) {
839 final FolderInfo info = folders.get(i).getInfo();
840 ids[i] = info.id;
841 }
842 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
843 } else {
844 super.onSaveInstanceState(outState);
845 }
846
Romain Guy3cf604f2009-06-16 13:12:53 -0700847 final boolean isConfigurationChange = getChangingConfigurations() != 0;
848
Romain Guy5a941392009-04-28 15:18:25 -0700849 // When the drawer is opened and we are saving the state because of a
850 // configuration change
Joe Onorato7404ee42009-07-31 11:54:44 -0700851 if (mAllAppsDialog.isOpen && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854
855 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
856 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
857 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
858
859 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
860 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
861 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
862 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
863 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
864 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
865 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
866 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
867 layout.getOccupiedCells());
868 }
869
870 if (mFolderInfo != null && mWaitingForResult) {
871 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
872 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
873 }
874 }
875
876 @Override
877 public void onDestroy() {
878 mDestroyed = true;
879
880 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700881
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700883 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700885 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 }
887
888 TextKeyListener.getInstance().release();
889
Joe Onorato93839052009-08-06 20:34:32 -0700890 // TODO mAllAppsGrid.setAdapter(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 sModel.unbind();
892 sModel.abortLoaders();
893
894 getContentResolver().unregisterContentObserver(mObserver);
895 unregisterReceiver(mApplicationsReceiver);
896 }
897
898 @Override
899 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700900 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 super.startActivityForResult(intent, requestCode);
902 }
903
904 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700905 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700907
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700908 closeAllAppsDialog(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700909
Karl Rosaen138a0412009-04-23 19:00:21 -0700910 // Slide the search widget to the top, if it's on the current screen,
911 // otherwise show the search dialog immediately.
912 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
913 if (searchWidget == null) {
914 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
915 } else {
916 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
917 // show the currently typed text in the search widget while sliding
918 searchWidget.setQuery(getTypedText());
919 }
920 }
Romain Guycbb89e42009-06-08 15:52:54 -0700921
Karl Rosaen138a0412009-04-23 19:00:21 -0700922 /**
923 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700924 *
925 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700926 */
Romain Guycbb89e42009-06-08 15:52:54 -0700927 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700928 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700929
Karl Rosaen138a0412009-04-23 19:00:21 -0700930 if (initialQuery == null) {
931 // Use any text typed in the launcher as the initial query
932 initialQuery = getTypedText();
933 clearTypedText();
934 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 if (appSearchData == null) {
936 appSearchData = new Bundle();
937 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
938 }
Romain Guycbb89e42009-06-08 15:52:54 -0700939
Karl Rosaen138a0412009-04-23 19:00:21 -0700940 final SearchManager searchManager =
941 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
942
943 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
944 if (searchWidget != null) {
945 // This gets called when the user leaves the search dialog to go back to
946 // the Launcher.
947 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
948 public void onCancel() {
949 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700950 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700951 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700952 });
953 }
Romain Guycbb89e42009-06-08 15:52:54 -0700954
Karl Rosaen138a0412009-04-23 19:00:21 -0700955 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700956 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 }
958
Karl Rosaen138a0412009-04-23 19:00:21 -0700959 /**
960 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700961 */
Romain Guy5a941392009-04-28 15:18:25 -0700962 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 // Close search dialog
964 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100965 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700966 // Restore search widget to its normal position
967 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
968 if (searchWidget != null) {
969 searchWidget.stopSearch(false);
970 }
971 }
Romain Guycbb89e42009-06-08 15:52:54 -0700972
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 @Override
974 public boolean onCreateOptionsMenu(Menu menu) {
975 if (mDesktopLocked) return false;
976
977 super.onCreateOptionsMenu(menu);
978 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
979 .setIcon(android.R.drawable.ic_menu_add)
980 .setAlphabeticShortcut('A');
981 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
982 .setIcon(android.R.drawable.ic_menu_gallery)
983 .setAlphabeticShortcut('W');
984 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
985 .setIcon(android.R.drawable.ic_search_category_default)
986 .setAlphabeticShortcut(SearchManager.MENU_KEY);
987 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
988 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
989 .setAlphabeticShortcut('N');
990
991 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700992 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
993 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994
995 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
996 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
997 .setIntent(settings);
998
999 return true;
1000 }
1001
1002 @Override
1003 public boolean onPrepareOptionsMenu(Menu menu) {
1004 super.onPrepareOptionsMenu(menu);
1005
1006 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1007 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1008
1009 return true;
1010 }
1011
1012 @Override
1013 public boolean onOptionsItemSelected(MenuItem item) {
1014 switch (item.getItemId()) {
1015 case MENU_ADD:
1016 addItems();
1017 return true;
1018 case MENU_WALLPAPER_SETTINGS:
1019 startWallpaper();
1020 return true;
1021 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001022 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 return true;
1024 case MENU_NOTIFICATIONS:
1025 showNotifications();
1026 return true;
1027 }
1028
1029 return super.onOptionsItemSelected(item);
1030 }
Romain Guycbb89e42009-06-08 15:52:54 -07001031
Karl Rosaen138a0412009-04-23 19:00:21 -07001032 /**
1033 * Indicates that we want global search for this activity by setting the globalSearch
1034 * argument for {@link #startSearch} to true.
1035 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001037 @Override
1038 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001039 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001040 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001041 }
1042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 private void addItems() {
1044 showAddDialog(mMenuAddInfo);
1045 }
1046
1047 private void removeShortcutsForPackage(String packageName) {
1048 if (packageName != null && packageName.length() > 0) {
1049 mWorkspace.removeShortcutsForPackage(packageName);
1050 }
1051 }
Romain Guycbb89e42009-06-08 15:52:54 -07001052
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001053 private void updateShortcutsForPackage(String packageName) {
1054 if (packageName != null && packageName.length() > 0) {
1055 mWorkspace.updateShortcutsForPackage(packageName);
1056 }
1057 }
1058
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001059 void addAppWidget(Intent data) {
1060 // TODO: catch bad widget exception when sent
1061 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001063 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1064 if (SEARCH_WIDGET.equals(customWidget)) {
1065 // We don't need this any more, since this isn't a real app widget.
1066 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1067 // add the search widget
1068 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001070 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1071
1072 if (appWidget.configure != null) {
1073 // Launch over to configure widget, if needed
1074 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1075 intent.setComponent(appWidget.configure);
1076 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1077
1078 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1079 } else {
1080 // Otherwise just add it
1081 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 }
1084 }
Romain Guycbb89e42009-06-08 15:52:54 -07001085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 void addSearch() {
1087 final Widget info = Widget.makeSearch();
1088 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 final int[] xy = mCellCoordinates;
1091 final int spanX = info.spanX;
1092 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 sModel.addDesktopItem(info);
1097 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1098 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 final View view = mInflater.inflate(info.layoutResource, null);
1101 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001102 Search search = (Search) view.findViewById(R.id.widget_search);
1103 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1106 }
1107
Romain Guy73b979d2009-06-09 12:57:21 -07001108 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001109 // Handle case where user selected "Applications"
1110 String applicationName = getResources().getString(R.string.group_applications);
1111 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001112
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001113 if (applicationName != null && applicationName.equals(shortcutName)) {
1114 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1115 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001116
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001117 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1118 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001119 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001120 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001121 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 }
1124
1125 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001126 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001127 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001128 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001129
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001130 if (folderName != null && folderName.equals(shortcutName)) {
1131 addFolder(!mDesktopLocked);
1132 } else {
1133 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 }
1136
1137 void addFolder(boolean insertAtFirst) {
1138 UserFolderInfo folderInfo = new UserFolderInfo();
1139 folderInfo.title = getText(R.string.folder_name);
1140
1141 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1142 cellInfo.screen = mWorkspace.getCurrentScreen();
1143 if (!findSingleSlot(cellInfo)) return;
1144
1145 // Update the model
1146 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1147 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1148 sModel.addDesktopItem(folderInfo);
1149 sModel.addFolder(folderInfo);
1150
1151 // Create the view
1152 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1153 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1154 mWorkspace.addInCurrentScreen(newFolder,
1155 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1156 }
Romain Guycbb89e42009-06-08 15:52:54 -07001157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1159 boolean insertAtFirst) {
1160 cellInfo.screen = mWorkspace.getCurrentScreen();
1161 if (!findSingleSlot(cellInfo)) return;
1162
1163 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1164
1165 if (!mRestoring) {
1166 sModel.addDesktopItem(info);
1167
1168 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1169 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1170 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1171 } else if (sModel.isDesktopLoaded()) {
1172 sModel.addDesktopItem(info);
1173 }
1174 }
1175
1176 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1177 CellLayout.CellInfo cellInfo, boolean notify) {
1178
1179 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1180 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1181
1182 Drawable icon = null;
1183 boolean filtered = false;
1184 Intent.ShortcutIconResource iconResource = null;
1185
1186 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1187 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1188 try {
1189 iconResource = (Intent.ShortcutIconResource) extra;
1190 final PackageManager packageManager = context.getPackageManager();
1191 Resources resources = packageManager.getResourcesForApplication(
1192 iconResource.packageName);
1193 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1194 icon = resources.getDrawable(id);
1195 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001196 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 }
1198 }
1199
1200 if (icon == null) {
1201 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1202 }
1203
1204 final LiveFolderInfo info = new LiveFolderInfo();
1205 info.icon = icon;
1206 info.filtered = filtered;
1207 info.title = name;
1208 info.iconResource = iconResource;
1209 info.uri = data.getData();
1210 info.baseIntent = baseIntent;
1211 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1212 LiveFolders.DISPLAY_MODE_GRID);
1213
1214 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1215 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1216 sModel.addFolder(info);
1217
1218 return info;
1219 }
1220
1221 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1222 final int[] xy = new int[2];
1223 if (findSlot(cellInfo, xy, 1, 1)) {
1224 cellInfo.cellX = xy[0];
1225 cellInfo.cellY = xy[1];
1226 return true;
1227 }
1228 return false;
1229 }
1230
1231 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1232 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1233 boolean[] occupied = mSavedState != null ?
1234 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1235 cellInfo = mWorkspace.findAllVacantCells(occupied);
1236 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1237 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1238 return false;
1239 }
1240 }
1241 return true;
1242 }
1243
1244 private void showNotifications() {
1245 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1246 if (statusBar != null) {
1247 statusBar.expand();
1248 }
1249 }
1250
1251 private void startWallpaper() {
1252 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1253 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1254 }
1255
1256 /**
1257 * Registers various intent receivers. The current implementation registers
1258 * only a wallpaper intent receiver to let other applications change the
1259 * wallpaper.
1260 */
1261 private void registerIntentReceivers() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1263 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1264 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1265 filter.addDataScheme("package");
1266 registerReceiver(mApplicationsReceiver, filter);
1267 }
1268
1269 /**
1270 * Registers various content observers. The current implementation registers
1271 * only a favorites observer to keep track of the favorites applications.
1272 */
1273 private void registerContentObservers() {
1274 ContentResolver resolver = getContentResolver();
1275 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1276 }
1277
1278 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001279 public void onWindowFocusChanged(boolean hasFocus) {
1280 super.onWindowFocusChanged(hasFocus);
1281 if (!hasFocus) {
1282 mBackDown = mHomeDown = false;
1283 }
1284 }
1285
1286 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 public boolean dispatchKeyEvent(KeyEvent event) {
1288 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1289 switch (event.getKeyCode()) {
1290 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001291 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 return true;
1293 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001294 mHomeDown = true;
1295 return true;
1296 }
1297 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1298 switch (event.getKeyCode()) {
1299 case KeyEvent.KEYCODE_BACK:
1300 if (!event.isCanceled()) {
1301 mWorkspace.dispatchKeyEvent(event);
Joe Onorato7404ee42009-07-31 11:54:44 -07001302 if (mAllAppsDialog.isOpen) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001303 closeAllAppsDialog(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001304 } else {
1305 closeFolder();
1306 }
1307 }
1308 mBackDown = false;
1309 return true;
1310 case KeyEvent.KEYCODE_HOME:
1311 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 return true;
1313 }
1314 }
1315
1316 return super.dispatchKeyEvent(event);
1317 }
1318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 private void closeFolder() {
1320 Folder folder = mWorkspace.getOpenFolder();
1321 if (folder != null) {
1322 closeFolder(folder);
1323 }
1324 }
1325
1326 void closeFolder(Folder folder) {
1327 folder.getInfo().opened = false;
1328 ViewGroup parent = (ViewGroup) folder.getParent();
1329 if (parent != null) {
1330 parent.removeView(folder);
Joe Onorato00acb122009-08-04 16:04:30 -04001331 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 }
1333 folder.onClose();
1334 }
1335
1336 /**
1337 * When the notification that favorites have changed is received, requests
1338 * a favorites list refresh.
1339 */
1340 private void onFavoritesChanged() {
1341 mDesktopLocked = true;
Joe Onorato7404ee42009-07-31 11:54:44 -07001342 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 sModel.loadUserItems(false, this, false, false);
1344 }
1345
1346 void onDesktopItemsLoaded() {
1347 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 bindDesktopItems();
1349 }
Romain Guycbb89e42009-06-08 15:52:54 -07001350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 /**
1352 * Refreshes the shortcuts shown on the workspace.
1353 */
1354 private void bindDesktopItems() {
1355 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001356 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001357 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1358 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 return;
1360 }
1361
1362 final Workspace workspace = mWorkspace;
1363 int count = workspace.getChildCount();
1364 for (int i = 0; i < count; i++) {
1365 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1366 }
Romain Guycbb89e42009-06-08 15:52:54 -07001367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 if (DEBUG_USER_INTERFACE) {
1369 android.widget.Button finishButton = new android.widget.Button(this);
1370 finishButton.setText("Finish");
1371 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1372
1373 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1374 public void onClick(View v) {
1375 finish();
1376 }
1377 });
1378 }
Romain Guycbb89e42009-06-08 15:52:54 -07001379
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001380 // Flag any old binder to terminate early
1381 if (mBinder != null) {
1382 mBinder.mTerminate = true;
1383 }
Romain Guycbb89e42009-06-08 15:52:54 -07001384
Karl Rosaen138a0412009-04-23 19:00:21 -07001385 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001386 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
1388
1389 private void bindItems(Launcher.DesktopBinder binder,
1390 ArrayList<ItemInfo> shortcuts, int start, int count) {
1391
1392 final Workspace workspace = mWorkspace;
1393 final boolean desktopLocked = mDesktopLocked;
1394
1395 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1396 int i = start;
1397
1398 for ( ; i < end; i++) {
1399 final ItemInfo item = shortcuts.get(i);
1400 switch (item.itemType) {
1401 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1402 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1403 final View shortcut = createShortcut((ApplicationInfo) item);
1404 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1405 !desktopLocked);
1406 break;
1407 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1408 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1409 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1410 (UserFolderInfo) item);
1411 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1412 !desktopLocked);
1413 break;
1414 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1415 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1416 R.layout.live_folder_icon, this,
1417 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1418 (LiveFolderInfo) item);
1419 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1420 !desktopLocked);
1421 break;
1422 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1423 final int screen = workspace.getCurrentScreen();
1424 final View view = mInflater.inflate(R.layout.widget_search,
1425 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001426
Karl Rosaen138a0412009-04-23 19:00:21 -07001427 Search search = (Search) view.findViewById(R.id.widget_search);
1428 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001429
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 final Widget widget = (Widget) item;
1431 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001432
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 workspace.addWidget(view, widget, !desktopLocked);
1434 break;
1435 }
1436 }
1437
1438 workspace.requestLayout();
1439
1440 if (end >= count) {
1441 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001442 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 } else {
1444 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1445 }
1446 }
1447
1448 private void finishBindDesktopItems() {
1449 if (mSavedState != null) {
1450 if (!mWorkspace.hasFocus()) {
1451 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1452 }
1453
1454 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1455 if (userFolders != null) {
1456 for (long folderId : userFolders) {
1457 final FolderInfo info = sModel.findFolderById(folderId);
1458 if (info != null) {
1459 openFolder(info);
1460 }
1461 }
1462 final Folder openFolder = mWorkspace.getOpenFolder();
1463 if (openFolder != null) {
1464 openFolder.requestFocus();
1465 }
1466 }
1467
1468 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1469 if (allApps) {
Joe Onorato45d43462009-07-31 14:36:51 -07001470 showAllAppsDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
1472
1473 mSavedState = null;
1474 }
1475
1476 if (mSavedInstanceState != null) {
1477 super.onRestoreInstanceState(mSavedInstanceState);
1478 mSavedInstanceState = null;
1479 }
1480
Joe Onorato7404ee42009-07-31 11:54:44 -07001481 /* TODO
1482 if (mAllAppsDialog.isOpen && !mDrawer.hasFocus()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 mDrawer.requestFocus();
1484 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001485 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486
1487 mDesktopLocked = false;
Joe Onorato7404ee42009-07-31 11:54:44 -07001488 mAllAppsDialog.unlock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
Romain Guycbb89e42009-06-08 15:52:54 -07001490
Karl Rosaen138a0412009-04-23 19:00:21 -07001491 private void bindDrawer(Launcher.DesktopBinder binder,
1492 ApplicationsAdapter drawerAdapter) {
Joe Onorato93839052009-08-06 20:34:32 -07001493 // TODO mAllAppsGrid.setAdapter(drawerAdapter);
Karl Rosaen138a0412009-04-23 19:00:21 -07001494 binder.startBindingAppWidgetsWhenIdle();
1495 }
Romain Guycbb89e42009-06-08 15:52:54 -07001496
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001497 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001498 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001499
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 final Workspace workspace = mWorkspace;
1501 final boolean desktopLocked = mDesktopLocked;
1502
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001503 if (!appWidgets.isEmpty()) {
1504 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001505
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001506 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001507 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001508 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001509
Karl Rosaen138a0412009-04-23 19:00:21 -07001510 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001511
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001512 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1513 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001514
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001515 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1516 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001518 workspace.requestLayout();
1519 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001521 if (appWidgets.isEmpty()) {
1522 if (PROFILE_ROTATE) {
1523 android.os.Debug.stopMethodTracing();
1524 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001526 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 }
1528 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001529
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 /**
1531 * Launches the intent referred by the clicked shortcut.
1532 *
1533 * @param v The view representing the clicked shortcut.
1534 */
1535 public void onClick(View v) {
1536 Object tag = v.getTag();
1537 if (tag instanceof ApplicationInfo) {
1538 // Open shortcut
1539 final Intent intent = ((ApplicationInfo) tag).intent;
1540 startActivitySafely(intent);
1541 } else if (tag instanceof FolderInfo) {
1542 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001543 } else if (v == mHandleView) {
1544 if (mAllAppsDialog.isOpen) {
1545 // TODO how can we be here?
1546 } else {
Joe Onorato45d43462009-07-31 14:36:51 -07001547 showAllAppsDialog();
Joe Onorato7404ee42009-07-31 11:54:44 -07001548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
1550 }
1551
1552 void startActivitySafely(Intent intent) {
1553 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1554 try {
1555 startActivity(intent);
1556 } catch (ActivityNotFoundException e) {
1557 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1558 } catch (SecurityException e) {
1559 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001560 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1562 "or use the exported attribute for this activity.", e);
1563 }
1564 }
1565
1566 private void handleFolderClick(FolderInfo folderInfo) {
1567 if (!folderInfo.opened) {
1568 // Close any open folder
1569 closeFolder();
1570 // Open the requested folder
1571 openFolder(folderInfo);
1572 } else {
1573 // Find the open folder...
1574 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1575 int folderScreen;
1576 if (openFolder != null) {
1577 folderScreen = mWorkspace.getScreenForView(openFolder);
1578 // .. and close it
1579 closeFolder(openFolder);
1580 if (folderScreen != mWorkspace.getCurrentScreen()) {
1581 // Close any folder open on the current screen
1582 closeFolder();
1583 // Pull the folder onto this screen
1584 openFolder(folderInfo);
1585 }
1586 }
1587 }
1588 }
1589
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 /**
1591 * Opens the user fodler described by the specified tag. The opening of the folder
1592 * is animated relative to the specified View. If the View is null, no animation
1593 * is played.
1594 *
1595 * @param folderInfo The FolderInfo describing the folder to open.
1596 */
1597 private void openFolder(FolderInfo folderInfo) {
1598 Folder openFolder;
1599
1600 if (folderInfo instanceof UserFolderInfo) {
1601 openFolder = UserFolder.fromXml(this);
1602 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001603 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 } else {
1605 return;
1606 }
1607
Joe Onorato00acb122009-08-04 16:04:30 -04001608 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 openFolder.setLauncher(this);
1610
1611 openFolder.bind(folderInfo);
1612 folderInfo.opened = true;
1613
1614 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1615 openFolder.onOpen();
1616 }
1617
1618 /**
1619 * Returns true if the workspace is being loaded. When the workspace is loading,
1620 * no user interaction should be allowed to avoid any conflict.
1621 *
1622 * @return True if the workspace is locked, false otherwise.
1623 */
1624 boolean isWorkspaceLocked() {
1625 return mDesktopLocked;
1626 }
1627
1628 public boolean onLongClick(View v) {
1629 if (mDesktopLocked) {
1630 return false;
1631 }
1632
1633 if (!(v instanceof CellLayout)) {
1634 v = (View) v.getParent();
1635 }
1636
1637 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1638
1639 // This happens when long clicking an item with the dpad/trackball
1640 if (cellInfo == null) {
1641 return true;
1642 }
1643
1644 if (mWorkspace.allowLongPress()) {
1645 if (cellInfo.cell == null) {
1646 if (cellInfo.valid) {
1647 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001648 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 showAddDialog(cellInfo);
1650 }
1651 } else {
1652 if (!(cellInfo.cell instanceof Folder)) {
1653 // User long pressed on an item
1654 mWorkspace.startDrag(cellInfo);
1655 }
1656 }
1657 }
1658 return true;
1659 }
1660
1661 static LauncherModel getModel() {
1662 return sModel;
1663 }
1664
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001665 View getDrawerHandle() {
1666 return mHandleView;
1667 }
1668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 boolean isDrawerDown() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001670 return /* TODO !mDrawer.isMoving() && */ !mAllAppsDialog.isOpen;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 }
1672
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 Workspace getWorkspace() {
1674 return mWorkspace;
1675 }
1676
Joe Onorato7404ee42009-07-31 11:54:44 -07001677 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 GridView getApplicationsGrid() {
1679 return mAllAppsGrid;
1680 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001681 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682
1683 @Override
1684 protected Dialog onCreateDialog(int id) {
1685 switch (id) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001686 case DIALOG_ALL_APPS:
1687 return mAllAppsDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 case DIALOG_CREATE_SHORTCUT:
1689 return new CreateShortcut().createDialog();
1690 case DIALOG_RENAME_FOLDER:
1691 return new RenameFolder().createDialog();
1692 }
1693
1694 return super.onCreateDialog(id);
1695 }
1696
1697 @Override
1698 protected void onPrepareDialog(int id, Dialog dialog) {
1699 switch (id) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001700 case DIALOG_ALL_APPS:
Joe Onorato93839052009-08-06 20:34:32 -07001701 // TODO mAllAppsGrid.onPrepareDialog();
Joe Onorato7404ee42009-07-31 11:54:44 -07001702 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 break;
1705 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001706 if (mFolderInfo != null) {
1707 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1708 final CharSequence text = mFolderInfo.title;
1709 input.setText(text);
1710 input.setSelection(0, text.length());
1711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 break;
1713 }
1714 }
1715
1716 void showRenameDialog(FolderInfo info) {
1717 mFolderInfo = info;
1718 mWaitingForResult = true;
1719 showDialog(DIALOG_RENAME_FOLDER);
1720 }
1721
1722 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1723 mAddItemCellInfo = cellInfo;
1724 mWaitingForResult = true;
1725 showDialog(DIALOG_CREATE_SHORTCUT);
1726 }
1727
Romain Guy73b979d2009-06-09 12:57:21 -07001728 private void pickShortcut(int requestCode, int title) {
1729 Bundle bundle = new Bundle();
1730
1731 ArrayList<String> shortcutNames = new ArrayList<String>();
1732 shortcutNames.add(getString(R.string.group_applications));
1733 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1734
1735 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1736 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1737 R.drawable.ic_launcher_application));
1738 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1739
1740 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1741 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1742 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1743 pickIntent.putExtras(bundle);
1744
1745 startActivityForResult(pickIntent, requestCode);
1746 }
1747
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 private class RenameFolder {
1749 private EditText mInput;
1750
1751 Dialog createDialog() {
1752 mWaitingForResult = true;
1753 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1754 mInput = (EditText) layout.findViewById(R.id.folder_name);
1755
1756 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1757 builder.setIcon(0);
1758 builder.setTitle(getString(R.string.rename_folder_title));
1759 builder.setCancelable(true);
1760 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1761 public void onCancel(DialogInterface dialog) {
1762 cleanup();
1763 }
1764 });
1765 builder.setNegativeButton(getString(R.string.cancel_action),
1766 new Dialog.OnClickListener() {
1767 public void onClick(DialogInterface dialog, int which) {
1768 cleanup();
1769 }
1770 }
1771 );
1772 builder.setPositiveButton(getString(R.string.rename_action),
1773 new Dialog.OnClickListener() {
1774 public void onClick(DialogInterface dialog, int which) {
1775 changeFolderName();
1776 }
1777 }
1778 );
1779 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001780
1781 final AlertDialog dialog = builder.create();
1782 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1783 public void onShow(DialogInterface dialog) {
1784 mWorkspace.lock();
1785 }
1786 });
1787
1788 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
1790
1791 private void changeFolderName() {
1792 final String name = mInput.getText().toString();
1793 if (!TextUtils.isEmpty(name)) {
1794 // Make sure we have the right folder info
1795 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1796 mFolderInfo.title = name;
1797 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1798
1799 if (mDesktopLocked) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001800 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 sModel.loadUserItems(false, Launcher.this, false, false);
1802 } else {
1803 final FolderIcon folderIcon = (FolderIcon)
1804 mWorkspace.getViewForTag(mFolderInfo);
1805 if (folderIcon != null) {
1806 folderIcon.setText(name);
1807 getWorkspace().requestLayout();
1808 } else {
1809 mDesktopLocked = true;
Joe Onorato7404ee42009-07-31 11:54:44 -07001810 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 sModel.loadUserItems(false, Launcher.this, false, false);
1812 }
1813 }
1814 }
1815 cleanup();
1816 }
1817
1818 private void cleanup() {
1819 mWorkspace.unlock();
1820 dismissDialog(DIALOG_RENAME_FOLDER);
1821 mWaitingForResult = false;
1822 mFolderInfo = null;
1823 }
1824 }
1825
1826 /**
Joe Onorato7404ee42009-07-31 11:54:44 -07001827 * Holds the 3d all apps view.
1828 */
1829 private class AllAppsDialog extends Dialog implements DialogInterface.OnCancelListener,
1830 DialogInterface.OnDismissListener, DialogInterface.OnShowListener {
1831
1832 boolean isOpen;
1833
1834 AllAppsDialog(Context context) {
1835 super(context, android.R.style.Theme_Translucent_NoTitleBar);
1836
1837 setOnCancelListener(this);
1838 setOnDismissListener(this);
1839 setOnShowListener(this);
1840
1841 setContentView(R.layout.all_apps);
Romain Guyb1b69f52009-08-10 15:10:15 -07001842 mAllAppsGrid = (AllAppsView) findViewById(R.id.all_apps);
Joe Onorato7404ee42009-07-31 11:54:44 -07001843
Joe Onorato00acb122009-08-04 16:04:30 -04001844 DragLayer dragLayer = (DragLayer)findViewById(R.id.drag_layer);
1845 dragLayer.setDragController(mDragController);
1846
Joe Onorato93839052009-08-06 20:34:32 -07001847 // TODO grid.setDragController(mDragController);
1848 // TODO grid.setLauncher(Launcher.this);
Joe Onorato7404ee42009-07-31 11:54:44 -07001849 }
1850
1851 public void onCancel(DialogInterface dialog) {
1852 onDestroy();
1853 }
1854
1855 public void onDismiss(DialogInterface dialog) {
1856 onDestroy();
1857 }
1858
1859 public void onShow(DialogInterface dialog) {
1860 }
1861
1862 private void onDestroy() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001863 }
1864
1865 void lock() {
1866 // TODO
1867 }
1868
1869 void unlock() {
1870 // TODO
1871 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001872
Romain Guyb1b69f52009-08-10 15:10:15 -07001873 @Override
1874 public boolean onKeyDown(int keyCode, KeyEvent event) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001875 switch (keyCode) {
1876 case KeyEvent.KEYCODE_BACK:
1877 closeAllAppsDialog(true);
1878 return true;
1879 default:
1880 return super.onKeyDown(keyCode, event);
1881 }
1882 }
1883 }
1884
1885 void showAllAppsDialog() {
1886 mAllAppsDialog.isOpen = true;
1887 showDialog(DIALOG_ALL_APPS);
1888 mWorkspace.hide();
1889 }
1890
Joe Onorato00acb122009-08-04 16:04:30 -04001891 void showWorkspace() {
1892 mWorkspace.show();
1893 }
1894
1895 void closeAllAppsDialog(boolean animated) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001896 if (mAllAppsDialog.isOpen) {
1897 if (animated) {
1898 // TODO mDrawer.animateClose();
1899 mAllAppsDialog.dismiss();
1900 } else {
1901 mAllAppsDialog.dismiss();
1902 }
1903 mAllAppsDialog.isOpen = false;
1904 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1905 mWorkspace.show();
1906 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001907 }
1908
1909 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 * Displays the shortcut creation dialog and launches, if necessary, the
1911 * appropriate activity.
1912 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001913 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001914 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1915 DialogInterface.OnShowListener {
1916
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001918
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 Dialog createDialog() {
1920 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001921
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001923
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1925 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001926 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 builder.setInverseBackgroundForced(true);
1929
1930 AlertDialog dialog = builder.create();
1931 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001932 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001933 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 return dialog;
1936 }
1937
1938 public void onCancel(DialogInterface dialog) {
1939 mWaitingForResult = false;
1940 cleanup();
1941 }
1942
Romain Guycbb89e42009-06-08 15:52:54 -07001943 public void onDismiss(DialogInterface dialog) {
1944 mWorkspace.unlock();
1945 }
1946
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 private void cleanup() {
1948 mWorkspace.unlock();
1949 dismissDialog(DIALOG_CREATE_SHORTCUT);
1950 }
1951
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001952 /**
1953 * Handle the action clicked in the "Add to home" dialog.
1954 */
1955 public void onClick(DialogInterface dialog, int which) {
1956 Resources res = getResources();
1957 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001958
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001959 switch (which) {
1960 case AddAdapter.ITEM_SHORTCUT: {
1961 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001962 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001963 break;
1964 }
Romain Guycbb89e42009-06-08 15:52:54 -07001965
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001966 case AddAdapter.ITEM_APPWIDGET: {
1967 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001968
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001969 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1970 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1971 // add the search widget
1972 ArrayList<AppWidgetProviderInfo> customInfo =
1973 new ArrayList<AppWidgetProviderInfo>();
1974 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1975 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1976 info.label = getString(R.string.group_search);
1977 info.icon = R.drawable.ic_search_widget;
1978 customInfo.add(info);
1979 pickIntent.putParcelableArrayListExtra(
1980 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1981 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1982 Bundle b = new Bundle();
1983 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1984 customExtras.add(b);
1985 pickIntent.putParcelableArrayListExtra(
1986 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1987 // start the pick activity
1988 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1989 break;
1990 }
Romain Guycbb89e42009-06-08 15:52:54 -07001991
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001992 case AddAdapter.ITEM_LIVE_FOLDER: {
1993 // Insert extra item to handle inserting folder
1994 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001995
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001996 ArrayList<String> shortcutNames = new ArrayList<String>();
1997 shortcutNames.add(res.getString(R.string.group_folder));
1998 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001999
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002000 ArrayList<ShortcutIconResource> shortcutIcons =
2001 new ArrayList<ShortcutIconResource>();
2002 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2003 R.drawable.ic_launcher_folder));
2004 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2005
2006 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2007 pickIntent.putExtra(Intent.EXTRA_INTENT,
2008 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2009 pickIntent.putExtra(Intent.EXTRA_TITLE,
2010 getText(R.string.title_select_live_folder));
2011 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002012
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002013 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2014 break;
2015 }
2016
2017 case AddAdapter.ITEM_WALLPAPER: {
2018 startWallpaper();
2019 break;
2020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 }
2022 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002023
2024 public void onShow(DialogInterface dialog) {
2025 mWorkspace.lock();
2026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028
2029 /**
2030 * Receives notifications when applications are added/removed.
2031 */
2032 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2033 @Override
2034 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002035 final String action = intent.getAction();
2036 final String packageName = intent.getData().getSchemeSpecificPart();
2037 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2038
2039 if (LauncherModel.DEBUG_LOADERS) {
2040 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2041 ", replacing=" + replacing);
2042 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002044
2045 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2046 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2047 if (!replacing) {
2048 removeShortcutsForPackage(packageName);
2049 if (LauncherModel.DEBUG_LOADERS) {
2050 d(LauncherModel.LOG_TAG, " --> remove package");
2051 }
2052 sModel.removePackage(Launcher.this, packageName);
2053 }
2054 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2055 // later, we will update the package at this time
2056 } else {
2057 if (!replacing) {
2058 if (LauncherModel.DEBUG_LOADERS) {
2059 d(LauncherModel.LOG_TAG, " --> add package");
2060 }
2061 sModel.addPackage(Launcher.this, packageName);
2062 } else {
2063 if (LauncherModel.DEBUG_LOADERS) {
2064 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2065 }
2066 sModel.updatePackage(Launcher.this, packageName);
2067 updateShortcutsForPackage(packageName);
2068 }
2069 }
2070 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002072 if (LauncherModel.DEBUG_LOADERS) {
2073 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2074 }
2075 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 }
2077 }
2078 }
2079
2080 /**
2081 * Receives notifications whenever the user favorites have changed.
2082 */
2083 private class FavoritesChangeObserver extends ContentObserver {
2084 public FavoritesChangeObserver() {
2085 super(new Handler());
2086 }
2087
2088 @Override
2089 public void onChange(boolean selfChange) {
2090 onFavoritesChanged();
2091 }
2092 }
2093
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002094 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002096 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002097 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 // Number of items to bind in every pass
2100 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101
2102 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002103 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002104 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002106
Karl Rosaen138a0412009-04-23 19:00:21 -07002107 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108
2109 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002110 ArrayList<LauncherAppWidgetInfo> appWidgets,
2111 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112
2113 mLauncher = new WeakReference<Launcher>(launcher);
2114 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002115 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002116
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002117 // Sort widgets so active workspace is bound first
2118 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2119 final int size = appWidgets.size();
2120 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002121
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002122 for (int i = 0; i < size; i++) {
2123 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2124 if (appWidgetInfo.screen == currentScreen) {
2125 mAppWidgets.addFirst(appWidgetInfo);
2126 } else {
2127 mAppWidgets.addLast(appWidgetInfo);
2128 }
2129 }
2130 }
Romain Guycbb89e42009-06-08 15:52:54 -07002131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 public void startBindingItems() {
2133 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2134 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002135
2136 public void startBindingDrawer() {
2137 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2138 }
Romain Guycbb89e42009-06-08 15:52:54 -07002139
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002140 public void startBindingAppWidgetsWhenIdle() {
2141 // Ask for notification when message queue becomes idle
2142 final MessageQueue messageQueue = Looper.myQueue();
2143 messageQueue.addIdleHandler(this);
2144 }
Romain Guycbb89e42009-06-08 15:52:54 -07002145
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002146 public boolean queueIdle() {
2147 // Queue is idle, so start binding items
2148 startBindingAppWidgets();
2149 return false;
2150 }
2151
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002152 public void startBindingAppWidgets() {
2153 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2154 }
2155
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 @Override
2157 public void handleMessage(Message msg) {
2158 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002159 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 return;
2161 }
Romain Guycbb89e42009-06-08 15:52:54 -07002162
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 switch (msg.what) {
2164 case MESSAGE_BIND_ITEMS: {
2165 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2166 break;
2167 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002168 case MESSAGE_BIND_DRAWER: {
2169 launcher.bindDrawer(this, mDrawerAdapter);
2170 break;
2171 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002172 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002173 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 break;
2175 }
2176 }
2177 }
2178 }
2179}