blob: 5eead8b2339a91734a320a98bb96debaa61d67d1 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
20import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070022import android.app.ISearchManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.SearchManager;
24import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070025import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.ActivityNotFoundException;
27import android.content.BroadcastReceiver;
28import android.content.ComponentName;
29import android.content.ContentResolver;
30import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
33import android.content.IntentFilter;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070034import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.ActivityInfo;
36import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070039import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.drawable.Drawable;
43import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.os.Bundle;
45import android.os.Handler;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070046import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070047import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070048import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.os.Parcelable;
50import android.os.RemoteException;
51import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070052import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.text.Selection;
54import android.text.SpannableStringBuilder;
55import android.text.TextUtils;
56import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070057import static android.util.Log.*;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.view.KeyEvent;
60import android.view.LayoutInflater;
61import android.view.Menu;
62import android.view.MenuItem;
63import android.view.View;
64import android.view.ViewGroup;
Romain Guyb1b69f52009-08-10 15:10:15 -070065import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.view.View.OnLongClickListener;
67import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.widget.TextView;
70import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070071import android.appwidget.AppWidgetManager;
72import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073
74import java.lang.ref.WeakReference;
75import java.util.ArrayList;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070076import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070077import java.io.DataOutputStream;
78import java.io.FileNotFoundException;
79import java.io.IOException;
80import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081
82/**
83 * Default launcher application.
84 */
85public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener {
86 static final String LOG_TAG = "Launcher";
87 static final boolean LOGD = false;
88
89 private static final boolean PROFILE_STARTUP = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070090 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 private static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070092
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 private static final int WALLPAPER_SCREENS_SPAN = 2;
94
95 private static final int MENU_GROUP_ADD = 1;
96 private static final int MENU_ADD = Menu.FIRST + 1;
97 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
98 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
99 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700100 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
102 private static final int REQUEST_CREATE_SHORTCUT = 1;
103 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700104 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 private static final int REQUEST_PICK_APPLICATION = 6;
106 private static final int REQUEST_PICK_SHORTCUT = 7;
107 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700108 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
110 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
111
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700112 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
113 static final String SEARCH_WIDGET = "search_widget";
114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 static final int SCREEN_COUNT = 3;
116 static final int DEFAULT_SCREN = 1;
117 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700118 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato7404ee42009-07-31 11:54:44 -0700120 static final int DIALOG_ALL_APPS = 1;
121 static final int DIALOG_CREATE_SHORTCUT = 2;
122 static final int DIALOG_RENAME_FOLDER = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Romain Guy98d01652009-06-30 16:21:04 -0700124 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 // Type: int
127 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
128 // Type: boolean
129 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
130 // Type: long
131 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
146 // Type: int[]
147 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
148 // Type: boolean
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
150 // Type: long
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
152
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700153 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final Object sLock = new Object();
156 private static int sScreen = DEFAULT_SCREN;
157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
159 private final ContentObserver mObserver = new FavoritesChangeObserver();
160
161 private LayoutInflater mInflater;
162
Joe Onorato00acb122009-08-04 16:04:30 -0400163 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700165
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700166 private AppWidgetManager mAppWidgetManager;
167 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private CellLayout.CellInfo mAddItemCellInfo;
172 private CellLayout.CellInfo mMenuAddInfo;
173 private final int[] mCellCoordinates = new int[2];
174 private FolderInfo mFolderInfo;
175
Joe Onorato7404ee42009-07-31 11:54:44 -0700176 private AllAppsDialog mAllAppsDialog;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private HandleView mHandleView;
Joe Onorato93839052009-08-06 20:34:32 -0700178 private AllAppsView mAllAppsGrid; // TODO: put this into AllAppsDialog
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
180 private boolean mDesktopLocked = true;
181 private Bundle mSavedState;
182
183 private SpannableStringBuilder mDefaultKeySsb = null;
184
185 private boolean mDestroyed;
Mike LeBeau736cf282009-07-02 17:46:59 -0700186
187 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
189 private boolean mRestoring;
190 private boolean mWaitingForResult;
191 private boolean mLocaleChanged;
192
Dianne Hackborn67800862009-07-24 17:15:20 -0700193 private boolean mHomeDown;
194 private boolean mBackDown;
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private Bundle mSavedInstanceState;
197
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700198 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 @Override
201 protected void onCreate(Bundle savedInstanceState) {
202 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700203 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700206
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700207 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700208
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700209 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
210 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 if (PROFILE_STARTUP) {
213 android.os.Debug.startMethodTracing("/sdcard/launcher");
214 }
215
216 checkForLocaleChange();
217 setWallpaperDimension();
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 setContentView(R.layout.launcher);
220 setupViews();
221
Joe Onorato7404ee42009-07-31 11:54:44 -0700222 mAllAppsDialog = new AllAppsDialog(this);
223 mAllAppsDialog.lock();
224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 registerIntentReceivers();
226 registerContentObservers();
227
228 mSavedState = savedInstanceState;
229 restoreState(mSavedState);
230
231 if (PROFILE_STARTUP) {
232 android.os.Debug.stopMethodTracing();
233 }
234
235 if (!mRestoring) {
236 startLoaders();
237 }
238
239 // For handling default keys
240 mDefaultKeySsb = new SpannableStringBuilder();
241 Selection.setSelection(mDefaultKeySsb, 0);
242 }
Romain Guycbb89e42009-06-08 15:52:54 -0700243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700245 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
246 readConfiguration(this, localeConfiguration);
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 final Configuration configuration = getResources().getConfiguration();
249
Romain Guy98d01652009-06-30 16:21:04 -0700250 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 final String locale = configuration.locale.toString();
252
Romain Guy98d01652009-06-30 16:21:04 -0700253 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 final int mcc = configuration.mcc;
255
Romain Guy98d01652009-06-30 16:21:04 -0700256 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 final int mnc = configuration.mnc;
258
259 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
260
261 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700262 localeConfiguration.locale = locale;
263 localeConfiguration.mcc = mcc;
264 localeConfiguration.mnc = mnc;
265
266 writeConfiguration(this, localeConfiguration);
267 }
268 }
269
270 private static class LocaleConfiguration {
271 public String locale;
272 public int mcc = -1;
273 public int mnc = -1;
274 }
275
276 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
277 DataInputStream in = null;
278 try {
279 in = new DataInputStream(context.openFileInput(PREFERENCES));
280 configuration.locale = in.readUTF();
281 configuration.mcc = in.readInt();
282 configuration.mnc = in.readInt();
283 } catch (FileNotFoundException e) {
284 // Ignore
285 } catch (IOException e) {
286 // Ignore
287 } finally {
288 if (in != null) {
289 try {
290 in.close();
291 } catch (IOException e) {
292 // Ignore
293 }
294 }
295 }
296 }
297
298 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
299 DataOutputStream out = null;
300 try {
301 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
302 out.writeUTF(configuration.locale);
303 out.writeInt(configuration.mcc);
304 out.writeInt(configuration.mnc);
305 out.flush();
306 } catch (FileNotFoundException e) {
307 // Ignore
308 } catch (IOException e) {
309 //noinspection ResultOfMethodCallIgnored
310 context.getFileStreamPath(PREFERENCES).delete();
311 } finally {
312 if (out != null) {
313 try {
314 out.close();
315 } catch (IOException e) {
316 // Ignore
317 }
318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 }
320 }
321
322 static int getScreen() {
323 synchronized (sLock) {
324 return sScreen;
325 }
326 }
327
328 static void setScreen(int screen) {
329 synchronized (sLock) {
330 sScreen = screen;
331 }
332 }
333
334 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700335 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
336 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
337
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 mRestoring = false;
339 }
340
341 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700342 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343
344 Display display = getWindowManager().getDefaultDisplay();
345 boolean isPortrait = display.getWidth() < display.getHeight();
346
347 final int width = isPortrait ? display.getWidth() : display.getHeight();
348 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700349 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 }
351
352 @Override
353 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700354 mWaitingForResult = false;
355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 // The pattern used here is that a user PICKs a specific application,
357 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
360 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700361
Romain Guy94dabf12009-07-21 10:55:43 -0700362 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 switch (requestCode) {
364 case REQUEST_PICK_APPLICATION:
365 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
366 break;
367 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700368 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 break;
370 case REQUEST_CREATE_SHORTCUT:
371 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
372 break;
373 case REQUEST_PICK_LIVE_FOLDER:
374 addLiveFolder(data);
375 break;
376 case REQUEST_CREATE_LIVE_FOLDER:
377 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
378 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700379 case REQUEST_PICK_APPWIDGET:
380 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 case REQUEST_CREATE_APPWIDGET:
383 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 break;
385 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700386 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700388 // Clean up the appWidgetId if we canceled
389 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
390 if (appWidgetId != -1) {
391 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
393 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 }
395
396 @Override
397 protected void onResume() {
398 super.onResume();
399
400 if (mRestoring) {
401 startLoaders();
402 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700403
404 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
405 // onNewIntent), then close the search dialog if needed, because it probably
406 // came from the user pressing 'home' (rather than, for example, pressing 'back').
407 if (mIsNewIntent) {
408 // Post to a handler so that this happens after the search dialog tries to open
409 // itself again.
410 mWorkspace.post(new Runnable() {
411 public void run() {
412 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
413 ServiceManager.getService(Context.SEARCH_SERVICE));
414 try {
415 searchManagerService.stopSearch();
416 } catch (RemoteException e) {
417 e(LOG_TAG, "error stopping search", e);
418 }
419 }
420 });
421 }
422
423 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425
426 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 protected void onPause() {
428 super.onPause();
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700429 closeAllAppsDialog(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 }
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700432 @Override
433 public Object onRetainNonConfigurationInstance() {
434 // Flag any binder to stop early before switching
435 if (mBinder != null) {
436 mBinder.mTerminate = true;
437 }
Romain Guycbb89e42009-06-08 15:52:54 -0700438
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700439 if (PROFILE_ROTATE) {
440 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
441 }
442 return null;
443 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 private boolean acceptFilter() {
446 final InputMethodManager inputManager = (InputMethodManager)
447 getSystemService(Context.INPUT_METHOD_SERVICE);
448 return !inputManager.isFullscreenMode();
449 }
450
451 @Override
452 public boolean onKeyDown(int keyCode, KeyEvent event) {
453 boolean handled = super.onKeyDown(keyCode, event);
454 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
455 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
456 keyCode, event);
457 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700458 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700459 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700460 // showSearchDialog()
461 // If there are multiple keystrokes before the search dialog takes focus,
462 // onSearchRequested() will be called for every keystroke,
463 // but it is idempotent, so it's fine.
464 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 }
466 }
467
468 return handled;
469 }
470
Karl Rosaen138a0412009-04-23 19:00:21 -0700471 private String getTypedText() {
472 return mDefaultKeySsb.toString();
473 }
474
475 private void clearTypedText() {
476 mDefaultKeySsb.clear();
477 mDefaultKeySsb.clearSpans();
478 Selection.setSelection(mDefaultKeySsb, 0);
479 }
480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 /**
482 * Restores the previous state, if it exists.
483 *
484 * @param savedState The previous state.
485 */
486 private void restoreState(Bundle savedState) {
487 if (savedState == null) {
488 return;
489 }
490
491 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
492 if (currentScreen > -1) {
493 mWorkspace.setCurrentScreen(currentScreen);
494 }
495
496 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
497 if (addScreen > -1) {
498 mAddItemCellInfo = new CellLayout.CellInfo();
499 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
500 addItemCellInfo.valid = true;
501 addItemCellInfo.screen = addScreen;
502 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
503 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
504 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
505 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
506 addItemCellInfo.findVacantCellsFromOccupied(
507 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
508 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
509 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
510 mRestoring = true;
511 }
512
513 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
514 if (renameFolder) {
515 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
516 mFolderInfo = sModel.getFolderById(this, id);
517 mRestoring = true;
518 }
519 }
520
521 /**
522 * Finds all the views we need and configure them properly.
523 */
524 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400525 mDragController = new DragController(this);
526 DragController dragController = mDragController;
527
Romain Guyb1b69f52009-08-10 15:10:15 -0700528 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400529 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530
531 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
532 final Workspace workspace = mWorkspace;
533
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
535
Joe Onorato7404ee42009-07-31 11:54:44 -0700536 mHandleView = (HandleView) findViewById(R.id.all_apps);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700537 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700538 mHandleView.setOnClickListener(this);
Romain Guyb1b69f52009-08-10 15:10:15 -0700539 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
540 handleIcon.setCrossFadeEnabled(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400543 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545
546 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400547 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700548 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549
Joe Onorato00acb122009-08-04 16:04:30 -0400550 dragController.setDragScoller(workspace);
551 dragController.setDragListener(deleteZone);
552 dragController.setScrollView(dragLayer);
553
554 // The order here is bottom to top.
555 dragController.addDropTarget(workspace);
556 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 }
558
559 /**
560 * Creates a view representing a shortcut.
561 *
562 * @param info The data structure describing the shortcut.
563 *
564 * @return A View inflated from R.layout.application.
565 */
566 View createShortcut(ApplicationInfo info) {
567 return createShortcut(R.layout.application,
568 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
569 }
570
571 /**
572 * Creates a view representing a shortcut inflated from the specified resource.
573 *
574 * @param layoutResId The id of the XML layout used to create the shortcut.
575 * @param parent The group the shortcut belongs to.
576 * @param info The data structure describing the shortcut.
577 *
578 * @return A View inflated from layoutResId.
579 */
580 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
581 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
582
583 if (!info.filtered) {
584 info.icon = Utilities.createIconThumbnail(info.icon, this);
585 info.filtered = true;
586 }
587
588 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
589 favorite.setText(info.title);
590 favorite.setTag(info);
591 favorite.setOnClickListener(this);
592
593 return favorite;
594 }
595
596 /**
597 * Add an application shortcut to the workspace.
598 *
599 * @param data The intent describing the application.
600 * @param cellInfo The position on screen where to create the shortcut.
601 */
602 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
603 boolean insertAtFirst) {
604 cellInfo.screen = mWorkspace.getCurrentScreen();
605 if (!findSingleSlot(cellInfo)) return;
606
Romain Guy73b979d2009-06-09 12:57:21 -0700607 final ApplicationInfo info = infoFromApplicationIntent(context, data);
608 if (info != null) {
609 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
610 }
611 }
612
613 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 ComponentName component = data.getComponent();
615 PackageManager packageManager = context.getPackageManager();
616 ActivityInfo activityInfo = null;
617 try {
618 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
619 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700620 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 }
Romain Guycbb89e42009-06-08 15:52:54 -0700622
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 if (activityInfo != null) {
624 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 itemInfo.title = activityInfo.loadLabel(packageManager);
627 if (itemInfo.title == null) {
628 itemInfo.title = activityInfo.name;
629 }
Romain Guycbb89e42009-06-08 15:52:54 -0700630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
632 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
633 itemInfo.icon = activityInfo.loadIcon(packageManager);
634 itemInfo.container = ItemInfo.NO_ID;
635
Romain Guy73b979d2009-06-09 12:57:21 -0700636 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
Romain Guy73b979d2009-06-09 12:57:21 -0700638
639 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
Romain Guycbb89e42009-06-08 15:52:54 -0700641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 /**
643 * Add a shortcut to the workspace.
644 *
645 * @param data The intent describing the shortcut.
646 * @param cellInfo The position on screen where to create the shortcut.
647 * @param insertAtFirst
648 */
649 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
650 boolean insertAtFirst) {
651 cellInfo.screen = mWorkspace.getCurrentScreen();
652 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
655
656 if (!mRestoring) {
657 sModel.addDesktopItem(info);
658
659 final View view = createShortcut(info);
660 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
661 } else if (sModel.isDesktopLoaded()) {
662 sModel.addDesktopItem(info);
663 }
664 }
665
Romain Guycbb89e42009-06-08 15:52:54 -0700666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700668 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700670 * @param data The intent describing the appWidgetId.
671 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700673 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 boolean insertAtFirst) {
675
676 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700678
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700679 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700680
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700681 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700682
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700685 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700686
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 // Try finding open space on Launcher screen
688 final int[] xy = mCellCoordinates;
689 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
690
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 // Build Launcher-specific widget info and save to database
692 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 launcherInfo.spanX = spans[0];
694 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700695
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 LauncherModel.addItemToDatabase(this, launcherInfo,
697 LauncherSettings.Favorites.CONTAINER_DESKTOP,
698 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
699
700 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700701 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700702
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700704 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700705
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
710 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
711 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
714 }
Romain Guycbb89e42009-06-08 15:52:54 -0700715
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716 public LauncherAppWidgetHost getAppWidgetHost() {
717 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 }
Romain Guycbb89e42009-06-08 15:52:54 -0700719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 static ApplicationInfo addShortcut(Context context, Intent data,
721 CellLayout.CellInfo cellInfo, boolean notify) {
722
Romain Guy73b979d2009-06-09 12:57:21 -0700723 final ApplicationInfo info = infoFromShortcutIntent(context, data);
724 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
725 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
726
727 return info;
728 }
729
730 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
732 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
733 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
734
735 Drawable icon = null;
736 boolean filtered = false;
737 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700738 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739
740 if (bitmap != null) {
741 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
742 filtered = true;
743 customIcon = true;
744 } else {
745 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700746 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700748 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 final PackageManager packageManager = context.getPackageManager();
750 Resources resources = packageManager.getResourcesForApplication(
751 iconResource.packageName);
752 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
753 icon = resources.getDrawable(id);
754 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700755 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 }
757 }
758 }
759
760 if (icon == null) {
761 icon = context.getPackageManager().getDefaultActivityIcon();
762 }
763
764 final ApplicationInfo info = new ApplicationInfo();
765 info.icon = icon;
766 info.filtered = filtered;
767 info.title = name;
768 info.intent = intent;
769 info.customIcon = customIcon;
770 info.iconResource = iconResource;
771
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 return info;
773 }
774
775 @Override
776 protected void onNewIntent(Intent intent) {
777 super.onNewIntent(intent);
778
779 // Close the menu
780 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
781 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700782
783 // Set this flag so that onResume knows to close the search dialog if it's open,
784 // because this was a new intent (thus a press of 'home' or some such) rather than
785 // for example onResume being called when the user pressed the 'back' button.
786 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787
788 try {
789 dismissDialog(DIALOG_CREATE_SHORTCUT);
790 // Unlock the workspace if the dialog was showing
791 mWorkspace.unlock();
792 } catch (Exception e) {
793 // An exception is thrown if the dialog is not visible, which is fine
794 }
795
796 try {
797 dismissDialog(DIALOG_RENAME_FOLDER);
798 // Unlock the workspace if the dialog was showing
799 mWorkspace.unlock();
800 } catch (Exception e) {
801 // An exception is thrown if the dialog is not visible, which is fine
802 }
803
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
805 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700806
Romain Guy94dabf12009-07-21 10:55:43 -0700807 if (!mWorkspace.isDefaultScreenShowing()) {
808 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700810
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700811 closeAllAppsDialog(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700812
813 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 if (v != null && v.getWindowToken() != null) {
815 InputMethodManager imm = (InputMethodManager)getSystemService(
816 INPUT_METHOD_SERVICE);
817 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
818 }
819 } else {
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700820 closeAllAppsDialog(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 }
822 }
823 }
824
825 @Override
826 protected void onRestoreInstanceState(Bundle savedInstanceState) {
827 // Do not call super here
828 mSavedInstanceState = savedInstanceState;
829 }
830
831 @Override
832 protected void onSaveInstanceState(Bundle outState) {
833 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
834
835 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
836 if (folders.size() > 0) {
837 final int count = folders.size();
838 long[] ids = new long[count];
839 for (int i = 0; i < count; i++) {
840 final FolderInfo info = folders.get(i).getInfo();
841 ids[i] = info.id;
842 }
843 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
844 } else {
845 super.onSaveInstanceState(outState);
846 }
847
Romain Guy3cf604f2009-06-16 13:12:53 -0700848 final boolean isConfigurationChange = getChangingConfigurations() != 0;
849
Romain Guy5a941392009-04-28 15:18:25 -0700850 // When the drawer is opened and we are saving the state because of a
851 // configuration change
Joe Onorato7404ee42009-07-31 11:54:44 -0700852 if (mAllAppsDialog.isOpen && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700854 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855
856 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
857 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
858 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
859
860 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
861 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
862 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
863 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
864 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
865 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
866 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
867 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
868 layout.getOccupiedCells());
869 }
870
871 if (mFolderInfo != null && mWaitingForResult) {
872 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
873 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
874 }
875 }
876
877 @Override
878 public void onDestroy() {
879 mDestroyed = true;
880
881 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700882
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700884 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700886 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 }
888
889 TextKeyListener.getInstance().release();
890
Joe Onorato93839052009-08-06 20:34:32 -0700891 // TODO mAllAppsGrid.setAdapter(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 sModel.unbind();
893 sModel.abortLoaders();
894
895 getContentResolver().unregisterContentObserver(mObserver);
896 unregisterReceiver(mApplicationsReceiver);
897 }
898
899 @Override
900 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700901 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 super.startActivityForResult(intent, requestCode);
903 }
904
905 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700906 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700908
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700909 closeAllAppsDialog(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700910
Karl Rosaen138a0412009-04-23 19:00:21 -0700911 // Slide the search widget to the top, if it's on the current screen,
912 // otherwise show the search dialog immediately.
913 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
914 if (searchWidget == null) {
915 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
916 } else {
917 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
918 // show the currently typed text in the search widget while sliding
919 searchWidget.setQuery(getTypedText());
920 }
921 }
Romain Guycbb89e42009-06-08 15:52:54 -0700922
Karl Rosaen138a0412009-04-23 19:00:21 -0700923 /**
924 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700925 *
926 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700927 */
Romain Guycbb89e42009-06-08 15:52:54 -0700928 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700929 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700930
Karl Rosaen138a0412009-04-23 19:00:21 -0700931 if (initialQuery == null) {
932 // Use any text typed in the launcher as the initial query
933 initialQuery = getTypedText();
934 clearTypedText();
935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 if (appSearchData == null) {
937 appSearchData = new Bundle();
938 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
939 }
Romain Guycbb89e42009-06-08 15:52:54 -0700940
Karl Rosaen138a0412009-04-23 19:00:21 -0700941 final SearchManager searchManager =
942 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
943
944 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
945 if (searchWidget != null) {
946 // This gets called when the user leaves the search dialog to go back to
947 // the Launcher.
948 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
949 public void onCancel() {
950 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700951 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700952 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700953 });
954 }
Romain Guycbb89e42009-06-08 15:52:54 -0700955
Karl Rosaen138a0412009-04-23 19:00:21 -0700956 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700957 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
959
Karl Rosaen138a0412009-04-23 19:00:21 -0700960 /**
961 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 */
Romain Guy5a941392009-04-28 15:18:25 -0700963 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700964 // Close search dialog
965 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100966 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 // Restore search widget to its normal position
968 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
969 if (searchWidget != null) {
970 searchWidget.stopSearch(false);
971 }
972 }
Romain Guycbb89e42009-06-08 15:52:54 -0700973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 @Override
975 public boolean onCreateOptionsMenu(Menu menu) {
976 if (mDesktopLocked) return false;
977
978 super.onCreateOptionsMenu(menu);
979 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
980 .setIcon(android.R.drawable.ic_menu_add)
981 .setAlphabeticShortcut('A');
982 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
983 .setIcon(android.R.drawable.ic_menu_gallery)
984 .setAlphabeticShortcut('W');
985 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
986 .setIcon(android.R.drawable.ic_search_category_default)
987 .setAlphabeticShortcut(SearchManager.MENU_KEY);
988 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
989 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
990 .setAlphabeticShortcut('N');
991
992 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700993 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
994 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995
996 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
997 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
998 .setIntent(settings);
999
1000 return true;
1001 }
1002
1003 @Override
1004 public boolean onPrepareOptionsMenu(Menu menu) {
1005 super.onPrepareOptionsMenu(menu);
1006
1007 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1008 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1009
1010 return true;
1011 }
1012
1013 @Override
1014 public boolean onOptionsItemSelected(MenuItem item) {
1015 switch (item.getItemId()) {
1016 case MENU_ADD:
1017 addItems();
1018 return true;
1019 case MENU_WALLPAPER_SETTINGS:
1020 startWallpaper();
1021 return true;
1022 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001023 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 return true;
1025 case MENU_NOTIFICATIONS:
1026 showNotifications();
1027 return true;
1028 }
1029
1030 return super.onOptionsItemSelected(item);
1031 }
Romain Guycbb89e42009-06-08 15:52:54 -07001032
Karl Rosaen138a0412009-04-23 19:00:21 -07001033 /**
1034 * Indicates that we want global search for this activity by setting the globalSearch
1035 * argument for {@link #startSearch} to true.
1036 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001038 @Override
1039 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001040 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001041 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001042 }
1043
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 private void addItems() {
1045 showAddDialog(mMenuAddInfo);
1046 }
1047
1048 private void removeShortcutsForPackage(String packageName) {
1049 if (packageName != null && packageName.length() > 0) {
1050 mWorkspace.removeShortcutsForPackage(packageName);
1051 }
1052 }
Romain Guycbb89e42009-06-08 15:52:54 -07001053
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001054 private void updateShortcutsForPackage(String packageName) {
1055 if (packageName != null && packageName.length() > 0) {
1056 mWorkspace.updateShortcutsForPackage(packageName);
1057 }
1058 }
1059
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001060 void addAppWidget(Intent data) {
1061 // TODO: catch bad widget exception when sent
1062 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001064 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1065 if (SEARCH_WIDGET.equals(customWidget)) {
1066 // We don't need this any more, since this isn't a real app widget.
1067 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1068 // add the search widget
1069 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001071 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1072
1073 if (appWidget.configure != null) {
1074 // Launch over to configure widget, if needed
1075 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1076 intent.setComponent(appWidget.configure);
1077 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1078
1079 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1080 } else {
1081 // Otherwise just add it
1082 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 }
1085 }
Romain Guycbb89e42009-06-08 15:52:54 -07001086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 void addSearch() {
1088 final Widget info = Widget.makeSearch();
1089 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 final int[] xy = mCellCoordinates;
1092 final int spanX = info.spanX;
1093 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 sModel.addDesktopItem(info);
1098 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1099 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 final View view = mInflater.inflate(info.layoutResource, null);
1102 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001103 Search search = (Search) view.findViewById(R.id.widget_search);
1104 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1107 }
1108
Romain Guy73b979d2009-06-09 12:57:21 -07001109 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001110 // Handle case where user selected "Applications"
1111 String applicationName = getResources().getString(R.string.group_applications);
1112 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001113
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001114 if (applicationName != null && applicationName.equals(shortcutName)) {
1115 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1116 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001117
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001118 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1119 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001120 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001121 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001122 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 }
1125
1126 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001127 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001128 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001129 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001130
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001131 if (folderName != null && folderName.equals(shortcutName)) {
1132 addFolder(!mDesktopLocked);
1133 } else {
1134 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 }
1137
1138 void addFolder(boolean insertAtFirst) {
1139 UserFolderInfo folderInfo = new UserFolderInfo();
1140 folderInfo.title = getText(R.string.folder_name);
1141
1142 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1143 cellInfo.screen = mWorkspace.getCurrentScreen();
1144 if (!findSingleSlot(cellInfo)) return;
1145
1146 // Update the model
1147 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1148 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1149 sModel.addDesktopItem(folderInfo);
1150 sModel.addFolder(folderInfo);
1151
1152 // Create the view
1153 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1154 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1155 mWorkspace.addInCurrentScreen(newFolder,
1156 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1157 }
Romain Guycbb89e42009-06-08 15:52:54 -07001158
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1160 boolean insertAtFirst) {
1161 cellInfo.screen = mWorkspace.getCurrentScreen();
1162 if (!findSingleSlot(cellInfo)) return;
1163
1164 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1165
1166 if (!mRestoring) {
1167 sModel.addDesktopItem(info);
1168
1169 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1170 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1171 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1172 } else if (sModel.isDesktopLoaded()) {
1173 sModel.addDesktopItem(info);
1174 }
1175 }
1176
1177 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1178 CellLayout.CellInfo cellInfo, boolean notify) {
1179
1180 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1181 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1182
1183 Drawable icon = null;
1184 boolean filtered = false;
1185 Intent.ShortcutIconResource iconResource = null;
1186
1187 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1188 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1189 try {
1190 iconResource = (Intent.ShortcutIconResource) extra;
1191 final PackageManager packageManager = context.getPackageManager();
1192 Resources resources = packageManager.getResourcesForApplication(
1193 iconResource.packageName);
1194 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1195 icon = resources.getDrawable(id);
1196 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001197 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 }
1199 }
1200
1201 if (icon == null) {
1202 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1203 }
1204
1205 final LiveFolderInfo info = new LiveFolderInfo();
1206 info.icon = icon;
1207 info.filtered = filtered;
1208 info.title = name;
1209 info.iconResource = iconResource;
1210 info.uri = data.getData();
1211 info.baseIntent = baseIntent;
1212 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1213 LiveFolders.DISPLAY_MODE_GRID);
1214
1215 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1216 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1217 sModel.addFolder(info);
1218
1219 return info;
1220 }
1221
1222 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1223 final int[] xy = new int[2];
1224 if (findSlot(cellInfo, xy, 1, 1)) {
1225 cellInfo.cellX = xy[0];
1226 cellInfo.cellY = xy[1];
1227 return true;
1228 }
1229 return false;
1230 }
1231
1232 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1233 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1234 boolean[] occupied = mSavedState != null ?
1235 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1236 cellInfo = mWorkspace.findAllVacantCells(occupied);
1237 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1238 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1239 return false;
1240 }
1241 }
1242 return true;
1243 }
1244
1245 private void showNotifications() {
1246 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1247 if (statusBar != null) {
1248 statusBar.expand();
1249 }
1250 }
1251
1252 private void startWallpaper() {
1253 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1254 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1255 }
1256
1257 /**
1258 * Registers various intent receivers. The current implementation registers
1259 * only a wallpaper intent receiver to let other applications change the
1260 * wallpaper.
1261 */
1262 private void registerIntentReceivers() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1264 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1265 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1266 filter.addDataScheme("package");
1267 registerReceiver(mApplicationsReceiver, filter);
1268 }
1269
1270 /**
1271 * Registers various content observers. The current implementation registers
1272 * only a favorites observer to keep track of the favorites applications.
1273 */
1274 private void registerContentObservers() {
1275 ContentResolver resolver = getContentResolver();
1276 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1277 }
1278
1279 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001280 public void onWindowFocusChanged(boolean hasFocus) {
1281 super.onWindowFocusChanged(hasFocus);
1282 if (!hasFocus) {
1283 mBackDown = mHomeDown = false;
1284 }
1285 }
1286
1287 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 public boolean dispatchKeyEvent(KeyEvent event) {
1289 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1290 switch (event.getKeyCode()) {
1291 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001292 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 return true;
1294 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001295 mHomeDown = true;
1296 return true;
1297 }
1298 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1299 switch (event.getKeyCode()) {
1300 case KeyEvent.KEYCODE_BACK:
1301 if (!event.isCanceled()) {
1302 mWorkspace.dispatchKeyEvent(event);
Joe Onorato7404ee42009-07-31 11:54:44 -07001303 if (mAllAppsDialog.isOpen) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001304 closeAllAppsDialog(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001305 } else {
1306 closeFolder();
1307 }
1308 }
1309 mBackDown = false;
1310 return true;
1311 case KeyEvent.KEYCODE_HOME:
1312 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 return true;
1314 }
1315 }
1316
1317 return super.dispatchKeyEvent(event);
1318 }
1319
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 private void closeFolder() {
1321 Folder folder = mWorkspace.getOpenFolder();
1322 if (folder != null) {
1323 closeFolder(folder);
1324 }
1325 }
1326
1327 void closeFolder(Folder folder) {
1328 folder.getInfo().opened = false;
1329 ViewGroup parent = (ViewGroup) folder.getParent();
1330 if (parent != null) {
1331 parent.removeView(folder);
Joe Onorato00acb122009-08-04 16:04:30 -04001332 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334 folder.onClose();
1335 }
1336
1337 /**
1338 * When the notification that favorites have changed is received, requests
1339 * a favorites list refresh.
1340 */
1341 private void onFavoritesChanged() {
1342 mDesktopLocked = true;
Joe Onorato7404ee42009-07-31 11:54:44 -07001343 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 sModel.loadUserItems(false, this, false, false);
1345 }
1346
1347 void onDesktopItemsLoaded() {
1348 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 bindDesktopItems();
1350 }
Romain Guycbb89e42009-06-08 15:52:54 -07001351
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 /**
1353 * Refreshes the shortcuts shown on the workspace.
1354 */
1355 private void bindDesktopItems() {
1356 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001357 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001358 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1359 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 return;
1361 }
1362
1363 final Workspace workspace = mWorkspace;
1364 int count = workspace.getChildCount();
1365 for (int i = 0; i < count; i++) {
1366 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1367 }
Romain Guycbb89e42009-06-08 15:52:54 -07001368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 if (DEBUG_USER_INTERFACE) {
1370 android.widget.Button finishButton = new android.widget.Button(this);
1371 finishButton.setText("Finish");
1372 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1373
1374 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1375 public void onClick(View v) {
1376 finish();
1377 }
1378 });
1379 }
Romain Guycbb89e42009-06-08 15:52:54 -07001380
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001381 // Flag any old binder to terminate early
1382 if (mBinder != null) {
1383 mBinder.mTerminate = true;
1384 }
Romain Guycbb89e42009-06-08 15:52:54 -07001385
Karl Rosaen138a0412009-04-23 19:00:21 -07001386 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001387 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 }
1389
1390 private void bindItems(Launcher.DesktopBinder binder,
1391 ArrayList<ItemInfo> shortcuts, int start, int count) {
1392
1393 final Workspace workspace = mWorkspace;
1394 final boolean desktopLocked = mDesktopLocked;
1395
1396 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1397 int i = start;
1398
1399 for ( ; i < end; i++) {
1400 final ItemInfo item = shortcuts.get(i);
1401 switch (item.itemType) {
1402 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1403 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1404 final View shortcut = createShortcut((ApplicationInfo) item);
1405 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1406 !desktopLocked);
1407 break;
1408 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1409 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1410 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1411 (UserFolderInfo) item);
1412 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1413 !desktopLocked);
1414 break;
1415 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1416 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1417 R.layout.live_folder_icon, this,
1418 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1419 (LiveFolderInfo) item);
1420 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1421 !desktopLocked);
1422 break;
1423 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1424 final int screen = workspace.getCurrentScreen();
1425 final View view = mInflater.inflate(R.layout.widget_search,
1426 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001427
Karl Rosaen138a0412009-04-23 19:00:21 -07001428 Search search = (Search) view.findViewById(R.id.widget_search);
1429 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001430
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 final Widget widget = (Widget) item;
1432 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 workspace.addWidget(view, widget, !desktopLocked);
1435 break;
1436 }
1437 }
1438
1439 workspace.requestLayout();
1440
1441 if (end >= count) {
1442 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001443 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 } else {
1445 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1446 }
1447 }
1448
1449 private void finishBindDesktopItems() {
1450 if (mSavedState != null) {
1451 if (!mWorkspace.hasFocus()) {
1452 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1453 }
1454
1455 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1456 if (userFolders != null) {
1457 for (long folderId : userFolders) {
1458 final FolderInfo info = sModel.findFolderById(folderId);
1459 if (info != null) {
1460 openFolder(info);
1461 }
1462 }
1463 final Folder openFolder = mWorkspace.getOpenFolder();
1464 if (openFolder != null) {
1465 openFolder.requestFocus();
1466 }
1467 }
1468
1469 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1470 if (allApps) {
Joe Onorato45d43462009-07-31 14:36:51 -07001471 showAllAppsDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
1474 mSavedState = null;
1475 }
1476
1477 if (mSavedInstanceState != null) {
1478 super.onRestoreInstanceState(mSavedInstanceState);
1479 mSavedInstanceState = null;
1480 }
1481
Joe Onorato7404ee42009-07-31 11:54:44 -07001482 /* TODO
1483 if (mAllAppsDialog.isOpen && !mDrawer.hasFocus()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 mDrawer.requestFocus();
1485 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001486 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487
1488 mDesktopLocked = false;
Joe Onorato7404ee42009-07-31 11:54:44 -07001489 mAllAppsDialog.unlock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 }
Romain Guycbb89e42009-06-08 15:52:54 -07001491
Karl Rosaen138a0412009-04-23 19:00:21 -07001492 private void bindDrawer(Launcher.DesktopBinder binder,
1493 ApplicationsAdapter drawerAdapter) {
Joe Onorato93839052009-08-06 20:34:32 -07001494 // TODO mAllAppsGrid.setAdapter(drawerAdapter);
Karl Rosaen138a0412009-04-23 19:00:21 -07001495 binder.startBindingAppWidgetsWhenIdle();
1496 }
Romain Guycbb89e42009-06-08 15:52:54 -07001497
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001498 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001499 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001500
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 final Workspace workspace = mWorkspace;
1502 final boolean desktopLocked = mDesktopLocked;
1503
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001504 if (!appWidgets.isEmpty()) {
1505 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001506
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001507 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001508 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001509 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001510
Karl Rosaen138a0412009-04-23 19:00:21 -07001511 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001512
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001513 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1514 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001515
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001516 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1517 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001518
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001519 workspace.requestLayout();
1520 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001522 if (appWidgets.isEmpty()) {
1523 if (PROFILE_ROTATE) {
1524 android.os.Debug.stopMethodTracing();
1525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001527 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 }
1529 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001530
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 /**
1532 * Launches the intent referred by the clicked shortcut.
1533 *
1534 * @param v The view representing the clicked shortcut.
1535 */
1536 public void onClick(View v) {
1537 Object tag = v.getTag();
1538 if (tag instanceof ApplicationInfo) {
1539 // Open shortcut
1540 final Intent intent = ((ApplicationInfo) tag).intent;
1541 startActivitySafely(intent);
1542 } else if (tag instanceof FolderInfo) {
1543 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001544 } else if (v == mHandleView) {
1545 if (mAllAppsDialog.isOpen) {
1546 // TODO how can we be here?
1547 } else {
Joe Onorato45d43462009-07-31 14:36:51 -07001548 showAllAppsDialog();
Joe Onorato7404ee42009-07-31 11:54:44 -07001549 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
1551 }
1552
1553 void startActivitySafely(Intent intent) {
1554 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1555 try {
1556 startActivity(intent);
1557 } catch (ActivityNotFoundException e) {
1558 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1559 } catch (SecurityException e) {
1560 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001561 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1563 "or use the exported attribute for this activity.", e);
1564 }
1565 }
1566
1567 private void handleFolderClick(FolderInfo folderInfo) {
1568 if (!folderInfo.opened) {
1569 // Close any open folder
1570 closeFolder();
1571 // Open the requested folder
1572 openFolder(folderInfo);
1573 } else {
1574 // Find the open folder...
1575 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1576 int folderScreen;
1577 if (openFolder != null) {
1578 folderScreen = mWorkspace.getScreenForView(openFolder);
1579 // .. and close it
1580 closeFolder(openFolder);
1581 if (folderScreen != mWorkspace.getCurrentScreen()) {
1582 // Close any folder open on the current screen
1583 closeFolder();
1584 // Pull the folder onto this screen
1585 openFolder(folderInfo);
1586 }
1587 }
1588 }
1589 }
1590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 /**
1592 * Opens the user fodler described by the specified tag. The opening of the folder
1593 * is animated relative to the specified View. If the View is null, no animation
1594 * is played.
1595 *
1596 * @param folderInfo The FolderInfo describing the folder to open.
1597 */
1598 private void openFolder(FolderInfo folderInfo) {
1599 Folder openFolder;
1600
1601 if (folderInfo instanceof UserFolderInfo) {
1602 openFolder = UserFolder.fromXml(this);
1603 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001604 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 } else {
1606 return;
1607 }
1608
Joe Onorato00acb122009-08-04 16:04:30 -04001609 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 openFolder.setLauncher(this);
1611
1612 openFolder.bind(folderInfo);
1613 folderInfo.opened = true;
1614
1615 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1616 openFolder.onOpen();
1617 }
1618
1619 /**
1620 * Returns true if the workspace is being loaded. When the workspace is loading,
1621 * no user interaction should be allowed to avoid any conflict.
1622 *
1623 * @return True if the workspace is locked, false otherwise.
1624 */
1625 boolean isWorkspaceLocked() {
1626 return mDesktopLocked;
1627 }
1628
1629 public boolean onLongClick(View v) {
1630 if (mDesktopLocked) {
1631 return false;
1632 }
1633
1634 if (!(v instanceof CellLayout)) {
1635 v = (View) v.getParent();
1636 }
1637
1638 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1639
1640 // This happens when long clicking an item with the dpad/trackball
1641 if (cellInfo == null) {
1642 return true;
1643 }
1644
1645 if (mWorkspace.allowLongPress()) {
1646 if (cellInfo.cell == null) {
1647 if (cellInfo.valid) {
1648 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001649 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 showAddDialog(cellInfo);
1651 }
1652 } else {
1653 if (!(cellInfo.cell instanceof Folder)) {
1654 // User long pressed on an item
1655 mWorkspace.startDrag(cellInfo);
1656 }
1657 }
1658 }
1659 return true;
1660 }
1661
1662 static LauncherModel getModel() {
1663 return sModel;
1664 }
1665
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001666 View getDrawerHandle() {
1667 return mHandleView;
1668 }
1669
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 boolean isDrawerDown() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001671 return /* TODO !mDrawer.isMoving() && */ !mAllAppsDialog.isOpen;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 }
1673
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 Workspace getWorkspace() {
1675 return mWorkspace;
1676 }
1677
Joe Onorato7404ee42009-07-31 11:54:44 -07001678 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 GridView getApplicationsGrid() {
1680 return mAllAppsGrid;
1681 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001682 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683
1684 @Override
1685 protected Dialog onCreateDialog(int id) {
1686 switch (id) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001687 case DIALOG_ALL_APPS:
1688 return mAllAppsDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 case DIALOG_CREATE_SHORTCUT:
1690 return new CreateShortcut().createDialog();
1691 case DIALOG_RENAME_FOLDER:
1692 return new RenameFolder().createDialog();
1693 }
1694
1695 return super.onCreateDialog(id);
1696 }
1697
1698 @Override
1699 protected void onPrepareDialog(int id, Dialog dialog) {
1700 switch (id) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001701 case DIALOG_ALL_APPS:
Joe Onorato93839052009-08-06 20:34:32 -07001702 // TODO mAllAppsGrid.onPrepareDialog();
Joe Onorato7404ee42009-07-31 11:54:44 -07001703 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 break;
1706 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001707 if (mFolderInfo != null) {
1708 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1709 final CharSequence text = mFolderInfo.title;
1710 input.setText(text);
1711 input.setSelection(0, text.length());
1712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 break;
1714 }
1715 }
1716
1717 void showRenameDialog(FolderInfo info) {
1718 mFolderInfo = info;
1719 mWaitingForResult = true;
1720 showDialog(DIALOG_RENAME_FOLDER);
1721 }
1722
1723 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1724 mAddItemCellInfo = cellInfo;
1725 mWaitingForResult = true;
1726 showDialog(DIALOG_CREATE_SHORTCUT);
1727 }
1728
Romain Guy73b979d2009-06-09 12:57:21 -07001729 private void pickShortcut(int requestCode, int title) {
1730 Bundle bundle = new Bundle();
1731
1732 ArrayList<String> shortcutNames = new ArrayList<String>();
1733 shortcutNames.add(getString(R.string.group_applications));
1734 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1735
1736 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1737 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1738 R.drawable.ic_launcher_application));
1739 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1740
1741 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1742 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1743 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1744 pickIntent.putExtras(bundle);
1745
1746 startActivityForResult(pickIntent, requestCode);
1747 }
1748
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 private class RenameFolder {
1750 private EditText mInput;
1751
1752 Dialog createDialog() {
1753 mWaitingForResult = true;
1754 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1755 mInput = (EditText) layout.findViewById(R.id.folder_name);
1756
1757 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1758 builder.setIcon(0);
1759 builder.setTitle(getString(R.string.rename_folder_title));
1760 builder.setCancelable(true);
1761 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1762 public void onCancel(DialogInterface dialog) {
1763 cleanup();
1764 }
1765 });
1766 builder.setNegativeButton(getString(R.string.cancel_action),
1767 new Dialog.OnClickListener() {
1768 public void onClick(DialogInterface dialog, int which) {
1769 cleanup();
1770 }
1771 }
1772 );
1773 builder.setPositiveButton(getString(R.string.rename_action),
1774 new Dialog.OnClickListener() {
1775 public void onClick(DialogInterface dialog, int which) {
1776 changeFolderName();
1777 }
1778 }
1779 );
1780 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001781
1782 final AlertDialog dialog = builder.create();
1783 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1784 public void onShow(DialogInterface dialog) {
1785 mWorkspace.lock();
1786 }
1787 });
1788
1789 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
1791
1792 private void changeFolderName() {
1793 final String name = mInput.getText().toString();
1794 if (!TextUtils.isEmpty(name)) {
1795 // Make sure we have the right folder info
1796 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1797 mFolderInfo.title = name;
1798 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1799
1800 if (mDesktopLocked) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001801 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 sModel.loadUserItems(false, Launcher.this, false, false);
1803 } else {
1804 final FolderIcon folderIcon = (FolderIcon)
1805 mWorkspace.getViewForTag(mFolderInfo);
1806 if (folderIcon != null) {
1807 folderIcon.setText(name);
1808 getWorkspace().requestLayout();
1809 } else {
1810 mDesktopLocked = true;
Joe Onorato7404ee42009-07-31 11:54:44 -07001811 mAllAppsDialog.lock();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 sModel.loadUserItems(false, Launcher.this, false, false);
1813 }
1814 }
1815 }
1816 cleanup();
1817 }
1818
1819 private void cleanup() {
1820 mWorkspace.unlock();
1821 dismissDialog(DIALOG_RENAME_FOLDER);
1822 mWaitingForResult = false;
1823 mFolderInfo = null;
1824 }
1825 }
1826
1827 /**
Joe Onorato7404ee42009-07-31 11:54:44 -07001828 * Holds the 3d all apps view.
1829 */
1830 private class AllAppsDialog extends Dialog implements DialogInterface.OnCancelListener,
1831 DialogInterface.OnDismissListener, DialogInterface.OnShowListener {
1832
1833 boolean isOpen;
1834
1835 AllAppsDialog(Context context) {
1836 super(context, android.R.style.Theme_Translucent_NoTitleBar);
1837
1838 setOnCancelListener(this);
1839 setOnDismissListener(this);
1840 setOnShowListener(this);
1841
1842 setContentView(R.layout.all_apps);
Romain Guyb1b69f52009-08-10 15:10:15 -07001843 mAllAppsGrid = (AllAppsView) findViewById(R.id.all_apps);
Joe Onorato7404ee42009-07-31 11:54:44 -07001844
Joe Onorato00acb122009-08-04 16:04:30 -04001845 DragLayer dragLayer = (DragLayer)findViewById(R.id.drag_layer);
1846 dragLayer.setDragController(mDragController);
1847
Joe Onorato93839052009-08-06 20:34:32 -07001848 // TODO grid.setDragController(mDragController);
1849 // TODO grid.setLauncher(Launcher.this);
Joe Onorato7404ee42009-07-31 11:54:44 -07001850 }
1851
1852 public void onCancel(DialogInterface dialog) {
1853 onDestroy();
1854 }
1855
1856 public void onDismiss(DialogInterface dialog) {
1857 onDestroy();
1858 }
1859
1860 public void onShow(DialogInterface dialog) {
1861 }
1862
1863 private void onDestroy() {
Joe Onorato7404ee42009-07-31 11:54:44 -07001864 }
1865
1866 void lock() {
1867 // TODO
1868 }
1869
1870 void unlock() {
1871 // TODO
1872 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001873
Romain Guyb1b69f52009-08-10 15:10:15 -07001874 @Override
1875 public boolean onKeyDown(int keyCode, KeyEvent event) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001876 switch (keyCode) {
1877 case KeyEvent.KEYCODE_BACK:
1878 closeAllAppsDialog(true);
1879 return true;
1880 default:
1881 return super.onKeyDown(keyCode, event);
1882 }
1883 }
1884 }
1885
1886 void showAllAppsDialog() {
1887 mAllAppsDialog.isOpen = true;
1888 showDialog(DIALOG_ALL_APPS);
1889 mWorkspace.hide();
1890 }
1891
Joe Onorato00acb122009-08-04 16:04:30 -04001892 void showWorkspace() {
1893 mWorkspace.show();
1894 }
1895
1896 void closeAllAppsDialog(boolean animated) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001897 if (mAllAppsDialog.isOpen) {
1898 if (animated) {
1899 // TODO mDrawer.animateClose();
1900 mAllAppsDialog.dismiss();
1901 } else {
1902 mAllAppsDialog.dismiss();
1903 }
1904 mAllAppsDialog.isOpen = false;
1905 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1906 mWorkspace.show();
1907 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001908 }
1909
1910 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 * Displays the shortcut creation dialog and launches, if necessary, the
1912 * appropriate activity.
1913 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001914 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001915 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1916 DialogInterface.OnShowListener {
1917
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001919
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 Dialog createDialog() {
1921 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001924
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1926 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001927 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001928
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 builder.setInverseBackgroundForced(true);
1930
1931 AlertDialog dialog = builder.create();
1932 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001933 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001934 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 return dialog;
1937 }
1938
1939 public void onCancel(DialogInterface dialog) {
1940 mWaitingForResult = false;
1941 cleanup();
1942 }
1943
Romain Guycbb89e42009-06-08 15:52:54 -07001944 public void onDismiss(DialogInterface dialog) {
1945 mWorkspace.unlock();
1946 }
1947
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 private void cleanup() {
1949 mWorkspace.unlock();
1950 dismissDialog(DIALOG_CREATE_SHORTCUT);
1951 }
1952
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001953 /**
1954 * Handle the action clicked in the "Add to home" dialog.
1955 */
1956 public void onClick(DialogInterface dialog, int which) {
1957 Resources res = getResources();
1958 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001959
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001960 switch (which) {
1961 case AddAdapter.ITEM_SHORTCUT: {
1962 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001963 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001964 break;
1965 }
Romain Guycbb89e42009-06-08 15:52:54 -07001966
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001967 case AddAdapter.ITEM_APPWIDGET: {
1968 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001969
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001970 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1971 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1972 // add the search widget
1973 ArrayList<AppWidgetProviderInfo> customInfo =
1974 new ArrayList<AppWidgetProviderInfo>();
1975 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1976 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1977 info.label = getString(R.string.group_search);
1978 info.icon = R.drawable.ic_search_widget;
1979 customInfo.add(info);
1980 pickIntent.putParcelableArrayListExtra(
1981 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1982 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1983 Bundle b = new Bundle();
1984 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1985 customExtras.add(b);
1986 pickIntent.putParcelableArrayListExtra(
1987 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1988 // start the pick activity
1989 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1990 break;
1991 }
Romain Guycbb89e42009-06-08 15:52:54 -07001992
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001993 case AddAdapter.ITEM_LIVE_FOLDER: {
1994 // Insert extra item to handle inserting folder
1995 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001996
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001997 ArrayList<String> shortcutNames = new ArrayList<String>();
1998 shortcutNames.add(res.getString(R.string.group_folder));
1999 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002000
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002001 ArrayList<ShortcutIconResource> shortcutIcons =
2002 new ArrayList<ShortcutIconResource>();
2003 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2004 R.drawable.ic_launcher_folder));
2005 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2006
2007 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2008 pickIntent.putExtra(Intent.EXTRA_INTENT,
2009 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2010 pickIntent.putExtra(Intent.EXTRA_TITLE,
2011 getText(R.string.title_select_live_folder));
2012 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002013
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002014 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2015 break;
2016 }
2017
2018 case AddAdapter.ITEM_WALLPAPER: {
2019 startWallpaper();
2020 break;
2021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002024
2025 public void onShow(DialogInterface dialog) {
2026 mWorkspace.lock();
2027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 }
2029
2030 /**
2031 * Receives notifications when applications are added/removed.
2032 */
2033 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2034 @Override
2035 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002036 final String action = intent.getAction();
2037 final String packageName = intent.getData().getSchemeSpecificPart();
2038 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2039
2040 if (LauncherModel.DEBUG_LOADERS) {
2041 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2042 ", replacing=" + replacing);
2043 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002045
2046 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2047 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2048 if (!replacing) {
2049 removeShortcutsForPackage(packageName);
2050 if (LauncherModel.DEBUG_LOADERS) {
2051 d(LauncherModel.LOG_TAG, " --> remove package");
2052 }
2053 sModel.removePackage(Launcher.this, packageName);
2054 }
2055 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2056 // later, we will update the package at this time
2057 } else {
2058 if (!replacing) {
2059 if (LauncherModel.DEBUG_LOADERS) {
2060 d(LauncherModel.LOG_TAG, " --> add package");
2061 }
2062 sModel.addPackage(Launcher.this, packageName);
2063 } else {
2064 if (LauncherModel.DEBUG_LOADERS) {
2065 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2066 }
2067 sModel.updatePackage(Launcher.this, packageName);
2068 updateShortcutsForPackage(packageName);
2069 }
2070 }
2071 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002073 if (LauncherModel.DEBUG_LOADERS) {
2074 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2075 }
2076 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 }
2078 }
2079 }
2080
2081 /**
2082 * Receives notifications whenever the user favorites have changed.
2083 */
2084 private class FavoritesChangeObserver extends ContentObserver {
2085 public FavoritesChangeObserver() {
2086 super(new Handler());
2087 }
2088
2089 @Override
2090 public void onChange(boolean selfChange) {
2091 onFavoritesChanged();
2092 }
2093 }
2094
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002095 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002097 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002098 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 // Number of items to bind in every pass
2101 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102
2103 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002104 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002105 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002107
Karl Rosaen138a0412009-04-23 19:00:21 -07002108 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109
2110 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002111 ArrayList<LauncherAppWidgetInfo> appWidgets,
2112 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113
2114 mLauncher = new WeakReference<Launcher>(launcher);
2115 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002116 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002117
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002118 // Sort widgets so active workspace is bound first
2119 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2120 final int size = appWidgets.size();
2121 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002122
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002123 for (int i = 0; i < size; i++) {
2124 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2125 if (appWidgetInfo.screen == currentScreen) {
2126 mAppWidgets.addFirst(appWidgetInfo);
2127 } else {
2128 mAppWidgets.addLast(appWidgetInfo);
2129 }
2130 }
2131 }
Romain Guycbb89e42009-06-08 15:52:54 -07002132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 public void startBindingItems() {
2134 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2135 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002136
2137 public void startBindingDrawer() {
2138 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2139 }
Romain Guycbb89e42009-06-08 15:52:54 -07002140
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002141 public void startBindingAppWidgetsWhenIdle() {
2142 // Ask for notification when message queue becomes idle
2143 final MessageQueue messageQueue = Looper.myQueue();
2144 messageQueue.addIdleHandler(this);
2145 }
Romain Guycbb89e42009-06-08 15:52:54 -07002146
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002147 public boolean queueIdle() {
2148 // Queue is idle, so start binding items
2149 startBindingAppWidgets();
2150 return false;
2151 }
2152
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002153 public void startBindingAppWidgets() {
2154 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2155 }
2156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157 @Override
2158 public void handleMessage(Message msg) {
2159 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002160 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 return;
2162 }
Romain Guycbb89e42009-06-08 15:52:54 -07002163
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 switch (msg.what) {
2165 case MESSAGE_BIND_ITEMS: {
2166 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2167 break;
2168 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002169 case MESSAGE_BIND_DRAWER: {
2170 launcher.bindDrawer(this, mDrawerAdapter);
2171 break;
2172 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002173 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002174 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 break;
2176 }
2177 }
2178 }
2179 }
2180}