blob: 88c411a95daa22dffb0b7a613e64f5cf6da26e89 [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;
Romain Guyaad5ef42009-06-10 02:48:37 -0700233 private boolean mHideGesturesPanel;
Romain Guy73b979d2009-06-09 12:57:21 -0700234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 @Override
236 protected void onCreate(Bundle savedInstanceState) {
237 super.onCreate(savedInstanceState);
238 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700239
Romain Guy73b979d2009-06-09 12:57:21 -0700240 if (sLibrary == null) {
241 // The context is not kept by the library so it's safe to do this
242 sLibrary = GestureLibraries.fromPrivateFile(Launcher.this,
243 GesturesConstants.STORE_NAME);
244 }
245
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700246 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700247
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700248 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
249 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 if (PROFILE_STARTUP) {
252 android.os.Debug.startMethodTracing("/sdcard/launcher");
253 }
254
255 checkForLocaleChange();
256 setWallpaperDimension();
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 setContentView(R.layout.launcher);
259 setupViews();
260
261 registerIntentReceivers();
262 registerContentObservers();
263
264 mSavedState = savedInstanceState;
265 restoreState(mSavedState);
266
267 if (PROFILE_STARTUP) {
268 android.os.Debug.stopMethodTracing();
269 }
270
271 if (!mRestoring) {
272 startLoaders();
273 }
274
275 // For handling default keys
276 mDefaultKeySsb = new SpannableStringBuilder();
277 Selection.setSelection(mDefaultKeySsb, 0);
278 }
Romain Guycbb89e42009-06-08 15:52:54 -0700279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 private void checkForLocaleChange() {
281 final SharedPreferences preferences = getSharedPreferences(PREFERENCES, MODE_PRIVATE);
282 final Configuration configuration = getResources().getConfiguration();
283
284 final String previousLocale = preferences.getString(KEY_LOCALE, null);
285 final String locale = configuration.locale.toString();
286
287 final int previousMcc = preferences.getInt(KEY_MCC, -1);
288 final int mcc = configuration.mcc;
289
290 final int previousMnc = preferences.getInt(KEY_MNC, -1);
291 final int mnc = configuration.mnc;
292
293 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
294
295 if (mLocaleChanged) {
296 final SharedPreferences.Editor editor = preferences.edit();
297 editor.putString(KEY_LOCALE, locale);
298 editor.putInt(KEY_MCC, mcc);
299 editor.putInt(KEY_MNC, mnc);
300 editor.commit();
301 }
302 }
303
304 static int getScreen() {
305 synchronized (sLock) {
306 return sScreen;
307 }
308 }
309
310 static void setScreen(int screen) {
311 synchronized (sLock) {
312 sScreen = screen;
313 }
314 }
315
316 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700317 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
318 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
319
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 mRestoring = false;
321 }
322
323 private void setWallpaperDimension() {
324 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
325 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
326
327 Display display = getWindowManager().getDefaultDisplay();
328 boolean isPortrait = display.getWidth() < display.getHeight();
329
330 final int width = isPortrait ? display.getWidth() : display.getHeight();
331 final int height = isPortrait ? display.getHeight() : display.getWidth();
332 try {
333 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
334 } catch (RemoteException e) {
335 // System is dead!
336 }
337 }
338
339 @Override
340 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700341 mWaitingForResult = false;
342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 // The pattern used here is that a user PICKs a specific application,
344 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700345
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
347 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700348
Romain Guy73b979d2009-06-09 12:57:21 -0700349 if (resultCode == RESULT_OK && (mAddItemCellInfo != null ||
350 ((requestCode == REQUEST_PICK_GESTURE_ACTION ||
351 requestCode == REQUEST_CREATE_GESTURE_ACTION ||
352 requestCode == REQUEST_CREATE_GESTURE_APPLICATION_ACTION) && mCurrentGesture != null))) {
353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 switch (requestCode) {
355 case REQUEST_PICK_APPLICATION:
356 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
357 break;
358 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700359 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 break;
361 case REQUEST_CREATE_SHORTCUT:
362 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
363 break;
364 case REQUEST_PICK_LIVE_FOLDER:
365 addLiveFolder(data);
366 break;
367 case REQUEST_CREATE_LIVE_FOLDER:
368 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
369 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700370 case REQUEST_PICK_APPWIDGET:
371 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700373 case REQUEST_CREATE_APPWIDGET:
374 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 break;
Romain Guy73b979d2009-06-09 12:57:21 -0700376 case REQUEST_PICK_GESTURE_ACTION:
377 processShortcut(data, REQUEST_CREATE_GESTURE_APPLICATION_ACTION,
378 REQUEST_CREATE_GESTURE_ACTION);
379 break;
380 case REQUEST_CREATE_GESTURE_ACTION:
381 completeCreateGesture(data, true);
382 break;
383 case REQUEST_CREATE_GESTURE_APPLICATION_ACTION:
384 completeCreateGesture(data, false);
385 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700387 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700389 // Clean up the appWidgetId if we canceled
390 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
391 if (appWidgetId != -1) {
392 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 }
396
397 @Override
398 protected void onResume() {
399 super.onResume();
400
401 if (mRestoring) {
402 startLoaders();
403 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700404
405 // Make sure that the search gadget (if any) is in its normal place.
Romain Guy5a941392009-04-28 15:18:25 -0700406 stopSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408
409 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700410 protected void onPause() {
411 super.onPause();
Romain Guy73b979d2009-06-09 12:57:21 -0700412 if (mGesturesWindow != null) {
413 mGesturesWindow.setAnimationStyle(0);
414 mGesturesWindow.update();
415 }
Romain Guycbb89e42009-06-08 15:52:54 -0700416 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700417 }
Romain Guycbb89e42009-06-08 15:52:54 -0700418
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700419 @Override
Romain Guy73b979d2009-06-09 12:57:21 -0700420 protected void onStop() {
421 super.onStop();
Romain Guyaad5ef42009-06-10 02:48:37 -0700422 if (mHideGesturesPanel) {
423 mHideGesturesPanel = false;
424 hideGesturesPanel();
425 }
Romain Guy73b979d2009-06-09 12:57:21 -0700426 }
427
428 @Override
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700429 public Object onRetainNonConfigurationInstance() {
430 // Flag any binder to stop early before switching
431 if (mBinder != null) {
432 mBinder.mTerminate = true;
433 }
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700435 if (PROFILE_ROTATE) {
436 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
437 }
438 return null;
439 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 private boolean acceptFilter() {
442 final InputMethodManager inputManager = (InputMethodManager)
443 getSystemService(Context.INPUT_METHOD_SERVICE);
444 return !inputManager.isFullscreenMode();
445 }
446
447 @Override
448 public boolean onKeyDown(int keyCode, KeyEvent event) {
449 boolean handled = super.onKeyDown(keyCode, event);
450 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
451 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
452 keyCode, event);
453 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700454 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700455 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700456 // showSearchDialog()
457 // If there are multiple keystrokes before the search dialog takes focus,
458 // onSearchRequested() will be called for every keystroke,
459 // but it is idempotent, so it's fine.
460 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
462 }
463
464 return handled;
465 }
466
Karl Rosaen138a0412009-04-23 19:00:21 -0700467 private String getTypedText() {
468 return mDefaultKeySsb.toString();
469 }
470
471 private void clearTypedText() {
472 mDefaultKeySsb.clear();
473 mDefaultKeySsb.clearSpans();
474 Selection.setSelection(mDefaultKeySsb, 0);
475 }
476
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 /**
478 * Restores the previous state, if it exists.
479 *
480 * @param savedState The previous state.
481 */
482 private void restoreState(Bundle savedState) {
483 if (savedState == null) {
484 return;
485 }
486
487 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
488 if (currentScreen > -1) {
489 mWorkspace.setCurrentScreen(currentScreen);
490 }
491
492 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
493 if (addScreen > -1) {
494 mAddItemCellInfo = new CellLayout.CellInfo();
495 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
496 addItemCellInfo.valid = true;
497 addItemCellInfo.screen = addScreen;
498 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
499 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
500 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
501 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
502 addItemCellInfo.findVacantCellsFromOccupied(
503 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
504 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
505 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
506 mRestoring = true;
507 }
508
509 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
510 if (renameFolder) {
511 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
512 mFolderInfo = sModel.getFolderById(this, id);
513 mRestoring = true;
514 }
Romain Guy73b979d2009-06-09 12:57:21 -0700515
516 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_PENDING_GESTURE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517 }
518
519 /**
520 * Finds all the views we need and configure them properly.
521 */
522 private void setupViews() {
523 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
524 final DragLayer dragLayer = mDragLayer;
525
526 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
527 final Workspace workspace = mWorkspace;
528
529 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
530 final SlidingDrawer drawer = mDrawer;
531
532 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
533 final AllAppsGridView grid = mAllAppsGrid;
534
535 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
536
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700537 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
538 mHandleView.setLauncher(this);
539 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 mHandleIcon.setCrossFadeEnabled(true);
541
542 drawer.lock();
543 final DrawerManager drawerManager = new DrawerManager();
544 drawer.setOnDrawerOpenListener(drawerManager);
545 drawer.setOnDrawerCloseListener(drawerManager);
546 drawer.setOnDrawerScrollListener(drawerManager);
547
Karl Rosaen138a0412009-04-23 19:00:21 -0700548 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 grid.setDragger(dragLayer);
550 grid.setLauncher(this);
551
552 workspace.setOnLongClickListener(this);
553 workspace.setDragger(dragLayer);
554 workspace.setLauncher(this);
555 loadWallpaper();
556
557 deleteZone.setLauncher(this);
558 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700559 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560
561 dragLayer.setIgnoredDropTarget(grid);
562 dragLayer.setDragScoller(workspace);
563 dragLayer.setDragListener(deleteZone);
Romain Guy73b979d2009-06-09 12:57:21 -0700564
565 mGesturesPanel = mInflater.inflate(R.layout.gestures, mDragLayer, false);
566 final View gesturesPanel = mGesturesPanel;
567
568 mGesturesPrompt = (ViewSwitcher) gesturesPanel.findViewById(R.id.gestures_actions);
569 mGesturesAction = new GesturesAction();
570
571 mGesturesPrompt.getChildAt(0).setOnClickListener(mGesturesAction);
572 mGesturesPrompt.getChildAt(1).setOnClickListener(mGesturesAction);
573
574 mGesturesAdd = (ImageView) gesturesPanel.findViewById(R.id.gestures_add);
575 final ImageView gesturesAdd = mGesturesAdd;
576 gesturesAdd.setAlpha(128);
577 gesturesAdd.setEnabled(false);
578 gesturesAdd.setOnClickListener(new View.OnClickListener() {
579 public void onClick(View v) {
580 createGesture();
581 }
582 });
583
584 mGesturesOverlay = (GestureOverlayView) gesturesPanel.findViewById(R.id.gestures_overlay);
585 mGesturesProcessor = new GesturesProcessor();
586
587 final GestureOverlayView overlay = mGesturesOverlay;
588 overlay.setFadeOffset(GesturesConstants.MATCH_DELAY);
589 overlay.addOnGestureListener(mGesturesProcessor);
Romain Guyb8734242009-06-10 11:53:57 -0700590 overlay.getGesturePaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
Romain Guy73b979d2009-06-09 12:57:21 -0700591 }
592
593 private void createGesture() {
594 mCurrentGesture = mGesturesOverlay.getGesture();
595 mWaitingForResult = true;
596 pickShortcut(REQUEST_PICK_GESTURE_ACTION, R.string.title_select_shortcut);
597 }
598
599 private void completeCreateGesture(Intent data, boolean isShortcut) {
600 ApplicationInfo info;
601
602 if (isShortcut) {
603 info = infoFromShortcutIntent(this, data);
604 } else {
605 info = infoFromApplicationIntent(this, data);
606 }
607
608 boolean success = false;
609 if (info != null) {
610 info.isGesture = true;
611
612 if (LauncherModel.addGestureToDatabase(this, info, false)) {
613 mGesturesProcessor.addGesture(String.valueOf(info.id), mCurrentGesture);
614 mGesturesProcessor.update(info, mCurrentGesture);
615 Toast.makeText(this, getString(R.string.gestures_created, info.title),
616 Toast.LENGTH_SHORT).show();
617 success = true;
618 }
619 }
620
621 if (!success) {
622 Toast.makeText(this, getString(R.string.gestures_failed), Toast.LENGTH_SHORT).show();
623 }
624
625 mCurrentGesture = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 }
627
628 /**
629 * Creates a view representing a shortcut.
630 *
631 * @param info The data structure describing the shortcut.
632 *
633 * @return A View inflated from R.layout.application.
634 */
635 View createShortcut(ApplicationInfo info) {
636 return createShortcut(R.layout.application,
637 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
638 }
639
640 /**
641 * Creates a view representing a shortcut inflated from the specified resource.
642 *
643 * @param layoutResId The id of the XML layout used to create the shortcut.
644 * @param parent The group the shortcut belongs to.
645 * @param info The data structure describing the shortcut.
646 *
647 * @return A View inflated from layoutResId.
648 */
649 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
650 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
651
652 if (!info.filtered) {
653 info.icon = Utilities.createIconThumbnail(info.icon, this);
654 info.filtered = true;
655 }
656
657 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
658 favorite.setText(info.title);
659 favorite.setTag(info);
660 favorite.setOnClickListener(this);
661
662 return favorite;
663 }
664
665 /**
666 * Add an application shortcut to the workspace.
667 *
668 * @param data The intent describing the application.
669 * @param cellInfo The position on screen where to create the shortcut.
670 */
671 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
672 boolean insertAtFirst) {
673 cellInfo.screen = mWorkspace.getCurrentScreen();
674 if (!findSingleSlot(cellInfo)) return;
675
Romain Guy73b979d2009-06-09 12:57:21 -0700676 final ApplicationInfo info = infoFromApplicationIntent(context, data);
677 if (info != null) {
678 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
679 }
680 }
681
682 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 ComponentName component = data.getComponent();
684 PackageManager packageManager = context.getPackageManager();
685 ActivityInfo activityInfo = null;
686 try {
687 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
688 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700689 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 }
Romain Guycbb89e42009-06-08 15:52:54 -0700691
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 if (activityInfo != null) {
693 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 itemInfo.title = activityInfo.loadLabel(packageManager);
696 if (itemInfo.title == null) {
697 itemInfo.title = activityInfo.name;
698 }
Romain Guycbb89e42009-06-08 15:52:54 -0700699
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
701 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
702 itemInfo.icon = activityInfo.loadIcon(packageManager);
703 itemInfo.container = ItemInfo.NO_ID;
704
Romain Guy73b979d2009-06-09 12:57:21 -0700705 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 }
Romain Guy73b979d2009-06-09 12:57:21 -0700707
708 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 }
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 /**
712 * Add a shortcut to the workspace.
713 *
714 * @param data The intent describing the shortcut.
715 * @param cellInfo The position on screen where to create the shortcut.
716 * @param insertAtFirst
717 */
718 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
719 boolean insertAtFirst) {
720 cellInfo.screen = mWorkspace.getCurrentScreen();
721 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700722
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
724
725 if (!mRestoring) {
726 sModel.addDesktopItem(info);
727
728 final View view = createShortcut(info);
729 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
730 } else if (sModel.isDesktopLoaded()) {
731 sModel.addDesktopItem(info);
732 }
733 }
734
Romain Guycbb89e42009-06-08 15:52:54 -0700735
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700737 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700739 * @param data The intent describing the appWidgetId.
740 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700742 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 boolean insertAtFirst) {
744
745 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700746 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700747
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700748 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700749
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700750 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700751
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700752 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700754 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700755
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 // Try finding open space on Launcher screen
757 final int[] xy = mCellCoordinates;
758 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
759
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700760 // Build Launcher-specific widget info and save to database
761 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 launcherInfo.spanX = spans[0];
763 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700764
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 LauncherModel.addItemToDatabase(this, launcherInfo,
766 LauncherSettings.Favorites.CONTAINER_DESKTOP,
767 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
768
769 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700770 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700771
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700773 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700774
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700775 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700777
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
779 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
780 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700781 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 }
783 }
Romain Guycbb89e42009-06-08 15:52:54 -0700784
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700785 public LauncherAppWidgetHost getAppWidgetHost() {
786 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 }
Romain Guycbb89e42009-06-08 15:52:54 -0700788
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 static ApplicationInfo addShortcut(Context context, Intent data,
790 CellLayout.CellInfo cellInfo, boolean notify) {
791
Romain Guy73b979d2009-06-09 12:57:21 -0700792 final ApplicationInfo info = infoFromShortcutIntent(context, data);
793 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
794 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
795
796 return info;
797 }
798
799 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
801 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
802 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
803
804 Drawable icon = null;
805 boolean filtered = false;
806 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700807 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808
809 if (bitmap != null) {
810 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
811 filtered = true;
812 customIcon = true;
813 } else {
814 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700815 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700817 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 final PackageManager packageManager = context.getPackageManager();
819 Resources resources = packageManager.getResourcesForApplication(
820 iconResource.packageName);
821 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
822 icon = resources.getDrawable(id);
823 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700824 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 }
826 }
827 }
828
829 if (icon == null) {
830 icon = context.getPackageManager().getDefaultActivityIcon();
831 }
832
833 final ApplicationInfo info = new ApplicationInfo();
834 info.icon = icon;
835 info.filtered = filtered;
836 info.title = name;
837 info.intent = intent;
838 info.customIcon = customIcon;
839 info.iconResource = iconResource;
840
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 return info;
842 }
843
844 @Override
845 protected void onNewIntent(Intent intent) {
846 super.onNewIntent(intent);
847
848 // Close the menu
849 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
850 getWindow().closeAllPanels();
851
852 try {
853 dismissDialog(DIALOG_CREATE_SHORTCUT);
854 // Unlock the workspace if the dialog was showing
855 mWorkspace.unlock();
856 } catch (Exception e) {
857 // An exception is thrown if the dialog is not visible, which is fine
858 }
859
860 try {
861 dismissDialog(DIALOG_RENAME_FOLDER);
862 // Unlock the workspace if the dialog was showing
863 mWorkspace.unlock();
864 } catch (Exception e) {
865 // An exception is thrown if the dialog is not visible, which is fine
866 }
867
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
869 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700870
871 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE) {
Romain Guyb8734242009-06-10 11:53:57 -0700872 SearchManager searchManager =
873 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
874 if (!searchManager.isVisible()) {
875 onHomeKeyPressed();
876 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 }
878 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700879
880 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 if (v != null && v.getWindowToken() != null) {
882 InputMethodManager imm = (InputMethodManager)getSystemService(
883 INPUT_METHOD_SERVICE);
884 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
885 }
886 } else {
887 closeDrawer(false);
888 }
889 }
890 }
891
Romain Guy73b979d2009-06-09 12:57:21 -0700892 private void onHomeKeyPressed() {
893 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
894 showGesturesPanel();
895 } else {
896 hideGesturesPanel();
897 }
898 }
899
900 private void showGesturesPanel() {
901 resetGesturesPrompt();
902
903 mGesturesAdd.setEnabled(false);
904 mGesturesAdd.setAlpha(128);
905
906 mGesturesOverlay.clear(false);
907
908 PopupWindow window;
909 if (mGesturesWindow == null) {
910 mGesturesWindow = new PopupWindow(this);
911 window = mGesturesWindow;
912 window.setFocusable(true);
913 window.setTouchable(true);
914 window.setBackgroundDrawable(null);
915 window.setContentView(mGesturesPanel);
916 } else {
917 window = mGesturesWindow;
918 }
919 window.setAnimationStyle(com.android.internal.R.style.Animation_SlidingCard);
920
921 final int[] xy = new int[2];
922 final DragLayer dragLayer = mDragLayer;
923 dragLayer.getLocationOnScreen(xy);
924
925 window.setWidth(dragLayer.getWidth());
926 window.setHeight(dragLayer.getHeight() - 1);
927 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
928 }
929
930 private void resetGesturesPrompt() {
931 mGesturesAction.intent = null;
932 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
933 prompt.setText(R.string.gestures_instructions);
934 prompt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
935 prompt.setClickable(false);
936 }
937
938 private void resetGesturesNextPrompt() {
939 mGesturesAction.intent = null;
940 setGesturesNextPrompt(null, getString(R.string.gestures_instructions));
941 mGesturesPrompt.getNextView().setClickable(false);
942 }
943
944 private void setGesturesNextPrompt(Drawable icon, CharSequence title) {
945 final TextView prompt = (TextView) mGesturesPrompt.getNextView();
946 prompt.setText(title);
947 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
948 prompt.setClickable(true);
949 mGesturesPrompt.showNext();
950 }
951
952 void hideGesturesPanel() {
953 if (mGesturesWindow != null) {
954 mGesturesWindow.setAnimationStyle(com.android.internal.R.style.Animation_SlidingCard);
955 mGesturesWindow.update();
956 mGesturesWindow.dismiss();
957 }
958 }
959
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 @Override
961 protected void onRestoreInstanceState(Bundle savedInstanceState) {
962 // Do not call super here
963 mSavedInstanceState = savedInstanceState;
964 }
965
966 @Override
967 protected void onSaveInstanceState(Bundle outState) {
968 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
969
970 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
971 if (folders.size() > 0) {
972 final int count = folders.size();
973 long[] ids = new long[count];
974 for (int i = 0; i < count; i++) {
975 final FolderInfo info = folders.get(i).getInfo();
976 ids[i] = info.id;
977 }
978 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
979 } else {
980 super.onSaveInstanceState(outState);
981 }
982
Romain Guy5a941392009-04-28 15:18:25 -0700983 // When the drawer is opened and we are saving the state because of a
984 // configuration change
985 if (mDrawer.isOpened() && getChangingConfigurations() != 0) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988
989 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
990 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
991 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
992
993 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
994 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
995 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
996 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
997 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
998 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
999 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1000 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1001 layout.getOccupiedCells());
1002 }
1003
1004 if (mFolderInfo != null && mWaitingForResult) {
1005 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1006 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1007 }
Romain Guy73b979d2009-06-09 12:57:21 -07001008
1009 if (mCurrentGesture != null && mWaitingForResult) {
1010 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1011 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 }
1013
1014 @Override
1015 public void onDestroy() {
1016 mDestroyed = true;
1017
1018 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001021 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001023 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
1025
1026 TextKeyListener.getInstance().release();
1027
1028 mAllAppsGrid.clearTextFilter();
1029 mAllAppsGrid.setAdapter(null);
1030 sModel.unbind();
1031 sModel.abortLoaders();
1032
1033 getContentResolver().unregisterContentObserver(mObserver);
1034 unregisterReceiver(mApplicationsReceiver);
1035 }
1036
1037 @Override
1038 public void startActivityForResult(Intent intent, int requestCode) {
1039 mWaitingForResult = true;
1040 super.startActivityForResult(intent, requestCode);
1041 }
1042
1043 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001044 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001046
1047 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001048
Karl Rosaen138a0412009-04-23 19:00:21 -07001049 // Slide the search widget to the top, if it's on the current screen,
1050 // otherwise show the search dialog immediately.
1051 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1052 if (searchWidget == null) {
1053 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1054 } else {
1055 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1056 // show the currently typed text in the search widget while sliding
1057 searchWidget.setQuery(getTypedText());
1058 }
1059 }
Romain Guycbb89e42009-06-08 15:52:54 -07001060
Karl Rosaen138a0412009-04-23 19:00:21 -07001061 /**
1062 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001063 *
1064 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001065 */
Romain Guycbb89e42009-06-08 15:52:54 -07001066 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001067 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001068
Karl Rosaen138a0412009-04-23 19:00:21 -07001069 if (initialQuery == null) {
1070 // Use any text typed in the launcher as the initial query
1071 initialQuery = getTypedText();
1072 clearTypedText();
1073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 if (appSearchData == null) {
1075 appSearchData = new Bundle();
1076 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1077 }
Romain Guycbb89e42009-06-08 15:52:54 -07001078
Karl Rosaen138a0412009-04-23 19:00:21 -07001079 final SearchManager searchManager =
1080 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1081
1082 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1083 if (searchWidget != null) {
1084 // This gets called when the user leaves the search dialog to go back to
1085 // the Launcher.
1086 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1087 public void onCancel() {
1088 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001089 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001090 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001091 });
1092 }
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Karl Rosaen138a0412009-04-23 19:00:21 -07001094 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001095 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 }
1097
Karl Rosaen138a0412009-04-23 19:00:21 -07001098 /**
1099 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001100 */
Romain Guy5a941392009-04-28 15:18:25 -07001101 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001102 // Close search dialog
1103 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1104 if (searchManager.isVisible()) {
1105 searchManager.stopSearch();
1106 }
1107 // Restore search widget to its normal position
1108 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1109 if (searchWidget != null) {
1110 searchWidget.stopSearch(false);
1111 }
1112 }
Romain Guycbb89e42009-06-08 15:52:54 -07001113
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 @Override
1115 public boolean onCreateOptionsMenu(Menu menu) {
1116 if (mDesktopLocked) return false;
1117
1118 super.onCreateOptionsMenu(menu);
1119 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1120 .setIcon(android.R.drawable.ic_menu_add)
1121 .setAlphabeticShortcut('A');
1122 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1123 .setIcon(android.R.drawable.ic_menu_gallery)
1124 .setAlphabeticShortcut('W');
1125 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1126 .setIcon(android.R.drawable.ic_search_category_default)
1127 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1128 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1129 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1130 .setAlphabeticShortcut('N');
1131
Romain Guy73b979d2009-06-09 12:57:21 -07001132 final Intent gestures = new Intent(this, GesturesActivity.class);
1133 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1134 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1135 .setIntent(gestures);
1136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001138 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1139 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140
1141 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1142 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1143 .setIntent(settings);
1144
1145 return true;
1146 }
1147
1148 @Override
1149 public boolean onPrepareOptionsMenu(Menu menu) {
1150 super.onPrepareOptionsMenu(menu);
1151
1152 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1153 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1154
1155 return true;
1156 }
1157
1158 @Override
1159 public boolean onOptionsItemSelected(MenuItem item) {
1160 switch (item.getItemId()) {
1161 case MENU_ADD:
1162 addItems();
1163 return true;
1164 case MENU_WALLPAPER_SETTINGS:
1165 startWallpaper();
1166 return true;
1167 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001168 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 return true;
1170 case MENU_NOTIFICATIONS:
1171 showNotifications();
1172 return true;
1173 }
1174
1175 return super.onOptionsItemSelected(item);
1176 }
Romain Guycbb89e42009-06-08 15:52:54 -07001177
Karl Rosaen138a0412009-04-23 19:00:21 -07001178 /**
1179 * Indicates that we want global search for this activity by setting the globalSearch
1180 * argument for {@link #startSearch} to true.
1181 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001183 @Override
1184 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001185 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001186 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001187 }
1188
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 private void addItems() {
1190 showAddDialog(mMenuAddInfo);
1191 }
1192
1193 private void removeShortcutsForPackage(String packageName) {
1194 if (packageName != null && packageName.length() > 0) {
1195 mWorkspace.removeShortcutsForPackage(packageName);
1196 }
1197 }
Romain Guycbb89e42009-06-08 15:52:54 -07001198
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001199 private void updateShortcutsForPackage(String packageName) {
1200 if (packageName != null && packageName.length() > 0) {
1201 mWorkspace.updateShortcutsForPackage(packageName);
1202 }
1203 }
1204
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001205 void addAppWidget(Intent data) {
1206 // TODO: catch bad widget exception when sent
1207 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001209 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1210 if (SEARCH_WIDGET.equals(customWidget)) {
1211 // We don't need this any more, since this isn't a real app widget.
1212 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1213 // add the search widget
1214 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001216 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1217
1218 if (appWidget.configure != null) {
1219 // Launch over to configure widget, if needed
1220 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1221 intent.setComponent(appWidget.configure);
1222 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1223
1224 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1225 } else {
1226 // Otherwise just add it
1227 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 }
1230 }
Romain Guycbb89e42009-06-08 15:52:54 -07001231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 void addSearch() {
1233 final Widget info = Widget.makeSearch();
1234 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 final int[] xy = mCellCoordinates;
1237 final int spanX = info.spanX;
1238 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 sModel.addDesktopItem(info);
1243 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1244 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 final View view = mInflater.inflate(info.layoutResource, null);
1247 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001248 Search search = (Search) view.findViewById(R.id.widget_search);
1249 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1252 }
1253
Romain Guy73b979d2009-06-09 12:57:21 -07001254 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001255 // Handle case where user selected "Applications"
1256 String applicationName = getResources().getString(R.string.group_applications);
1257 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001258
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001259 if (applicationName != null && applicationName.equals(shortcutName)) {
1260 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1261 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001262
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001263 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1264 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001265 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001266 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001267 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
1270
1271 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001272 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001273 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001274 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001275
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001276 if (folderName != null && folderName.equals(shortcutName)) {
1277 addFolder(!mDesktopLocked);
1278 } else {
1279 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1280 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 }
1282
1283 void addFolder(boolean insertAtFirst) {
1284 UserFolderInfo folderInfo = new UserFolderInfo();
1285 folderInfo.title = getText(R.string.folder_name);
1286
1287 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1288 cellInfo.screen = mWorkspace.getCurrentScreen();
1289 if (!findSingleSlot(cellInfo)) return;
1290
1291 // Update the model
1292 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1293 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1294 sModel.addDesktopItem(folderInfo);
1295 sModel.addFolder(folderInfo);
1296
1297 // Create the view
1298 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1299 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1300 mWorkspace.addInCurrentScreen(newFolder,
1301 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1302 }
Romain Guycbb89e42009-06-08 15:52:54 -07001303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1305 boolean insertAtFirst) {
1306 cellInfo.screen = mWorkspace.getCurrentScreen();
1307 if (!findSingleSlot(cellInfo)) return;
1308
1309 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1310
1311 if (!mRestoring) {
1312 sModel.addDesktopItem(info);
1313
1314 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1315 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1316 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1317 } else if (sModel.isDesktopLoaded()) {
1318 sModel.addDesktopItem(info);
1319 }
1320 }
1321
1322 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1323 CellLayout.CellInfo cellInfo, boolean notify) {
1324
1325 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1326 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1327
1328 Drawable icon = null;
1329 boolean filtered = false;
1330 Intent.ShortcutIconResource iconResource = null;
1331
1332 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1333 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1334 try {
1335 iconResource = (Intent.ShortcutIconResource) extra;
1336 final PackageManager packageManager = context.getPackageManager();
1337 Resources resources = packageManager.getResourcesForApplication(
1338 iconResource.packageName);
1339 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1340 icon = resources.getDrawable(id);
1341 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001342 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344 }
1345
1346 if (icon == null) {
1347 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1348 }
1349
1350 final LiveFolderInfo info = new LiveFolderInfo();
1351 info.icon = icon;
1352 info.filtered = filtered;
1353 info.title = name;
1354 info.iconResource = iconResource;
1355 info.uri = data.getData();
1356 info.baseIntent = baseIntent;
1357 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1358 LiveFolders.DISPLAY_MODE_GRID);
1359
1360 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1361 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1362 sModel.addFolder(info);
1363
1364 return info;
1365 }
1366
1367 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1368 final int[] xy = new int[2];
1369 if (findSlot(cellInfo, xy, 1, 1)) {
1370 cellInfo.cellX = xy[0];
1371 cellInfo.cellY = xy[1];
1372 return true;
1373 }
1374 return false;
1375 }
1376
1377 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1378 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1379 boolean[] occupied = mSavedState != null ?
1380 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1381 cellInfo = mWorkspace.findAllVacantCells(occupied);
1382 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1383 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1384 return false;
1385 }
1386 }
1387 return true;
1388 }
1389
1390 private void showNotifications() {
1391 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1392 if (statusBar != null) {
1393 statusBar.expand();
1394 }
1395 }
1396
1397 private void startWallpaper() {
1398 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1399 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1400 }
1401
1402 /**
1403 * Registers various intent receivers. The current implementation registers
1404 * only a wallpaper intent receiver to let other applications change the
1405 * wallpaper.
1406 */
1407 private void registerIntentReceivers() {
1408 if (sWallpaperReceiver == null) {
1409 final Application application = getApplication();
1410
1411 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1412
1413 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1414 application.registerReceiver(sWallpaperReceiver, filter);
1415 } else {
1416 sWallpaperReceiver.setLauncher(this);
1417 }
1418
1419 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1420 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1421 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1422 filter.addDataScheme("package");
1423 registerReceiver(mApplicationsReceiver, filter);
1424 }
1425
1426 /**
1427 * Registers various content observers. The current implementation registers
1428 * only a favorites observer to keep track of the favorites applications.
1429 */
1430 private void registerContentObservers() {
1431 ContentResolver resolver = getContentResolver();
1432 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1433 }
1434
1435 @Override
1436 public boolean dispatchKeyEvent(KeyEvent event) {
1437 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1438 switch (event.getKeyCode()) {
1439 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001440 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001441 if (mDrawer.isOpened()) {
1442 closeDrawer();
1443 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001444 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001445 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 return true;
1447 case KeyEvent.KEYCODE_HOME:
1448 return true;
1449 }
1450 }
1451
1452 return super.dispatchKeyEvent(event);
1453 }
1454
1455 private void closeDrawer() {
1456 closeDrawer(true);
1457 }
1458
1459 private void closeDrawer(boolean animated) {
1460 if (mDrawer.isOpened()) {
1461 if (animated) {
1462 mDrawer.animateClose();
1463 } else {
1464 mDrawer.close();
1465 }
1466 if (mDrawer.hasFocus()) {
1467 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1468 }
1469 }
1470 }
1471
1472 private void closeFolder() {
1473 Folder folder = mWorkspace.getOpenFolder();
1474 if (folder != null) {
1475 closeFolder(folder);
1476 }
1477 }
1478
1479 void closeFolder(Folder folder) {
1480 folder.getInfo().opened = false;
1481 ViewGroup parent = (ViewGroup) folder.getParent();
1482 if (parent != null) {
1483 parent.removeView(folder);
1484 }
1485 folder.onClose();
1486 }
1487
1488 /**
1489 * When the notification that favorites have changed is received, requests
1490 * a favorites list refresh.
1491 */
1492 private void onFavoritesChanged() {
1493 mDesktopLocked = true;
1494 mDrawer.lock();
1495 sModel.loadUserItems(false, this, false, false);
1496 }
1497
1498 void onDesktopItemsLoaded() {
1499 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 bindDesktopItems();
1501 }
Romain Guycbb89e42009-06-08 15:52:54 -07001502
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 /**
1504 * Refreshes the shortcuts shown on the workspace.
1505 */
1506 private void bindDesktopItems() {
1507 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001508 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001509 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1510 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 return;
1512 }
1513
1514 final Workspace workspace = mWorkspace;
1515 int count = workspace.getChildCount();
1516 for (int i = 0; i < count; i++) {
1517 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1518 }
Romain Guycbb89e42009-06-08 15:52:54 -07001519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 if (DEBUG_USER_INTERFACE) {
1521 android.widget.Button finishButton = new android.widget.Button(this);
1522 finishButton.setText("Finish");
1523 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1524
1525 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1526 public void onClick(View v) {
1527 finish();
1528 }
1529 });
1530 }
Romain Guycbb89e42009-06-08 15:52:54 -07001531
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001532 // Flag any old binder to terminate early
1533 if (mBinder != null) {
1534 mBinder.mTerminate = true;
1535 }
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Karl Rosaen138a0412009-04-23 19:00:21 -07001537 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001538 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
1540
1541 private void bindItems(Launcher.DesktopBinder binder,
1542 ArrayList<ItemInfo> shortcuts, int start, int count) {
1543
1544 final Workspace workspace = mWorkspace;
1545 final boolean desktopLocked = mDesktopLocked;
1546
1547 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1548 int i = start;
1549
1550 for ( ; i < end; i++) {
1551 final ItemInfo item = shortcuts.get(i);
1552 switch (item.itemType) {
1553 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1554 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1555 final View shortcut = createShortcut((ApplicationInfo) item);
1556 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1557 !desktopLocked);
1558 break;
1559 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1560 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1561 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1562 (UserFolderInfo) item);
1563 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1564 !desktopLocked);
1565 break;
1566 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1567 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1568 R.layout.live_folder_icon, this,
1569 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1570 (LiveFolderInfo) item);
1571 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1572 !desktopLocked);
1573 break;
1574 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1575 final int screen = workspace.getCurrentScreen();
1576 final View view = mInflater.inflate(R.layout.widget_search,
1577 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001578
Karl Rosaen138a0412009-04-23 19:00:21 -07001579 Search search = (Search) view.findViewById(R.id.widget_search);
1580 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001581
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 final Widget widget = (Widget) item;
1583 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001584
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 workspace.addWidget(view, widget, !desktopLocked);
1586 break;
1587 }
1588 }
1589
1590 workspace.requestLayout();
1591
1592 if (end >= count) {
1593 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001594 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 } else {
1596 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1597 }
1598 }
1599
1600 private void finishBindDesktopItems() {
1601 if (mSavedState != null) {
1602 if (!mWorkspace.hasFocus()) {
1603 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1604 }
1605
1606 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1607 if (userFolders != null) {
1608 for (long folderId : userFolders) {
1609 final FolderInfo info = sModel.findFolderById(folderId);
1610 if (info != null) {
1611 openFolder(info);
1612 }
1613 }
1614 final Folder openFolder = mWorkspace.getOpenFolder();
1615 if (openFolder != null) {
1616 openFolder.requestFocus();
1617 }
1618 }
1619
1620 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1621 if (allApps) {
1622 mDrawer.open();
1623 }
1624
1625 mSavedState = null;
1626 }
1627
1628 if (mSavedInstanceState != null) {
1629 super.onRestoreInstanceState(mSavedInstanceState);
1630 mSavedInstanceState = null;
1631 }
1632
1633 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1634 mDrawer.requestFocus();
1635 }
1636
1637 mDesktopLocked = false;
1638 mDrawer.unlock();
1639 }
Romain Guycbb89e42009-06-08 15:52:54 -07001640
Karl Rosaen138a0412009-04-23 19:00:21 -07001641 private void bindDrawer(Launcher.DesktopBinder binder,
1642 ApplicationsAdapter drawerAdapter) {
1643 mAllAppsGrid.setAdapter(drawerAdapter);
1644 binder.startBindingAppWidgetsWhenIdle();
1645 }
Romain Guycbb89e42009-06-08 15:52:54 -07001646
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001647 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001648 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001649
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 final Workspace workspace = mWorkspace;
1651 final boolean desktopLocked = mDesktopLocked;
1652
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001653 if (!appWidgets.isEmpty()) {
1654 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001655
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001656 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001657 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001658 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001659
Karl Rosaen138a0412009-04-23 19:00:21 -07001660 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001661
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001662 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1663 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001664
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001665 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1666 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001667
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001668 workspace.requestLayout();
1669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001671 if (appWidgets.isEmpty()) {
1672 if (PROFILE_ROTATE) {
1673 android.os.Debug.stopMethodTracing();
1674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001676 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 }
1678 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 DragController getDragController() {
1681 return mDragLayer;
1682 }
1683
1684 /**
1685 * Launches the intent referred by the clicked shortcut.
1686 *
1687 * @param v The view representing the clicked shortcut.
1688 */
1689 public void onClick(View v) {
1690 Object tag = v.getTag();
1691 if (tag instanceof ApplicationInfo) {
1692 // Open shortcut
1693 final Intent intent = ((ApplicationInfo) tag).intent;
1694 startActivitySafely(intent);
1695 } else if (tag instanceof FolderInfo) {
1696 handleFolderClick((FolderInfo) tag);
1697 }
1698 }
1699
1700 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001701 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1703 try {
1704 startActivity(intent);
1705 } catch (ActivityNotFoundException e) {
1706 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1707 } catch (SecurityException e) {
1708 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001709 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1711 "or use the exported attribute for this activity.", e);
1712 }
1713 }
1714
1715 private void handleFolderClick(FolderInfo folderInfo) {
1716 if (!folderInfo.opened) {
1717 // Close any open folder
1718 closeFolder();
1719 // Open the requested folder
1720 openFolder(folderInfo);
1721 } else {
1722 // Find the open folder...
1723 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1724 int folderScreen;
1725 if (openFolder != null) {
1726 folderScreen = mWorkspace.getScreenForView(openFolder);
1727 // .. and close it
1728 closeFolder(openFolder);
1729 if (folderScreen != mWorkspace.getCurrentScreen()) {
1730 // Close any folder open on the current screen
1731 closeFolder();
1732 // Pull the folder onto this screen
1733 openFolder(folderInfo);
1734 }
1735 }
1736 }
1737 }
1738
1739 private void loadWallpaper() {
1740 // The first time the application is started, we load the wallpaper from
1741 // the ApplicationContext
1742 if (sWallpaper == null) {
1743 final Drawable drawable = getWallpaper();
1744 if (drawable instanceof BitmapDrawable) {
1745 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1746 } else {
1747 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1748 }
1749 }
1750 mWorkspace.loadWallpaper(sWallpaper);
1751 }
1752
1753 /**
1754 * Opens the user fodler described by the specified tag. The opening of the folder
1755 * is animated relative to the specified View. If the View is null, no animation
1756 * is played.
1757 *
1758 * @param folderInfo The FolderInfo describing the folder to open.
1759 */
1760 private void openFolder(FolderInfo folderInfo) {
1761 Folder openFolder;
1762
1763 if (folderInfo instanceof UserFolderInfo) {
1764 openFolder = UserFolder.fromXml(this);
1765 } else if (folderInfo instanceof LiveFolderInfo) {
1766 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1767 } else {
1768 return;
1769 }
1770
1771 openFolder.setDragger(mDragLayer);
1772 openFolder.setLauncher(this);
1773
1774 openFolder.bind(folderInfo);
1775 folderInfo.opened = true;
1776
1777 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1778 openFolder.onOpen();
1779 }
1780
1781 /**
1782 * Returns true if the workspace is being loaded. When the workspace is loading,
1783 * no user interaction should be allowed to avoid any conflict.
1784 *
1785 * @return True if the workspace is locked, false otherwise.
1786 */
1787 boolean isWorkspaceLocked() {
1788 return mDesktopLocked;
1789 }
1790
1791 public boolean onLongClick(View v) {
1792 if (mDesktopLocked) {
1793 return false;
1794 }
1795
1796 if (!(v instanceof CellLayout)) {
1797 v = (View) v.getParent();
1798 }
1799
1800 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1801
1802 // This happens when long clicking an item with the dpad/trackball
1803 if (cellInfo == null) {
1804 return true;
1805 }
1806
1807 if (mWorkspace.allowLongPress()) {
1808 if (cellInfo.cell == null) {
1809 if (cellInfo.valid) {
1810 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001811 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 showAddDialog(cellInfo);
1813 }
1814 } else {
1815 if (!(cellInfo.cell instanceof Folder)) {
1816 // User long pressed on an item
1817 mWorkspace.startDrag(cellInfo);
1818 }
1819 }
1820 }
1821 return true;
1822 }
1823
1824 static LauncherModel getModel() {
1825 return sModel;
1826 }
1827
Romain Guy73b979d2009-06-09 12:57:21 -07001828 static GestureLibrary getGestureLibrary() {
1829 return sLibrary;
1830 }
1831
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 void closeAllApplications() {
1833 mDrawer.close();
1834 }
1835
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001836 View getDrawerHandle() {
1837 return mHandleView;
1838 }
1839
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 boolean isDrawerDown() {
1841 return !mDrawer.isMoving() && !mDrawer.isOpened();
1842 }
1843
1844 boolean isDrawerUp() {
1845 return mDrawer.isOpened() && !mDrawer.isMoving();
1846 }
1847
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001848 boolean isDrawerMoving() {
1849 return mDrawer.isMoving();
1850 }
1851
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 Workspace getWorkspace() {
1853 return mWorkspace;
1854 }
1855
1856 GridView getApplicationsGrid() {
1857 return mAllAppsGrid;
1858 }
1859
1860 @Override
1861 protected Dialog onCreateDialog(int id) {
1862 switch (id) {
1863 case DIALOG_CREATE_SHORTCUT:
1864 return new CreateShortcut().createDialog();
1865 case DIALOG_RENAME_FOLDER:
1866 return new RenameFolder().createDialog();
1867 }
1868
1869 return super.onCreateDialog(id);
1870 }
1871
1872 @Override
1873 protected void onPrepareDialog(int id, Dialog dialog) {
1874 switch (id) {
1875 case DIALOG_CREATE_SHORTCUT:
1876 mWorkspace.lock();
1877 break;
1878 case DIALOG_RENAME_FOLDER:
1879 mWorkspace.lock();
1880 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1881 final CharSequence text = mFolderInfo.title;
1882 input.setText(text);
Romain Guycbb89e42009-06-08 15:52:54 -07001883 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 break;
1885 }
1886 }
1887
1888 void showRenameDialog(FolderInfo info) {
1889 mFolderInfo = info;
1890 mWaitingForResult = true;
1891 showDialog(DIALOG_RENAME_FOLDER);
1892 }
1893
1894 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1895 mAddItemCellInfo = cellInfo;
1896 mWaitingForResult = true;
1897 showDialog(DIALOG_CREATE_SHORTCUT);
1898 }
1899
Romain Guy73b979d2009-06-09 12:57:21 -07001900 private void pickShortcut(int requestCode, int title) {
1901 Bundle bundle = new Bundle();
1902
1903 ArrayList<String> shortcutNames = new ArrayList<String>();
1904 shortcutNames.add(getString(R.string.group_applications));
1905 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1906
1907 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1908 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1909 R.drawable.ic_launcher_application));
1910 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1911
1912 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1913 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1914 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1915 pickIntent.putExtras(bundle);
1916
1917 startActivityForResult(pickIntent, requestCode);
1918 }
1919
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 private class RenameFolder {
1921 private EditText mInput;
1922
1923 Dialog createDialog() {
1924 mWaitingForResult = true;
1925 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1926 mInput = (EditText) layout.findViewById(R.id.folder_name);
1927
1928 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1929 builder.setIcon(0);
1930 builder.setTitle(getString(R.string.rename_folder_title));
1931 builder.setCancelable(true);
1932 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1933 public void onCancel(DialogInterface dialog) {
1934 cleanup();
1935 }
1936 });
1937 builder.setNegativeButton(getString(R.string.cancel_action),
1938 new Dialog.OnClickListener() {
1939 public void onClick(DialogInterface dialog, int which) {
1940 cleanup();
1941 }
1942 }
1943 );
1944 builder.setPositiveButton(getString(R.string.rename_action),
1945 new Dialog.OnClickListener() {
1946 public void onClick(DialogInterface dialog, int which) {
1947 changeFolderName();
1948 }
1949 }
1950 );
1951 builder.setView(layout);
1952 return builder.create();
1953 }
1954
1955 private void changeFolderName() {
1956 final String name = mInput.getText().toString();
1957 if (!TextUtils.isEmpty(name)) {
1958 // Make sure we have the right folder info
1959 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1960 mFolderInfo.title = name;
1961 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1962
1963 if (mDesktopLocked) {
1964 mDrawer.lock();
1965 sModel.loadUserItems(false, Launcher.this, false, false);
1966 } else {
1967 final FolderIcon folderIcon = (FolderIcon)
1968 mWorkspace.getViewForTag(mFolderInfo);
1969 if (folderIcon != null) {
1970 folderIcon.setText(name);
1971 getWorkspace().requestLayout();
1972 } else {
1973 mDesktopLocked = true;
1974 mDrawer.lock();
1975 sModel.loadUserItems(false, Launcher.this, false, false);
1976 }
1977 }
1978 }
1979 cleanup();
1980 }
1981
1982 private void cleanup() {
1983 mWorkspace.unlock();
1984 dismissDialog(DIALOG_RENAME_FOLDER);
1985 mWaitingForResult = false;
1986 mFolderInfo = null;
1987 }
1988 }
1989
1990 /**
1991 * Displays the shortcut creation dialog and launches, if necessary, the
1992 * appropriate activity.
1993 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001994 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07001995 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 Dialog createDialog() {
1999 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2004 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002005 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 builder.setInverseBackgroundForced(true);
2008
2009 AlertDialog dialog = builder.create();
2010 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002011 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 return dialog;
2014 }
2015
2016 public void onCancel(DialogInterface dialog) {
2017 mWaitingForResult = false;
2018 cleanup();
2019 }
2020
Romain Guycbb89e42009-06-08 15:52:54 -07002021 public void onDismiss(DialogInterface dialog) {
2022 mWorkspace.unlock();
2023 }
2024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 private void cleanup() {
2026 mWorkspace.unlock();
2027 dismissDialog(DIALOG_CREATE_SHORTCUT);
2028 }
2029
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002030 /**
2031 * Handle the action clicked in the "Add to home" dialog.
2032 */
2033 public void onClick(DialogInterface dialog, int which) {
2034 Resources res = getResources();
2035 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002036
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002037 switch (which) {
2038 case AddAdapter.ITEM_SHORTCUT: {
2039 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002040 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002041 break;
2042 }
Romain Guycbb89e42009-06-08 15:52:54 -07002043
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002044 case AddAdapter.ITEM_APPWIDGET: {
2045 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002046
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002047 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2048 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2049 // add the search widget
2050 ArrayList<AppWidgetProviderInfo> customInfo =
2051 new ArrayList<AppWidgetProviderInfo>();
2052 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2053 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2054 info.label = getString(R.string.group_search);
2055 info.icon = R.drawable.ic_search_widget;
2056 customInfo.add(info);
2057 pickIntent.putParcelableArrayListExtra(
2058 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2059 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2060 Bundle b = new Bundle();
2061 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2062 customExtras.add(b);
2063 pickIntent.putParcelableArrayListExtra(
2064 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2065 // start the pick activity
2066 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2067 break;
2068 }
Romain Guycbb89e42009-06-08 15:52:54 -07002069
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002070 case AddAdapter.ITEM_LIVE_FOLDER: {
2071 // Insert extra item to handle inserting folder
2072 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002073
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002074 ArrayList<String> shortcutNames = new ArrayList<String>();
2075 shortcutNames.add(res.getString(R.string.group_folder));
2076 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002077
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002078 ArrayList<ShortcutIconResource> shortcutIcons =
2079 new ArrayList<ShortcutIconResource>();
2080 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2081 R.drawable.ic_launcher_folder));
2082 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2083
2084 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2085 pickIntent.putExtra(Intent.EXTRA_INTENT,
2086 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2087 pickIntent.putExtra(Intent.EXTRA_TITLE,
2088 getText(R.string.title_select_live_folder));
2089 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002090
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002091 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2092 break;
2093 }
2094
2095 case AddAdapter.ITEM_WALLPAPER: {
2096 startWallpaper();
2097 break;
2098 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 }
2100 }
2101 }
2102
2103 /**
2104 * Receives notifications when applications are added/removed.
2105 */
2106 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2107 @Override
2108 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002109 final String action = intent.getAction();
2110 final String packageName = intent.getData().getSchemeSpecificPart();
2111 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2112
2113 if (LauncherModel.DEBUG_LOADERS) {
2114 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2115 ", replacing=" + replacing);
2116 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002118
2119 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2120 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2121 if (!replacing) {
2122 removeShortcutsForPackage(packageName);
2123 if (LauncherModel.DEBUG_LOADERS) {
2124 d(LauncherModel.LOG_TAG, " --> remove package");
2125 }
2126 sModel.removePackage(Launcher.this, packageName);
2127 }
2128 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2129 // later, we will update the package at this time
2130 } else {
2131 if (!replacing) {
2132 if (LauncherModel.DEBUG_LOADERS) {
2133 d(LauncherModel.LOG_TAG, " --> add package");
2134 }
2135 sModel.addPackage(Launcher.this, packageName);
2136 } else {
2137 if (LauncherModel.DEBUG_LOADERS) {
2138 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2139 }
2140 sModel.updatePackage(Launcher.this, packageName);
2141 updateShortcutsForPackage(packageName);
2142 }
2143 }
2144 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002145 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002146 if (LauncherModel.DEBUG_LOADERS) {
2147 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2148 }
2149 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 }
2151 }
2152 }
2153
2154 /**
2155 * Receives notifications whenever the user favorites have changed.
2156 */
2157 private class FavoritesChangeObserver extends ContentObserver {
2158 public FavoritesChangeObserver() {
2159 super(new Handler());
2160 }
2161
2162 @Override
2163 public void onChange(boolean selfChange) {
2164 onFavoritesChanged();
2165 }
2166 }
2167
2168 /**
2169 * Receives intents from other applications to change the wallpaper.
2170 */
2171 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2172 private final Application mApplication;
2173 private WeakReference<Launcher> mLauncher;
2174
2175 WallpaperIntentReceiver(Application application, Launcher launcher) {
2176 mApplication = application;
2177 setLauncher(launcher);
2178 }
2179
2180 void setLauncher(Launcher launcher) {
2181 mLauncher = new WeakReference<Launcher>(launcher);
2182 }
2183
2184 @Override
2185 public void onReceive(Context context, Intent intent) {
2186 // Load the wallpaper from the ApplicationContext and store it locally
2187 // until the Launcher Activity is ready to use it
2188 final Drawable drawable = mApplication.getWallpaper();
2189 if (drawable instanceof BitmapDrawable) {
2190 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2191 } else {
2192 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2193 }
2194
2195 // If Launcher is alive, notify we have a new wallpaper
2196 if (mLauncher != null) {
2197 final Launcher launcher = mLauncher.get();
2198 if (launcher != null) {
2199 launcher.loadWallpaper();
2200 }
2201 }
2202 }
2203 }
2204
2205 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2206 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2207 private boolean mOpen;
2208
2209 public void onDrawerOpened() {
2210 if (!mOpen) {
2211 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002214 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215
2216 mOpen = true;
2217 }
2218 }
2219
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002220 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2221 view.getDrawingRect(bounds);
2222
2223 while (view != mDragLayer) {
2224 bounds.offset(view.getLeft(), view.getTop());
2225 view = (View) view.getParent();
2226 }
2227 }
2228
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 public void onDrawerClosed() {
2230 if (mOpen) {
2231 mHandleIcon.reverseTransition(150);
2232 mWorkspace.mDrawerBounds.setEmpty();
2233 mOpen = false;
2234 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 mAllAppsGrid.setSelection(0);
2237 mAllAppsGrid.clearTextFilter();
2238 }
2239
2240 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002241 if (PROFILE_DRAWER) {
2242 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2243 }
2244
2245 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2246 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247 }
2248
2249 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002250 if (PROFILE_DRAWER) {
2251 android.os.Debug.stopMethodTracing();
2252 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 }
2254 }
2255
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002256 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002258 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002259 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 // Number of items to bind in every pass
2262 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263
2264 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002265 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002266 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002268
Karl Rosaen138a0412009-04-23 19:00:21 -07002269 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002270
2271 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002272 ArrayList<LauncherAppWidgetInfo> appWidgets,
2273 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274
2275 mLauncher = new WeakReference<Launcher>(launcher);
2276 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002277 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002278
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002279 // Sort widgets so active workspace is bound first
2280 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2281 final int size = appWidgets.size();
2282 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002283
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002284 for (int i = 0; i < size; i++) {
2285 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2286 if (appWidgetInfo.screen == currentScreen) {
2287 mAppWidgets.addFirst(appWidgetInfo);
2288 } else {
2289 mAppWidgets.addLast(appWidgetInfo);
2290 }
2291 }
2292 }
Romain Guycbb89e42009-06-08 15:52:54 -07002293
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002294 public void startBindingItems() {
2295 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2296 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002297
2298 public void startBindingDrawer() {
2299 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2300 }
Romain Guycbb89e42009-06-08 15:52:54 -07002301
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002302 public void startBindingAppWidgetsWhenIdle() {
2303 // Ask for notification when message queue becomes idle
2304 final MessageQueue messageQueue = Looper.myQueue();
2305 messageQueue.addIdleHandler(this);
2306 }
Romain Guycbb89e42009-06-08 15:52:54 -07002307
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002308 public boolean queueIdle() {
2309 // Queue is idle, so start binding items
2310 startBindingAppWidgets();
2311 return false;
2312 }
2313
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002314 public void startBindingAppWidgets() {
2315 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2316 }
2317
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002318 @Override
2319 public void handleMessage(Message msg) {
2320 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002321 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002322 return;
2323 }
Romain Guycbb89e42009-06-08 15:52:54 -07002324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325 switch (msg.what) {
2326 case MESSAGE_BIND_ITEMS: {
2327 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2328 break;
2329 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002330 case MESSAGE_BIND_DRAWER: {
2331 launcher.bindDrawer(this, mDrawerAdapter);
2332 break;
2333 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002334 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002335 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 break;
2337 }
2338 }
2339 }
2340 }
Romain Guy73b979d2009-06-09 12:57:21 -07002341
2342 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2343 GestureOverlayView.OnGesturePerformedListener {
2344
2345 private final GestureMatcher mMatcher = new GestureMatcher();
2346
2347 GesturesProcessor() {
2348 // TODO: Maybe the load should happen on a background thread?
2349 sLibrary.load();
2350 }
2351
2352 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
2353 overlay.removeCallbacks(mMatcher);
2354 resetGesturesNextPrompt();
2355
2356 mGesturesAdd.setAlpha(128);
2357 mGesturesAdd.setEnabled(false);
2358 }
2359
2360 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2361 }
2362
2363 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2364 }
2365
2366 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
2367 overlay.removeCallbacks(mMatcher);
2368
2369 mMatcher.gesture = overlay.getGesture();
2370 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2371 overlay.clear(false);
2372 } else {
2373 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2374 }
2375 }
2376
2377 private void matchGesture(Gesture gesture) {
2378 mGesturesAdd.setAlpha(255);
2379 mGesturesAdd.setEnabled(true);
2380
2381 if (gesture != null) {
2382 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2383
2384 if (DEBUG_GESTURES) {
2385 for (Prediction p : predictions) {
2386 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2387 }
2388 }
2389
2390 boolean match = false;
2391 if (predictions.size() > 0) {
2392 final Prediction prediction = predictions.get(0);
2393 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2394 match = true;
2395
2396 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2397 if (info != null) {
2398 updatePrompt(info);
2399 }
2400 }
2401 }
2402
2403 if (!match){
2404 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2405 }
2406 }
2407 }
2408
2409 private void updatePrompt(ApplicationInfo info) {
2410 setGesturesNextPrompt(info.icon, info.title);
2411 mGesturesAction.intent = info.intent;
2412 }
2413
2414 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
2415 overlay.removeCallbacks(mMatcher);
2416 }
2417
2418 void addGesture(String name, Gesture gesture) {
2419 sLibrary.addGesture(name, gesture);
2420 // TODO: On a background thread?
2421 sLibrary.save();
2422 }
2423
2424 void update(ApplicationInfo info, Gesture gesture) {
2425 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002426 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002427 }
2428
2429 class GestureMatcher implements Runnable {
2430 Gesture gesture;
2431
2432 public void run() {
2433 if (gesture != null) {
2434 matchGesture(gesture);
2435 }
2436 }
2437 }
2438 }
2439
2440 private class GesturesAction implements View.OnClickListener {
2441 Intent intent;
2442
2443 public void onClick(View v) {
2444 if (intent != null) {
2445 startActivitySafely(intent);
2446 }
2447 }
2448 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449}
Karl Rosaen138a0412009-04-23 19:00:21 -07002450