blob: 0c54382dc844e0810d0d63ca9b85d9d0e5b1ee9f [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
17package com.android.launcher;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Application;
22import android.app.Dialog;
Karl Rosaen138a0412009-04-23 19:00:21 -070023import android.app.IWallpaperService;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.app.SearchManager;
25import android.app.StatusBarManager;
26import android.content.ActivityNotFoundException;
27import android.content.BroadcastReceiver;
28import android.content.ComponentName;
29import android.content.ContentResolver;
30import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
33import android.content.IntentFilter;
34import android.content.SharedPreferences;
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;
Romain Guy73b979d2009-06-09 12:57:21 -070044import android.graphics.PorterDuffXfermode;
45import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.graphics.drawable.BitmapDrawable;
47import android.graphics.drawable.Drawable;
48import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.os.Bundle;
50import android.os.Handler;
51import android.os.IBinder;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070052import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070053import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070054import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Parcelable;
56import android.os.RemoteException;
57import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070058import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.text.Selection;
60import android.text.SpannableStringBuilder;
61import android.text.TextUtils;
62import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070063import static android.util.Log.*;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.KeyEvent;
66import android.view.LayoutInflater;
67import android.view.Menu;
68import android.view.MenuItem;
69import android.view.View;
70import android.view.ViewGroup;
Romain Guy73b979d2009-06-09 12:57:21 -070071import android.view.MotionEvent;
72import android.view.Gravity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.View.OnLongClickListener;
74import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.widget.EditText;
Karl Rosaen138a0412009-04-23 19:00:21 -070076import android.widget.GridView;
Karl Rosaen138a0412009-04-23 19:00:21 -070077import android.widget.SlidingDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.widget.TextView;
79import android.widget.Toast;
Romain Guy73b979d2009-06-09 12:57:21 -070080import android.widget.ImageView;
81import android.widget.PopupWindow;
82import android.widget.ViewSwitcher;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070083import android.appwidget.AppWidgetManager;
84import android.appwidget.AppWidgetProviderInfo;
Romain Guy73b979d2009-06-09 12:57:21 -070085import android.gesture.GestureOverlayView;
86import android.gesture.GestureLibraries;
87import android.gesture.GestureLibrary;
88import android.gesture.Gesture;
89import android.gesture.Prediction;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
91import java.lang.ref.WeakReference;
92import java.util.ArrayList;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070093import java.util.LinkedList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
95/**
96 * Default launcher application.
97 */
98public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener {
99 static final String LOG_TAG = "Launcher";
100 static final boolean LOGD = false;
101
102 private static final boolean PROFILE_STARTUP = false;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700103 private static final boolean PROFILE_DRAWER = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700104 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 private static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700106 private static final boolean DEBUG_GESTURES = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
108 private static final int WALLPAPER_SCREENS_SPAN = 2;
109
110 private static final int MENU_GROUP_ADD = 1;
111 private static final int MENU_ADD = Menu.FIRST + 1;
112 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
113 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
114 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy73b979d2009-06-09 12:57:21 -0700115 private static final int MENU_GESTURES = MENU_NOTIFICATIONS + 1;
116 private static final int MENU_SETTINGS = MENU_GESTURES + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
118 private static final int REQUEST_CREATE_SHORTCUT = 1;
119 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700120 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int REQUEST_PICK_APPLICATION = 6;
122 private static final int REQUEST_PICK_SHORTCUT = 7;
123 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_PICK_APPWIDGET = 9;
Romain Guy73b979d2009-06-09 12:57:21 -0700125 private static final int REQUEST_PICK_GESTURE_ACTION = 10;
126 private static final int REQUEST_CREATE_GESTURE_ACTION = 11;
127 private static final int REQUEST_CREATE_GESTURE_APPLICATION_ACTION = 12;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
130
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700131 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
132 static final String SEARCH_WIDGET = "search_widget";
133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 static final int SCREEN_COUNT = 3;
135 static final int DEFAULT_SCREN = 1;
136 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700137 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
139 private static final int DIALOG_CREATE_SHORTCUT = 1;
Romain Guycbb89e42009-06-08 15:52:54 -0700140 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
142 private static final String PREFERENCES = "launcher";
143 private static final String KEY_LOCALE = "locale";
144 private static final String KEY_MCC = "mcc";
145 private static final String KEY_MNC = "mnc";
146
147 // Type: int
148 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
149 // Type: boolean
150 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
151 // Type: long
152 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
167 // Type: int[]
168 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
169 // Type: boolean
170 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
171 // Type: long
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Romain Guy73b979d2009-06-09 12:57:21 -0700173 // Type: Gesture (Parcelable)
174 private static final String RUNTIME_STATE_PENDING_GESTURE = "launcher.gesture";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700176 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
178 private static Bitmap sWallpaper;
179
180 private static final Object sLock = new Object();
181 private static int sScreen = DEFAULT_SCREN;
182
183 private static WallpaperIntentReceiver sWallpaperReceiver;
184
Romain Guy73b979d2009-06-09 12:57:21 -0700185 private static GestureLibrary sLibrary;
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
188 private final ContentObserver mObserver = new FavoritesChangeObserver();
189
190 private LayoutInflater mInflater;
191
192 private DragLayer mDragLayer;
193 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700194
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700195 private AppWidgetManager mAppWidgetManager;
196 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700197
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700198 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private CellLayout.CellInfo mAddItemCellInfo;
201 private CellLayout.CellInfo mMenuAddInfo;
202 private final int[] mCellCoordinates = new int[2];
203 private FolderInfo mFolderInfo;
204
205 private SlidingDrawer mDrawer;
206 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700207 private HandleView mHandleView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private AllAppsGridView mAllAppsGrid;
209
210 private boolean mDesktopLocked = true;
211 private Bundle mSavedState;
212
213 private SpannableStringBuilder mDefaultKeySsb = null;
214
215 private boolean mDestroyed;
216
217 private boolean mRestoring;
218 private boolean mWaitingForResult;
219 private boolean mLocaleChanged;
220
221 private Bundle mSavedInstanceState;
222
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700223 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700224
Romain Guy73b979d2009-06-09 12:57:21 -0700225 private View mGesturesPanel;
226 private GestureOverlayView mGesturesOverlay;
227 private ViewSwitcher mGesturesPrompt;
228 private ImageView mGesturesAdd;
229 private PopupWindow mGesturesWindow;
230 private Launcher.GesturesProcessor mGesturesProcessor;
231 private Gesture mCurrentGesture;
232 private GesturesAction mGesturesAction;
233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 @Override
235 protected void onCreate(Bundle savedInstanceState) {
236 super.onCreate(savedInstanceState);
237 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Romain Guy73b979d2009-06-09 12:57:21 -0700239 if (sLibrary == null) {
240 // The context is not kept by the library so it's safe to do this
241 sLibrary = GestureLibraries.fromPrivateFile(Launcher.this,
242 GesturesConstants.STORE_NAME);
243 }
244
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700245 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700246
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700247 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
248 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 if (PROFILE_STARTUP) {
251 android.os.Debug.startMethodTracing("/sdcard/launcher");
252 }
253
254 checkForLocaleChange();
255 setWallpaperDimension();
256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 setContentView(R.layout.launcher);
258 setupViews();
259
260 registerIntentReceivers();
261 registerContentObservers();
262
263 mSavedState = savedInstanceState;
264 restoreState(mSavedState);
265
266 if (PROFILE_STARTUP) {
267 android.os.Debug.stopMethodTracing();
268 }
269
270 if (!mRestoring) {
271 startLoaders();
272 }
273
274 // For handling default keys
275 mDefaultKeySsb = new SpannableStringBuilder();
276 Selection.setSelection(mDefaultKeySsb, 0);
277 }
Romain Guycbb89e42009-06-08 15:52:54 -0700278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private void checkForLocaleChange() {
280 final SharedPreferences preferences = getSharedPreferences(PREFERENCES, MODE_PRIVATE);
281 final Configuration configuration = getResources().getConfiguration();
282
283 final String previousLocale = preferences.getString(KEY_LOCALE, null);
284 final String locale = configuration.locale.toString();
285
286 final int previousMcc = preferences.getInt(KEY_MCC, -1);
287 final int mcc = configuration.mcc;
288
289 final int previousMnc = preferences.getInt(KEY_MNC, -1);
290 final int mnc = configuration.mnc;
291
292 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
293
294 if (mLocaleChanged) {
295 final SharedPreferences.Editor editor = preferences.edit();
296 editor.putString(KEY_LOCALE, locale);
297 editor.putInt(KEY_MCC, mcc);
298 editor.putInt(KEY_MNC, mnc);
299 editor.commit();
300 }
301 }
302
303 static int getScreen() {
304 synchronized (sLock) {
305 return sScreen;
306 }
307 }
308
309 static void setScreen(int screen) {
310 synchronized (sLock) {
311 sScreen = screen;
312 }
313 }
314
315 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700316 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
317 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
318
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 mRestoring = false;
320 }
321
322 private void setWallpaperDimension() {
323 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
324 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
325
326 Display display = getWindowManager().getDefaultDisplay();
327 boolean isPortrait = display.getWidth() < display.getHeight();
328
329 final int width = isPortrait ? display.getWidth() : display.getHeight();
330 final int height = isPortrait ? display.getHeight() : display.getWidth();
331 try {
332 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
333 } catch (RemoteException e) {
334 // System is dead!
335 }
336 }
337
338 @Override
339 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
340 // The pattern used here is that a user PICKs a specific application,
341 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
344 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700345
Romain Guy73b979d2009-06-09 12:57:21 -0700346 if (resultCode == RESULT_OK && (mAddItemCellInfo != null ||
347 ((requestCode == REQUEST_PICK_GESTURE_ACTION ||
348 requestCode == REQUEST_CREATE_GESTURE_ACTION ||
349 requestCode == REQUEST_CREATE_GESTURE_APPLICATION_ACTION) && mCurrentGesture != null))) {
350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 switch (requestCode) {
352 case REQUEST_PICK_APPLICATION:
353 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
354 break;
355 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700356 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 break;
358 case REQUEST_CREATE_SHORTCUT:
359 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
360 break;
361 case REQUEST_PICK_LIVE_FOLDER:
362 addLiveFolder(data);
363 break;
364 case REQUEST_CREATE_LIVE_FOLDER:
365 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
366 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700367 case REQUEST_PICK_APPWIDGET:
368 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700370 case REQUEST_CREATE_APPWIDGET:
371 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 break;
Romain Guy73b979d2009-06-09 12:57:21 -0700373 case REQUEST_PICK_GESTURE_ACTION:
374 processShortcut(data, REQUEST_CREATE_GESTURE_APPLICATION_ACTION,
375 REQUEST_CREATE_GESTURE_ACTION);
376 break;
377 case REQUEST_CREATE_GESTURE_ACTION:
378 completeCreateGesture(data, true);
379 break;
380 case REQUEST_CREATE_GESTURE_APPLICATION_ACTION:
381 completeCreateGesture(data, false);
382 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700384 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700386 // Clean up the appWidgetId if we canceled
387 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
388 if (appWidgetId != -1) {
389 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
391 }
392 mWaitingForResult = false;
393 }
394
395 @Override
396 protected void onResume() {
397 super.onResume();
398
399 if (mRestoring) {
400 startLoaders();
401 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700402
403 // Make sure that the search gadget (if any) is in its normal place.
Romain Guy5a941392009-04-28 15:18:25 -0700404 stopSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 }
406
407 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700408 protected void onPause() {
409 super.onPause();
Romain Guy73b979d2009-06-09 12:57:21 -0700410 if (mGesturesWindow != null) {
411 mGesturesWindow.setAnimationStyle(0);
412 mGesturesWindow.update();
413 }
Romain Guycbb89e42009-06-08 15:52:54 -0700414 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700415 }
Romain Guycbb89e42009-06-08 15:52:54 -0700416
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700417 @Override
Romain Guy73b979d2009-06-09 12:57:21 -0700418 protected void onStop() {
419 super.onStop();
420 hideGesturesPanel();
421 }
422
423 @Override
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700424 public Object onRetainNonConfigurationInstance() {
425 // Flag any binder to stop early before switching
426 if (mBinder != null) {
427 mBinder.mTerminate = true;
428 }
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700430 if (PROFILE_ROTATE) {
431 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
432 }
433 return null;
434 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 private boolean acceptFilter() {
437 final InputMethodManager inputManager = (InputMethodManager)
438 getSystemService(Context.INPUT_METHOD_SERVICE);
439 return !inputManager.isFullscreenMode();
440 }
441
442 @Override
443 public boolean onKeyDown(int keyCode, KeyEvent event) {
444 boolean handled = super.onKeyDown(keyCode, event);
445 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
446 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
447 keyCode, event);
448 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700449 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700450 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700451 // showSearchDialog()
452 // If there are multiple keystrokes before the search dialog takes focus,
453 // onSearchRequested() will be called for every keystroke,
454 // but it is idempotent, so it's fine.
455 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 }
457 }
458
459 return handled;
460 }
461
Karl Rosaen138a0412009-04-23 19:00:21 -0700462 private String getTypedText() {
463 return mDefaultKeySsb.toString();
464 }
465
466 private void clearTypedText() {
467 mDefaultKeySsb.clear();
468 mDefaultKeySsb.clearSpans();
469 Selection.setSelection(mDefaultKeySsb, 0);
470 }
471
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 /**
473 * Restores the previous state, if it exists.
474 *
475 * @param savedState The previous state.
476 */
477 private void restoreState(Bundle savedState) {
478 if (savedState == null) {
479 return;
480 }
481
482 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
483 if (currentScreen > -1) {
484 mWorkspace.setCurrentScreen(currentScreen);
485 }
486
487 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
488 if (addScreen > -1) {
489 mAddItemCellInfo = new CellLayout.CellInfo();
490 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
491 addItemCellInfo.valid = true;
492 addItemCellInfo.screen = addScreen;
493 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
494 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
495 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
496 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
497 addItemCellInfo.findVacantCellsFromOccupied(
498 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
499 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
500 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
501 mRestoring = true;
502 }
503
504 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
505 if (renameFolder) {
506 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
507 mFolderInfo = sModel.getFolderById(this, id);
508 mRestoring = true;
509 }
Romain Guy73b979d2009-06-09 12:57:21 -0700510
511 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_PENDING_GESTURE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 }
513
514 /**
515 * Finds all the views we need and configure them properly.
516 */
517 private void setupViews() {
518 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
519 final DragLayer dragLayer = mDragLayer;
520
521 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
522 final Workspace workspace = mWorkspace;
523
524 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
525 final SlidingDrawer drawer = mDrawer;
526
527 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
528 final AllAppsGridView grid = mAllAppsGrid;
529
530 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
531
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700532 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
533 mHandleView.setLauncher(this);
534 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 mHandleIcon.setCrossFadeEnabled(true);
536
537 drawer.lock();
538 final DrawerManager drawerManager = new DrawerManager();
539 drawer.setOnDrawerOpenListener(drawerManager);
540 drawer.setOnDrawerCloseListener(drawerManager);
541 drawer.setOnDrawerScrollListener(drawerManager);
542
Karl Rosaen138a0412009-04-23 19:00:21 -0700543 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 grid.setDragger(dragLayer);
545 grid.setLauncher(this);
546
547 workspace.setOnLongClickListener(this);
548 workspace.setDragger(dragLayer);
549 workspace.setLauncher(this);
550 loadWallpaper();
551
552 deleteZone.setLauncher(this);
553 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700554 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555
556 dragLayer.setIgnoredDropTarget(grid);
557 dragLayer.setDragScoller(workspace);
558 dragLayer.setDragListener(deleteZone);
Romain Guy73b979d2009-06-09 12:57:21 -0700559
560 mGesturesPanel = mInflater.inflate(R.layout.gestures, mDragLayer, false);
561 final View gesturesPanel = mGesturesPanel;
562
563 mGesturesPrompt = (ViewSwitcher) gesturesPanel.findViewById(R.id.gestures_actions);
564 mGesturesAction = new GesturesAction();
565
566 mGesturesPrompt.getChildAt(0).setOnClickListener(mGesturesAction);
567 mGesturesPrompt.getChildAt(1).setOnClickListener(mGesturesAction);
568
569 mGesturesAdd = (ImageView) gesturesPanel.findViewById(R.id.gestures_add);
570 final ImageView gesturesAdd = mGesturesAdd;
571 gesturesAdd.setAlpha(128);
572 gesturesAdd.setEnabled(false);
573 gesturesAdd.setOnClickListener(new View.OnClickListener() {
574 public void onClick(View v) {
575 createGesture();
576 }
577 });
578
579 mGesturesOverlay = (GestureOverlayView) gesturesPanel.findViewById(R.id.gestures_overlay);
580 mGesturesProcessor = new GesturesProcessor();
581
582 final GestureOverlayView overlay = mGesturesOverlay;
583 overlay.setFadeOffset(GesturesConstants.MATCH_DELAY);
584 overlay.addOnGestureListener(mGesturesProcessor);
585 overlay.getGesturePaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
586 }
587
588 private void createGesture() {
589 mCurrentGesture = mGesturesOverlay.getGesture();
590 mWaitingForResult = true;
591 pickShortcut(REQUEST_PICK_GESTURE_ACTION, R.string.title_select_shortcut);
592 }
593
594 private void completeCreateGesture(Intent data, boolean isShortcut) {
595 ApplicationInfo info;
596
597 if (isShortcut) {
598 info = infoFromShortcutIntent(this, data);
599 } else {
600 info = infoFromApplicationIntent(this, data);
601 }
602
603 boolean success = false;
604 if (info != null) {
605 info.isGesture = true;
606
607 if (LauncherModel.addGestureToDatabase(this, info, false)) {
608 mGesturesProcessor.addGesture(String.valueOf(info.id), mCurrentGesture);
609 mGesturesProcessor.update(info, mCurrentGesture);
610 Toast.makeText(this, getString(R.string.gestures_created, info.title),
611 Toast.LENGTH_SHORT).show();
612 success = true;
613 }
614 }
615
616 if (!success) {
617 Toast.makeText(this, getString(R.string.gestures_failed), Toast.LENGTH_SHORT).show();
618 }
619
620 mCurrentGesture = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 }
622
623 /**
624 * Creates a view representing a shortcut.
625 *
626 * @param info The data structure describing the shortcut.
627 *
628 * @return A View inflated from R.layout.application.
629 */
630 View createShortcut(ApplicationInfo info) {
631 return createShortcut(R.layout.application,
632 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
633 }
634
635 /**
636 * Creates a view representing a shortcut inflated from the specified resource.
637 *
638 * @param layoutResId The id of the XML layout used to create the shortcut.
639 * @param parent The group the shortcut belongs to.
640 * @param info The data structure describing the shortcut.
641 *
642 * @return A View inflated from layoutResId.
643 */
644 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
645 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
646
647 if (!info.filtered) {
648 info.icon = Utilities.createIconThumbnail(info.icon, this);
649 info.filtered = true;
650 }
651
652 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
653 favorite.setText(info.title);
654 favorite.setTag(info);
655 favorite.setOnClickListener(this);
656
657 return favorite;
658 }
659
660 /**
661 * Add an application shortcut to the workspace.
662 *
663 * @param data The intent describing the application.
664 * @param cellInfo The position on screen where to create the shortcut.
665 */
666 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
667 boolean insertAtFirst) {
668 cellInfo.screen = mWorkspace.getCurrentScreen();
669 if (!findSingleSlot(cellInfo)) return;
670
Romain Guy73b979d2009-06-09 12:57:21 -0700671 final ApplicationInfo info = infoFromApplicationIntent(context, data);
672 if (info != null) {
673 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
674 }
675 }
676
677 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 ComponentName component = data.getComponent();
679 PackageManager packageManager = context.getPackageManager();
680 ActivityInfo activityInfo = null;
681 try {
682 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
683 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700684 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 }
Romain Guycbb89e42009-06-08 15:52:54 -0700686
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 if (activityInfo != null) {
688 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 itemInfo.title = activityInfo.loadLabel(packageManager);
691 if (itemInfo.title == null) {
692 itemInfo.title = activityInfo.name;
693 }
Romain Guycbb89e42009-06-08 15:52:54 -0700694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
696 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
697 itemInfo.icon = activityInfo.loadIcon(packageManager);
698 itemInfo.container = ItemInfo.NO_ID;
699
Romain Guy73b979d2009-06-09 12:57:21 -0700700 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 }
Romain Guy73b979d2009-06-09 12:57:21 -0700702
703 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 }
Romain Guycbb89e42009-06-08 15:52:54 -0700705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 /**
707 * Add a shortcut to the workspace.
708 *
709 * @param data The intent describing the shortcut.
710 * @param cellInfo The position on screen where to create the shortcut.
711 * @param insertAtFirst
712 */
713 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
714 boolean insertAtFirst) {
715 cellInfo.screen = mWorkspace.getCurrentScreen();
716 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700717
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
719
720 if (!mRestoring) {
721 sModel.addDesktopItem(info);
722
723 final View view = createShortcut(info);
724 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
725 } else if (sModel.isDesktopLoaded()) {
726 sModel.addDesktopItem(info);
727 }
728 }
729
Romain Guycbb89e42009-06-08 15:52:54 -0700730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700734 * @param data The intent describing the appWidgetId.
735 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700737 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 boolean insertAtFirst) {
739
740 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700741 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700742
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700743 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700744
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700745 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700746
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700747 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700749 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700750
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 // Try finding open space on Launcher screen
752 final int[] xy = mCellCoordinates;
753 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
754
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700755 // Build Launcher-specific widget info and save to database
756 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 launcherInfo.spanX = spans[0];
758 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700759
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 LauncherModel.addItemToDatabase(this, launcherInfo,
761 LauncherSettings.Favorites.CONTAINER_DESKTOP,
762 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
763
764 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700765 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700766
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700768 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700769
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700770 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700772
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
774 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
775 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700776 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 }
778 }
Romain Guycbb89e42009-06-08 15:52:54 -0700779
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700780 public LauncherAppWidgetHost getAppWidgetHost() {
781 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 }
Romain Guycbb89e42009-06-08 15:52:54 -0700783
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 static ApplicationInfo addShortcut(Context context, Intent data,
785 CellLayout.CellInfo cellInfo, boolean notify) {
786
Romain Guy73b979d2009-06-09 12:57:21 -0700787 final ApplicationInfo info = infoFromShortcutIntent(context, data);
788 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
789 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
790
791 return info;
792 }
793
794 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
796 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
797 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
798
799 Drawable icon = null;
800 boolean filtered = false;
801 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700802 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803
804 if (bitmap != null) {
805 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
806 filtered = true;
807 customIcon = true;
808 } else {
809 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700810 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700812 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 final PackageManager packageManager = context.getPackageManager();
814 Resources resources = packageManager.getResourcesForApplication(
815 iconResource.packageName);
816 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
817 icon = resources.getDrawable(id);
818 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700819 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 }
821 }
822 }
823
824 if (icon == null) {
825 icon = context.getPackageManager().getDefaultActivityIcon();
826 }
827
828 final ApplicationInfo info = new ApplicationInfo();
829 info.icon = icon;
830 info.filtered = filtered;
831 info.title = name;
832 info.intent = intent;
833 info.customIcon = customIcon;
834 info.iconResource = iconResource;
835
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 return info;
837 }
838
839 @Override
840 protected void onNewIntent(Intent intent) {
841 super.onNewIntent(intent);
842
843 // Close the menu
844 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
845 getWindow().closeAllPanels();
846
847 try {
848 dismissDialog(DIALOG_CREATE_SHORTCUT);
849 // Unlock the workspace if the dialog was showing
850 mWorkspace.unlock();
851 } catch (Exception e) {
852 // An exception is thrown if the dialog is not visible, which is fine
853 }
854
855 try {
856 dismissDialog(DIALOG_RENAME_FOLDER);
857 // Unlock the workspace if the dialog was showing
858 mWorkspace.unlock();
859 } catch (Exception e) {
860 // An exception is thrown if the dialog is not visible, which is fine
861 }
862
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
864 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700865
866 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE) {
867 onHomeKeyPressed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 }
869 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700870
871 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 if (v != null && v.getWindowToken() != null) {
873 InputMethodManager imm = (InputMethodManager)getSystemService(
874 INPUT_METHOD_SERVICE);
875 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
876 }
877 } else {
878 closeDrawer(false);
879 }
880 }
881 }
882
Romain Guy73b979d2009-06-09 12:57:21 -0700883 private void onHomeKeyPressed() {
884 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
885 showGesturesPanel();
886 } else {
887 hideGesturesPanel();
888 }
889 }
890
891 private void showGesturesPanel() {
892 resetGesturesPrompt();
893
894 mGesturesAdd.setEnabled(false);
895 mGesturesAdd.setAlpha(128);
896
897 mGesturesOverlay.clear(false);
898
899 PopupWindow window;
900 if (mGesturesWindow == null) {
901 mGesturesWindow = new PopupWindow(this);
902 window = mGesturesWindow;
903 window.setFocusable(true);
904 window.setTouchable(true);
905 window.setBackgroundDrawable(null);
906 window.setContentView(mGesturesPanel);
907 } else {
908 window = mGesturesWindow;
909 }
910 window.setAnimationStyle(com.android.internal.R.style.Animation_SlidingCard);
911
912 final int[] xy = new int[2];
913 final DragLayer dragLayer = mDragLayer;
914 dragLayer.getLocationOnScreen(xy);
915
916 window.setWidth(dragLayer.getWidth());
917 window.setHeight(dragLayer.getHeight() - 1);
918 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
919 }
920
921 private void resetGesturesPrompt() {
922 mGesturesAction.intent = null;
923 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
924 prompt.setText(R.string.gestures_instructions);
925 prompt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
926 prompt.setClickable(false);
927 }
928
929 private void resetGesturesNextPrompt() {
930 mGesturesAction.intent = null;
931 setGesturesNextPrompt(null, getString(R.string.gestures_instructions));
932 mGesturesPrompt.getNextView().setClickable(false);
933 }
934
935 private void setGesturesNextPrompt(Drawable icon, CharSequence title) {
936 final TextView prompt = (TextView) mGesturesPrompt.getNextView();
937 prompt.setText(title);
938 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
939 prompt.setClickable(true);
940 mGesturesPrompt.showNext();
941 }
942
943 void hideGesturesPanel() {
944 if (mGesturesWindow != null) {
945 mGesturesWindow.setAnimationStyle(com.android.internal.R.style.Animation_SlidingCard);
946 mGesturesWindow.update();
947 mGesturesWindow.dismiss();
948 }
949 }
950
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 @Override
952 protected void onRestoreInstanceState(Bundle savedInstanceState) {
953 // Do not call super here
954 mSavedInstanceState = savedInstanceState;
955 }
956
957 @Override
958 protected void onSaveInstanceState(Bundle outState) {
959 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
960
961 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
962 if (folders.size() > 0) {
963 final int count = folders.size();
964 long[] ids = new long[count];
965 for (int i = 0; i < count; i++) {
966 final FolderInfo info = folders.get(i).getInfo();
967 ids[i] = info.id;
968 }
969 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
970 } else {
971 super.onSaveInstanceState(outState);
972 }
973
Romain Guy5a941392009-04-28 15:18:25 -0700974 // When the drawer is opened and we are saving the state because of a
975 // configuration change
976 if (mDrawer.isOpened() && getChangingConfigurations() != 0) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979
980 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
981 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
982 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
983
984 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
985 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
986 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
987 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
988 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
989 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
990 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
991 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
992 layout.getOccupiedCells());
993 }
994
995 if (mFolderInfo != null && mWaitingForResult) {
996 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
997 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
998 }
Romain Guy73b979d2009-06-09 12:57:21 -0700999
1000 if (mCurrentGesture != null && mWaitingForResult) {
1001 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
1004
1005 @Override
1006 public void onDestroy() {
1007 mDestroyed = true;
1008
1009 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001012 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001014 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 }
1016
1017 TextKeyListener.getInstance().release();
1018
1019 mAllAppsGrid.clearTextFilter();
1020 mAllAppsGrid.setAdapter(null);
1021 sModel.unbind();
1022 sModel.abortLoaders();
1023
1024 getContentResolver().unregisterContentObserver(mObserver);
1025 unregisterReceiver(mApplicationsReceiver);
1026 }
1027
1028 @Override
1029 public void startActivityForResult(Intent intent, int requestCode) {
1030 mWaitingForResult = true;
1031 super.startActivityForResult(intent, requestCode);
1032 }
1033
1034 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001035 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001037
1038 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001039
Karl Rosaen138a0412009-04-23 19:00:21 -07001040 // Slide the search widget to the top, if it's on the current screen,
1041 // otherwise show the search dialog immediately.
1042 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1043 if (searchWidget == null) {
1044 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1045 } else {
1046 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1047 // show the currently typed text in the search widget while sliding
1048 searchWidget.setQuery(getTypedText());
1049 }
1050 }
Romain Guycbb89e42009-06-08 15:52:54 -07001051
Karl Rosaen138a0412009-04-23 19:00:21 -07001052 /**
1053 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001054 *
1055 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001056 */
Romain Guycbb89e42009-06-08 15:52:54 -07001057 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001058 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001059
Karl Rosaen138a0412009-04-23 19:00:21 -07001060 if (initialQuery == null) {
1061 // Use any text typed in the launcher as the initial query
1062 initialQuery = getTypedText();
1063 clearTypedText();
1064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 if (appSearchData == null) {
1066 appSearchData = new Bundle();
1067 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1068 }
Romain Guycbb89e42009-06-08 15:52:54 -07001069
Karl Rosaen138a0412009-04-23 19:00:21 -07001070 final SearchManager searchManager =
1071 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1072
1073 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1074 if (searchWidget != null) {
1075 // This gets called when the user leaves the search dialog to go back to
1076 // the Launcher.
1077 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1078 public void onCancel() {
1079 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001080 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001081 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001082 });
1083 }
Romain Guycbb89e42009-06-08 15:52:54 -07001084
Karl Rosaen138a0412009-04-23 19:00:21 -07001085 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001086 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 }
1088
Karl Rosaen138a0412009-04-23 19:00:21 -07001089 /**
1090 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001091 */
Romain Guy5a941392009-04-28 15:18:25 -07001092 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001093 // Close search dialog
1094 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1095 if (searchManager.isVisible()) {
1096 searchManager.stopSearch();
1097 }
1098 // Restore search widget to its normal position
1099 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1100 if (searchWidget != null) {
1101 searchWidget.stopSearch(false);
1102 }
1103 }
Romain Guycbb89e42009-06-08 15:52:54 -07001104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 @Override
1106 public boolean onCreateOptionsMenu(Menu menu) {
1107 if (mDesktopLocked) return false;
1108
1109 super.onCreateOptionsMenu(menu);
1110 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1111 .setIcon(android.R.drawable.ic_menu_add)
1112 .setAlphabeticShortcut('A');
1113 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1114 .setIcon(android.R.drawable.ic_menu_gallery)
1115 .setAlphabeticShortcut('W');
1116 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1117 .setIcon(android.R.drawable.ic_search_category_default)
1118 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1119 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1120 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1121 .setAlphabeticShortcut('N');
1122
Romain Guy73b979d2009-06-09 12:57:21 -07001123 final Intent gestures = new Intent(this, GesturesActivity.class);
1124 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1125 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1126 .setIntent(gestures);
1127
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001129 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1130 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131
1132 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1133 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1134 .setIntent(settings);
1135
1136 return true;
1137 }
1138
1139 @Override
1140 public boolean onPrepareOptionsMenu(Menu menu) {
1141 super.onPrepareOptionsMenu(menu);
1142
1143 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1144 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1145
1146 return true;
1147 }
1148
1149 @Override
1150 public boolean onOptionsItemSelected(MenuItem item) {
1151 switch (item.getItemId()) {
1152 case MENU_ADD:
1153 addItems();
1154 return true;
1155 case MENU_WALLPAPER_SETTINGS:
1156 startWallpaper();
1157 return true;
1158 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001159 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 return true;
1161 case MENU_NOTIFICATIONS:
1162 showNotifications();
1163 return true;
1164 }
1165
1166 return super.onOptionsItemSelected(item);
1167 }
Romain Guycbb89e42009-06-08 15:52:54 -07001168
Karl Rosaen138a0412009-04-23 19:00:21 -07001169 /**
1170 * Indicates that we want global search for this activity by setting the globalSearch
1171 * argument for {@link #startSearch} to true.
1172 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001174 @Override
1175 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001176 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001177 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001178 }
1179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 private void addItems() {
1181 showAddDialog(mMenuAddInfo);
1182 }
1183
1184 private void removeShortcutsForPackage(String packageName) {
1185 if (packageName != null && packageName.length() > 0) {
1186 mWorkspace.removeShortcutsForPackage(packageName);
1187 }
1188 }
Romain Guycbb89e42009-06-08 15:52:54 -07001189
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001190 private void updateShortcutsForPackage(String packageName) {
1191 if (packageName != null && packageName.length() > 0) {
1192 mWorkspace.updateShortcutsForPackage(packageName);
1193 }
1194 }
1195
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001196 void addAppWidget(Intent data) {
1197 // TODO: catch bad widget exception when sent
1198 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001200 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1201 if (SEARCH_WIDGET.equals(customWidget)) {
1202 // We don't need this any more, since this isn't a real app widget.
1203 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1204 // add the search widget
1205 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001207 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1208
1209 if (appWidget.configure != null) {
1210 // Launch over to configure widget, if needed
1211 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1212 intent.setComponent(appWidget.configure);
1213 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1214
1215 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1216 } else {
1217 // Otherwise just add it
1218 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1219 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
1221 }
Romain Guycbb89e42009-06-08 15:52:54 -07001222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 void addSearch() {
1224 final Widget info = Widget.makeSearch();
1225 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 final int[] xy = mCellCoordinates;
1228 final int spanX = info.spanX;
1229 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 sModel.addDesktopItem(info);
1234 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1235 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 final View view = mInflater.inflate(info.layoutResource, null);
1238 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001239 Search search = (Search) view.findViewById(R.id.widget_search);
1240 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1243 }
1244
Romain Guy73b979d2009-06-09 12:57:21 -07001245 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001246 // Handle case where user selected "Applications"
1247 String applicationName = getResources().getString(R.string.group_applications);
1248 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001249
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001250 if (applicationName != null && applicationName.equals(shortcutName)) {
1251 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1252 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001253
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001254 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1255 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001256 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001257 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001258 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001259 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
1261
1262 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001263 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001264 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001265 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001266
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001267 if (folderName != null && folderName.equals(shortcutName)) {
1268 addFolder(!mDesktopLocked);
1269 } else {
1270 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1271 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 }
1273
1274 void addFolder(boolean insertAtFirst) {
1275 UserFolderInfo folderInfo = new UserFolderInfo();
1276 folderInfo.title = getText(R.string.folder_name);
1277
1278 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1279 cellInfo.screen = mWorkspace.getCurrentScreen();
1280 if (!findSingleSlot(cellInfo)) return;
1281
1282 // Update the model
1283 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1284 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1285 sModel.addDesktopItem(folderInfo);
1286 sModel.addFolder(folderInfo);
1287
1288 // Create the view
1289 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1290 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1291 mWorkspace.addInCurrentScreen(newFolder,
1292 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1293 }
Romain Guycbb89e42009-06-08 15:52:54 -07001294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1296 boolean insertAtFirst) {
1297 cellInfo.screen = mWorkspace.getCurrentScreen();
1298 if (!findSingleSlot(cellInfo)) return;
1299
1300 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1301
1302 if (!mRestoring) {
1303 sModel.addDesktopItem(info);
1304
1305 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1306 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1307 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1308 } else if (sModel.isDesktopLoaded()) {
1309 sModel.addDesktopItem(info);
1310 }
1311 }
1312
1313 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1314 CellLayout.CellInfo cellInfo, boolean notify) {
1315
1316 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1317 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1318
1319 Drawable icon = null;
1320 boolean filtered = false;
1321 Intent.ShortcutIconResource iconResource = null;
1322
1323 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1324 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1325 try {
1326 iconResource = (Intent.ShortcutIconResource) extra;
1327 final PackageManager packageManager = context.getPackageManager();
1328 Resources resources = packageManager.getResourcesForApplication(
1329 iconResource.packageName);
1330 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1331 icon = resources.getDrawable(id);
1332 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001333 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335 }
1336
1337 if (icon == null) {
1338 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1339 }
1340
1341 final LiveFolderInfo info = new LiveFolderInfo();
1342 info.icon = icon;
1343 info.filtered = filtered;
1344 info.title = name;
1345 info.iconResource = iconResource;
1346 info.uri = data.getData();
1347 info.baseIntent = baseIntent;
1348 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1349 LiveFolders.DISPLAY_MODE_GRID);
1350
1351 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1352 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1353 sModel.addFolder(info);
1354
1355 return info;
1356 }
1357
1358 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1359 final int[] xy = new int[2];
1360 if (findSlot(cellInfo, xy, 1, 1)) {
1361 cellInfo.cellX = xy[0];
1362 cellInfo.cellY = xy[1];
1363 return true;
1364 }
1365 return false;
1366 }
1367
1368 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1369 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1370 boolean[] occupied = mSavedState != null ?
1371 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1372 cellInfo = mWorkspace.findAllVacantCells(occupied);
1373 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1374 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1375 return false;
1376 }
1377 }
1378 return true;
1379 }
1380
1381 private void showNotifications() {
1382 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1383 if (statusBar != null) {
1384 statusBar.expand();
1385 }
1386 }
1387
1388 private void startWallpaper() {
1389 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1390 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1391 }
1392
1393 /**
1394 * Registers various intent receivers. The current implementation registers
1395 * only a wallpaper intent receiver to let other applications change the
1396 * wallpaper.
1397 */
1398 private void registerIntentReceivers() {
1399 if (sWallpaperReceiver == null) {
1400 final Application application = getApplication();
1401
1402 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1403
1404 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1405 application.registerReceiver(sWallpaperReceiver, filter);
1406 } else {
1407 sWallpaperReceiver.setLauncher(this);
1408 }
1409
1410 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1411 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1412 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1413 filter.addDataScheme("package");
1414 registerReceiver(mApplicationsReceiver, filter);
1415 }
1416
1417 /**
1418 * Registers various content observers. The current implementation registers
1419 * only a favorites observer to keep track of the favorites applications.
1420 */
1421 private void registerContentObservers() {
1422 ContentResolver resolver = getContentResolver();
1423 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1424 }
1425
1426 @Override
1427 public boolean dispatchKeyEvent(KeyEvent event) {
1428 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1429 switch (event.getKeyCode()) {
1430 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001431 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001432 if (mDrawer.isOpened()) {
1433 closeDrawer();
1434 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001435 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001436 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 return true;
1438 case KeyEvent.KEYCODE_HOME:
1439 return true;
1440 }
1441 }
1442
1443 return super.dispatchKeyEvent(event);
1444 }
1445
1446 private void closeDrawer() {
1447 closeDrawer(true);
1448 }
1449
1450 private void closeDrawer(boolean animated) {
1451 if (mDrawer.isOpened()) {
1452 if (animated) {
1453 mDrawer.animateClose();
1454 } else {
1455 mDrawer.close();
1456 }
1457 if (mDrawer.hasFocus()) {
1458 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1459 }
1460 }
1461 }
1462
1463 private void closeFolder() {
1464 Folder folder = mWorkspace.getOpenFolder();
1465 if (folder != null) {
1466 closeFolder(folder);
1467 }
1468 }
1469
1470 void closeFolder(Folder folder) {
1471 folder.getInfo().opened = false;
1472 ViewGroup parent = (ViewGroup) folder.getParent();
1473 if (parent != null) {
1474 parent.removeView(folder);
1475 }
1476 folder.onClose();
1477 }
1478
1479 /**
1480 * When the notification that favorites have changed is received, requests
1481 * a favorites list refresh.
1482 */
1483 private void onFavoritesChanged() {
1484 mDesktopLocked = true;
1485 mDrawer.lock();
1486 sModel.loadUserItems(false, this, false, false);
1487 }
1488
1489 void onDesktopItemsLoaded() {
1490 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 bindDesktopItems();
1492 }
Romain Guycbb89e42009-06-08 15:52:54 -07001493
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 /**
1495 * Refreshes the shortcuts shown on the workspace.
1496 */
1497 private void bindDesktopItems() {
1498 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001499 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001500 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1501 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 return;
1503 }
1504
1505 final Workspace workspace = mWorkspace;
1506 int count = workspace.getChildCount();
1507 for (int i = 0; i < count; i++) {
1508 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1509 }
Romain Guycbb89e42009-06-08 15:52:54 -07001510
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 if (DEBUG_USER_INTERFACE) {
1512 android.widget.Button finishButton = new android.widget.Button(this);
1513 finishButton.setText("Finish");
1514 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1515
1516 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1517 public void onClick(View v) {
1518 finish();
1519 }
1520 });
1521 }
Romain Guycbb89e42009-06-08 15:52:54 -07001522
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001523 // Flag any old binder to terminate early
1524 if (mBinder != null) {
1525 mBinder.mTerminate = true;
1526 }
Romain Guycbb89e42009-06-08 15:52:54 -07001527
Karl Rosaen138a0412009-04-23 19:00:21 -07001528 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001529 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
1531
1532 private void bindItems(Launcher.DesktopBinder binder,
1533 ArrayList<ItemInfo> shortcuts, int start, int count) {
1534
1535 final Workspace workspace = mWorkspace;
1536 final boolean desktopLocked = mDesktopLocked;
1537
1538 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1539 int i = start;
1540
1541 for ( ; i < end; i++) {
1542 final ItemInfo item = shortcuts.get(i);
1543 switch (item.itemType) {
1544 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1545 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1546 final View shortcut = createShortcut((ApplicationInfo) item);
1547 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1548 !desktopLocked);
1549 break;
1550 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1551 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1552 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1553 (UserFolderInfo) item);
1554 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1555 !desktopLocked);
1556 break;
1557 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1558 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1559 R.layout.live_folder_icon, this,
1560 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1561 (LiveFolderInfo) item);
1562 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1563 !desktopLocked);
1564 break;
1565 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1566 final int screen = workspace.getCurrentScreen();
1567 final View view = mInflater.inflate(R.layout.widget_search,
1568 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001569
Karl Rosaen138a0412009-04-23 19:00:21 -07001570 Search search = (Search) view.findViewById(R.id.widget_search);
1571 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001572
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 final Widget widget = (Widget) item;
1574 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 workspace.addWidget(view, widget, !desktopLocked);
1577 break;
1578 }
1579 }
1580
1581 workspace.requestLayout();
1582
1583 if (end >= count) {
1584 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001585 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 } else {
1587 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1588 }
1589 }
1590
1591 private void finishBindDesktopItems() {
1592 if (mSavedState != null) {
1593 if (!mWorkspace.hasFocus()) {
1594 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1595 }
1596
1597 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1598 if (userFolders != null) {
1599 for (long folderId : userFolders) {
1600 final FolderInfo info = sModel.findFolderById(folderId);
1601 if (info != null) {
1602 openFolder(info);
1603 }
1604 }
1605 final Folder openFolder = mWorkspace.getOpenFolder();
1606 if (openFolder != null) {
1607 openFolder.requestFocus();
1608 }
1609 }
1610
1611 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1612 if (allApps) {
1613 mDrawer.open();
1614 }
1615
1616 mSavedState = null;
1617 }
1618
1619 if (mSavedInstanceState != null) {
1620 super.onRestoreInstanceState(mSavedInstanceState);
1621 mSavedInstanceState = null;
1622 }
1623
1624 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1625 mDrawer.requestFocus();
1626 }
1627
1628 mDesktopLocked = false;
1629 mDrawer.unlock();
1630 }
Romain Guycbb89e42009-06-08 15:52:54 -07001631
Karl Rosaen138a0412009-04-23 19:00:21 -07001632 private void bindDrawer(Launcher.DesktopBinder binder,
1633 ApplicationsAdapter drawerAdapter) {
1634 mAllAppsGrid.setAdapter(drawerAdapter);
1635 binder.startBindingAppWidgetsWhenIdle();
1636 }
Romain Guycbb89e42009-06-08 15:52:54 -07001637
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001638 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001639 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001640
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 final Workspace workspace = mWorkspace;
1642 final boolean desktopLocked = mDesktopLocked;
1643
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001644 if (!appWidgets.isEmpty()) {
1645 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001646
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001647 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001648 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001649 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001650
Karl Rosaen138a0412009-04-23 19:00:21 -07001651 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001652
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001653 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1654 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001655
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001656 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1657 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001658
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001659 workspace.requestLayout();
1660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001662 if (appWidgets.isEmpty()) {
1663 if (PROFILE_ROTATE) {
1664 android.os.Debug.stopMethodTracing();
1665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001667 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 }
1669 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001670
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 DragController getDragController() {
1672 return mDragLayer;
1673 }
1674
1675 /**
1676 * Launches the intent referred by the clicked shortcut.
1677 *
1678 * @param v The view representing the clicked shortcut.
1679 */
1680 public void onClick(View v) {
1681 Object tag = v.getTag();
1682 if (tag instanceof ApplicationInfo) {
1683 // Open shortcut
1684 final Intent intent = ((ApplicationInfo) tag).intent;
1685 startActivitySafely(intent);
1686 } else if (tag instanceof FolderInfo) {
1687 handleFolderClick((FolderInfo) tag);
1688 }
1689 }
1690
1691 void startActivitySafely(Intent intent) {
1692 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1693 try {
1694 startActivity(intent);
1695 } catch (ActivityNotFoundException e) {
1696 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1697 } catch (SecurityException e) {
1698 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001699 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1701 "or use the exported attribute for this activity.", e);
1702 }
1703 }
1704
1705 private void handleFolderClick(FolderInfo folderInfo) {
1706 if (!folderInfo.opened) {
1707 // Close any open folder
1708 closeFolder();
1709 // Open the requested folder
1710 openFolder(folderInfo);
1711 } else {
1712 // Find the open folder...
1713 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1714 int folderScreen;
1715 if (openFolder != null) {
1716 folderScreen = mWorkspace.getScreenForView(openFolder);
1717 // .. and close it
1718 closeFolder(openFolder);
1719 if (folderScreen != mWorkspace.getCurrentScreen()) {
1720 // Close any folder open on the current screen
1721 closeFolder();
1722 // Pull the folder onto this screen
1723 openFolder(folderInfo);
1724 }
1725 }
1726 }
1727 }
1728
1729 private void loadWallpaper() {
1730 // The first time the application is started, we load the wallpaper from
1731 // the ApplicationContext
1732 if (sWallpaper == null) {
1733 final Drawable drawable = getWallpaper();
1734 if (drawable instanceof BitmapDrawable) {
1735 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1736 } else {
1737 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1738 }
1739 }
1740 mWorkspace.loadWallpaper(sWallpaper);
1741 }
1742
1743 /**
1744 * Opens the user fodler described by the specified tag. The opening of the folder
1745 * is animated relative to the specified View. If the View is null, no animation
1746 * is played.
1747 *
1748 * @param folderInfo The FolderInfo describing the folder to open.
1749 */
1750 private void openFolder(FolderInfo folderInfo) {
1751 Folder openFolder;
1752
1753 if (folderInfo instanceof UserFolderInfo) {
1754 openFolder = UserFolder.fromXml(this);
1755 } else if (folderInfo instanceof LiveFolderInfo) {
1756 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1757 } else {
1758 return;
1759 }
1760
1761 openFolder.setDragger(mDragLayer);
1762 openFolder.setLauncher(this);
1763
1764 openFolder.bind(folderInfo);
1765 folderInfo.opened = true;
1766
1767 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1768 openFolder.onOpen();
1769 }
1770
1771 /**
1772 * Returns true if the workspace is being loaded. When the workspace is loading,
1773 * no user interaction should be allowed to avoid any conflict.
1774 *
1775 * @return True if the workspace is locked, false otherwise.
1776 */
1777 boolean isWorkspaceLocked() {
1778 return mDesktopLocked;
1779 }
1780
1781 public boolean onLongClick(View v) {
1782 if (mDesktopLocked) {
1783 return false;
1784 }
1785
1786 if (!(v instanceof CellLayout)) {
1787 v = (View) v.getParent();
1788 }
1789
1790 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1791
1792 // This happens when long clicking an item with the dpad/trackball
1793 if (cellInfo == null) {
1794 return true;
1795 }
1796
1797 if (mWorkspace.allowLongPress()) {
1798 if (cellInfo.cell == null) {
1799 if (cellInfo.valid) {
1800 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001801 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 showAddDialog(cellInfo);
1803 }
1804 } else {
1805 if (!(cellInfo.cell instanceof Folder)) {
1806 // User long pressed on an item
1807 mWorkspace.startDrag(cellInfo);
1808 }
1809 }
1810 }
1811 return true;
1812 }
1813
1814 static LauncherModel getModel() {
1815 return sModel;
1816 }
1817
Romain Guy73b979d2009-06-09 12:57:21 -07001818 static GestureLibrary getGestureLibrary() {
1819 return sLibrary;
1820 }
1821
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 void closeAllApplications() {
1823 mDrawer.close();
1824 }
1825
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001826 View getDrawerHandle() {
1827 return mHandleView;
1828 }
1829
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 boolean isDrawerDown() {
1831 return !mDrawer.isMoving() && !mDrawer.isOpened();
1832 }
1833
1834 boolean isDrawerUp() {
1835 return mDrawer.isOpened() && !mDrawer.isMoving();
1836 }
1837
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001838 boolean isDrawerMoving() {
1839 return mDrawer.isMoving();
1840 }
1841
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 Workspace getWorkspace() {
1843 return mWorkspace;
1844 }
1845
1846 GridView getApplicationsGrid() {
1847 return mAllAppsGrid;
1848 }
1849
1850 @Override
1851 protected Dialog onCreateDialog(int id) {
1852 switch (id) {
1853 case DIALOG_CREATE_SHORTCUT:
1854 return new CreateShortcut().createDialog();
1855 case DIALOG_RENAME_FOLDER:
1856 return new RenameFolder().createDialog();
1857 }
1858
1859 return super.onCreateDialog(id);
1860 }
1861
1862 @Override
1863 protected void onPrepareDialog(int id, Dialog dialog) {
1864 switch (id) {
1865 case DIALOG_CREATE_SHORTCUT:
1866 mWorkspace.lock();
1867 break;
1868 case DIALOG_RENAME_FOLDER:
1869 mWorkspace.lock();
1870 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1871 final CharSequence text = mFolderInfo.title;
1872 input.setText(text);
Romain Guycbb89e42009-06-08 15:52:54 -07001873 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 break;
1875 }
1876 }
1877
1878 void showRenameDialog(FolderInfo info) {
1879 mFolderInfo = info;
1880 mWaitingForResult = true;
1881 showDialog(DIALOG_RENAME_FOLDER);
1882 }
1883
1884 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1885 mAddItemCellInfo = cellInfo;
1886 mWaitingForResult = true;
1887 showDialog(DIALOG_CREATE_SHORTCUT);
1888 }
1889
Romain Guy73b979d2009-06-09 12:57:21 -07001890 private void pickShortcut(int requestCode, int title) {
1891 Bundle bundle = new Bundle();
1892
1893 ArrayList<String> shortcutNames = new ArrayList<String>();
1894 shortcutNames.add(getString(R.string.group_applications));
1895 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1896
1897 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1898 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1899 R.drawable.ic_launcher_application));
1900 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1901
1902 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1903 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1904 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1905 pickIntent.putExtras(bundle);
1906
1907 startActivityForResult(pickIntent, requestCode);
1908 }
1909
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 private class RenameFolder {
1911 private EditText mInput;
1912
1913 Dialog createDialog() {
1914 mWaitingForResult = true;
1915 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1916 mInput = (EditText) layout.findViewById(R.id.folder_name);
1917
1918 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1919 builder.setIcon(0);
1920 builder.setTitle(getString(R.string.rename_folder_title));
1921 builder.setCancelable(true);
1922 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1923 public void onCancel(DialogInterface dialog) {
1924 cleanup();
1925 }
1926 });
1927 builder.setNegativeButton(getString(R.string.cancel_action),
1928 new Dialog.OnClickListener() {
1929 public void onClick(DialogInterface dialog, int which) {
1930 cleanup();
1931 }
1932 }
1933 );
1934 builder.setPositiveButton(getString(R.string.rename_action),
1935 new Dialog.OnClickListener() {
1936 public void onClick(DialogInterface dialog, int which) {
1937 changeFolderName();
1938 }
1939 }
1940 );
1941 builder.setView(layout);
1942 return builder.create();
1943 }
1944
1945 private void changeFolderName() {
1946 final String name = mInput.getText().toString();
1947 if (!TextUtils.isEmpty(name)) {
1948 // Make sure we have the right folder info
1949 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1950 mFolderInfo.title = name;
1951 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1952
1953 if (mDesktopLocked) {
1954 mDrawer.lock();
1955 sModel.loadUserItems(false, Launcher.this, false, false);
1956 } else {
1957 final FolderIcon folderIcon = (FolderIcon)
1958 mWorkspace.getViewForTag(mFolderInfo);
1959 if (folderIcon != null) {
1960 folderIcon.setText(name);
1961 getWorkspace().requestLayout();
1962 } else {
1963 mDesktopLocked = true;
1964 mDrawer.lock();
1965 sModel.loadUserItems(false, Launcher.this, false, false);
1966 }
1967 }
1968 }
1969 cleanup();
1970 }
1971
1972 private void cleanup() {
1973 mWorkspace.unlock();
1974 dismissDialog(DIALOG_RENAME_FOLDER);
1975 mWaitingForResult = false;
1976 mFolderInfo = null;
1977 }
1978 }
1979
1980 /**
1981 * Displays the shortcut creation dialog and launches, if necessary, the
1982 * appropriate activity.
1983 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001984 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07001985 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 Dialog createDialog() {
1989 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001990
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1994 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001995 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001996
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 builder.setInverseBackgroundForced(true);
1998
1999 AlertDialog dialog = builder.create();
2000 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002001 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 return dialog;
2004 }
2005
2006 public void onCancel(DialogInterface dialog) {
2007 mWaitingForResult = false;
2008 cleanup();
2009 }
2010
Romain Guycbb89e42009-06-08 15:52:54 -07002011 public void onDismiss(DialogInterface dialog) {
2012 mWorkspace.unlock();
2013 }
2014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 private void cleanup() {
2016 mWorkspace.unlock();
2017 dismissDialog(DIALOG_CREATE_SHORTCUT);
2018 }
2019
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002020 /**
2021 * Handle the action clicked in the "Add to home" dialog.
2022 */
2023 public void onClick(DialogInterface dialog, int which) {
2024 Resources res = getResources();
2025 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002026
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002027 switch (which) {
2028 case AddAdapter.ITEM_SHORTCUT: {
2029 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002030 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002031 break;
2032 }
Romain Guycbb89e42009-06-08 15:52:54 -07002033
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002034 case AddAdapter.ITEM_APPWIDGET: {
2035 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002036
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002037 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2038 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2039 // add the search widget
2040 ArrayList<AppWidgetProviderInfo> customInfo =
2041 new ArrayList<AppWidgetProviderInfo>();
2042 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2043 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2044 info.label = getString(R.string.group_search);
2045 info.icon = R.drawable.ic_search_widget;
2046 customInfo.add(info);
2047 pickIntent.putParcelableArrayListExtra(
2048 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2049 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2050 Bundle b = new Bundle();
2051 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2052 customExtras.add(b);
2053 pickIntent.putParcelableArrayListExtra(
2054 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2055 // start the pick activity
2056 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2057 break;
2058 }
Romain Guycbb89e42009-06-08 15:52:54 -07002059
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002060 case AddAdapter.ITEM_LIVE_FOLDER: {
2061 // Insert extra item to handle inserting folder
2062 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002063
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002064 ArrayList<String> shortcutNames = new ArrayList<String>();
2065 shortcutNames.add(res.getString(R.string.group_folder));
2066 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002067
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002068 ArrayList<ShortcutIconResource> shortcutIcons =
2069 new ArrayList<ShortcutIconResource>();
2070 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2071 R.drawable.ic_launcher_folder));
2072 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2073
2074 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2075 pickIntent.putExtra(Intent.EXTRA_INTENT,
2076 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2077 pickIntent.putExtra(Intent.EXTRA_TITLE,
2078 getText(R.string.title_select_live_folder));
2079 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002080
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002081 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2082 break;
2083 }
2084
2085 case AddAdapter.ITEM_WALLPAPER: {
2086 startWallpaper();
2087 break;
2088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 }
2090 }
2091 }
2092
2093 /**
2094 * Receives notifications when applications are added/removed.
2095 */
2096 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2097 @Override
2098 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002099 final String action = intent.getAction();
2100 final String packageName = intent.getData().getSchemeSpecificPart();
2101 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2102
2103 if (LauncherModel.DEBUG_LOADERS) {
2104 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2105 ", replacing=" + replacing);
2106 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002108
2109 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2110 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2111 if (!replacing) {
2112 removeShortcutsForPackage(packageName);
2113 if (LauncherModel.DEBUG_LOADERS) {
2114 d(LauncherModel.LOG_TAG, " --> remove package");
2115 }
2116 sModel.removePackage(Launcher.this, packageName);
2117 }
2118 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2119 // later, we will update the package at this time
2120 } else {
2121 if (!replacing) {
2122 if (LauncherModel.DEBUG_LOADERS) {
2123 d(LauncherModel.LOG_TAG, " --> add package");
2124 }
2125 sModel.addPackage(Launcher.this, packageName);
2126 } else {
2127 if (LauncherModel.DEBUG_LOADERS) {
2128 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2129 }
2130 sModel.updatePackage(Launcher.this, packageName);
2131 updateShortcutsForPackage(packageName);
2132 }
2133 }
2134 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002136 if (LauncherModel.DEBUG_LOADERS) {
2137 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2138 }
2139 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 }
2141 }
2142 }
2143
2144 /**
2145 * Receives notifications whenever the user favorites have changed.
2146 */
2147 private class FavoritesChangeObserver extends ContentObserver {
2148 public FavoritesChangeObserver() {
2149 super(new Handler());
2150 }
2151
2152 @Override
2153 public void onChange(boolean selfChange) {
2154 onFavoritesChanged();
2155 }
2156 }
2157
2158 /**
2159 * Receives intents from other applications to change the wallpaper.
2160 */
2161 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2162 private final Application mApplication;
2163 private WeakReference<Launcher> mLauncher;
2164
2165 WallpaperIntentReceiver(Application application, Launcher launcher) {
2166 mApplication = application;
2167 setLauncher(launcher);
2168 }
2169
2170 void setLauncher(Launcher launcher) {
2171 mLauncher = new WeakReference<Launcher>(launcher);
2172 }
2173
2174 @Override
2175 public void onReceive(Context context, Intent intent) {
2176 // Load the wallpaper from the ApplicationContext and store it locally
2177 // until the Launcher Activity is ready to use it
2178 final Drawable drawable = mApplication.getWallpaper();
2179 if (drawable instanceof BitmapDrawable) {
2180 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2181 } else {
2182 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2183 }
2184
2185 // If Launcher is alive, notify we have a new wallpaper
2186 if (mLauncher != null) {
2187 final Launcher launcher = mLauncher.get();
2188 if (launcher != null) {
2189 launcher.loadWallpaper();
2190 }
2191 }
2192 }
2193 }
2194
2195 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2196 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2197 private boolean mOpen;
2198
2199 public void onDrawerOpened() {
2200 if (!mOpen) {
2201 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002204 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205
2206 mOpen = true;
2207 }
2208 }
2209
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002210 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2211 view.getDrawingRect(bounds);
2212
2213 while (view != mDragLayer) {
2214 bounds.offset(view.getLeft(), view.getTop());
2215 view = (View) view.getParent();
2216 }
2217 }
2218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 public void onDrawerClosed() {
2220 if (mOpen) {
2221 mHandleIcon.reverseTransition(150);
2222 mWorkspace.mDrawerBounds.setEmpty();
2223 mOpen = false;
2224 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002225
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 mAllAppsGrid.setSelection(0);
2227 mAllAppsGrid.clearTextFilter();
2228 }
2229
2230 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002231 if (PROFILE_DRAWER) {
2232 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2233 }
2234
2235 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2236 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 }
2238
2239 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002240 if (PROFILE_DRAWER) {
2241 android.os.Debug.stopMethodTracing();
2242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 }
2244 }
2245
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002246 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002248 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002249 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 // Number of items to bind in every pass
2252 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253
2254 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002255 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002256 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002258
Karl Rosaen138a0412009-04-23 19:00:21 -07002259 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260
2261 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002262 ArrayList<LauncherAppWidgetInfo> appWidgets,
2263 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264
2265 mLauncher = new WeakReference<Launcher>(launcher);
2266 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002267 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002268
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002269 // Sort widgets so active workspace is bound first
2270 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2271 final int size = appWidgets.size();
2272 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002273
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002274 for (int i = 0; i < size; i++) {
2275 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2276 if (appWidgetInfo.screen == currentScreen) {
2277 mAppWidgets.addFirst(appWidgetInfo);
2278 } else {
2279 mAppWidgets.addLast(appWidgetInfo);
2280 }
2281 }
2282 }
Romain Guycbb89e42009-06-08 15:52:54 -07002283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 public void startBindingItems() {
2285 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2286 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002287
2288 public void startBindingDrawer() {
2289 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2290 }
Romain Guycbb89e42009-06-08 15:52:54 -07002291
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002292 public void startBindingAppWidgetsWhenIdle() {
2293 // Ask for notification when message queue becomes idle
2294 final MessageQueue messageQueue = Looper.myQueue();
2295 messageQueue.addIdleHandler(this);
2296 }
Romain Guycbb89e42009-06-08 15:52:54 -07002297
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002298 public boolean queueIdle() {
2299 // Queue is idle, so start binding items
2300 startBindingAppWidgets();
2301 return false;
2302 }
2303
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002304 public void startBindingAppWidgets() {
2305 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2306 }
2307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 @Override
2309 public void handleMessage(Message msg) {
2310 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002311 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 return;
2313 }
Romain Guycbb89e42009-06-08 15:52:54 -07002314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 switch (msg.what) {
2316 case MESSAGE_BIND_ITEMS: {
2317 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2318 break;
2319 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002320 case MESSAGE_BIND_DRAWER: {
2321 launcher.bindDrawer(this, mDrawerAdapter);
2322 break;
2323 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002324 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002325 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 break;
2327 }
2328 }
2329 }
2330 }
Romain Guy73b979d2009-06-09 12:57:21 -07002331
2332 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2333 GestureOverlayView.OnGesturePerformedListener {
2334
2335 private final GestureMatcher mMatcher = new GestureMatcher();
2336
2337 GesturesProcessor() {
2338 // TODO: Maybe the load should happen on a background thread?
2339 sLibrary.load();
2340 }
2341
2342 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
2343 overlay.removeCallbacks(mMatcher);
2344 resetGesturesNextPrompt();
2345
2346 mGesturesAdd.setAlpha(128);
2347 mGesturesAdd.setEnabled(false);
2348 }
2349
2350 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2351 }
2352
2353 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2354 }
2355
2356 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
2357 overlay.removeCallbacks(mMatcher);
2358
2359 mMatcher.gesture = overlay.getGesture();
2360 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2361 overlay.clear(false);
2362 } else {
2363 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2364 }
2365 }
2366
2367 private void matchGesture(Gesture gesture) {
2368 mGesturesAdd.setAlpha(255);
2369 mGesturesAdd.setEnabled(true);
2370
2371 if (gesture != null) {
2372 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2373
2374 if (DEBUG_GESTURES) {
2375 for (Prediction p : predictions) {
2376 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2377 }
2378 }
2379
2380 boolean match = false;
2381 if (predictions.size() > 0) {
2382 final Prediction prediction = predictions.get(0);
2383 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2384 match = true;
2385
2386 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2387 if (info != null) {
2388 updatePrompt(info);
2389 }
2390 }
2391 }
2392
2393 if (!match){
2394 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2395 }
2396 }
2397 }
2398
2399 private void updatePrompt(ApplicationInfo info) {
2400 setGesturesNextPrompt(info.icon, info.title);
2401 mGesturesAction.intent = info.intent;
2402 }
2403
2404 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
2405 overlay.removeCallbacks(mMatcher);
2406 }
2407
2408 void addGesture(String name, Gesture gesture) {
2409 sLibrary.addGesture(name, gesture);
2410 // TODO: On a background thread?
2411 sLibrary.save();
2412 }
2413
2414 void update(ApplicationInfo info, Gesture gesture) {
2415 mGesturesOverlay.setGesture(gesture);
2416 updatePrompt(info);
2417 }
2418
2419 class GestureMatcher implements Runnable {
2420 Gesture gesture;
2421
2422 public void run() {
2423 if (gesture != null) {
2424 matchGesture(gesture);
2425 }
2426 }
2427 }
2428 }
2429
2430 private class GesturesAction implements View.OnClickListener {
2431 Intent intent;
2432
2433 public void onClick(View v) {
2434 if (intent != null) {
2435 startActivitySafely(intent);
2436 }
2437 }
2438 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439}
Karl Rosaen138a0412009-04-23 19:00:21 -07002440