blob: 661fbdb7745592a6a480c64e1b837e912940cbb5 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Application;
22import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070023import android.app.ISearchManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.app.SearchManager;
25import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
28import android.content.BroadcastReceiver;
29import android.content.ComponentName;
30import android.content.ContentResolver;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070035import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.pm.ActivityInfo;
37import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070040import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.graphics.drawable.BitmapDrawable;
44import android.graphics.drawable.Drawable;
45import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Bundle;
47import android.os.Handler;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070048import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070050import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.os.Parcelable;
52import android.os.RemoteException;
53import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070054import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.text.Selection;
56import android.text.SpannableStringBuilder;
57import android.text.TextUtils;
58import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070059import static android.util.Log.*;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.view.KeyEvent;
62import android.view.LayoutInflater;
63import android.view.Menu;
64import android.view.MenuItem;
65import android.view.View;
66import android.view.ViewGroup;
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;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070091 private static final boolean PROFILE_DRAWER = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070092 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 private static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070094
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 private static final int WALLPAPER_SCREENS_SPAN = 2;
96
97 private static final int MENU_GROUP_ADD = 1;
98 private static final int MENU_ADD = Menu.FIRST + 1;
99 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
100 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
101 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700102 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103
104 private static final int REQUEST_CREATE_SHORTCUT = 1;
105 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700106 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 private static final int REQUEST_PICK_APPLICATION = 6;
108 private static final int REQUEST_PICK_SHORTCUT = 7;
109 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700110 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
112 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
113
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700114 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
115 static final String SEARCH_WIDGET = "search_widget";
116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 static final int SCREEN_COUNT = 3;
118 static final int DEFAULT_SCREN = 1;
119 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700120 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Joe Onorato7404ee42009-07-31 11:54:44 -0700122 static final int DIALOG_ALL_APPS = 1;
123 static final int DIALOG_CREATE_SHORTCUT = 2;
124 static final int DIALOG_RENAME_FOLDER = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Romain Guy98d01652009-06-30 16:21:04 -0700126 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
128 // Type: int
129 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
130 // Type: boolean
131 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
132 // Type: long
133 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
148 // Type: int[]
149 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
150 // Type: boolean
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
152 // Type: long
153 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
154
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700155 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
157 private static Bitmap sWallpaper;
158
159 private static final Object sLock = new Object();
160 private static int sScreen = DEFAULT_SCREN;
161
162 private static WallpaperIntentReceiver sWallpaperReceiver;
163
164 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
165 private final ContentObserver mObserver = new FavoritesChangeObserver();
166
167 private LayoutInflater mInflater;
168
169 private DragLayer mDragLayer;
Joe Onorato59791172009-07-31 16:21:40 -0700170 private WallpaperView mWallpaperView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700172
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700173 private AppWidgetManager mAppWidgetManager;
174 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700175
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700176 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private CellLayout.CellInfo mAddItemCellInfo;
179 private CellLayout.CellInfo mMenuAddInfo;
180 private final int[] mCellCoordinates = new int[2];
181 private FolderInfo mFolderInfo;
182
Joe Onorato7404ee42009-07-31 11:54:44 -0700183 private AllAppsDialog mAllAppsDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700185 private HandleView mHandleView;
Joe Onorato7404ee42009-07-31 11:54:44 -0700186 private AllAppsGridView mAllAppsGrid; // TODO: put this into AllAppsDialog
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
188 private boolean mDesktopLocked = true;
189 private Bundle mSavedState;
190
191 private SpannableStringBuilder mDefaultKeySsb = null;
192
193 private boolean mDestroyed;
Mike LeBeau736cf282009-07-02 17:46:59 -0700194
195 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
197 private boolean mRestoring;
198 private boolean mWaitingForResult;
199 private boolean mLocaleChanged;
200
Dianne Hackborn67800862009-07-24 17:15:20 -0700201 private boolean mHomeDown;
202 private boolean mBackDown;
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 private Bundle mSavedInstanceState;
205
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700206 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 @Override
209 protected void onCreate(Bundle savedInstanceState) {
210 super.onCreate(savedInstanceState);
211 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700212
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700214
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
216 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 if (PROFILE_STARTUP) {
219 android.os.Debug.startMethodTracing("/sdcard/launcher");
220 }
221
222 checkForLocaleChange();
223 setWallpaperDimension();
224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 setContentView(R.layout.launcher);
226 setupViews();
227
Joe Onorato7404ee42009-07-31 11:54:44 -0700228 mAllAppsDialog = new AllAppsDialog(this);
229 mAllAppsDialog.lock();
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 registerIntentReceivers();
232 registerContentObservers();
233
234 mSavedState = savedInstanceState;
235 restoreState(mSavedState);
236
237 if (PROFILE_STARTUP) {
238 android.os.Debug.stopMethodTracing();
239 }
240
241 if (!mRestoring) {
242 startLoaders();
243 }
244
245 // For handling default keys
246 mDefaultKeySsb = new SpannableStringBuilder();
247 Selection.setSelection(mDefaultKeySsb, 0);
248 }
Romain Guycbb89e42009-06-08 15:52:54 -0700249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700251 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
252 readConfiguration(this, localeConfiguration);
253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 final Configuration configuration = getResources().getConfiguration();
255
Romain Guy98d01652009-06-30 16:21:04 -0700256 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 final String locale = configuration.locale.toString();
258
Romain Guy98d01652009-06-30 16:21:04 -0700259 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 final int mcc = configuration.mcc;
261
Romain Guy98d01652009-06-30 16:21:04 -0700262 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 final int mnc = configuration.mnc;
264
265 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
266
267 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700268 localeConfiguration.locale = locale;
269 localeConfiguration.mcc = mcc;
270 localeConfiguration.mnc = mnc;
271
272 writeConfiguration(this, localeConfiguration);
273 }
274 }
275
276 private static class LocaleConfiguration {
277 public String locale;
278 public int mcc = -1;
279 public int mnc = -1;
280 }
281
282 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
283 DataInputStream in = null;
284 try {
285 in = new DataInputStream(context.openFileInput(PREFERENCES));
286 configuration.locale = in.readUTF();
287 configuration.mcc = in.readInt();
288 configuration.mnc = in.readInt();
289 } catch (FileNotFoundException e) {
290 // Ignore
291 } catch (IOException e) {
292 // Ignore
293 } finally {
294 if (in != null) {
295 try {
296 in.close();
297 } catch (IOException e) {
298 // Ignore
299 }
300 }
301 }
302 }
303
304 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
305 DataOutputStream out = null;
306 try {
307 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
308 out.writeUTF(configuration.locale);
309 out.writeInt(configuration.mcc);
310 out.writeInt(configuration.mnc);
311 out.flush();
312 } catch (FileNotFoundException e) {
313 // Ignore
314 } catch (IOException e) {
315 //noinspection ResultOfMethodCallIgnored
316 context.getFileStreamPath(PREFERENCES).delete();
317 } finally {
318 if (out != null) {
319 try {
320 out.close();
321 } catch (IOException e) {
322 // Ignore
323 }
324 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 }
326 }
327
328 static int getScreen() {
329 synchronized (sLock) {
330 return sScreen;
331 }
332 }
333
334 static void setScreen(int screen) {
335 synchronized (sLock) {
336 sScreen = screen;
337 }
338 }
339
340 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700341 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
342 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
343
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 mRestoring = false;
345 }
346
347 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700348 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349
350 Display display = getWindowManager().getDefaultDisplay();
351 boolean isPortrait = display.getWidth() < display.getHeight();
352
353 final int width = isPortrait ? display.getWidth() : display.getHeight();
354 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn107f8392009-08-05 21:32:16 -0700355 wpm.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 }
357
358 @Override
359 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700360 mWaitingForResult = false;
361
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 // The pattern used here is that a user PICKs a specific application,
363 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700364
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
366 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700367
Romain Guy94dabf12009-07-21 10:55:43 -0700368 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 switch (requestCode) {
370 case REQUEST_PICK_APPLICATION:
371 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
372 break;
373 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700374 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 break;
376 case REQUEST_CREATE_SHORTCUT:
377 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
378 break;
379 case REQUEST_PICK_LIVE_FOLDER:
380 addLiveFolder(data);
381 break;
382 case REQUEST_CREATE_LIVE_FOLDER:
383 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
384 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700385 case REQUEST_PICK_APPWIDGET:
386 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700388 case REQUEST_CREATE_APPWIDGET:
389 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 break;
391 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700392 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700394 // Clean up the appWidgetId if we canceled
395 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
396 if (appWidgetId != -1) {
397 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 }
401
402 @Override
403 protected void onResume() {
404 super.onResume();
405
406 if (mRestoring) {
407 startLoaders();
408 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700409
410 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
411 // onNewIntent), then close the search dialog if needed, because it probably
412 // came from the user pressing 'home' (rather than, for example, pressing 'back').
413 if (mIsNewIntent) {
414 // Post to a handler so that this happens after the search dialog tries to open
415 // itself again.
416 mWorkspace.post(new Runnable() {
417 public void run() {
418 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
419 ServiceManager.getService(Context.SEARCH_SERVICE));
420 try {
421 searchManagerService.stopSearch();
422 } catch (RemoteException e) {
423 e(LOG_TAG, "error stopping search", e);
424 }
425 }
426 });
427 }
428
429 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 }
431
432 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 protected void onPause() {
434 super.onPause();
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700435 closeAllAppsDialog(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700436 }
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700438 @Override
439 public Object onRetainNonConfigurationInstance() {
440 // Flag any binder to stop early before switching
441 if (mBinder != null) {
442 mBinder.mTerminate = true;
443 }
Romain Guycbb89e42009-06-08 15:52:54 -0700444
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700445 if (PROFILE_ROTATE) {
446 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
447 }
448 return null;
449 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 private boolean acceptFilter() {
452 final InputMethodManager inputManager = (InputMethodManager)
453 getSystemService(Context.INPUT_METHOD_SERVICE);
454 return !inputManager.isFullscreenMode();
455 }
456
457 @Override
458 public boolean onKeyDown(int keyCode, KeyEvent event) {
459 boolean handled = super.onKeyDown(keyCode, event);
460 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
461 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
462 keyCode, event);
463 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700464 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700465 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700466 // showSearchDialog()
467 // If there are multiple keystrokes before the search dialog takes focus,
468 // onSearchRequested() will be called for every keystroke,
469 // but it is idempotent, so it's fine.
470 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 }
472 }
473
474 return handled;
475 }
476
Karl Rosaen138a0412009-04-23 19:00:21 -0700477 private String getTypedText() {
478 return mDefaultKeySsb.toString();
479 }
480
481 private void clearTypedText() {
482 mDefaultKeySsb.clear();
483 mDefaultKeySsb.clearSpans();
484 Selection.setSelection(mDefaultKeySsb, 0);
485 }
486
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 /**
488 * Restores the previous state, if it exists.
489 *
490 * @param savedState The previous state.
491 */
492 private void restoreState(Bundle savedState) {
493 if (savedState == null) {
494 return;
495 }
496
497 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
498 if (currentScreen > -1) {
499 mWorkspace.setCurrentScreen(currentScreen);
500 }
501
502 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
503 if (addScreen > -1) {
504 mAddItemCellInfo = new CellLayout.CellInfo();
505 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
506 addItemCellInfo.valid = true;
507 addItemCellInfo.screen = addScreen;
508 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
509 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
510 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
511 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
512 addItemCellInfo.findVacantCellsFromOccupied(
513 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
514 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
515 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
516 mRestoring = true;
517 }
518
519 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
520 if (renameFolder) {
521 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
522 mFolderInfo = sModel.getFolderById(this, id);
523 mRestoring = true;
524 }
525 }
526
527 /**
528 * Finds all the views we need and configure them properly.
529 */
530 private void setupViews() {
531 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
532 final DragLayer dragLayer = mDragLayer;
533
Joe Onorato59791172009-07-31 16:21:40 -0700534 mWallpaperView = (WallpaperView) dragLayer.findViewById(R.id.wallpaper);
535 final WallpaperView wallpaper = mWallpaperView;
536
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
538 final Workspace workspace = mWorkspace;
539
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
541
Joe Onorato7404ee42009-07-31 11:54:44 -0700542 mHandleView = (HandleView) findViewById(R.id.all_apps);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700543 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700544 mHandleView.setOnClickListener(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700545 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 mHandleIcon.setCrossFadeEnabled(true);
547
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 workspace.setOnLongClickListener(this);
549 workspace.setDragger(dragLayer);
550 workspace.setLauncher(this);
Joe Onorato59791172009-07-31 16:21:40 -0700551 workspace.setWallpaper(wallpaper);
552
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 loadWallpaper();
Joe Onorato59791172009-07-31 16:21:40 -0700554 wallpaper.setScreenCount(workspace.getScreenCount());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555
556 deleteZone.setLauncher(this);
557 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700558 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559
Joe Onorato7404ee42009-07-31 11:54:44 -0700560 // TODO dragLayer.setIgnoredDropTarget(grid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 dragLayer.setDragScoller(workspace);
562 dragLayer.setDragListener(deleteZone);
563 }
564
565 /**
566 * Creates a view representing a shortcut.
567 *
568 * @param info The data structure describing the shortcut.
569 *
570 * @return A View inflated from R.layout.application.
571 */
572 View createShortcut(ApplicationInfo info) {
573 return createShortcut(R.layout.application,
574 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
575 }
576
577 /**
578 * Creates a view representing a shortcut inflated from the specified resource.
579 *
580 * @param layoutResId The id of the XML layout used to create the shortcut.
581 * @param parent The group the shortcut belongs to.
582 * @param info The data structure describing the shortcut.
583 *
584 * @return A View inflated from layoutResId.
585 */
586 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
587 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
588
589 if (!info.filtered) {
590 info.icon = Utilities.createIconThumbnail(info.icon, this);
591 info.filtered = true;
592 }
593
594 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
595 favorite.setText(info.title);
596 favorite.setTag(info);
597 favorite.setOnClickListener(this);
598
599 return favorite;
600 }
601
602 /**
603 * Add an application shortcut to the workspace.
604 *
605 * @param data The intent describing the application.
606 * @param cellInfo The position on screen where to create the shortcut.
607 */
608 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
609 boolean insertAtFirst) {
610 cellInfo.screen = mWorkspace.getCurrentScreen();
611 if (!findSingleSlot(cellInfo)) return;
612
Romain Guy73b979d2009-06-09 12:57:21 -0700613 final ApplicationInfo info = infoFromApplicationIntent(context, data);
614 if (info != null) {
615 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
616 }
617 }
618
619 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 ComponentName component = data.getComponent();
621 PackageManager packageManager = context.getPackageManager();
622 ActivityInfo activityInfo = null;
623 try {
624 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
625 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700626 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 }
Romain Guycbb89e42009-06-08 15:52:54 -0700628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 if (activityInfo != null) {
630 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 itemInfo.title = activityInfo.loadLabel(packageManager);
633 if (itemInfo.title == null) {
634 itemInfo.title = activityInfo.name;
635 }
Romain Guycbb89e42009-06-08 15:52:54 -0700636
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
638 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
639 itemInfo.icon = activityInfo.loadIcon(packageManager);
640 itemInfo.container = ItemInfo.NO_ID;
641
Romain Guy73b979d2009-06-09 12:57:21 -0700642 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 }
Romain Guy73b979d2009-06-09 12:57:21 -0700644
645 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
Romain Guycbb89e42009-06-08 15:52:54 -0700647
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 /**
649 * Add a shortcut to the workspace.
650 *
651 * @param data The intent describing the shortcut.
652 * @param cellInfo The position on screen where to create the shortcut.
653 * @param insertAtFirst
654 */
655 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
656 boolean insertAtFirst) {
657 cellInfo.screen = mWorkspace.getCurrentScreen();
658 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
661
662 if (!mRestoring) {
663 sModel.addDesktopItem(info);
664
665 final View view = createShortcut(info);
666 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
667 } else if (sModel.isDesktopLoaded()) {
668 sModel.addDesktopItem(info);
669 }
670 }
671
Romain Guycbb89e42009-06-08 15:52:54 -0700672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700674 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 * @param data The intent describing the appWidgetId.
677 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700679 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 boolean insertAtFirst) {
681
682 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700684
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700685 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700686
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700688
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700689 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 // Try finding open space on Launcher screen
694 final int[] xy = mCellCoordinates;
695 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
696
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 // Build Launcher-specific widget info and save to database
698 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 launcherInfo.spanX = spans[0];
700 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 LauncherModel.addItemToDatabase(this, launcherInfo,
703 LauncherSettings.Favorites.CONTAINER_DESKTOP,
704 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
705
706 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700710 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700711
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
716 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
717 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 }
720 }
Romain Guycbb89e42009-06-08 15:52:54 -0700721
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700722 public LauncherAppWidgetHost getAppWidgetHost() {
723 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 }
Romain Guycbb89e42009-06-08 15:52:54 -0700725
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 static ApplicationInfo addShortcut(Context context, Intent data,
727 CellLayout.CellInfo cellInfo, boolean notify) {
728
Romain Guy73b979d2009-06-09 12:57:21 -0700729 final ApplicationInfo info = infoFromShortcutIntent(context, data);
730 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
731 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
732
733 return info;
734 }
735
736 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
738 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
739 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
740
741 Drawable icon = null;
742 boolean filtered = false;
743 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700744 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745
746 if (bitmap != null) {
747 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
748 filtered = true;
749 customIcon = true;
750 } else {
751 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700752 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700754 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 final PackageManager packageManager = context.getPackageManager();
756 Resources resources = packageManager.getResourcesForApplication(
757 iconResource.packageName);
758 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
759 icon = resources.getDrawable(id);
760 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700761 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
763 }
764 }
765
766 if (icon == null) {
767 icon = context.getPackageManager().getDefaultActivityIcon();
768 }
769
770 final ApplicationInfo info = new ApplicationInfo();
771 info.icon = icon;
772 info.filtered = filtered;
773 info.title = name;
774 info.intent = intent;
775 info.customIcon = customIcon;
776 info.iconResource = iconResource;
777
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 return info;
779 }
780
781 @Override
782 protected void onNewIntent(Intent intent) {
783 super.onNewIntent(intent);
784
785 // Close the menu
786 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
787 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700788
789 // Set this flag so that onResume knows to close the search dialog if it's open,
790 // because this was a new intent (thus a press of 'home' or some such) rather than
791 // for example onResume being called when the user pressed the 'back' button.
792 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793
794 try {
795 dismissDialog(DIALOG_CREATE_SHORTCUT);
796 // Unlock the workspace if the dialog was showing
797 mWorkspace.unlock();
798 } catch (Exception e) {
799 // An exception is thrown if the dialog is not visible, which is fine
800 }
801
802 try {
803 dismissDialog(DIALOG_RENAME_FOLDER);
804 // Unlock the workspace if the dialog was showing
805 mWorkspace.unlock();
806 } catch (Exception e) {
807 // An exception is thrown if the dialog is not visible, which is fine
808 }
809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
811 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700812
Romain Guy94dabf12009-07-21 10:55:43 -0700813 if (!mWorkspace.isDefaultScreenShowing()) {
814 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700816
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700817 closeAllAppsDialog(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700818
819 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 if (v != null && v.getWindowToken() != null) {
821 InputMethodManager imm = (InputMethodManager)getSystemService(
822 INPUT_METHOD_SERVICE);
823 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
824 }
825 } else {
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700826 closeAllAppsDialog(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 }
828 }
829 }
830
831 @Override
832 protected void onRestoreInstanceState(Bundle savedInstanceState) {
833 // Do not call super here
834 mSavedInstanceState = savedInstanceState;
835 }
836
837 @Override
838 protected void onSaveInstanceState(Bundle outState) {
839 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
840
841 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
842 if (folders.size() > 0) {
843 final int count = folders.size();
844 long[] ids = new long[count];
845 for (int i = 0; i < count; i++) {
846 final FolderInfo info = folders.get(i).getInfo();
847 ids[i] = info.id;
848 }
849 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
850 } else {
851 super.onSaveInstanceState(outState);
852 }
853
Romain Guy3cf604f2009-06-16 13:12:53 -0700854 final boolean isConfigurationChange = getChangingConfigurations() != 0;
855
Romain Guy5a941392009-04-28 15:18:25 -0700856 // When the drawer is opened and we are saving the state because of a
857 // configuration change
Joe Onorato7404ee42009-07-31 11:54:44 -0700858 if (mAllAppsDialog.isOpen && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861
862 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
863 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
864 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
865
866 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
867 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
868 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
869 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
870 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
871 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
872 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
873 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
874 layout.getOccupiedCells());
875 }
876
877 if (mFolderInfo != null && mWaitingForResult) {
878 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
879 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
880 }
881 }
882
883 @Override
884 public void onDestroy() {
885 mDestroyed = true;
886
887 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700888
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700890 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700892 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 }
894
895 TextKeyListener.getInstance().release();
896
897 mAllAppsGrid.clearTextFilter();
898 mAllAppsGrid.setAdapter(null);
899 sModel.unbind();
900 sModel.abortLoaders();
901
902 getContentResolver().unregisterContentObserver(mObserver);
903 unregisterReceiver(mApplicationsReceiver);
904 }
905
906 @Override
907 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700908 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 super.startActivityForResult(intent, requestCode);
910 }
911
912 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700913 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700915
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700916 closeAllAppsDialog(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700917
Karl Rosaen138a0412009-04-23 19:00:21 -0700918 // Slide the search widget to the top, if it's on the current screen,
919 // otherwise show the search dialog immediately.
920 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
921 if (searchWidget == null) {
922 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
923 } else {
924 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
925 // show the currently typed text in the search widget while sliding
926 searchWidget.setQuery(getTypedText());
927 }
928 }
Romain Guycbb89e42009-06-08 15:52:54 -0700929
Karl Rosaen138a0412009-04-23 19:00:21 -0700930 /**
931 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700932 *
933 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700934 */
Romain Guycbb89e42009-06-08 15:52:54 -0700935 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700936 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700937
Karl Rosaen138a0412009-04-23 19:00:21 -0700938 if (initialQuery == null) {
939 // Use any text typed in the launcher as the initial query
940 initialQuery = getTypedText();
941 clearTypedText();
942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 if (appSearchData == null) {
944 appSearchData = new Bundle();
945 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
946 }
Romain Guycbb89e42009-06-08 15:52:54 -0700947
Karl Rosaen138a0412009-04-23 19:00:21 -0700948 final SearchManager searchManager =
949 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
950
951 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
952 if (searchWidget != null) {
953 // This gets called when the user leaves the search dialog to go back to
954 // the Launcher.
955 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
956 public void onCancel() {
957 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700958 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700959 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700960 });
961 }
Romain Guycbb89e42009-06-08 15:52:54 -0700962
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700964 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 }
966
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 /**
968 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 */
Romain Guy5a941392009-04-28 15:18:25 -0700970 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 // Close search dialog
972 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100973 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700974 // Restore search widget to its normal position
975 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
976 if (searchWidget != null) {
977 searchWidget.stopSearch(false);
978 }
979 }
Romain Guycbb89e42009-06-08 15:52:54 -0700980
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 @Override
982 public boolean onCreateOptionsMenu(Menu menu) {
983 if (mDesktopLocked) return false;
984
985 super.onCreateOptionsMenu(menu);
986 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
987 .setIcon(android.R.drawable.ic_menu_add)
988 .setAlphabeticShortcut('A');
989 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
990 .setIcon(android.R.drawable.ic_menu_gallery)
991 .setAlphabeticShortcut('W');
992 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
993 .setIcon(android.R.drawable.ic_search_category_default)
994 .setAlphabeticShortcut(SearchManager.MENU_KEY);
995 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
996 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
997 .setAlphabeticShortcut('N');
998
999 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001000 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1001 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002
1003 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1004 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1005 .setIntent(settings);
1006
1007 return true;
1008 }
1009
1010 @Override
1011 public boolean onPrepareOptionsMenu(Menu menu) {
1012 super.onPrepareOptionsMenu(menu);
1013
1014 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1015 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1016
1017 return true;
1018 }
1019
1020 @Override
1021 public boolean onOptionsItemSelected(MenuItem item) {
1022 switch (item.getItemId()) {
1023 case MENU_ADD:
1024 addItems();
1025 return true;
1026 case MENU_WALLPAPER_SETTINGS:
1027 startWallpaper();
1028 return true;
1029 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001030 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 return true;
1032 case MENU_NOTIFICATIONS:
1033 showNotifications();
1034 return true;
1035 }
1036
1037 return super.onOptionsItemSelected(item);
1038 }
Romain Guycbb89e42009-06-08 15:52:54 -07001039
Karl Rosaen138a0412009-04-23 19:00:21 -07001040 /**
1041 * Indicates that we want global search for this activity by setting the globalSearch
1042 * argument for {@link #startSearch} to true.
1043 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001045 @Override
1046 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001047 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001048 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001049 }
1050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 private void addItems() {
1052 showAddDialog(mMenuAddInfo);
1053 }
1054
1055 private void removeShortcutsForPackage(String packageName) {
1056 if (packageName != null && packageName.length() > 0) {
1057 mWorkspace.removeShortcutsForPackage(packageName);
1058 }
1059 }
Romain Guycbb89e42009-06-08 15:52:54 -07001060
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001061 private void updateShortcutsForPackage(String packageName) {
1062 if (packageName != null && packageName.length() > 0) {
1063 mWorkspace.updateShortcutsForPackage(packageName);
1064 }
1065 }
1066
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001067 void addAppWidget(Intent data) {
1068 // TODO: catch bad widget exception when sent
1069 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001071 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1072 if (SEARCH_WIDGET.equals(customWidget)) {
1073 // We don't need this any more, since this isn't a real app widget.
1074 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1075 // add the search widget
1076 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001078 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1079
1080 if (appWidget.configure != null) {
1081 // Launch over to configure widget, if needed
1082 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1083 intent.setComponent(appWidget.configure);
1084 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1085
1086 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1087 } else {
1088 // Otherwise just add it
1089 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1090 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 }
1092 }
Romain Guycbb89e42009-06-08 15:52:54 -07001093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 void addSearch() {
1095 final Widget info = Widget.makeSearch();
1096 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 final int[] xy = mCellCoordinates;
1099 final int spanX = info.spanX;
1100 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 sModel.addDesktopItem(info);
1105 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1106 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 final View view = mInflater.inflate(info.layoutResource, null);
1109 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001110 Search search = (Search) view.findViewById(R.id.widget_search);
1111 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1114 }
1115
Romain Guy73b979d2009-06-09 12:57:21 -07001116 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001117 // Handle case where user selected "Applications"
1118 String applicationName = getResources().getString(R.string.group_applications);
1119 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001120
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001121 if (applicationName != null && applicationName.equals(shortcutName)) {
1122 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1123 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001124
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001125 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1126 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001127 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001128 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001129 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 }
1132
1133 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001134 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001135 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001136 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001137
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001138 if (folderName != null && folderName.equals(shortcutName)) {
1139 addFolder(!mDesktopLocked);
1140 } else {
1141 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 }
1144
1145 void addFolder(boolean insertAtFirst) {
1146 UserFolderInfo folderInfo = new UserFolderInfo();
1147 folderInfo.title = getText(R.string.folder_name);
1148
1149 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1150 cellInfo.screen = mWorkspace.getCurrentScreen();
1151 if (!findSingleSlot(cellInfo)) return;
1152
1153 // Update the model
1154 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1155 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1156 sModel.addDesktopItem(folderInfo);
1157 sModel.addFolder(folderInfo);
1158
1159 // Create the view
1160 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1161 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1162 mWorkspace.addInCurrentScreen(newFolder,
1163 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1164 }
Romain Guycbb89e42009-06-08 15:52:54 -07001165
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1167 boolean insertAtFirst) {
1168 cellInfo.screen = mWorkspace.getCurrentScreen();
1169 if (!findSingleSlot(cellInfo)) return;
1170
1171 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1172
1173 if (!mRestoring) {
1174 sModel.addDesktopItem(info);
1175
1176 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1177 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1178 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1179 } else if (sModel.isDesktopLoaded()) {
1180 sModel.addDesktopItem(info);
1181 }
1182 }
1183
1184 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1185 CellLayout.CellInfo cellInfo, boolean notify) {
1186
1187 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1188 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1189
1190 Drawable icon = null;
1191 boolean filtered = false;
1192 Intent.ShortcutIconResource iconResource = null;
1193
1194 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1195 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1196 try {
1197 iconResource = (Intent.ShortcutIconResource) extra;
1198 final PackageManager packageManager = context.getPackageManager();
1199 Resources resources = packageManager.getResourcesForApplication(
1200 iconResource.packageName);
1201 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1202 icon = resources.getDrawable(id);
1203 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001204 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
1206 }
1207
1208 if (icon == null) {
1209 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1210 }
1211
1212 final LiveFolderInfo info = new LiveFolderInfo();
1213 info.icon = icon;
1214 info.filtered = filtered;
1215 info.title = name;
1216 info.iconResource = iconResource;
1217 info.uri = data.getData();
1218 info.baseIntent = baseIntent;
1219 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1220 LiveFolders.DISPLAY_MODE_GRID);
1221
1222 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1223 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1224 sModel.addFolder(info);
1225
1226 return info;
1227 }
1228
1229 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1230 final int[] xy = new int[2];
1231 if (findSlot(cellInfo, xy, 1, 1)) {
1232 cellInfo.cellX = xy[0];
1233 cellInfo.cellY = xy[1];
1234 return true;
1235 }
1236 return false;
1237 }
1238
1239 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1240 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1241 boolean[] occupied = mSavedState != null ?
1242 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1243 cellInfo = mWorkspace.findAllVacantCells(occupied);
1244 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1245 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1246 return false;
1247 }
1248 }
1249 return true;
1250 }
1251
1252 private void showNotifications() {
1253 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1254 if (statusBar != null) {
1255 statusBar.expand();
1256 }
1257 }
1258
1259 private void startWallpaper() {
1260 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1261 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1262 }
1263
1264 /**
1265 * Registers various intent receivers. The current implementation registers
1266 * only a wallpaper intent receiver to let other applications change the
1267 * wallpaper.
1268 */
1269 private void registerIntentReceivers() {
1270 if (sWallpaperReceiver == null) {
1271 final Application application = getApplication();
1272
1273 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1274
1275 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1276 application.registerReceiver(sWallpaperReceiver, filter);
1277 } else {
1278 sWallpaperReceiver.setLauncher(this);
1279 }
1280
1281 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1282 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1283 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1284 filter.addDataScheme("package");
1285 registerReceiver(mApplicationsReceiver, filter);
1286 }
1287
1288 /**
1289 * Registers various content observers. The current implementation registers
1290 * only a favorites observer to keep track of the favorites applications.
1291 */
1292 private void registerContentObservers() {
1293 ContentResolver resolver = getContentResolver();
1294 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1295 }
1296
1297 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001298 public void onWindowFocusChanged(boolean hasFocus) {
1299 super.onWindowFocusChanged(hasFocus);
1300 if (!hasFocus) {
1301 mBackDown = mHomeDown = false;
1302 }
1303 }
1304
1305 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 public boolean dispatchKeyEvent(KeyEvent event) {
1307 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1308 switch (event.getKeyCode()) {
1309 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001310 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 return true;
1312 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001313 mHomeDown = true;
1314 return true;
1315 }
1316 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1317 switch (event.getKeyCode()) {
1318 case KeyEvent.KEYCODE_BACK:
1319 if (!event.isCanceled()) {
1320 mWorkspace.dispatchKeyEvent(event);
Joe Onorato7404ee42009-07-31 11:54:44 -07001321 if (mAllAppsDialog.isOpen) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001322 closeAllAppsDialog(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001323 } else {
1324 closeFolder();
1325 }
1326 }
1327 mBackDown = false;
1328 return true;
1329 case KeyEvent.KEYCODE_HOME:
1330 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 return true;
1332 }
1333 }
1334
1335 return super.dispatchKeyEvent(event);
1336 }
1337
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 private void closeFolder() {
1339 Folder folder = mWorkspace.getOpenFolder();
1340 if (folder != null) {
1341 closeFolder(folder);
1342 }
1343 }
1344
1345 void closeFolder(Folder folder) {
1346 folder.getInfo().opened = false;
1347 ViewGroup parent = (ViewGroup) folder.getParent();
1348 if (parent != null) {
1349 parent.removeView(folder);
1350 }
1351 folder.onClose();
1352 }
1353
1354 /**
1355 * When the notification that favorites have changed is received, requests
1356 * a favorites list refresh.
1357 */
1358 private void onFavoritesChanged() {
1359 mDesktopLocked = true;
Joe Onorato7404ee42009-07-31 11:54:44 -07001360 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 sModel.loadUserItems(false, this, false, false);
1362 }
1363
1364 void onDesktopItemsLoaded() {
1365 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 bindDesktopItems();
1367 }
Romain Guycbb89e42009-06-08 15:52:54 -07001368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 /**
1370 * Refreshes the shortcuts shown on the workspace.
1371 */
1372 private void bindDesktopItems() {
1373 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001374 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001375 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1376 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 return;
1378 }
1379
1380 final Workspace workspace = mWorkspace;
1381 int count = workspace.getChildCount();
1382 for (int i = 0; i < count; i++) {
1383 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1384 }
Romain Guycbb89e42009-06-08 15:52:54 -07001385
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 if (DEBUG_USER_INTERFACE) {
1387 android.widget.Button finishButton = new android.widget.Button(this);
1388 finishButton.setText("Finish");
1389 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1390
1391 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1392 public void onClick(View v) {
1393 finish();
1394 }
1395 });
1396 }
Romain Guycbb89e42009-06-08 15:52:54 -07001397
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001398 // Flag any old binder to terminate early
1399 if (mBinder != null) {
1400 mBinder.mTerminate = true;
1401 }
Romain Guycbb89e42009-06-08 15:52:54 -07001402
Karl Rosaen138a0412009-04-23 19:00:21 -07001403 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001404 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
1406
1407 private void bindItems(Launcher.DesktopBinder binder,
1408 ArrayList<ItemInfo> shortcuts, int start, int count) {
1409
1410 final Workspace workspace = mWorkspace;
1411 final boolean desktopLocked = mDesktopLocked;
1412
1413 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1414 int i = start;
1415
1416 for ( ; i < end; i++) {
1417 final ItemInfo item = shortcuts.get(i);
1418 switch (item.itemType) {
1419 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1420 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1421 final View shortcut = createShortcut((ApplicationInfo) item);
1422 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1423 !desktopLocked);
1424 break;
1425 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1426 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1427 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1428 (UserFolderInfo) item);
1429 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1430 !desktopLocked);
1431 break;
1432 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1433 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1434 R.layout.live_folder_icon, this,
1435 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1436 (LiveFolderInfo) item);
1437 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1438 !desktopLocked);
1439 break;
1440 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1441 final int screen = workspace.getCurrentScreen();
1442 final View view = mInflater.inflate(R.layout.widget_search,
1443 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001444
Karl Rosaen138a0412009-04-23 19:00:21 -07001445 Search search = (Search) view.findViewById(R.id.widget_search);
1446 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 final Widget widget = (Widget) item;
1449 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 workspace.addWidget(view, widget, !desktopLocked);
1452 break;
1453 }
1454 }
1455
1456 workspace.requestLayout();
1457
1458 if (end >= count) {
1459 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001460 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 } else {
1462 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1463 }
1464 }
1465
1466 private void finishBindDesktopItems() {
1467 if (mSavedState != null) {
1468 if (!mWorkspace.hasFocus()) {
1469 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1470 }
1471
1472 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1473 if (userFolders != null) {
1474 for (long folderId : userFolders) {
1475 final FolderInfo info = sModel.findFolderById(folderId);
1476 if (info != null) {
1477 openFolder(info);
1478 }
1479 }
1480 final Folder openFolder = mWorkspace.getOpenFolder();
1481 if (openFolder != null) {
1482 openFolder.requestFocus();
1483 }
1484 }
1485
1486 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1487 if (allApps) {
Joe Onorato45d43462009-07-31 14:36:51 -07001488 showAllAppsDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
1490
1491 mSavedState = null;
1492 }
1493
1494 if (mSavedInstanceState != null) {
1495 super.onRestoreInstanceState(mSavedInstanceState);
1496 mSavedInstanceState = null;
1497 }
1498
Joe Onorato7404ee42009-07-31 11:54:44 -07001499 /* TODO
1500 if (mAllAppsDialog.isOpen && !mDrawer.hasFocus()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 mDrawer.requestFocus();
1502 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001503 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504
1505 mDesktopLocked = false;
Joe Onorato7404ee42009-07-31 11:54:44 -07001506 mAllAppsDialog.unlock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
Romain Guycbb89e42009-06-08 15:52:54 -07001508
Karl Rosaen138a0412009-04-23 19:00:21 -07001509 private void bindDrawer(Launcher.DesktopBinder binder,
1510 ApplicationsAdapter drawerAdapter) {
1511 mAllAppsGrid.setAdapter(drawerAdapter);
1512 binder.startBindingAppWidgetsWhenIdle();
1513 }
Romain Guycbb89e42009-06-08 15:52:54 -07001514
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001515 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001516 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001517
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 final Workspace workspace = mWorkspace;
1519 final boolean desktopLocked = mDesktopLocked;
1520
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001521 if (!appWidgets.isEmpty()) {
1522 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001523
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001524 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001525 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001526 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001527
Karl Rosaen138a0412009-04-23 19:00:21 -07001528 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001529
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001530 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1531 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001532
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001533 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1534 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001535
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001536 workspace.requestLayout();
1537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001539 if (appWidgets.isEmpty()) {
1540 if (PROFILE_ROTATE) {
1541 android.os.Debug.stopMethodTracing();
1542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001544 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 }
1546 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001547
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 DragController getDragController() {
1549 return mDragLayer;
1550 }
1551
1552 /**
1553 * Launches the intent referred by the clicked shortcut.
1554 *
1555 * @param v The view representing the clicked shortcut.
1556 */
1557 public void onClick(View v) {
1558 Object tag = v.getTag();
1559 if (tag instanceof ApplicationInfo) {
1560 // Open shortcut
1561 final Intent intent = ((ApplicationInfo) tag).intent;
1562 startActivitySafely(intent);
1563 } else if (tag instanceof FolderInfo) {
1564 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001565 } else if (v == mHandleView) {
1566 if (mAllAppsDialog.isOpen) {
1567 // TODO how can we be here?
1568 } else {
Joe Onorato45d43462009-07-31 14:36:51 -07001569 showAllAppsDialog();
Joe Onorato7404ee42009-07-31 11:54:44 -07001570 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
1572 }
1573
1574 void startActivitySafely(Intent intent) {
1575 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1576 try {
1577 startActivity(intent);
1578 } catch (ActivityNotFoundException e) {
1579 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1580 } catch (SecurityException e) {
1581 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001582 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1584 "or use the exported attribute for this activity.", e);
1585 }
1586 }
1587
1588 private void handleFolderClick(FolderInfo folderInfo) {
1589 if (!folderInfo.opened) {
1590 // Close any open folder
1591 closeFolder();
1592 // Open the requested folder
1593 openFolder(folderInfo);
1594 } else {
1595 // Find the open folder...
1596 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1597 int folderScreen;
1598 if (openFolder != null) {
1599 folderScreen = mWorkspace.getScreenForView(openFolder);
1600 // .. and close it
1601 closeFolder(openFolder);
1602 if (folderScreen != mWorkspace.getCurrentScreen()) {
1603 // Close any folder open on the current screen
1604 closeFolder();
1605 // Pull the folder onto this screen
1606 openFolder(folderInfo);
1607 }
1608 }
1609 }
1610 }
1611
1612 private void loadWallpaper() {
1613 // The first time the application is started, we load the wallpaper from
1614 // the ApplicationContext
1615 if (sWallpaper == null) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001616 final Drawable drawable;
1617 if (false) {
1618 drawable = getWallpaper();
1619 } else {
1620 drawable = getResources().getDrawable(R.drawable.wallpaper_path);
1621 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 if (drawable instanceof BitmapDrawable) {
1623 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1624 } else {
1625 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1626 }
1627 }
Joe Onorato59791172009-07-31 16:21:40 -07001628 mWallpaperView.loadWallpaper(sWallpaper);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 }
1630
1631 /**
1632 * Opens the user fodler described by the specified tag. The opening of the folder
1633 * is animated relative to the specified View. If the View is null, no animation
1634 * is played.
1635 *
1636 * @param folderInfo The FolderInfo describing the folder to open.
1637 */
1638 private void openFolder(FolderInfo folderInfo) {
1639 Folder openFolder;
1640
1641 if (folderInfo instanceof UserFolderInfo) {
1642 openFolder = UserFolder.fromXml(this);
1643 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001644 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 } else {
1646 return;
1647 }
1648
1649 openFolder.setDragger(mDragLayer);
1650 openFolder.setLauncher(this);
1651
1652 openFolder.bind(folderInfo);
1653 folderInfo.opened = true;
1654
1655 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1656 openFolder.onOpen();
1657 }
1658
1659 /**
1660 * Returns true if the workspace is being loaded. When the workspace is loading,
1661 * no user interaction should be allowed to avoid any conflict.
1662 *
1663 * @return True if the workspace is locked, false otherwise.
1664 */
1665 boolean isWorkspaceLocked() {
1666 return mDesktopLocked;
1667 }
1668
1669 public boolean onLongClick(View v) {
1670 if (mDesktopLocked) {
1671 return false;
1672 }
1673
1674 if (!(v instanceof CellLayout)) {
1675 v = (View) v.getParent();
1676 }
1677
1678 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1679
1680 // This happens when long clicking an item with the dpad/trackball
1681 if (cellInfo == null) {
1682 return true;
1683 }
1684
1685 if (mWorkspace.allowLongPress()) {
1686 if (cellInfo.cell == null) {
1687 if (cellInfo.valid) {
1688 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001689 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 showAddDialog(cellInfo);
1691 }
1692 } else {
1693 if (!(cellInfo.cell instanceof Folder)) {
1694 // User long pressed on an item
1695 mWorkspace.startDrag(cellInfo);
1696 }
1697 }
1698 }
1699 return true;
1700 }
1701
1702 static LauncherModel getModel() {
1703 return sModel;
1704 }
1705
1706 void closeAllApplications() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001707 // TODO mDrawer.close();
1708 mAllAppsDialog.dismiss();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 }
1710
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001711 View getDrawerHandle() {
1712 return mHandleView;
1713 }
1714
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 boolean isDrawerDown() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001716 return /* TODO !mDrawer.isMoving() && */ !mAllAppsDialog.isOpen;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 }
1718
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 Workspace getWorkspace() {
1720 return mWorkspace;
1721 }
1722
Joe Onorato7404ee42009-07-31 11:54:44 -07001723 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 GridView getApplicationsGrid() {
1725 return mAllAppsGrid;
1726 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001727 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728
1729 @Override
1730 protected Dialog onCreateDialog(int id) {
1731 switch (id) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001732 case DIALOG_ALL_APPS:
1733 return mAllAppsDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 case DIALOG_CREATE_SHORTCUT:
1735 return new CreateShortcut().createDialog();
1736 case DIALOG_RENAME_FOLDER:
1737 return new RenameFolder().createDialog();
1738 }
1739
1740 return super.onCreateDialog(id);
1741 }
1742
1743 @Override
1744 protected void onPrepareDialog(int id, Dialog dialog) {
1745 switch (id) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001746 case DIALOG_ALL_APPS:
Joe Onorato7404ee42009-07-31 11:54:44 -07001747 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 break;
1750 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001751 if (mFolderInfo != null) {
1752 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1753 final CharSequence text = mFolderInfo.title;
1754 input.setText(text);
1755 input.setSelection(0, text.length());
1756 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 break;
1758 }
1759 }
1760
1761 void showRenameDialog(FolderInfo info) {
1762 mFolderInfo = info;
1763 mWaitingForResult = true;
1764 showDialog(DIALOG_RENAME_FOLDER);
1765 }
1766
1767 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1768 mAddItemCellInfo = cellInfo;
1769 mWaitingForResult = true;
1770 showDialog(DIALOG_CREATE_SHORTCUT);
1771 }
1772
Romain Guy73b979d2009-06-09 12:57:21 -07001773 private void pickShortcut(int requestCode, int title) {
1774 Bundle bundle = new Bundle();
1775
1776 ArrayList<String> shortcutNames = new ArrayList<String>();
1777 shortcutNames.add(getString(R.string.group_applications));
1778 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1779
1780 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1781 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1782 R.drawable.ic_launcher_application));
1783 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1784
1785 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1786 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1787 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1788 pickIntent.putExtras(bundle);
1789
1790 startActivityForResult(pickIntent, requestCode);
1791 }
1792
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 private class RenameFolder {
1794 private EditText mInput;
1795
1796 Dialog createDialog() {
1797 mWaitingForResult = true;
1798 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1799 mInput = (EditText) layout.findViewById(R.id.folder_name);
1800
1801 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1802 builder.setIcon(0);
1803 builder.setTitle(getString(R.string.rename_folder_title));
1804 builder.setCancelable(true);
1805 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1806 public void onCancel(DialogInterface dialog) {
1807 cleanup();
1808 }
1809 });
1810 builder.setNegativeButton(getString(R.string.cancel_action),
1811 new Dialog.OnClickListener() {
1812 public void onClick(DialogInterface dialog, int which) {
1813 cleanup();
1814 }
1815 }
1816 );
1817 builder.setPositiveButton(getString(R.string.rename_action),
1818 new Dialog.OnClickListener() {
1819 public void onClick(DialogInterface dialog, int which) {
1820 changeFolderName();
1821 }
1822 }
1823 );
1824 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001825
1826 final AlertDialog dialog = builder.create();
1827 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1828 public void onShow(DialogInterface dialog) {
1829 mWorkspace.lock();
1830 }
1831 });
1832
1833 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 }
1835
1836 private void changeFolderName() {
1837 final String name = mInput.getText().toString();
1838 if (!TextUtils.isEmpty(name)) {
1839 // Make sure we have the right folder info
1840 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1841 mFolderInfo.title = name;
1842 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1843
1844 if (mDesktopLocked) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001845 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 sModel.loadUserItems(false, Launcher.this, false, false);
1847 } else {
1848 final FolderIcon folderIcon = (FolderIcon)
1849 mWorkspace.getViewForTag(mFolderInfo);
1850 if (folderIcon != null) {
1851 folderIcon.setText(name);
1852 getWorkspace().requestLayout();
1853 } else {
1854 mDesktopLocked = true;
Joe Onorato7404ee42009-07-31 11:54:44 -07001855 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 sModel.loadUserItems(false, Launcher.this, false, false);
1857 }
1858 }
1859 }
1860 cleanup();
1861 }
1862
1863 private void cleanup() {
1864 mWorkspace.unlock();
1865 dismissDialog(DIALOG_RENAME_FOLDER);
1866 mWaitingForResult = false;
1867 mFolderInfo = null;
1868 }
1869 }
1870
1871 /**
Joe Onorato7404ee42009-07-31 11:54:44 -07001872 * Holds the 3d all apps view.
1873 */
1874 private class AllAppsDialog extends Dialog implements DialogInterface.OnCancelListener,
1875 DialogInterface.OnDismissListener, DialogInterface.OnShowListener {
1876
1877 boolean isOpen;
1878
1879 AllAppsDialog(Context context) {
1880 super(context, android.R.style.Theme_Translucent_NoTitleBar);
1881
1882 setOnCancelListener(this);
1883 setOnDismissListener(this);
1884 setOnShowListener(this);
1885
1886 setContentView(R.layout.all_apps);
1887 AllAppsGridView grid = mAllAppsGrid = (AllAppsGridView)findViewById(R.id.all_apps);
1888
1889 grid.setTextFilterEnabled(false);
1890 // TODO grid.setDragger(dragLayer);
1891 grid.setLauncher(Launcher.this);
1892 }
1893
1894 public void onCancel(DialogInterface dialog) {
1895 onDestroy();
1896 }
1897
1898 public void onDismiss(DialogInterface dialog) {
1899 onDestroy();
1900 }
1901
1902 public void onShow(DialogInterface dialog) {
1903 }
1904
1905 private void onDestroy() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001906 }
1907
1908 void lock() {
1909 // TODO
1910 }
1911
1912 void unlock() {
1913 // TODO
1914 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001915
1916 @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
1917 switch (keyCode) {
1918 case KeyEvent.KEYCODE_BACK:
1919 closeAllAppsDialog(true);
1920 return true;
1921 default:
1922 return super.onKeyDown(keyCode, event);
1923 }
1924 }
1925 }
1926
1927 void showAllAppsDialog() {
1928 mAllAppsDialog.isOpen = true;
1929 showDialog(DIALOG_ALL_APPS);
1930 mWorkspace.hide();
1931 }
1932
1933 private void closeAllAppsDialog(boolean animated) {
1934 if (mAllAppsDialog.isOpen) {
1935 if (animated) {
1936 // TODO mDrawer.animateClose();
1937 mAllAppsDialog.dismiss();
1938 } else {
1939 mAllAppsDialog.dismiss();
1940 }
1941 mAllAppsDialog.isOpen = false;
1942 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1943 mWorkspace.show();
1944 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001945 }
1946
1947 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 * Displays the shortcut creation dialog and launches, if necessary, the
1949 * appropriate activity.
1950 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001951 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001952 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1953 DialogInterface.OnShowListener {
1954
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001956
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 Dialog createDialog() {
1958 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001961
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1963 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001964 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 builder.setInverseBackgroundForced(true);
1967
1968 AlertDialog dialog = builder.create();
1969 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001970 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001971 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 return dialog;
1974 }
1975
1976 public void onCancel(DialogInterface dialog) {
1977 mWaitingForResult = false;
1978 cleanup();
1979 }
1980
Romain Guycbb89e42009-06-08 15:52:54 -07001981 public void onDismiss(DialogInterface dialog) {
1982 mWorkspace.unlock();
1983 }
1984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 private void cleanup() {
1986 mWorkspace.unlock();
1987 dismissDialog(DIALOG_CREATE_SHORTCUT);
1988 }
1989
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001990 /**
1991 * Handle the action clicked in the "Add to home" dialog.
1992 */
1993 public void onClick(DialogInterface dialog, int which) {
1994 Resources res = getResources();
1995 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001996
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001997 switch (which) {
1998 case AddAdapter.ITEM_SHORTCUT: {
1999 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002000 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002001 break;
2002 }
Romain Guycbb89e42009-06-08 15:52:54 -07002003
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002004 case AddAdapter.ITEM_APPWIDGET: {
2005 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002006
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002007 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2008 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2009 // add the search widget
2010 ArrayList<AppWidgetProviderInfo> customInfo =
2011 new ArrayList<AppWidgetProviderInfo>();
2012 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2013 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2014 info.label = getString(R.string.group_search);
2015 info.icon = R.drawable.ic_search_widget;
2016 customInfo.add(info);
2017 pickIntent.putParcelableArrayListExtra(
2018 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2019 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2020 Bundle b = new Bundle();
2021 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2022 customExtras.add(b);
2023 pickIntent.putParcelableArrayListExtra(
2024 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2025 // start the pick activity
2026 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2027 break;
2028 }
Romain Guycbb89e42009-06-08 15:52:54 -07002029
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002030 case AddAdapter.ITEM_LIVE_FOLDER: {
2031 // Insert extra item to handle inserting folder
2032 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002033
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002034 ArrayList<String> shortcutNames = new ArrayList<String>();
2035 shortcutNames.add(res.getString(R.string.group_folder));
2036 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002037
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002038 ArrayList<ShortcutIconResource> shortcutIcons =
2039 new ArrayList<ShortcutIconResource>();
2040 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2041 R.drawable.ic_launcher_folder));
2042 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2043
2044 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2045 pickIntent.putExtra(Intent.EXTRA_INTENT,
2046 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2047 pickIntent.putExtra(Intent.EXTRA_TITLE,
2048 getText(R.string.title_select_live_folder));
2049 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002050
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002051 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2052 break;
2053 }
2054
2055 case AddAdapter.ITEM_WALLPAPER: {
2056 startWallpaper();
2057 break;
2058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
2060 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002061
2062 public void onShow(DialogInterface dialog) {
2063 mWorkspace.lock();
2064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 }
2066
2067 /**
2068 * Receives notifications when applications are added/removed.
2069 */
2070 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2071 @Override
2072 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002073 final String action = intent.getAction();
2074 final String packageName = intent.getData().getSchemeSpecificPart();
2075 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2076
2077 if (LauncherModel.DEBUG_LOADERS) {
2078 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2079 ", replacing=" + replacing);
2080 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002082
2083 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2084 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2085 if (!replacing) {
2086 removeShortcutsForPackage(packageName);
2087 if (LauncherModel.DEBUG_LOADERS) {
2088 d(LauncherModel.LOG_TAG, " --> remove package");
2089 }
2090 sModel.removePackage(Launcher.this, packageName);
2091 }
2092 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2093 // later, we will update the package at this time
2094 } else {
2095 if (!replacing) {
2096 if (LauncherModel.DEBUG_LOADERS) {
2097 d(LauncherModel.LOG_TAG, " --> add package");
2098 }
2099 sModel.addPackage(Launcher.this, packageName);
2100 } else {
2101 if (LauncherModel.DEBUG_LOADERS) {
2102 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2103 }
2104 sModel.updatePackage(Launcher.this, packageName);
2105 updateShortcutsForPackage(packageName);
2106 }
2107 }
2108 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002110 if (LauncherModel.DEBUG_LOADERS) {
2111 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2112 }
2113 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 }
2115 }
2116 }
2117
2118 /**
2119 * Receives notifications whenever the user favorites have changed.
2120 */
2121 private class FavoritesChangeObserver extends ContentObserver {
2122 public FavoritesChangeObserver() {
2123 super(new Handler());
2124 }
2125
2126 @Override
2127 public void onChange(boolean selfChange) {
2128 onFavoritesChanged();
2129 }
2130 }
2131
2132 /**
2133 * Receives intents from other applications to change the wallpaper.
2134 */
2135 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2136 private final Application mApplication;
2137 private WeakReference<Launcher> mLauncher;
2138
2139 WallpaperIntentReceiver(Application application, Launcher launcher) {
2140 mApplication = application;
2141 setLauncher(launcher);
2142 }
2143
2144 void setLauncher(Launcher launcher) {
2145 mLauncher = new WeakReference<Launcher>(launcher);
2146 }
2147
2148 @Override
2149 public void onReceive(Context context, Intent intent) {
2150 // Load the wallpaper from the ApplicationContext and store it locally
2151 // until the Launcher Activity is ready to use it
2152 final Drawable drawable = mApplication.getWallpaper();
2153 if (drawable instanceof BitmapDrawable) {
2154 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2155 } else {
2156 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2157 }
2158
2159 // If Launcher is alive, notify we have a new wallpaper
2160 if (mLauncher != null) {
2161 final Launcher launcher = mLauncher.get();
2162 if (launcher != null) {
2163 launcher.loadWallpaper();
2164 }
2165 }
2166 }
2167 }
2168
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002169 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002171 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002172 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 // Number of items to bind in every pass
2175 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176
2177 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002178 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002179 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002181
Karl Rosaen138a0412009-04-23 19:00:21 -07002182 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183
2184 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002185 ArrayList<LauncherAppWidgetInfo> appWidgets,
2186 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187
2188 mLauncher = new WeakReference<Launcher>(launcher);
2189 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002190 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002191
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002192 // Sort widgets so active workspace is bound first
2193 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2194 final int size = appWidgets.size();
2195 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002196
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002197 for (int i = 0; i < size; i++) {
2198 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2199 if (appWidgetInfo.screen == currentScreen) {
2200 mAppWidgets.addFirst(appWidgetInfo);
2201 } else {
2202 mAppWidgets.addLast(appWidgetInfo);
2203 }
2204 }
2205 }
Romain Guycbb89e42009-06-08 15:52:54 -07002206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 public void startBindingItems() {
2208 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2209 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002210
2211 public void startBindingDrawer() {
2212 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2213 }
Romain Guycbb89e42009-06-08 15:52:54 -07002214
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002215 public void startBindingAppWidgetsWhenIdle() {
2216 // Ask for notification when message queue becomes idle
2217 final MessageQueue messageQueue = Looper.myQueue();
2218 messageQueue.addIdleHandler(this);
2219 }
Romain Guycbb89e42009-06-08 15:52:54 -07002220
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002221 public boolean queueIdle() {
2222 // Queue is idle, so start binding items
2223 startBindingAppWidgets();
2224 return false;
2225 }
2226
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002227 public void startBindingAppWidgets() {
2228 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2229 }
2230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 @Override
2232 public void handleMessage(Message msg) {
2233 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002234 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 return;
2236 }
Romain Guycbb89e42009-06-08 15:52:54 -07002237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 switch (msg.what) {
2239 case MESSAGE_BIND_ITEMS: {
2240 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2241 break;
2242 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002243 case MESSAGE_BIND_DRAWER: {
2244 launcher.bindDrawer(this, mDrawerAdapter);
2245 break;
2246 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002247 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002248 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 break;
2250 }
2251 }
2252 }
2253 }
2254}