blob: db6e93976cebe481b19cf3b9013025a455a70f47 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Application;
22import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070023import android.app.ISearchManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070024import android.app.IWallpaperService;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.app.SearchManager;
26import android.app.StatusBarManager;
27import android.content.ActivityNotFoundException;
28import android.content.BroadcastReceiver;
29import android.content.ComponentName;
30import android.content.ContentResolver;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070035import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.pm.ActivityInfo;
37import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070040import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.Bitmap;
43import android.graphics.Rect;
44import android.graphics.drawable.BitmapDrawable;
45import android.graphics.drawable.Drawable;
46import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.os.Bundle;
48import android.os.Handler;
49import android.os.IBinder;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070050import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070052import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.os.Parcelable;
54import android.os.RemoteException;
55import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070056import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.text.Selection;
58import android.text.SpannableStringBuilder;
59import android.text.TextUtils;
60import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070061import static android.util.Log.*;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.KeyEvent;
64import android.view.LayoutInflater;
65import android.view.Menu;
66import android.view.MenuItem;
67import android.view.View;
68import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.view.View.OnLongClickListener;
70import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.widget.EditText;
Karl Rosaen138a0412009-04-23 19:00:21 -070072import android.widget.GridView;
Karl Rosaen138a0412009-04-23 19:00:21 -070073import android.widget.SlidingDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.widget.TextView;
75import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070076import android.appwidget.AppWidgetManager;
77import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
79import java.lang.ref.WeakReference;
80import java.util.ArrayList;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070081import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070082import java.io.DataOutputStream;
83import java.io.FileNotFoundException;
84import java.io.IOException;
85import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
87/**
88 * Default launcher application.
89 */
90public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener {
91 static final String LOG_TAG = "Launcher";
92 static final boolean LOGD = false;
93
94 private static final boolean PROFILE_STARTUP = false;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070095 private static final boolean PROFILE_DRAWER = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070096 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 private static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070098
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 private static final int WALLPAPER_SCREENS_SPAN = 2;
100
101 private static final int MENU_GROUP_ADD = 1;
102 private static final int MENU_ADD = Menu.FIRST + 1;
103 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
104 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
105 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700106 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
108 private static final int REQUEST_CREATE_SHORTCUT = 1;
109 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700110 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 private static final int REQUEST_PICK_APPLICATION = 6;
112 private static final int REQUEST_PICK_SHORTCUT = 7;
113 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700114 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
116 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
117
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700118 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
119 static final String SEARCH_WIDGET = "search_widget";
120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 static final int SCREEN_COUNT = 3;
122 static final int DEFAULT_SCREN = 1;
123 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700124 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 private static final int DIALOG_CREATE_SHORTCUT = 1;
Romain Guycbb89e42009-06-08 15:52:54 -0700127 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Romain Guy98d01652009-06-30 16:21:04 -0700129 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
131 // Type: int
132 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
133 // Type: boolean
134 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
135 // Type: long
136 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
149 // Type: int
150 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
151 // Type: int[]
152 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
153 // Type: boolean
154 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
155 // Type: long
156 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
157
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700158 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
160 private static Bitmap sWallpaper;
161
162 private static final Object sLock = new Object();
163 private static int sScreen = DEFAULT_SCREN;
164
165 private static WallpaperIntentReceiver sWallpaperReceiver;
166
167 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
168 private final ContentObserver mObserver = new FavoritesChangeObserver();
169
170 private LayoutInflater mInflater;
171
172 private DragLayer mDragLayer;
173 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700174
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700175 private AppWidgetManager mAppWidgetManager;
176 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700177
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private CellLayout.CellInfo mAddItemCellInfo;
181 private CellLayout.CellInfo mMenuAddInfo;
182 private final int[] mCellCoordinates = new int[2];
183 private FolderInfo mFolderInfo;
184
185 private SlidingDrawer mDrawer;
186 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700187 private HandleView mHandleView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private AllAppsGridView mAllAppsGrid;
189
190 private boolean mDesktopLocked = true;
191 private Bundle mSavedState;
192
193 private SpannableStringBuilder mDefaultKeySsb = null;
194
195 private boolean mDestroyed;
Mike LeBeau736cf282009-07-02 17:46:59 -0700196
197 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
199 private boolean mRestoring;
200 private boolean mWaitingForResult;
201 private boolean mLocaleChanged;
202
Dianne Hackborn67800862009-07-24 17:15:20 -0700203 private boolean mHomeDown;
204 private boolean mBackDown;
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private Bundle mSavedInstanceState;
207
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700208 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 @Override
211 protected void onCreate(Bundle savedInstanceState) {
212 super.onCreate(savedInstanceState);
213 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700214
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700216
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700217 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
218 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 if (PROFILE_STARTUP) {
221 android.os.Debug.startMethodTracing("/sdcard/launcher");
222 }
223
224 checkForLocaleChange();
225 setWallpaperDimension();
226
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 setContentView(R.layout.launcher);
228 setupViews();
229
230 registerIntentReceivers();
231 registerContentObservers();
232
233 mSavedState = savedInstanceState;
234 restoreState(mSavedState);
235
236 if (PROFILE_STARTUP) {
237 android.os.Debug.stopMethodTracing();
238 }
239
240 if (!mRestoring) {
241 startLoaders();
242 }
243
244 // For handling default keys
245 mDefaultKeySsb = new SpannableStringBuilder();
246 Selection.setSelection(mDefaultKeySsb, 0);
247 }
Romain Guycbb89e42009-06-08 15:52:54 -0700248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700250 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
251 readConfiguration(this, localeConfiguration);
252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 final Configuration configuration = getResources().getConfiguration();
254
Romain Guy98d01652009-06-30 16:21:04 -0700255 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 final String locale = configuration.locale.toString();
257
Romain Guy98d01652009-06-30 16:21:04 -0700258 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 final int mcc = configuration.mcc;
260
Romain Guy98d01652009-06-30 16:21:04 -0700261 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 final int mnc = configuration.mnc;
263
264 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
265
266 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700267 localeConfiguration.locale = locale;
268 localeConfiguration.mcc = mcc;
269 localeConfiguration.mnc = mnc;
270
271 writeConfiguration(this, localeConfiguration);
272 }
273 }
274
275 private static class LocaleConfiguration {
276 public String locale;
277 public int mcc = -1;
278 public int mnc = -1;
279 }
280
281 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
282 DataInputStream in = null;
283 try {
284 in = new DataInputStream(context.openFileInput(PREFERENCES));
285 configuration.locale = in.readUTF();
286 configuration.mcc = in.readInt();
287 configuration.mnc = in.readInt();
288 } catch (FileNotFoundException e) {
289 // Ignore
290 } catch (IOException e) {
291 // Ignore
292 } finally {
293 if (in != null) {
294 try {
295 in.close();
296 } catch (IOException e) {
297 // Ignore
298 }
299 }
300 }
301 }
302
303 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
304 DataOutputStream out = null;
305 try {
306 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
307 out.writeUTF(configuration.locale);
308 out.writeInt(configuration.mcc);
309 out.writeInt(configuration.mnc);
310 out.flush();
311 } catch (FileNotFoundException e) {
312 // Ignore
313 } catch (IOException e) {
314 //noinspection ResultOfMethodCallIgnored
315 context.getFileStreamPath(PREFERENCES).delete();
316 } finally {
317 if (out != null) {
318 try {
319 out.close();
320 } catch (IOException e) {
321 // Ignore
322 }
323 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 }
325 }
326
327 static int getScreen() {
328 synchronized (sLock) {
329 return sScreen;
330 }
331 }
332
333 static void setScreen(int screen) {
334 synchronized (sLock) {
335 sScreen = screen;
336 }
337 }
338
339 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700340 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
341 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 mRestoring = false;
344 }
345
346 private void setWallpaperDimension() {
347 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
348 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
349
350 Display display = getWindowManager().getDefaultDisplay();
351 boolean isPortrait = display.getWidth() < display.getHeight();
352
353 final int width = isPortrait ? display.getWidth() : display.getHeight();
354 final int height = isPortrait ? display.getHeight() : display.getWidth();
355 try {
356 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
357 } catch (RemoteException e) {
358 // System is dead!
359 }
360 }
361
362 @Override
363 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700364 mWaitingForResult = false;
365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 // The pattern used here is that a user PICKs a specific application,
367 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
370 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700371
Romain Guy94dabf12009-07-21 10:55:43 -0700372 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 switch (requestCode) {
374 case REQUEST_PICK_APPLICATION:
375 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
376 break;
377 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700378 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 break;
380 case REQUEST_CREATE_SHORTCUT:
381 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
382 break;
383 case REQUEST_PICK_LIVE_FOLDER:
384 addLiveFolder(data);
385 break;
386 case REQUEST_CREATE_LIVE_FOLDER:
387 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
388 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700389 case REQUEST_PICK_APPWIDGET:
390 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700392 case REQUEST_CREATE_APPWIDGET:
393 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 break;
395 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700396 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700398 // Clean up the appWidgetId if we canceled
399 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
400 if (appWidgetId != -1) {
401 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 }
403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 }
405
406 @Override
407 protected void onResume() {
408 super.onResume();
409
410 if (mRestoring) {
411 startLoaders();
412 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700413
414 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
415 // onNewIntent), then close the search dialog if needed, because it probably
416 // came from the user pressing 'home' (rather than, for example, pressing 'back').
417 if (mIsNewIntent) {
418 // Post to a handler so that this happens after the search dialog tries to open
419 // itself again.
420 mWorkspace.post(new Runnable() {
421 public void run() {
422 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
423 ServiceManager.getService(Context.SEARCH_SERVICE));
424 try {
425 searchManagerService.stopSearch();
426 } catch (RemoteException e) {
427 e(LOG_TAG, "error stopping search", e);
428 }
429 }
430 });
431 }
432
433 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 }
435
436 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700437 protected void onPause() {
438 super.onPause();
Romain Guycbb89e42009-06-08 15:52:54 -0700439 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700440 }
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700442 @Override
443 public Object onRetainNonConfigurationInstance() {
444 // Flag any binder to stop early before switching
445 if (mBinder != null) {
446 mBinder.mTerminate = true;
447 }
Romain Guycbb89e42009-06-08 15:52:54 -0700448
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700449 if (PROFILE_ROTATE) {
450 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
451 }
452 return null;
453 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 private boolean acceptFilter() {
456 final InputMethodManager inputManager = (InputMethodManager)
457 getSystemService(Context.INPUT_METHOD_SERVICE);
458 return !inputManager.isFullscreenMode();
459 }
460
461 @Override
462 public boolean onKeyDown(int keyCode, KeyEvent event) {
463 boolean handled = super.onKeyDown(keyCode, event);
464 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
465 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
466 keyCode, event);
467 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700468 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700469 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700470 // showSearchDialog()
471 // If there are multiple keystrokes before the search dialog takes focus,
472 // onSearchRequested() will be called for every keystroke,
473 // but it is idempotent, so it's fine.
474 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 }
476 }
477
478 return handled;
479 }
480
Karl Rosaen138a0412009-04-23 19:00:21 -0700481 private String getTypedText() {
482 return mDefaultKeySsb.toString();
483 }
484
485 private void clearTypedText() {
486 mDefaultKeySsb.clear();
487 mDefaultKeySsb.clearSpans();
488 Selection.setSelection(mDefaultKeySsb, 0);
489 }
490
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491 /**
492 * Restores the previous state, if it exists.
493 *
494 * @param savedState The previous state.
495 */
496 private void restoreState(Bundle savedState) {
497 if (savedState == null) {
498 return;
499 }
500
501 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
502 if (currentScreen > -1) {
503 mWorkspace.setCurrentScreen(currentScreen);
504 }
505
506 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
507 if (addScreen > -1) {
508 mAddItemCellInfo = new CellLayout.CellInfo();
509 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
510 addItemCellInfo.valid = true;
511 addItemCellInfo.screen = addScreen;
512 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
513 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
514 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
515 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
516 addItemCellInfo.findVacantCellsFromOccupied(
517 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
518 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
519 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
520 mRestoring = true;
521 }
522
523 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
524 if (renameFolder) {
525 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
526 mFolderInfo = sModel.getFolderById(this, id);
527 mRestoring = true;
528 }
529 }
530
531 /**
532 * Finds all the views we need and configure them properly.
533 */
534 private void setupViews() {
535 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
536 final DragLayer dragLayer = mDragLayer;
537
538 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
539 final Workspace workspace = mWorkspace;
540
541 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
542 final SlidingDrawer drawer = mDrawer;
543
544 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
545 final AllAppsGridView grid = mAllAppsGrid;
546
547 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
548
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700549 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
550 mHandleView.setLauncher(this);
551 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 mHandleIcon.setCrossFadeEnabled(true);
553
554 drawer.lock();
555 final DrawerManager drawerManager = new DrawerManager();
556 drawer.setOnDrawerOpenListener(drawerManager);
557 drawer.setOnDrawerCloseListener(drawerManager);
558 drawer.setOnDrawerScrollListener(drawerManager);
559
Karl Rosaen138a0412009-04-23 19:00:21 -0700560 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 grid.setDragger(dragLayer);
562 grid.setLauncher(this);
563
564 workspace.setOnLongClickListener(this);
565 workspace.setDragger(dragLayer);
566 workspace.setLauncher(this);
567 loadWallpaper();
568
569 deleteZone.setLauncher(this);
570 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700571 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572
573 dragLayer.setIgnoredDropTarget(grid);
574 dragLayer.setDragScoller(workspace);
575 dragLayer.setDragListener(deleteZone);
576 }
577
578 /**
579 * Creates a view representing a shortcut.
580 *
581 * @param info The data structure describing the shortcut.
582 *
583 * @return A View inflated from R.layout.application.
584 */
585 View createShortcut(ApplicationInfo info) {
586 return createShortcut(R.layout.application,
587 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
588 }
589
590 /**
591 * Creates a view representing a shortcut inflated from the specified resource.
592 *
593 * @param layoutResId The id of the XML layout used to create the shortcut.
594 * @param parent The group the shortcut belongs to.
595 * @param info The data structure describing the shortcut.
596 *
597 * @return A View inflated from layoutResId.
598 */
599 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
600 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
601
602 if (!info.filtered) {
603 info.icon = Utilities.createIconThumbnail(info.icon, this);
604 info.filtered = true;
605 }
606
607 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
608 favorite.setText(info.title);
609 favorite.setTag(info);
610 favorite.setOnClickListener(this);
611
612 return favorite;
613 }
614
615 /**
616 * Add an application shortcut to the workspace.
617 *
618 * @param data The intent describing the application.
619 * @param cellInfo The position on screen where to create the shortcut.
620 */
621 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
622 boolean insertAtFirst) {
623 cellInfo.screen = mWorkspace.getCurrentScreen();
624 if (!findSingleSlot(cellInfo)) return;
625
Romain Guy73b979d2009-06-09 12:57:21 -0700626 final ApplicationInfo info = infoFromApplicationIntent(context, data);
627 if (info != null) {
628 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
629 }
630 }
631
632 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 ComponentName component = data.getComponent();
634 PackageManager packageManager = context.getPackageManager();
635 ActivityInfo activityInfo = null;
636 try {
637 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
638 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700639 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
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 if (activityInfo != null) {
643 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700644
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 itemInfo.title = activityInfo.loadLabel(packageManager);
646 if (itemInfo.title == null) {
647 itemInfo.title = activityInfo.name;
648 }
Romain Guycbb89e42009-06-08 15:52:54 -0700649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
651 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
652 itemInfo.icon = activityInfo.loadIcon(packageManager);
653 itemInfo.container = ItemInfo.NO_ID;
654
Romain Guy73b979d2009-06-09 12:57:21 -0700655 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 }
Romain Guy73b979d2009-06-09 12:57:21 -0700657
658 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
Romain Guycbb89e42009-06-08 15:52:54 -0700660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 /**
662 * Add a shortcut to the workspace.
663 *
664 * @param data The intent describing the shortcut.
665 * @param cellInfo The position on screen where to create the shortcut.
666 * @param insertAtFirst
667 */
668 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
669 boolean insertAtFirst) {
670 cellInfo.screen = mWorkspace.getCurrentScreen();
671 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
674
675 if (!mRestoring) {
676 sModel.addDesktopItem(info);
677
678 final View view = createShortcut(info);
679 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
680 } else if (sModel.isDesktopLoaded()) {
681 sModel.addDesktopItem(info);
682 }
683 }
684
Romain Guycbb89e42009-06-08 15:52:54 -0700685
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700689 * @param data The intent describing the appWidgetId.
690 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700692 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 boolean insertAtFirst) {
694
695 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700696 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700697
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700698 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700699
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700700 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700702 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700704 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 // Try finding open space on Launcher screen
707 final int[] xy = mCellCoordinates;
708 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
709
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700710 // Build Launcher-specific widget info and save to database
711 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 launcherInfo.spanX = spans[0];
713 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 LauncherModel.addItemToDatabase(this, launcherInfo,
716 LauncherSettings.Favorites.CONTAINER_DESKTOP,
717 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
718
719 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700720 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700723 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700724
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700725 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
729 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
730 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700731 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 }
733 }
Romain Guycbb89e42009-06-08 15:52:54 -0700734
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700735 public LauncherAppWidgetHost getAppWidgetHost() {
736 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 }
Romain Guycbb89e42009-06-08 15:52:54 -0700738
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 static ApplicationInfo addShortcut(Context context, Intent data,
740 CellLayout.CellInfo cellInfo, boolean notify) {
741
Romain Guy73b979d2009-06-09 12:57:21 -0700742 final ApplicationInfo info = infoFromShortcutIntent(context, data);
743 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
744 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
745
746 return info;
747 }
748
749 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
751 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
752 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
753
754 Drawable icon = null;
755 boolean filtered = false;
756 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700757 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758
759 if (bitmap != null) {
760 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
761 filtered = true;
762 customIcon = true;
763 } else {
764 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700765 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700767 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 final PackageManager packageManager = context.getPackageManager();
769 Resources resources = packageManager.getResourcesForApplication(
770 iconResource.packageName);
771 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
772 icon = resources.getDrawable(id);
773 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700774 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 }
776 }
777 }
778
779 if (icon == null) {
780 icon = context.getPackageManager().getDefaultActivityIcon();
781 }
782
783 final ApplicationInfo info = new ApplicationInfo();
784 info.icon = icon;
785 info.filtered = filtered;
786 info.title = name;
787 info.intent = intent;
788 info.customIcon = customIcon;
789 info.iconResource = iconResource;
790
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 return info;
792 }
793
794 @Override
795 protected void onNewIntent(Intent intent) {
796 super.onNewIntent(intent);
797
798 // Close the menu
799 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
800 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700801
802 // Set this flag so that onResume knows to close the search dialog if it's open,
803 // because this was a new intent (thus a press of 'home' or some such) rather than
804 // for example onResume being called when the user pressed the 'back' button.
805 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806
807 try {
808 dismissDialog(DIALOG_CREATE_SHORTCUT);
809 // Unlock the workspace if the dialog was showing
810 mWorkspace.unlock();
811 } catch (Exception e) {
812 // An exception is thrown if the dialog is not visible, which is fine
813 }
814
815 try {
816 dismissDialog(DIALOG_RENAME_FOLDER);
817 // Unlock the workspace if the dialog was showing
818 mWorkspace.unlock();
819 } catch (Exception e) {
820 // An exception is thrown if the dialog is not visible, which is fine
821 }
822
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
824 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700825
Romain Guy94dabf12009-07-21 10:55:43 -0700826 if (!mWorkspace.isDefaultScreenShowing()) {
827 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700829
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700831
832 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 if (v != null && v.getWindowToken() != null) {
834 InputMethodManager imm = (InputMethodManager)getSystemService(
835 INPUT_METHOD_SERVICE);
836 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
837 }
838 } else {
839 closeDrawer(false);
840 }
841 }
842 }
843
844 @Override
845 protected void onRestoreInstanceState(Bundle savedInstanceState) {
846 // Do not call super here
847 mSavedInstanceState = savedInstanceState;
848 }
849
850 @Override
851 protected void onSaveInstanceState(Bundle outState) {
852 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
853
854 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
855 if (folders.size() > 0) {
856 final int count = folders.size();
857 long[] ids = new long[count];
858 for (int i = 0; i < count; i++) {
859 final FolderInfo info = folders.get(i).getInfo();
860 ids[i] = info.id;
861 }
862 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
863 } else {
864 super.onSaveInstanceState(outState);
865 }
866
Romain Guy3cf604f2009-06-16 13:12:53 -0700867 final boolean isConfigurationChange = getChangingConfigurations() != 0;
868
Romain Guy5a941392009-04-28 15:18:25 -0700869 // When the drawer is opened and we are saving the state because of a
870 // configuration change
Romain Guy3cf604f2009-06-16 13:12:53 -0700871 if (mDrawer.isOpened() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874
875 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
876 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
877 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
878
879 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
880 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
881 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
882 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
883 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
884 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
885 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
886 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
887 layout.getOccupiedCells());
888 }
889
890 if (mFolderInfo != null && mWaitingForResult) {
891 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
892 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
893 }
894 }
895
896 @Override
897 public void onDestroy() {
898 mDestroyed = true;
899
900 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700901
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700903 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700905 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 }
907
908 TextKeyListener.getInstance().release();
909
910 mAllAppsGrid.clearTextFilter();
911 mAllAppsGrid.setAdapter(null);
912 sModel.unbind();
913 sModel.abortLoaders();
914
915 getContentResolver().unregisterContentObserver(mObserver);
916 unregisterReceiver(mApplicationsReceiver);
917 }
918
919 @Override
920 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700921 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 super.startActivityForResult(intent, requestCode);
923 }
924
925 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700926 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700928
929 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700930
Karl Rosaen138a0412009-04-23 19:00:21 -0700931 // Slide the search widget to the top, if it's on the current screen,
932 // otherwise show the search dialog immediately.
933 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
934 if (searchWidget == null) {
935 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
936 } else {
937 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
938 // show the currently typed text in the search widget while sliding
939 searchWidget.setQuery(getTypedText());
940 }
941 }
Romain Guycbb89e42009-06-08 15:52:54 -0700942
Karl Rosaen138a0412009-04-23 19:00:21 -0700943 /**
944 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700945 *
946 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700947 */
Romain Guycbb89e42009-06-08 15:52:54 -0700948 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700949 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700950
Karl Rosaen138a0412009-04-23 19:00:21 -0700951 if (initialQuery == null) {
952 // Use any text typed in the launcher as the initial query
953 initialQuery = getTypedText();
954 clearTypedText();
955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 if (appSearchData == null) {
957 appSearchData = new Bundle();
958 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
959 }
Romain Guycbb89e42009-06-08 15:52:54 -0700960
Karl Rosaen138a0412009-04-23 19:00:21 -0700961 final SearchManager searchManager =
962 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
963
964 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
965 if (searchWidget != null) {
966 // This gets called when the user leaves the search dialog to go back to
967 // the Launcher.
968 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
969 public void onCancel() {
970 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700971 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700972 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700973 });
974 }
Romain Guycbb89e42009-06-08 15:52:54 -0700975
Karl Rosaen138a0412009-04-23 19:00:21 -0700976 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700977 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 }
979
Karl Rosaen138a0412009-04-23 19:00:21 -0700980 /**
981 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700982 */
Romain Guy5a941392009-04-28 15:18:25 -0700983 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700984 // Close search dialog
985 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100986 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700987 // Restore search widget to its normal position
988 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
989 if (searchWidget != null) {
990 searchWidget.stopSearch(false);
991 }
992 }
Romain Guycbb89e42009-06-08 15:52:54 -0700993
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 @Override
995 public boolean onCreateOptionsMenu(Menu menu) {
996 if (mDesktopLocked) return false;
997
998 super.onCreateOptionsMenu(menu);
999 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1000 .setIcon(android.R.drawable.ic_menu_add)
1001 .setAlphabeticShortcut('A');
1002 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1003 .setIcon(android.R.drawable.ic_menu_gallery)
1004 .setAlphabeticShortcut('W');
1005 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1006 .setIcon(android.R.drawable.ic_search_category_default)
1007 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1008 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1009 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1010 .setAlphabeticShortcut('N');
1011
1012 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001013 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1014 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015
1016 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1017 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1018 .setIntent(settings);
1019
1020 return true;
1021 }
1022
1023 @Override
1024 public boolean onPrepareOptionsMenu(Menu menu) {
1025 super.onPrepareOptionsMenu(menu);
1026
1027 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1028 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1029
1030 return true;
1031 }
1032
1033 @Override
1034 public boolean onOptionsItemSelected(MenuItem item) {
1035 switch (item.getItemId()) {
1036 case MENU_ADD:
1037 addItems();
1038 return true;
1039 case MENU_WALLPAPER_SETTINGS:
1040 startWallpaper();
1041 return true;
1042 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001043 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 return true;
1045 case MENU_NOTIFICATIONS:
1046 showNotifications();
1047 return true;
1048 }
1049
1050 return super.onOptionsItemSelected(item);
1051 }
Romain Guycbb89e42009-06-08 15:52:54 -07001052
Karl Rosaen138a0412009-04-23 19:00:21 -07001053 /**
1054 * Indicates that we want global search for this activity by setting the globalSearch
1055 * argument for {@link #startSearch} to true.
1056 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001058 @Override
1059 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001060 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001061 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001062 }
1063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 private void addItems() {
1065 showAddDialog(mMenuAddInfo);
1066 }
1067
1068 private void removeShortcutsForPackage(String packageName) {
1069 if (packageName != null && packageName.length() > 0) {
1070 mWorkspace.removeShortcutsForPackage(packageName);
1071 }
1072 }
Romain Guycbb89e42009-06-08 15:52:54 -07001073
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001074 private void updateShortcutsForPackage(String packageName) {
1075 if (packageName != null && packageName.length() > 0) {
1076 mWorkspace.updateShortcutsForPackage(packageName);
1077 }
1078 }
1079
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001080 void addAppWidget(Intent data) {
1081 // TODO: catch bad widget exception when sent
1082 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001084 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1085 if (SEARCH_WIDGET.equals(customWidget)) {
1086 // We don't need this any more, since this isn't a real app widget.
1087 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1088 // add the search widget
1089 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001091 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1092
1093 if (appWidget.configure != null) {
1094 // Launch over to configure widget, if needed
1095 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1096 intent.setComponent(appWidget.configure);
1097 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1098
1099 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1100 } else {
1101 // Otherwise just add it
1102 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 }
1105 }
Romain Guycbb89e42009-06-08 15:52:54 -07001106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 void addSearch() {
1108 final Widget info = Widget.makeSearch();
1109 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 final int[] xy = mCellCoordinates;
1112 final int spanX = info.spanX;
1113 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 sModel.addDesktopItem(info);
1118 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1119 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 final View view = mInflater.inflate(info.layoutResource, null);
1122 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001123 Search search = (Search) view.findViewById(R.id.widget_search);
1124 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1127 }
1128
Romain Guy73b979d2009-06-09 12:57:21 -07001129 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001130 // Handle case where user selected "Applications"
1131 String applicationName = getResources().getString(R.string.group_applications);
1132 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001133
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001134 if (applicationName != null && applicationName.equals(shortcutName)) {
1135 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1136 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001137
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001138 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1139 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001140 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001141 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001142 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001143 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 }
1145
1146 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001147 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001148 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001149 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001150
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001151 if (folderName != null && folderName.equals(shortcutName)) {
1152 addFolder(!mDesktopLocked);
1153 } else {
1154 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1155 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 }
1157
1158 void addFolder(boolean insertAtFirst) {
1159 UserFolderInfo folderInfo = new UserFolderInfo();
1160 folderInfo.title = getText(R.string.folder_name);
1161
1162 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1163 cellInfo.screen = mWorkspace.getCurrentScreen();
1164 if (!findSingleSlot(cellInfo)) return;
1165
1166 // Update the model
1167 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1168 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1169 sModel.addDesktopItem(folderInfo);
1170 sModel.addFolder(folderInfo);
1171
1172 // Create the view
1173 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1174 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1175 mWorkspace.addInCurrentScreen(newFolder,
1176 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1177 }
Romain Guycbb89e42009-06-08 15:52:54 -07001178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1180 boolean insertAtFirst) {
1181 cellInfo.screen = mWorkspace.getCurrentScreen();
1182 if (!findSingleSlot(cellInfo)) return;
1183
1184 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1185
1186 if (!mRestoring) {
1187 sModel.addDesktopItem(info);
1188
1189 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1190 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1191 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1192 } else if (sModel.isDesktopLoaded()) {
1193 sModel.addDesktopItem(info);
1194 }
1195 }
1196
1197 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1198 CellLayout.CellInfo cellInfo, boolean notify) {
1199
1200 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1201 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1202
1203 Drawable icon = null;
1204 boolean filtered = false;
1205 Intent.ShortcutIconResource iconResource = null;
1206
1207 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1208 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1209 try {
1210 iconResource = (Intent.ShortcutIconResource) extra;
1211 final PackageManager packageManager = context.getPackageManager();
1212 Resources resources = packageManager.getResourcesForApplication(
1213 iconResource.packageName);
1214 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1215 icon = resources.getDrawable(id);
1216 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001217 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 }
1219 }
1220
1221 if (icon == null) {
1222 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1223 }
1224
1225 final LiveFolderInfo info = new LiveFolderInfo();
1226 info.icon = icon;
1227 info.filtered = filtered;
1228 info.title = name;
1229 info.iconResource = iconResource;
1230 info.uri = data.getData();
1231 info.baseIntent = baseIntent;
1232 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1233 LiveFolders.DISPLAY_MODE_GRID);
1234
1235 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1236 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1237 sModel.addFolder(info);
1238
1239 return info;
1240 }
1241
1242 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1243 final int[] xy = new int[2];
1244 if (findSlot(cellInfo, xy, 1, 1)) {
1245 cellInfo.cellX = xy[0];
1246 cellInfo.cellY = xy[1];
1247 return true;
1248 }
1249 return false;
1250 }
1251
1252 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1253 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1254 boolean[] occupied = mSavedState != null ?
1255 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1256 cellInfo = mWorkspace.findAllVacantCells(occupied);
1257 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1258 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1259 return false;
1260 }
1261 }
1262 return true;
1263 }
1264
1265 private void showNotifications() {
1266 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1267 if (statusBar != null) {
1268 statusBar.expand();
1269 }
1270 }
1271
1272 private void startWallpaper() {
1273 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1274 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1275 }
1276
1277 /**
1278 * Registers various intent receivers. The current implementation registers
1279 * only a wallpaper intent receiver to let other applications change the
1280 * wallpaper.
1281 */
1282 private void registerIntentReceivers() {
1283 if (sWallpaperReceiver == null) {
1284 final Application application = getApplication();
1285
1286 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1287
1288 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1289 application.registerReceiver(sWallpaperReceiver, filter);
1290 } else {
1291 sWallpaperReceiver.setLauncher(this);
1292 }
1293
1294 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1295 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1296 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1297 filter.addDataScheme("package");
1298 registerReceiver(mApplicationsReceiver, filter);
1299 }
1300
1301 /**
1302 * Registers various content observers. The current implementation registers
1303 * only a favorites observer to keep track of the favorites applications.
1304 */
1305 private void registerContentObservers() {
1306 ContentResolver resolver = getContentResolver();
1307 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1308 }
1309
1310 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001311 public void onWindowFocusChanged(boolean hasFocus) {
1312 super.onWindowFocusChanged(hasFocus);
1313 if (!hasFocus) {
1314 mBackDown = mHomeDown = false;
1315 }
1316 }
1317
1318 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 public boolean dispatchKeyEvent(KeyEvent event) {
1320 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1321 switch (event.getKeyCode()) {
1322 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001323 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 return true;
1325 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001326 mHomeDown = true;
1327 return true;
1328 }
1329 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1330 switch (event.getKeyCode()) {
1331 case KeyEvent.KEYCODE_BACK:
1332 if (!event.isCanceled()) {
1333 mWorkspace.dispatchKeyEvent(event);
1334 if (mDrawer.isOpened()) {
1335 closeDrawer();
1336 } else {
1337 closeFolder();
1338 }
1339 }
1340 mBackDown = false;
1341 return true;
1342 case KeyEvent.KEYCODE_HOME:
1343 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 return true;
1345 }
1346 }
1347
1348 return super.dispatchKeyEvent(event);
1349 }
1350
1351 private void closeDrawer() {
1352 closeDrawer(true);
1353 }
1354
1355 private void closeDrawer(boolean animated) {
1356 if (mDrawer.isOpened()) {
1357 if (animated) {
1358 mDrawer.animateClose();
1359 } else {
1360 mDrawer.close();
1361 }
1362 if (mDrawer.hasFocus()) {
1363 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1364 }
1365 }
1366 }
1367
1368 private void closeFolder() {
1369 Folder folder = mWorkspace.getOpenFolder();
1370 if (folder != null) {
1371 closeFolder(folder);
1372 }
1373 }
1374
1375 void closeFolder(Folder folder) {
1376 folder.getInfo().opened = false;
1377 ViewGroup parent = (ViewGroup) folder.getParent();
1378 if (parent != null) {
1379 parent.removeView(folder);
1380 }
1381 folder.onClose();
1382 }
1383
1384 /**
1385 * When the notification that favorites have changed is received, requests
1386 * a favorites list refresh.
1387 */
1388 private void onFavoritesChanged() {
1389 mDesktopLocked = true;
1390 mDrawer.lock();
1391 sModel.loadUserItems(false, this, false, false);
1392 }
1393
1394 void onDesktopItemsLoaded() {
1395 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 bindDesktopItems();
1397 }
Romain Guycbb89e42009-06-08 15:52:54 -07001398
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 /**
1400 * Refreshes the shortcuts shown on the workspace.
1401 */
1402 private void bindDesktopItems() {
1403 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001404 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001405 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1406 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 return;
1408 }
1409
1410 final Workspace workspace = mWorkspace;
1411 int count = workspace.getChildCount();
1412 for (int i = 0; i < count; i++) {
1413 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1414 }
Romain Guycbb89e42009-06-08 15:52:54 -07001415
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 if (DEBUG_USER_INTERFACE) {
1417 android.widget.Button finishButton = new android.widget.Button(this);
1418 finishButton.setText("Finish");
1419 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1420
1421 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1422 public void onClick(View v) {
1423 finish();
1424 }
1425 });
1426 }
Romain Guycbb89e42009-06-08 15:52:54 -07001427
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001428 // Flag any old binder to terminate early
1429 if (mBinder != null) {
1430 mBinder.mTerminate = true;
1431 }
Romain Guycbb89e42009-06-08 15:52:54 -07001432
Karl Rosaen138a0412009-04-23 19:00:21 -07001433 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001434 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
1436
1437 private void bindItems(Launcher.DesktopBinder binder,
1438 ArrayList<ItemInfo> shortcuts, int start, int count) {
1439
1440 final Workspace workspace = mWorkspace;
1441 final boolean desktopLocked = mDesktopLocked;
1442
1443 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1444 int i = start;
1445
1446 for ( ; i < end; i++) {
1447 final ItemInfo item = shortcuts.get(i);
1448 switch (item.itemType) {
1449 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1450 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1451 final View shortcut = createShortcut((ApplicationInfo) item);
1452 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1453 !desktopLocked);
1454 break;
1455 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1456 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1457 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1458 (UserFolderInfo) item);
1459 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1460 !desktopLocked);
1461 break;
1462 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1463 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1464 R.layout.live_folder_icon, this,
1465 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1466 (LiveFolderInfo) item);
1467 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1468 !desktopLocked);
1469 break;
1470 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1471 final int screen = workspace.getCurrentScreen();
1472 final View view = mInflater.inflate(R.layout.widget_search,
1473 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001474
Karl Rosaen138a0412009-04-23 19:00:21 -07001475 Search search = (Search) view.findViewById(R.id.widget_search);
1476 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 final Widget widget = (Widget) item;
1479 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 workspace.addWidget(view, widget, !desktopLocked);
1482 break;
1483 }
1484 }
1485
1486 workspace.requestLayout();
1487
1488 if (end >= count) {
1489 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001490 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 } else {
1492 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1493 }
1494 }
1495
1496 private void finishBindDesktopItems() {
1497 if (mSavedState != null) {
1498 if (!mWorkspace.hasFocus()) {
1499 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1500 }
1501
1502 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1503 if (userFolders != null) {
1504 for (long folderId : userFolders) {
1505 final FolderInfo info = sModel.findFolderById(folderId);
1506 if (info != null) {
1507 openFolder(info);
1508 }
1509 }
1510 final Folder openFolder = mWorkspace.getOpenFolder();
1511 if (openFolder != null) {
1512 openFolder.requestFocus();
1513 }
1514 }
1515
1516 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1517 if (allApps) {
1518 mDrawer.open();
1519 }
1520
1521 mSavedState = null;
1522 }
1523
1524 if (mSavedInstanceState != null) {
1525 super.onRestoreInstanceState(mSavedInstanceState);
1526 mSavedInstanceState = null;
1527 }
1528
1529 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1530 mDrawer.requestFocus();
1531 }
1532
1533 mDesktopLocked = false;
1534 mDrawer.unlock();
1535 }
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Karl Rosaen138a0412009-04-23 19:00:21 -07001537 private void bindDrawer(Launcher.DesktopBinder binder,
1538 ApplicationsAdapter drawerAdapter) {
1539 mAllAppsGrid.setAdapter(drawerAdapter);
1540 binder.startBindingAppWidgetsWhenIdle();
1541 }
Romain Guycbb89e42009-06-08 15:52:54 -07001542
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001543 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001544 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001545
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 final Workspace workspace = mWorkspace;
1547 final boolean desktopLocked = mDesktopLocked;
1548
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001549 if (!appWidgets.isEmpty()) {
1550 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001551
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001552 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001553 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001554 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Karl Rosaen138a0412009-04-23 19:00:21 -07001556 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001557
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001558 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1559 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001560
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001561 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1562 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001563
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001564 workspace.requestLayout();
1565 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001567 if (appWidgets.isEmpty()) {
1568 if (PROFILE_ROTATE) {
1569 android.os.Debug.stopMethodTracing();
1570 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001572 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 DragController getDragController() {
1577 return mDragLayer;
1578 }
1579
1580 /**
1581 * Launches the intent referred by the clicked shortcut.
1582 *
1583 * @param v The view representing the clicked shortcut.
1584 */
1585 public void onClick(View v) {
1586 Object tag = v.getTag();
1587 if (tag instanceof ApplicationInfo) {
1588 // Open shortcut
1589 final Intent intent = ((ApplicationInfo) tag).intent;
1590 startActivitySafely(intent);
1591 } else if (tag instanceof FolderInfo) {
1592 handleFolderClick((FolderInfo) tag);
1593 }
1594 }
1595
1596 void startActivitySafely(Intent intent) {
1597 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1598 try {
1599 startActivity(intent);
1600 } catch (ActivityNotFoundException e) {
1601 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1602 } catch (SecurityException e) {
1603 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001604 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1606 "or use the exported attribute for this activity.", e);
1607 }
1608 }
1609
1610 private void handleFolderClick(FolderInfo folderInfo) {
1611 if (!folderInfo.opened) {
1612 // Close any open folder
1613 closeFolder();
1614 // Open the requested folder
1615 openFolder(folderInfo);
1616 } else {
1617 // Find the open folder...
1618 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1619 int folderScreen;
1620 if (openFolder != null) {
1621 folderScreen = mWorkspace.getScreenForView(openFolder);
1622 // .. and close it
1623 closeFolder(openFolder);
1624 if (folderScreen != mWorkspace.getCurrentScreen()) {
1625 // Close any folder open on the current screen
1626 closeFolder();
1627 // Pull the folder onto this screen
1628 openFolder(folderInfo);
1629 }
1630 }
1631 }
1632 }
1633
1634 private void loadWallpaper() {
1635 // The first time the application is started, we load the wallpaper from
1636 // the ApplicationContext
1637 if (sWallpaper == null) {
1638 final Drawable drawable = getWallpaper();
1639 if (drawable instanceof BitmapDrawable) {
1640 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1641 } else {
1642 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1643 }
1644 }
1645 mWorkspace.loadWallpaper(sWallpaper);
1646 }
1647
1648 /**
1649 * Opens the user fodler described by the specified tag. The opening of the folder
1650 * is animated relative to the specified View. If the View is null, no animation
1651 * is played.
1652 *
1653 * @param folderInfo The FolderInfo describing the folder to open.
1654 */
1655 private void openFolder(FolderInfo folderInfo) {
1656 Folder openFolder;
1657
1658 if (folderInfo instanceof UserFolderInfo) {
1659 openFolder = UserFolder.fromXml(this);
1660 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001661 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 } else {
1663 return;
1664 }
1665
1666 openFolder.setDragger(mDragLayer);
1667 openFolder.setLauncher(this);
1668
1669 openFolder.bind(folderInfo);
1670 folderInfo.opened = true;
1671
1672 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1673 openFolder.onOpen();
1674 }
1675
1676 /**
1677 * Returns true if the workspace is being loaded. When the workspace is loading,
1678 * no user interaction should be allowed to avoid any conflict.
1679 *
1680 * @return True if the workspace is locked, false otherwise.
1681 */
1682 boolean isWorkspaceLocked() {
1683 return mDesktopLocked;
1684 }
1685
1686 public boolean onLongClick(View v) {
1687 if (mDesktopLocked) {
1688 return false;
1689 }
1690
1691 if (!(v instanceof CellLayout)) {
1692 v = (View) v.getParent();
1693 }
1694
1695 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1696
1697 // This happens when long clicking an item with the dpad/trackball
1698 if (cellInfo == null) {
1699 return true;
1700 }
1701
1702 if (mWorkspace.allowLongPress()) {
1703 if (cellInfo.cell == null) {
1704 if (cellInfo.valid) {
1705 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001706 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 showAddDialog(cellInfo);
1708 }
1709 } else {
1710 if (!(cellInfo.cell instanceof Folder)) {
1711 // User long pressed on an item
1712 mWorkspace.startDrag(cellInfo);
1713 }
1714 }
1715 }
1716 return true;
1717 }
1718
1719 static LauncherModel getModel() {
1720 return sModel;
1721 }
1722
1723 void closeAllApplications() {
1724 mDrawer.close();
1725 }
1726
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001727 View getDrawerHandle() {
1728 return mHandleView;
1729 }
1730
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 boolean isDrawerDown() {
1732 return !mDrawer.isMoving() && !mDrawer.isOpened();
1733 }
1734
1735 boolean isDrawerUp() {
1736 return mDrawer.isOpened() && !mDrawer.isMoving();
1737 }
1738
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001739 boolean isDrawerMoving() {
1740 return mDrawer.isMoving();
1741 }
1742
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 Workspace getWorkspace() {
1744 return mWorkspace;
1745 }
1746
1747 GridView getApplicationsGrid() {
1748 return mAllAppsGrid;
1749 }
1750
1751 @Override
1752 protected Dialog onCreateDialog(int id) {
1753 switch (id) {
1754 case DIALOG_CREATE_SHORTCUT:
1755 return new CreateShortcut().createDialog();
1756 case DIALOG_RENAME_FOLDER:
1757 return new RenameFolder().createDialog();
1758 }
1759
1760 return super.onCreateDialog(id);
1761 }
1762
1763 @Override
1764 protected void onPrepareDialog(int id, Dialog dialog) {
1765 switch (id) {
1766 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 break;
1768 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001769 if (mFolderInfo != null) {
1770 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1771 final CharSequence text = mFolderInfo.title;
1772 input.setText(text);
1773 input.setSelection(0, text.length());
1774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 break;
1776 }
1777 }
1778
1779 void showRenameDialog(FolderInfo info) {
1780 mFolderInfo = info;
1781 mWaitingForResult = true;
1782 showDialog(DIALOG_RENAME_FOLDER);
1783 }
1784
1785 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1786 mAddItemCellInfo = cellInfo;
1787 mWaitingForResult = true;
1788 showDialog(DIALOG_CREATE_SHORTCUT);
1789 }
1790
Romain Guy73b979d2009-06-09 12:57:21 -07001791 private void pickShortcut(int requestCode, int title) {
1792 Bundle bundle = new Bundle();
1793
1794 ArrayList<String> shortcutNames = new ArrayList<String>();
1795 shortcutNames.add(getString(R.string.group_applications));
1796 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1797
1798 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1799 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1800 R.drawable.ic_launcher_application));
1801 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1802
1803 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1804 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1805 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1806 pickIntent.putExtras(bundle);
1807
1808 startActivityForResult(pickIntent, requestCode);
1809 }
1810
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 private class RenameFolder {
1812 private EditText mInput;
1813
1814 Dialog createDialog() {
1815 mWaitingForResult = true;
1816 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1817 mInput = (EditText) layout.findViewById(R.id.folder_name);
1818
1819 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1820 builder.setIcon(0);
1821 builder.setTitle(getString(R.string.rename_folder_title));
1822 builder.setCancelable(true);
1823 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1824 public void onCancel(DialogInterface dialog) {
1825 cleanup();
1826 }
1827 });
1828 builder.setNegativeButton(getString(R.string.cancel_action),
1829 new Dialog.OnClickListener() {
1830 public void onClick(DialogInterface dialog, int which) {
1831 cleanup();
1832 }
1833 }
1834 );
1835 builder.setPositiveButton(getString(R.string.rename_action),
1836 new Dialog.OnClickListener() {
1837 public void onClick(DialogInterface dialog, int which) {
1838 changeFolderName();
1839 }
1840 }
1841 );
1842 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001843
1844 final AlertDialog dialog = builder.create();
1845 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1846 public void onShow(DialogInterface dialog) {
1847 mWorkspace.lock();
1848 }
1849 });
1850
1851 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 }
1853
1854 private void changeFolderName() {
1855 final String name = mInput.getText().toString();
1856 if (!TextUtils.isEmpty(name)) {
1857 // Make sure we have the right folder info
1858 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1859 mFolderInfo.title = name;
1860 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1861
1862 if (mDesktopLocked) {
1863 mDrawer.lock();
1864 sModel.loadUserItems(false, Launcher.this, false, false);
1865 } else {
1866 final FolderIcon folderIcon = (FolderIcon)
1867 mWorkspace.getViewForTag(mFolderInfo);
1868 if (folderIcon != null) {
1869 folderIcon.setText(name);
1870 getWorkspace().requestLayout();
1871 } else {
1872 mDesktopLocked = true;
1873 mDrawer.lock();
1874 sModel.loadUserItems(false, Launcher.this, false, false);
1875 }
1876 }
1877 }
1878 cleanup();
1879 }
1880
1881 private void cleanup() {
1882 mWorkspace.unlock();
1883 dismissDialog(DIALOG_RENAME_FOLDER);
1884 mWaitingForResult = false;
1885 mFolderInfo = null;
1886 }
1887 }
1888
1889 /**
1890 * Displays the shortcut creation dialog and launches, if necessary, the
1891 * appropriate activity.
1892 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001893 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001894 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1895 DialogInterface.OnShowListener {
1896
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001898
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 Dialog createDialog() {
1900 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001901
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001903
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1905 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001906 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001907
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 builder.setInverseBackgroundForced(true);
1909
1910 AlertDialog dialog = builder.create();
1911 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001912 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001913 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 return dialog;
1916 }
1917
1918 public void onCancel(DialogInterface dialog) {
1919 mWaitingForResult = false;
1920 cleanup();
1921 }
1922
Romain Guycbb89e42009-06-08 15:52:54 -07001923 public void onDismiss(DialogInterface dialog) {
1924 mWorkspace.unlock();
1925 }
1926
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 private void cleanup() {
1928 mWorkspace.unlock();
1929 dismissDialog(DIALOG_CREATE_SHORTCUT);
1930 }
1931
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001932 /**
1933 * Handle the action clicked in the "Add to home" dialog.
1934 */
1935 public void onClick(DialogInterface dialog, int which) {
1936 Resources res = getResources();
1937 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001938
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001939 switch (which) {
1940 case AddAdapter.ITEM_SHORTCUT: {
1941 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001942 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001943 break;
1944 }
Romain Guycbb89e42009-06-08 15:52:54 -07001945
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001946 case AddAdapter.ITEM_APPWIDGET: {
1947 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001948
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001949 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1950 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1951 // add the search widget
1952 ArrayList<AppWidgetProviderInfo> customInfo =
1953 new ArrayList<AppWidgetProviderInfo>();
1954 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1955 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1956 info.label = getString(R.string.group_search);
1957 info.icon = R.drawable.ic_search_widget;
1958 customInfo.add(info);
1959 pickIntent.putParcelableArrayListExtra(
1960 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1961 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1962 Bundle b = new Bundle();
1963 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1964 customExtras.add(b);
1965 pickIntent.putParcelableArrayListExtra(
1966 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1967 // start the pick activity
1968 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1969 break;
1970 }
Romain Guycbb89e42009-06-08 15:52:54 -07001971
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001972 case AddAdapter.ITEM_LIVE_FOLDER: {
1973 // Insert extra item to handle inserting folder
1974 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001975
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001976 ArrayList<String> shortcutNames = new ArrayList<String>();
1977 shortcutNames.add(res.getString(R.string.group_folder));
1978 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001979
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001980 ArrayList<ShortcutIconResource> shortcutIcons =
1981 new ArrayList<ShortcutIconResource>();
1982 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1983 R.drawable.ic_launcher_folder));
1984 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1985
1986 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1987 pickIntent.putExtra(Intent.EXTRA_INTENT,
1988 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1989 pickIntent.putExtra(Intent.EXTRA_TITLE,
1990 getText(R.string.title_select_live_folder));
1991 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001992
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001993 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1994 break;
1995 }
1996
1997 case AddAdapter.ITEM_WALLPAPER: {
1998 startWallpaper();
1999 break;
2000 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 }
2002 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002003
2004 public void onShow(DialogInterface dialog) {
2005 mWorkspace.lock();
2006 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
2008
2009 /**
2010 * Receives notifications when applications are added/removed.
2011 */
2012 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2013 @Override
2014 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002015 final String action = intent.getAction();
2016 final String packageName = intent.getData().getSchemeSpecificPart();
2017 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2018
2019 if (LauncherModel.DEBUG_LOADERS) {
2020 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2021 ", replacing=" + replacing);
2022 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002024
2025 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2026 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2027 if (!replacing) {
2028 removeShortcutsForPackage(packageName);
2029 if (LauncherModel.DEBUG_LOADERS) {
2030 d(LauncherModel.LOG_TAG, " --> remove package");
2031 }
2032 sModel.removePackage(Launcher.this, packageName);
2033 }
2034 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2035 // later, we will update the package at this time
2036 } else {
2037 if (!replacing) {
2038 if (LauncherModel.DEBUG_LOADERS) {
2039 d(LauncherModel.LOG_TAG, " --> add package");
2040 }
2041 sModel.addPackage(Launcher.this, packageName);
2042 } else {
2043 if (LauncherModel.DEBUG_LOADERS) {
2044 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2045 }
2046 sModel.updatePackage(Launcher.this, packageName);
2047 updateShortcutsForPackage(packageName);
2048 }
2049 }
2050 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002052 if (LauncherModel.DEBUG_LOADERS) {
2053 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2054 }
2055 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 }
2057 }
2058 }
2059
2060 /**
2061 * Receives notifications whenever the user favorites have changed.
2062 */
2063 private class FavoritesChangeObserver extends ContentObserver {
2064 public FavoritesChangeObserver() {
2065 super(new Handler());
2066 }
2067
2068 @Override
2069 public void onChange(boolean selfChange) {
2070 onFavoritesChanged();
2071 }
2072 }
2073
2074 /**
2075 * Receives intents from other applications to change the wallpaper.
2076 */
2077 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2078 private final Application mApplication;
2079 private WeakReference<Launcher> mLauncher;
2080
2081 WallpaperIntentReceiver(Application application, Launcher launcher) {
2082 mApplication = application;
2083 setLauncher(launcher);
2084 }
2085
2086 void setLauncher(Launcher launcher) {
2087 mLauncher = new WeakReference<Launcher>(launcher);
2088 }
2089
2090 @Override
2091 public void onReceive(Context context, Intent intent) {
2092 // Load the wallpaper from the ApplicationContext and store it locally
2093 // until the Launcher Activity is ready to use it
2094 final Drawable drawable = mApplication.getWallpaper();
2095 if (drawable instanceof BitmapDrawable) {
2096 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2097 } else {
2098 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2099 }
2100
2101 // If Launcher is alive, notify we have a new wallpaper
2102 if (mLauncher != null) {
2103 final Launcher launcher = mLauncher.get();
2104 if (launcher != null) {
2105 launcher.loadWallpaper();
2106 }
2107 }
2108 }
2109 }
2110
2111 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2112 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2113 private boolean mOpen;
2114
2115 public void onDrawerOpened() {
2116 if (!mOpen) {
2117 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002118
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002120 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121
2122 mOpen = true;
2123 }
2124 }
2125
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002126 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2127 view.getDrawingRect(bounds);
2128
2129 while (view != mDragLayer) {
2130 bounds.offset(view.getLeft(), view.getTop());
2131 view = (View) view.getParent();
2132 }
2133 }
2134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 public void onDrawerClosed() {
2136 if (mOpen) {
2137 mHandleIcon.reverseTransition(150);
2138 mWorkspace.mDrawerBounds.setEmpty();
2139 mOpen = false;
2140 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 mAllAppsGrid.setSelection(0);
2143 mAllAppsGrid.clearTextFilter();
2144 }
2145
2146 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002147 if (PROFILE_DRAWER) {
2148 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2149 }
2150
2151 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2152 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 }
2154
2155 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002156 if (PROFILE_DRAWER) {
2157 android.os.Debug.stopMethodTracing();
2158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 }
2160 }
2161
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002162 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002164 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002165 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 // Number of items to bind in every pass
2168 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169
2170 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002171 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002172 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002174
Karl Rosaen138a0412009-04-23 19:00:21 -07002175 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176
2177 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002178 ArrayList<LauncherAppWidgetInfo> appWidgets,
2179 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180
2181 mLauncher = new WeakReference<Launcher>(launcher);
2182 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002183 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002184
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002185 // Sort widgets so active workspace is bound first
2186 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2187 final int size = appWidgets.size();
2188 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002189
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002190 for (int i = 0; i < size; i++) {
2191 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2192 if (appWidgetInfo.screen == currentScreen) {
2193 mAppWidgets.addFirst(appWidgetInfo);
2194 } else {
2195 mAppWidgets.addLast(appWidgetInfo);
2196 }
2197 }
2198 }
Romain Guycbb89e42009-06-08 15:52:54 -07002199
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 public void startBindingItems() {
2201 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2202 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002203
2204 public void startBindingDrawer() {
2205 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2206 }
Romain Guycbb89e42009-06-08 15:52:54 -07002207
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002208 public void startBindingAppWidgetsWhenIdle() {
2209 // Ask for notification when message queue becomes idle
2210 final MessageQueue messageQueue = Looper.myQueue();
2211 messageQueue.addIdleHandler(this);
2212 }
Romain Guycbb89e42009-06-08 15:52:54 -07002213
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002214 public boolean queueIdle() {
2215 // Queue is idle, so start binding items
2216 startBindingAppWidgets();
2217 return false;
2218 }
2219
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002220 public void startBindingAppWidgets() {
2221 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2222 }
2223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 @Override
2225 public void handleMessage(Message msg) {
2226 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002227 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002228 return;
2229 }
Romain Guycbb89e42009-06-08 15:52:54 -07002230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 switch (msg.what) {
2232 case MESSAGE_BIND_ITEMS: {
2233 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2234 break;
2235 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002236 case MESSAGE_BIND_DRAWER: {
2237 launcher.bindDrawer(this, mDrawerAdapter);
2238 break;
2239 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002240 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002241 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 break;
2243 }
2244 }
2245 }
2246 }
2247}