blob: e388ac3a341fcd5c12f590d050366bd04a150eda [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.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070058import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.view.KeyEvent;
61import android.view.LayoutInflater;
62import android.view.Menu;
63import android.view.MenuItem;
64import android.view.View;
65import android.view.ViewGroup;
Romain Guyb1b69f52009-08-10 15:10:15 -070066import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.view.View.OnLongClickListener;
68import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.widget.TextView;
71import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070072import android.appwidget.AppWidgetManager;
73import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
75import java.lang.ref.WeakReference;
76import java.util.ArrayList;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070077import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070078import java.io.DataOutputStream;
79import java.io.FileNotFoundException;
80import java.io.IOException;
81import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082
83/**
84 * Default launcher application.
85 */
86public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener {
87 static final String LOG_TAG = "Launcher";
88 static final boolean LOGD = false;
89
90 private static final boolean PROFILE_STARTUP = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070091 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 private static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 private static final int WALLPAPER_SCREENS_SPAN = 2;
95
96 private static final int MENU_GROUP_ADD = 1;
97 private static final int MENU_ADD = Menu.FIRST + 1;
98 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
99 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
100 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700101 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
103 private static final int REQUEST_CREATE_SHORTCUT = 1;
104 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700105 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 private static final int REQUEST_PICK_APPLICATION = 6;
107 private static final int REQUEST_PICK_SHORTCUT = 7;
108 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700109 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
111 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
112
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700113 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
114 static final String SEARCH_WIDGET = "search_widget";
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 static final int SCREEN_COUNT = 3;
117 static final int DEFAULT_SCREN = 1;
118 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700119 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Joe Onorato7c312c12009-08-13 21:36:53 -0700121 static final int DIALOG_CREATE_SHORTCUT = 1;
122 static final int DIALOG_RENAME_FOLDER = 2;
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 Onorato7c312c12009-08-13 21:36:53 -0700176 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700178 private AllAppsView mAllAppsGrid;
179 private boolean mAllAppsVisible;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
181 private boolean mDesktopLocked = true;
182 private Bundle mSavedState;
183
184 private SpannableStringBuilder mDefaultKeySsb = null;
185
186 private boolean mDestroyed;
Mike LeBeau736cf282009-07-02 17:46:59 -0700187
188 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
190 private boolean mRestoring;
191 private boolean mWaitingForResult;
192 private boolean mLocaleChanged;
193
Dianne Hackborn67800862009-07-24 17:15:20 -0700194 private boolean mHomeDown;
195 private boolean mBackDown;
196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private Bundle mSavedInstanceState;
198
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700199 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 @Override
202 protected void onCreate(Bundle savedInstanceState) {
203 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700206
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700207 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700208
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700209 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
210 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 if (PROFILE_STARTUP) {
213 android.os.Debug.startMethodTracing("/sdcard/launcher");
214 }
215
216 checkForLocaleChange();
217 setWallpaperDimension();
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 setContentView(R.layout.launcher);
220 setupViews();
221
Joe Onorato7c312c12009-08-13 21:36:53 -0700222 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700223
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
Joe Onorato7c312c12009-08-13 21:36:53 -0700530 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
531
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
533 final Workspace workspace = mWorkspace;
534
Joe Onorato7c312c12009-08-13 21:36:53 -0700535 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
536 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537
Joe Onorato7c312c12009-08-13 21:36:53 -0700538 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700539 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700540 mHandleView.setOnClickListener(this);
Romain Guyb1b69f52009-08-10 15:10:15 -0700541 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
542 handleIcon.setCrossFadeEnabled(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400545 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547
548 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400549 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700550 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551
Joe Onorato00acb122009-08-04 16:04:30 -0400552 dragController.setDragScoller(workspace);
553 dragController.setDragListener(deleteZone);
554 dragController.setScrollView(dragLayer);
555
556 // The order here is bottom to top.
557 dragController.addDropTarget(workspace);
558 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 }
560
561 /**
562 * Creates a view representing a shortcut.
563 *
564 * @param info The data structure describing the shortcut.
565 *
566 * @return A View inflated from R.layout.application.
567 */
568 View createShortcut(ApplicationInfo info) {
569 return createShortcut(R.layout.application,
570 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
571 }
572
573 /**
574 * Creates a view representing a shortcut inflated from the specified resource.
575 *
576 * @param layoutResId The id of the XML layout used to create the shortcut.
577 * @param parent The group the shortcut belongs to.
578 * @param info The data structure describing the shortcut.
579 *
580 * @return A View inflated from layoutResId.
581 */
582 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
583 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
584
585 if (!info.filtered) {
586 info.icon = Utilities.createIconThumbnail(info.icon, this);
587 info.filtered = true;
588 }
589
590 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
591 favorite.setText(info.title);
592 favorite.setTag(info);
593 favorite.setOnClickListener(this);
594
595 return favorite;
596 }
597
598 /**
599 * Add an application shortcut to the workspace.
600 *
601 * @param data The intent describing the application.
602 * @param cellInfo The position on screen where to create the shortcut.
603 */
604 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
605 boolean insertAtFirst) {
606 cellInfo.screen = mWorkspace.getCurrentScreen();
607 if (!findSingleSlot(cellInfo)) return;
608
Romain Guy73b979d2009-06-09 12:57:21 -0700609 final ApplicationInfo info = infoFromApplicationIntent(context, data);
610 if (info != null) {
611 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
612 }
613 }
614
615 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 ComponentName component = data.getComponent();
617 PackageManager packageManager = context.getPackageManager();
618 ActivityInfo activityInfo = null;
619 try {
620 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
621 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700622 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 }
Romain Guycbb89e42009-06-08 15:52:54 -0700624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 if (activityInfo != null) {
626 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700627
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 itemInfo.title = activityInfo.loadLabel(packageManager);
629 if (itemInfo.title == null) {
630 itemInfo.title = activityInfo.name;
631 }
Romain Guycbb89e42009-06-08 15:52:54 -0700632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
634 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
635 itemInfo.icon = activityInfo.loadIcon(packageManager);
636 itemInfo.container = ItemInfo.NO_ID;
637
Romain Guy73b979d2009-06-09 12:57:21 -0700638 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 }
Romain Guy73b979d2009-06-09 12:57:21 -0700640
641 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 }
Romain Guycbb89e42009-06-08 15:52:54 -0700643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 /**
645 * Add a shortcut to the workspace.
646 *
647 * @param data The intent describing the shortcut.
648 * @param cellInfo The position on screen where to create the shortcut.
649 * @param insertAtFirst
650 */
651 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
652 boolean insertAtFirst) {
653 cellInfo.screen = mWorkspace.getCurrentScreen();
654 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
657
658 if (!mRestoring) {
659 sModel.addDesktopItem(info);
660
661 final View view = createShortcut(info);
662 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
663 } else if (sModel.isDesktopLoaded()) {
664 sModel.addDesktopItem(info);
665 }
666 }
667
Romain Guycbb89e42009-06-08 15:52:54 -0700668
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700670 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700672 * @param data The intent describing the appWidgetId.
673 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700675 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 boolean insertAtFirst) {
677
678 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700679 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700680
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700681 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700682
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700684
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700685 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700688
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 // Try finding open space on Launcher screen
690 final int[] xy = mCellCoordinates;
691 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
692
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700693 // Build Launcher-specific widget info and save to database
694 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 launcherInfo.spanX = spans[0];
696 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 LauncherModel.addItemToDatabase(this, launcherInfo,
699 LauncherSettings.Favorites.CONTAINER_DESKTOP,
700 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
701
702 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700703 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700707
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
712 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
713 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700714 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 }
716 }
Romain Guycbb89e42009-06-08 15:52:54 -0700717
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718 public LauncherAppWidgetHost getAppWidgetHost() {
719 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 }
Romain Guycbb89e42009-06-08 15:52:54 -0700721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 static ApplicationInfo addShortcut(Context context, Intent data,
723 CellLayout.CellInfo cellInfo, boolean notify) {
724
Romain Guy73b979d2009-06-09 12:57:21 -0700725 final ApplicationInfo info = infoFromShortcutIntent(context, data);
726 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
727 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
728
729 return info;
730 }
731
732 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
734 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
735 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
736
737 Drawable icon = null;
738 boolean filtered = false;
739 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700740 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741
742 if (bitmap != null) {
743 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
744 filtered = true;
745 customIcon = true;
746 } else {
747 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700748 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700750 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 final PackageManager packageManager = context.getPackageManager();
752 Resources resources = packageManager.getResourcesForApplication(
753 iconResource.packageName);
754 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
755 icon = resources.getDrawable(id);
756 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700757 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
759 }
760 }
761
762 if (icon == null) {
763 icon = context.getPackageManager().getDefaultActivityIcon();
764 }
765
766 final ApplicationInfo info = new ApplicationInfo();
767 info.icon = icon;
768 info.filtered = filtered;
769 info.title = name;
770 info.intent = intent;
771 info.customIcon = customIcon;
772 info.iconResource = iconResource;
773
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 return info;
775 }
776
777 @Override
778 protected void onNewIntent(Intent intent) {
779 super.onNewIntent(intent);
780
781 // Close the menu
782 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
783 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700784
785 // Set this flag so that onResume knows to close the search dialog if it's open,
786 // because this was a new intent (thus a press of 'home' or some such) rather than
787 // for example onResume being called when the user pressed the 'back' button.
788 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789
790 try {
791 dismissDialog(DIALOG_CREATE_SHORTCUT);
792 // Unlock the workspace if the dialog was showing
793 mWorkspace.unlock();
794 } catch (Exception e) {
795 // An exception is thrown if the dialog is not visible, which is fine
796 }
797
798 try {
799 dismissDialog(DIALOG_RENAME_FOLDER);
800 // Unlock the workspace if the dialog was showing
801 mWorkspace.unlock();
802 } catch (Exception e) {
803 // An exception is thrown if the dialog is not visible, which is fine
804 }
805
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
807 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700808
Romain Guy94dabf12009-07-21 10:55:43 -0700809 if (!mWorkspace.isDefaultScreenShowing()) {
810 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700812
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700813 closeAllAppsDialog(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700814
815 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 if (v != null && v.getWindowToken() != null) {
817 InputMethodManager imm = (InputMethodManager)getSystemService(
818 INPUT_METHOD_SERVICE);
819 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
820 }
821 } else {
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700822 closeAllAppsDialog(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 }
824 }
825 }
826
827 @Override
828 protected void onRestoreInstanceState(Bundle savedInstanceState) {
829 // Do not call super here
830 mSavedInstanceState = savedInstanceState;
831 }
832
833 @Override
834 protected void onSaveInstanceState(Bundle outState) {
835 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
836
837 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
838 if (folders.size() > 0) {
839 final int count = folders.size();
840 long[] ids = new long[count];
841 for (int i = 0; i < count; i++) {
842 final FolderInfo info = folders.get(i).getInfo();
843 ids[i] = info.id;
844 }
845 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
846 } else {
847 super.onSaveInstanceState(outState);
848 }
849
Romain Guy3cf604f2009-06-16 13:12:53 -0700850 final boolean isConfigurationChange = getChangingConfigurations() != 0;
851
Romain Guy5a941392009-04-28 15:18:25 -0700852 // When the drawer is opened and we are saving the state because of a
853 // configuration change
Joe Onorato7c312c12009-08-13 21:36:53 -0700854 if (mAllAppsVisible && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700856 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857
858 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
859 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
860 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
861
862 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
863 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
864 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
865 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
866 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
867 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
868 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
869 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
870 layout.getOccupiedCells());
871 }
872
873 if (mFolderInfo != null && mWaitingForResult) {
874 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
875 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
876 }
877 }
878
879 @Override
880 public void onDestroy() {
881 mDestroyed = true;
882
883 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700884
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700886 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700888 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
890
891 TextKeyListener.getInstance().release();
892
Joe Onorato7c312c12009-08-13 21:36:53 -0700893 mAllAppsGrid.setAdapter(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 sModel.unbind();
895 sModel.abortLoaders();
896
897 getContentResolver().unregisterContentObserver(mObserver);
898 unregisterReceiver(mApplicationsReceiver);
899 }
900
901 @Override
902 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700903 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 super.startActivityForResult(intent, requestCode);
905 }
906
907 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700908 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700910
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700911 closeAllAppsDialog(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700912
Karl Rosaen138a0412009-04-23 19:00:21 -0700913 // Slide the search widget to the top, if it's on the current screen,
914 // otherwise show the search dialog immediately.
915 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
916 if (searchWidget == null) {
917 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
918 } else {
919 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
920 // show the currently typed text in the search widget while sliding
921 searchWidget.setQuery(getTypedText());
922 }
923 }
Romain Guycbb89e42009-06-08 15:52:54 -0700924
Karl Rosaen138a0412009-04-23 19:00:21 -0700925 /**
926 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700927 *
928 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700929 */
Romain Guycbb89e42009-06-08 15:52:54 -0700930 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700931 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700932
Karl Rosaen138a0412009-04-23 19:00:21 -0700933 if (initialQuery == null) {
934 // Use any text typed in the launcher as the initial query
935 initialQuery = getTypedText();
936 clearTypedText();
937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 if (appSearchData == null) {
939 appSearchData = new Bundle();
940 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
941 }
Romain Guycbb89e42009-06-08 15:52:54 -0700942
Karl Rosaen138a0412009-04-23 19:00:21 -0700943 final SearchManager searchManager =
944 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
945
946 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
947 if (searchWidget != null) {
948 // This gets called when the user leaves the search dialog to go back to
949 // the Launcher.
950 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
951 public void onCancel() {
952 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700953 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700954 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700955 });
956 }
Romain Guycbb89e42009-06-08 15:52:54 -0700957
Karl Rosaen138a0412009-04-23 19:00:21 -0700958 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700959 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 }
961
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 /**
963 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700964 */
Romain Guy5a941392009-04-28 15:18:25 -0700965 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700966 // Close search dialog
967 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100968 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 // Restore search widget to its normal position
970 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
971 if (searchWidget != null) {
972 searchWidget.stopSearch(false);
973 }
974 }
Romain Guycbb89e42009-06-08 15:52:54 -0700975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 @Override
977 public boolean onCreateOptionsMenu(Menu menu) {
978 if (mDesktopLocked) return false;
979
980 super.onCreateOptionsMenu(menu);
981 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
982 .setIcon(android.R.drawable.ic_menu_add)
983 .setAlphabeticShortcut('A');
984 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
985 .setIcon(android.R.drawable.ic_menu_gallery)
986 .setAlphabeticShortcut('W');
987 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
988 .setIcon(android.R.drawable.ic_search_category_default)
989 .setAlphabeticShortcut(SearchManager.MENU_KEY);
990 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
991 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
992 .setAlphabeticShortcut('N');
993
994 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700995 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
996 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997
998 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
999 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1000 .setIntent(settings);
1001
1002 return true;
1003 }
1004
1005 @Override
1006 public boolean onPrepareOptionsMenu(Menu menu) {
1007 super.onPrepareOptionsMenu(menu);
1008
1009 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1010 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1011
1012 return true;
1013 }
1014
1015 @Override
1016 public boolean onOptionsItemSelected(MenuItem item) {
1017 switch (item.getItemId()) {
1018 case MENU_ADD:
1019 addItems();
1020 return true;
1021 case MENU_WALLPAPER_SETTINGS:
1022 startWallpaper();
1023 return true;
1024 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001025 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 return true;
1027 case MENU_NOTIFICATIONS:
1028 showNotifications();
1029 return true;
1030 }
1031
1032 return super.onOptionsItemSelected(item);
1033 }
Romain Guycbb89e42009-06-08 15:52:54 -07001034
Karl Rosaen138a0412009-04-23 19:00:21 -07001035 /**
1036 * Indicates that we want global search for this activity by setting the globalSearch
1037 * argument for {@link #startSearch} to true.
1038 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001040 @Override
1041 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001042 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001043 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001044 }
1045
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 private void addItems() {
1047 showAddDialog(mMenuAddInfo);
1048 }
1049
1050 private void removeShortcutsForPackage(String packageName) {
1051 if (packageName != null && packageName.length() > 0) {
1052 mWorkspace.removeShortcutsForPackage(packageName);
1053 }
1054 }
Romain Guycbb89e42009-06-08 15:52:54 -07001055
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001056 private void updateShortcutsForPackage(String packageName) {
1057 if (packageName != null && packageName.length() > 0) {
1058 mWorkspace.updateShortcutsForPackage(packageName);
1059 }
1060 }
1061
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001062 void addAppWidget(Intent data) {
1063 // TODO: catch bad widget exception when sent
1064 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001066 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1067 if (SEARCH_WIDGET.equals(customWidget)) {
1068 // We don't need this any more, since this isn't a real app widget.
1069 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1070 // add the search widget
1071 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001073 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1074
1075 if (appWidget.configure != null) {
1076 // Launch over to configure widget, if needed
1077 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1078 intent.setComponent(appWidget.configure);
1079 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1080
1081 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1082 } else {
1083 // Otherwise just add it
1084 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1085 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 }
1087 }
Romain Guycbb89e42009-06-08 15:52:54 -07001088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 void addSearch() {
1090 final Widget info = Widget.makeSearch();
1091 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 final int[] xy = mCellCoordinates;
1094 final int spanX = info.spanX;
1095 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 sModel.addDesktopItem(info);
1100 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1101 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 final View view = mInflater.inflate(info.layoutResource, null);
1104 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001105 Search search = (Search) view.findViewById(R.id.widget_search);
1106 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1109 }
1110
Romain Guy73b979d2009-06-09 12:57:21 -07001111 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001112 // Handle case where user selected "Applications"
1113 String applicationName = getResources().getString(R.string.group_applications);
1114 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001115
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001116 if (applicationName != null && applicationName.equals(shortcutName)) {
1117 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1118 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001119
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001120 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1121 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001122 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001123 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001124 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 }
1127
1128 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001129 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001130 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001131 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001132
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001133 if (folderName != null && folderName.equals(shortcutName)) {
1134 addFolder(!mDesktopLocked);
1135 } else {
1136 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
1139
1140 void addFolder(boolean insertAtFirst) {
1141 UserFolderInfo folderInfo = new UserFolderInfo();
1142 folderInfo.title = getText(R.string.folder_name);
1143
1144 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1145 cellInfo.screen = mWorkspace.getCurrentScreen();
1146 if (!findSingleSlot(cellInfo)) return;
1147
1148 // Update the model
1149 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1150 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1151 sModel.addDesktopItem(folderInfo);
1152 sModel.addFolder(folderInfo);
1153
1154 // Create the view
1155 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1156 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1157 mWorkspace.addInCurrentScreen(newFolder,
1158 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1159 }
Romain Guycbb89e42009-06-08 15:52:54 -07001160
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1162 boolean insertAtFirst) {
1163 cellInfo.screen = mWorkspace.getCurrentScreen();
1164 if (!findSingleSlot(cellInfo)) return;
1165
1166 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1167
1168 if (!mRestoring) {
1169 sModel.addDesktopItem(info);
1170
1171 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1172 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1173 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1174 } else if (sModel.isDesktopLoaded()) {
1175 sModel.addDesktopItem(info);
1176 }
1177 }
1178
1179 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1180 CellLayout.CellInfo cellInfo, boolean notify) {
1181
1182 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1183 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1184
1185 Drawable icon = null;
1186 boolean filtered = false;
1187 Intent.ShortcutIconResource iconResource = null;
1188
1189 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1190 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1191 try {
1192 iconResource = (Intent.ShortcutIconResource) extra;
1193 final PackageManager packageManager = context.getPackageManager();
1194 Resources resources = packageManager.getResourcesForApplication(
1195 iconResource.packageName);
1196 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1197 icon = resources.getDrawable(id);
1198 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001199 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 }
1201 }
1202
1203 if (icon == null) {
1204 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1205 }
1206
1207 final LiveFolderInfo info = new LiveFolderInfo();
1208 info.icon = icon;
1209 info.filtered = filtered;
1210 info.title = name;
1211 info.iconResource = iconResource;
1212 info.uri = data.getData();
1213 info.baseIntent = baseIntent;
1214 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1215 LiveFolders.DISPLAY_MODE_GRID);
1216
1217 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1218 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1219 sModel.addFolder(info);
1220
1221 return info;
1222 }
1223
1224 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1225 final int[] xy = new int[2];
1226 if (findSlot(cellInfo, xy, 1, 1)) {
1227 cellInfo.cellX = xy[0];
1228 cellInfo.cellY = xy[1];
1229 return true;
1230 }
1231 return false;
1232 }
1233
1234 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1235 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1236 boolean[] occupied = mSavedState != null ?
1237 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1238 cellInfo = mWorkspace.findAllVacantCells(occupied);
1239 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1240 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1241 return false;
1242 }
1243 }
1244 return true;
1245 }
1246
1247 private void showNotifications() {
1248 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1249 if (statusBar != null) {
1250 statusBar.expand();
1251 }
1252 }
1253
1254 private void startWallpaper() {
1255 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1256 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1257 }
1258
1259 /**
1260 * Registers various intent receivers. The current implementation registers
1261 * only a wallpaper intent receiver to let other applications change the
1262 * wallpaper.
1263 */
1264 private void registerIntentReceivers() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1266 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1267 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1268 filter.addDataScheme("package");
1269 registerReceiver(mApplicationsReceiver, filter);
1270 }
1271
1272 /**
1273 * Registers various content observers. The current implementation registers
1274 * only a favorites observer to keep track of the favorites applications.
1275 */
1276 private void registerContentObservers() {
1277 ContentResolver resolver = getContentResolver();
1278 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1279 }
1280
1281 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001282 public void onWindowFocusChanged(boolean hasFocus) {
1283 super.onWindowFocusChanged(hasFocus);
1284 if (!hasFocus) {
1285 mBackDown = mHomeDown = false;
1286 }
1287 }
1288
1289 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 public boolean dispatchKeyEvent(KeyEvent event) {
1291 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1292 switch (event.getKeyCode()) {
1293 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001294 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 return true;
1296 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001297 mHomeDown = true;
1298 return true;
1299 }
1300 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1301 switch (event.getKeyCode()) {
1302 case KeyEvent.KEYCODE_BACK:
1303 if (!event.isCanceled()) {
1304 mWorkspace.dispatchKeyEvent(event);
Joe Onorato7c312c12009-08-13 21:36:53 -07001305 if (mAllAppsVisible) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001306 closeAllAppsDialog(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001307 } else {
1308 closeFolder();
1309 }
1310 }
1311 mBackDown = false;
1312 return true;
1313 case KeyEvent.KEYCODE_HOME:
1314 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 return true;
1316 }
1317 }
1318
1319 return super.dispatchKeyEvent(event);
1320 }
1321
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 private void closeFolder() {
1323 Folder folder = mWorkspace.getOpenFolder();
1324 if (folder != null) {
1325 closeFolder(folder);
1326 }
1327 }
1328
1329 void closeFolder(Folder folder) {
1330 folder.getInfo().opened = false;
1331 ViewGroup parent = (ViewGroup) folder.getParent();
1332 if (parent != null) {
1333 parent.removeView(folder);
Joe Onorato00acb122009-08-04 16:04:30 -04001334 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 }
1336 folder.onClose();
1337 }
1338
1339 /**
1340 * When the notification that favorites have changed is received, requests
1341 * a favorites list refresh.
1342 */
1343 private void onFavoritesChanged() {
1344 mDesktopLocked = true;
Joe Onorato7c312c12009-08-13 21:36:53 -07001345 lockAllApps();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 sModel.loadUserItems(false, this, false, false);
1347 }
1348
1349 void onDesktopItemsLoaded() {
1350 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 bindDesktopItems();
1352 }
Romain Guycbb89e42009-06-08 15:52:54 -07001353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 /**
1355 * Refreshes the shortcuts shown on the workspace.
1356 */
1357 private void bindDesktopItems() {
1358 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001359 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001360 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1361 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 return;
1363 }
1364
1365 final Workspace workspace = mWorkspace;
1366 int count = workspace.getChildCount();
1367 for (int i = 0; i < count; i++) {
1368 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1369 }
Romain Guycbb89e42009-06-08 15:52:54 -07001370
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 if (DEBUG_USER_INTERFACE) {
1372 android.widget.Button finishButton = new android.widget.Button(this);
1373 finishButton.setText("Finish");
1374 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1375
1376 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1377 public void onClick(View v) {
1378 finish();
1379 }
1380 });
1381 }
Romain Guycbb89e42009-06-08 15:52:54 -07001382
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001383 // Flag any old binder to terminate early
1384 if (mBinder != null) {
1385 mBinder.mTerminate = true;
1386 }
Romain Guycbb89e42009-06-08 15:52:54 -07001387
Karl Rosaen138a0412009-04-23 19:00:21 -07001388 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001389 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 }
1391
1392 private void bindItems(Launcher.DesktopBinder binder,
1393 ArrayList<ItemInfo> shortcuts, int start, int count) {
1394
1395 final Workspace workspace = mWorkspace;
1396 final boolean desktopLocked = mDesktopLocked;
1397
1398 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1399 int i = start;
1400
1401 for ( ; i < end; i++) {
1402 final ItemInfo item = shortcuts.get(i);
1403 switch (item.itemType) {
1404 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1405 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1406 final View shortcut = createShortcut((ApplicationInfo) item);
1407 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1408 !desktopLocked);
1409 break;
1410 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1411 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1412 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1413 (UserFolderInfo) item);
1414 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1415 !desktopLocked);
1416 break;
1417 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1418 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1419 R.layout.live_folder_icon, this,
1420 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1421 (LiveFolderInfo) item);
1422 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1423 !desktopLocked);
1424 break;
1425 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1426 final int screen = workspace.getCurrentScreen();
1427 final View view = mInflater.inflate(R.layout.widget_search,
1428 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001429
Karl Rosaen138a0412009-04-23 19:00:21 -07001430 Search search = (Search) view.findViewById(R.id.widget_search);
1431 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001432
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 final Widget widget = (Widget) item;
1434 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001435
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 workspace.addWidget(view, widget, !desktopLocked);
1437 break;
1438 }
1439 }
1440
1441 workspace.requestLayout();
1442
1443 if (end >= count) {
1444 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001445 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 } else {
1447 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1448 }
1449 }
1450
1451 private void finishBindDesktopItems() {
1452 if (mSavedState != null) {
1453 if (!mWorkspace.hasFocus()) {
1454 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1455 }
1456
1457 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1458 if (userFolders != null) {
1459 for (long folderId : userFolders) {
1460 final FolderInfo info = sModel.findFolderById(folderId);
1461 if (info != null) {
1462 openFolder(info);
1463 }
1464 }
1465 final Folder openFolder = mWorkspace.getOpenFolder();
1466 if (openFolder != null) {
1467 openFolder.requestFocus();
1468 }
1469 }
1470
1471 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1472 if (allApps) {
Joe Onorato45d43462009-07-31 14:36:51 -07001473 showAllAppsDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475
1476 mSavedState = null;
1477 }
1478
1479 if (mSavedInstanceState != null) {
1480 super.onRestoreInstanceState(mSavedInstanceState);
1481 mSavedInstanceState = null;
1482 }
1483
Joe Onorato7404ee42009-07-31 11:54:44 -07001484 /* TODO
Joe Onorato7c312c12009-08-13 21:36:53 -07001485 if (mAllAppsVisible && !mDrawer.hasFocus()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 mDrawer.requestFocus();
1487 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001488 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489
1490 mDesktopLocked = false;
Joe Onorato7c312c12009-08-13 21:36:53 -07001491 unlockAllApps();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 }
Romain Guycbb89e42009-06-08 15:52:54 -07001493
Karl Rosaen138a0412009-04-23 19:00:21 -07001494 private void bindDrawer(Launcher.DesktopBinder binder,
1495 ApplicationsAdapter drawerAdapter) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001496 mAllAppsGrid.setAdapter(drawerAdapter);
Karl Rosaen138a0412009-04-23 19:00:21 -07001497 binder.startBindingAppWidgetsWhenIdle();
1498 }
Romain Guycbb89e42009-06-08 15:52:54 -07001499
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001500 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001501 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001502
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 final Workspace workspace = mWorkspace;
1504 final boolean desktopLocked = mDesktopLocked;
1505
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001506 if (!appWidgets.isEmpty()) {
1507 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001508
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001509 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001510 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001511 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001512
Karl Rosaen138a0412009-04-23 19:00:21 -07001513 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001514
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001515 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1516 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001518 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1519 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001520
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001521 workspace.requestLayout();
1522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001524 if (appWidgets.isEmpty()) {
1525 if (PROFILE_ROTATE) {
1526 android.os.Debug.stopMethodTracing();
1527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001529 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
1531 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 /**
1534 * Launches the intent referred by the clicked shortcut.
1535 *
1536 * @param v The view representing the clicked shortcut.
1537 */
1538 public void onClick(View v) {
1539 Object tag = v.getTag();
1540 if (tag instanceof ApplicationInfo) {
1541 // Open shortcut
1542 final Intent intent = ((ApplicationInfo) tag).intent;
1543 startActivitySafely(intent);
1544 } else if (tag instanceof FolderInfo) {
1545 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001546 } else if (v == mHandleView) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001547 if (mAllAppsVisible) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001548 // TODO how can we be here?
1549 } else {
Joe Onorato45d43462009-07-31 14:36:51 -07001550 showAllAppsDialog();
Joe Onorato7404ee42009-07-31 11:54:44 -07001551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
1553 }
1554
1555 void startActivitySafely(Intent intent) {
1556 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1557 try {
1558 startActivity(intent);
1559 } catch (ActivityNotFoundException e) {
1560 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1561 } catch (SecurityException e) {
1562 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001563 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1565 "or use the exported attribute for this activity.", e);
1566 }
1567 }
1568
1569 private void handleFolderClick(FolderInfo folderInfo) {
1570 if (!folderInfo.opened) {
1571 // Close any open folder
1572 closeFolder();
1573 // Open the requested folder
1574 openFolder(folderInfo);
1575 } else {
1576 // Find the open folder...
1577 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1578 int folderScreen;
1579 if (openFolder != null) {
1580 folderScreen = mWorkspace.getScreenForView(openFolder);
1581 // .. and close it
1582 closeFolder(openFolder);
1583 if (folderScreen != mWorkspace.getCurrentScreen()) {
1584 // Close any folder open on the current screen
1585 closeFolder();
1586 // Pull the folder onto this screen
1587 openFolder(folderInfo);
1588 }
1589 }
1590 }
1591 }
1592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 /**
1594 * Opens the user fodler described by the specified tag. The opening of the folder
1595 * is animated relative to the specified View. If the View is null, no animation
1596 * is played.
1597 *
1598 * @param folderInfo The FolderInfo describing the folder to open.
1599 */
1600 private void openFolder(FolderInfo folderInfo) {
1601 Folder openFolder;
1602
1603 if (folderInfo instanceof UserFolderInfo) {
1604 openFolder = UserFolder.fromXml(this);
1605 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001606 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 } else {
1608 return;
1609 }
1610
Joe Onorato00acb122009-08-04 16:04:30 -04001611 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 openFolder.setLauncher(this);
1613
1614 openFolder.bind(folderInfo);
1615 folderInfo.opened = true;
1616
1617 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1618 openFolder.onOpen();
1619 }
1620
1621 /**
1622 * Returns true if the workspace is being loaded. When the workspace is loading,
1623 * no user interaction should be allowed to avoid any conflict.
1624 *
1625 * @return True if the workspace is locked, false otherwise.
1626 */
1627 boolean isWorkspaceLocked() {
1628 return mDesktopLocked;
1629 }
1630
1631 public boolean onLongClick(View v) {
1632 if (mDesktopLocked) {
1633 return false;
1634 }
1635
1636 if (!(v instanceof CellLayout)) {
1637 v = (View) v.getParent();
1638 }
1639
1640 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1641
1642 // This happens when long clicking an item with the dpad/trackball
1643 if (cellInfo == null) {
1644 return true;
1645 }
1646
1647 if (mWorkspace.allowLongPress()) {
1648 if (cellInfo.cell == null) {
1649 if (cellInfo.valid) {
1650 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001651 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 showAddDialog(cellInfo);
1653 }
1654 } else {
1655 if (!(cellInfo.cell instanceof Folder)) {
1656 // User long pressed on an item
1657 mWorkspace.startDrag(cellInfo);
1658 }
1659 }
1660 }
1661 return true;
1662 }
1663
1664 static LauncherModel getModel() {
1665 return sModel;
1666 }
1667
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001668 View getDrawerHandle() {
1669 return mHandleView;
1670 }
1671
Joe Onorato7c312c12009-08-13 21:36:53 -07001672 boolean isDrawerDown() { // TODO rename to isAllAppsVisible()
1673 return /* TODO !mDrawer.isMoving() && */ !mAllAppsVisible;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 }
1675
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 Workspace getWorkspace() {
1677 return mWorkspace;
1678 }
1679
Joe Onorato7404ee42009-07-31 11:54:44 -07001680 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 GridView getApplicationsGrid() {
1682 return mAllAppsGrid;
1683 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001684 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685
1686 @Override
1687 protected Dialog onCreateDialog(int id) {
1688 switch (id) {
1689 case DIALOG_CREATE_SHORTCUT:
1690 return new CreateShortcut().createDialog();
1691 case DIALOG_RENAME_FOLDER:
1692 return new RenameFolder().createDialog();
1693 }
1694
1695 return super.onCreateDialog(id);
1696 }
1697
1698 @Override
1699 protected void onPrepareDialog(int id, Dialog dialog) {
1700 switch (id) {
1701 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 break;
1703 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001704 if (mFolderInfo != null) {
1705 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1706 final CharSequence text = mFolderInfo.title;
1707 input.setText(text);
1708 input.setSelection(0, text.length());
1709 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 break;
1711 }
1712 }
1713
1714 void showRenameDialog(FolderInfo info) {
1715 mFolderInfo = info;
1716 mWaitingForResult = true;
1717 showDialog(DIALOG_RENAME_FOLDER);
1718 }
1719
1720 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1721 mAddItemCellInfo = cellInfo;
1722 mWaitingForResult = true;
1723 showDialog(DIALOG_CREATE_SHORTCUT);
1724 }
1725
Romain Guy73b979d2009-06-09 12:57:21 -07001726 private void pickShortcut(int requestCode, int title) {
1727 Bundle bundle = new Bundle();
1728
1729 ArrayList<String> shortcutNames = new ArrayList<String>();
1730 shortcutNames.add(getString(R.string.group_applications));
1731 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1732
1733 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1734 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1735 R.drawable.ic_launcher_application));
1736 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1737
1738 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1739 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1740 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1741 pickIntent.putExtras(bundle);
1742
1743 startActivityForResult(pickIntent, requestCode);
1744 }
1745
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 private class RenameFolder {
1747 private EditText mInput;
1748
1749 Dialog createDialog() {
1750 mWaitingForResult = true;
1751 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1752 mInput = (EditText) layout.findViewById(R.id.folder_name);
1753
1754 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1755 builder.setIcon(0);
1756 builder.setTitle(getString(R.string.rename_folder_title));
1757 builder.setCancelable(true);
1758 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1759 public void onCancel(DialogInterface dialog) {
1760 cleanup();
1761 }
1762 });
1763 builder.setNegativeButton(getString(R.string.cancel_action),
1764 new Dialog.OnClickListener() {
1765 public void onClick(DialogInterface dialog, int which) {
1766 cleanup();
1767 }
1768 }
1769 );
1770 builder.setPositiveButton(getString(R.string.rename_action),
1771 new Dialog.OnClickListener() {
1772 public void onClick(DialogInterface dialog, int which) {
1773 changeFolderName();
1774 }
1775 }
1776 );
1777 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001778
1779 final AlertDialog dialog = builder.create();
1780 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1781 public void onShow(DialogInterface dialog) {
1782 mWorkspace.lock();
1783 }
1784 });
1785
1786 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 }
1788
1789 private void changeFolderName() {
1790 final String name = mInput.getText().toString();
1791 if (!TextUtils.isEmpty(name)) {
1792 // Make sure we have the right folder info
1793 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1794 mFolderInfo.title = name;
1795 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1796
1797 if (mDesktopLocked) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001798 lockAllApps();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 sModel.loadUserItems(false, Launcher.this, false, false);
1800 } else {
1801 final FolderIcon folderIcon = (FolderIcon)
1802 mWorkspace.getViewForTag(mFolderInfo);
1803 if (folderIcon != null) {
1804 folderIcon.setText(name);
1805 getWorkspace().requestLayout();
1806 } else {
1807 mDesktopLocked = true;
Joe Onorato7c312c12009-08-13 21:36:53 -07001808 lockAllApps();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 sModel.loadUserItems(false, Launcher.this, false, false);
1810 }
1811 }
1812 }
1813 cleanup();
1814 }
1815
1816 private void cleanup() {
1817 mWorkspace.unlock();
1818 dismissDialog(DIALOG_RENAME_FOLDER);
1819 mWaitingForResult = false;
1820 mFolderInfo = null;
1821 }
1822 }
1823
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001824 void showAllAppsDialog() {
Joe Onorato7c312c12009-08-13 21:36:53 -07001825 mAllAppsVisible = true;
1826 mAllAppsGrid.setVisibility(View.VISIBLE);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001827 mWorkspace.hide();
Joe Onorato7c312c12009-08-13 21:36:53 -07001828
1829 // TODO: fade these two too
1830 mDeleteZone.setVisibility(View.GONE);
1831 mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001832 }
1833
1834 void closeAllAppsDialog(boolean animated) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001835 if (mAllAppsVisible) {
1836 Log.d(LOG_TAG, "closing all apps");
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001837 if (animated) {
1838 // TODO mDrawer.animateClose();
Joe Onorato7c312c12009-08-13 21:36:53 -07001839 mAllAppsGrid.setVisibility(View.GONE);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001840 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001841 mAllAppsGrid.setVisibility(View.GONE);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001842 }
Joe Onorato7c312c12009-08-13 21:36:53 -07001843 mAllAppsVisible = false;
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001844 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1845 mWorkspace.show();
Joe Onorato7c312c12009-08-13 21:36:53 -07001846
1847 // TODO: fade these two too
1848 mDeleteZone.setVisibility(View.VISIBLE);
1849 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001850 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001851 }
1852
Joe Onorato7c312c12009-08-13 21:36:53 -07001853 void lockAllApps() {
1854 // TODO
1855 }
1856
1857 void unlockAllApps() {
1858 // TODO
1859 }
1860
Joe Onorato7404ee42009-07-31 11:54:44 -07001861 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 * Displays the shortcut creation dialog and launches, if necessary, the
1863 * appropriate activity.
1864 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001865 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001866 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1867 DialogInterface.OnShowListener {
1868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 Dialog createDialog() {
1872 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001873
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1877 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001878 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001879
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 builder.setInverseBackgroundForced(true);
1881
1882 AlertDialog dialog = builder.create();
1883 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001884 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001885 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 return dialog;
1888 }
1889
1890 public void onCancel(DialogInterface dialog) {
1891 mWaitingForResult = false;
1892 cleanup();
1893 }
1894
Romain Guycbb89e42009-06-08 15:52:54 -07001895 public void onDismiss(DialogInterface dialog) {
1896 mWorkspace.unlock();
1897 }
1898
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 private void cleanup() {
1900 mWorkspace.unlock();
1901 dismissDialog(DIALOG_CREATE_SHORTCUT);
1902 }
1903
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001904 /**
1905 * Handle the action clicked in the "Add to home" dialog.
1906 */
1907 public void onClick(DialogInterface dialog, int which) {
1908 Resources res = getResources();
1909 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001910
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001911 switch (which) {
1912 case AddAdapter.ITEM_SHORTCUT: {
1913 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001914 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001915 break;
1916 }
Romain Guycbb89e42009-06-08 15:52:54 -07001917
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001918 case AddAdapter.ITEM_APPWIDGET: {
1919 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001920
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001921 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1922 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1923 // add the search widget
1924 ArrayList<AppWidgetProviderInfo> customInfo =
1925 new ArrayList<AppWidgetProviderInfo>();
1926 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1927 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1928 info.label = getString(R.string.group_search);
1929 info.icon = R.drawable.ic_search_widget;
1930 customInfo.add(info);
1931 pickIntent.putParcelableArrayListExtra(
1932 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1933 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1934 Bundle b = new Bundle();
1935 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1936 customExtras.add(b);
1937 pickIntent.putParcelableArrayListExtra(
1938 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1939 // start the pick activity
1940 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1941 break;
1942 }
Romain Guycbb89e42009-06-08 15:52:54 -07001943
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001944 case AddAdapter.ITEM_LIVE_FOLDER: {
1945 // Insert extra item to handle inserting folder
1946 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001947
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001948 ArrayList<String> shortcutNames = new ArrayList<String>();
1949 shortcutNames.add(res.getString(R.string.group_folder));
1950 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001951
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001952 ArrayList<ShortcutIconResource> shortcutIcons =
1953 new ArrayList<ShortcutIconResource>();
1954 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1955 R.drawable.ic_launcher_folder));
1956 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1957
1958 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1959 pickIntent.putExtra(Intent.EXTRA_INTENT,
1960 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1961 pickIntent.putExtra(Intent.EXTRA_TITLE,
1962 getText(R.string.title_select_live_folder));
1963 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001964
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001965 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1966 break;
1967 }
1968
1969 case AddAdapter.ITEM_WALLPAPER: {
1970 startWallpaper();
1971 break;
1972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
1974 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001975
1976 public void onShow(DialogInterface dialog) {
1977 mWorkspace.lock();
1978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 }
1980
1981 /**
1982 * Receives notifications when applications are added/removed.
1983 */
1984 private class ApplicationsIntentReceiver extends BroadcastReceiver {
1985 @Override
1986 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001987 final String action = intent.getAction();
1988 final String packageName = intent.getData().getSchemeSpecificPart();
1989 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1990
1991 if (LauncherModel.DEBUG_LOADERS) {
1992 d(LauncherModel.LOG_TAG, "application intent received: " + action +
1993 ", replacing=" + replacing);
1994 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001996
1997 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1998 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1999 if (!replacing) {
2000 removeShortcutsForPackage(packageName);
2001 if (LauncherModel.DEBUG_LOADERS) {
2002 d(LauncherModel.LOG_TAG, " --> remove package");
2003 }
2004 sModel.removePackage(Launcher.this, packageName);
2005 }
2006 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2007 // later, we will update the package at this time
2008 } else {
2009 if (!replacing) {
2010 if (LauncherModel.DEBUG_LOADERS) {
2011 d(LauncherModel.LOG_TAG, " --> add package");
2012 }
2013 sModel.addPackage(Launcher.this, packageName);
2014 } else {
2015 if (LauncherModel.DEBUG_LOADERS) {
2016 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2017 }
2018 sModel.updatePackage(Launcher.this, packageName);
2019 updateShortcutsForPackage(packageName);
2020 }
2021 }
2022 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002024 if (LauncherModel.DEBUG_LOADERS) {
2025 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2026 }
2027 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 }
2029 }
2030 }
2031
2032 /**
2033 * Receives notifications whenever the user favorites have changed.
2034 */
2035 private class FavoritesChangeObserver extends ContentObserver {
2036 public FavoritesChangeObserver() {
2037 super(new Handler());
2038 }
2039
2040 @Override
2041 public void onChange(boolean selfChange) {
2042 onFavoritesChanged();
2043 }
2044 }
2045
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002046 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002048 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002049 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 // Number of items to bind in every pass
2052 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053
2054 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002055 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002056 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002058
Karl Rosaen138a0412009-04-23 19:00:21 -07002059 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060
2061 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002062 ArrayList<LauncherAppWidgetInfo> appWidgets,
2063 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064
2065 mLauncher = new WeakReference<Launcher>(launcher);
2066 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002067 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002068
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002069 // Sort widgets so active workspace is bound first
2070 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2071 final int size = appWidgets.size();
2072 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002073
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002074 for (int i = 0; i < size; i++) {
2075 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2076 if (appWidgetInfo.screen == currentScreen) {
2077 mAppWidgets.addFirst(appWidgetInfo);
2078 } else {
2079 mAppWidgets.addLast(appWidgetInfo);
2080 }
2081 }
2082 }
Romain Guycbb89e42009-06-08 15:52:54 -07002083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 public void startBindingItems() {
2085 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2086 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002087
2088 public void startBindingDrawer() {
2089 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2090 }
Romain Guycbb89e42009-06-08 15:52:54 -07002091
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002092 public void startBindingAppWidgetsWhenIdle() {
2093 // Ask for notification when message queue becomes idle
2094 final MessageQueue messageQueue = Looper.myQueue();
2095 messageQueue.addIdleHandler(this);
2096 }
Romain Guycbb89e42009-06-08 15:52:54 -07002097
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002098 public boolean queueIdle() {
2099 // Queue is idle, so start binding items
2100 startBindingAppWidgets();
2101 return false;
2102 }
2103
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002104 public void startBindingAppWidgets() {
2105 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2106 }
2107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 @Override
2109 public void handleMessage(Message msg) {
2110 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002111 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 return;
2113 }
Romain Guycbb89e42009-06-08 15:52:54 -07002114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 switch (msg.what) {
2116 case MESSAGE_BIND_ITEMS: {
2117 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2118 break;
2119 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002120 case MESSAGE_BIND_DRAWER: {
2121 launcher.bindDrawer(this, mDrawerAdapter);
2122 break;
2123 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002124 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002125 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 break;
2127 }
2128 }
2129 }
2130 }
2131}