blob: 51b720bcfd6573c720bfe745ecb1f1376c4a6cf4 [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);
590 overlay.getGesturePaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
591 }
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) {
872 onHomeKeyPressed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 }
874 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700875
876 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 if (v != null && v.getWindowToken() != null) {
878 InputMethodManager imm = (InputMethodManager)getSystemService(
879 INPUT_METHOD_SERVICE);
880 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
881 }
882 } else {
883 closeDrawer(false);
884 }
885 }
886 }
887
Romain Guy73b979d2009-06-09 12:57:21 -0700888 private void onHomeKeyPressed() {
889 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
890 showGesturesPanel();
891 } else {
892 hideGesturesPanel();
893 }
894 }
895
896 private void showGesturesPanel() {
897 resetGesturesPrompt();
898
899 mGesturesAdd.setEnabled(false);
900 mGesturesAdd.setAlpha(128);
901
902 mGesturesOverlay.clear(false);
903
904 PopupWindow window;
905 if (mGesturesWindow == null) {
906 mGesturesWindow = new PopupWindow(this);
907 window = mGesturesWindow;
908 window.setFocusable(true);
909 window.setTouchable(true);
910 window.setBackgroundDrawable(null);
911 window.setContentView(mGesturesPanel);
912 } else {
913 window = mGesturesWindow;
914 }
915 window.setAnimationStyle(com.android.internal.R.style.Animation_SlidingCard);
916
917 final int[] xy = new int[2];
918 final DragLayer dragLayer = mDragLayer;
919 dragLayer.getLocationOnScreen(xy);
920
921 window.setWidth(dragLayer.getWidth());
922 window.setHeight(dragLayer.getHeight() - 1);
923 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
924 }
925
926 private void resetGesturesPrompt() {
927 mGesturesAction.intent = null;
928 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
929 prompt.setText(R.string.gestures_instructions);
930 prompt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
931 prompt.setClickable(false);
932 }
933
934 private void resetGesturesNextPrompt() {
935 mGesturesAction.intent = null;
936 setGesturesNextPrompt(null, getString(R.string.gestures_instructions));
937 mGesturesPrompt.getNextView().setClickable(false);
938 }
939
940 private void setGesturesNextPrompt(Drawable icon, CharSequence title) {
941 final TextView prompt = (TextView) mGesturesPrompt.getNextView();
942 prompt.setText(title);
943 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
944 prompt.setClickable(true);
945 mGesturesPrompt.showNext();
946 }
947
948 void hideGesturesPanel() {
949 if (mGesturesWindow != null) {
950 mGesturesWindow.setAnimationStyle(com.android.internal.R.style.Animation_SlidingCard);
951 mGesturesWindow.update();
952 mGesturesWindow.dismiss();
953 }
954 }
955
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 @Override
957 protected void onRestoreInstanceState(Bundle savedInstanceState) {
958 // Do not call super here
959 mSavedInstanceState = savedInstanceState;
960 }
961
962 @Override
963 protected void onSaveInstanceState(Bundle outState) {
964 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
965
966 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
967 if (folders.size() > 0) {
968 final int count = folders.size();
969 long[] ids = new long[count];
970 for (int i = 0; i < count; i++) {
971 final FolderInfo info = folders.get(i).getInfo();
972 ids[i] = info.id;
973 }
974 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
975 } else {
976 super.onSaveInstanceState(outState);
977 }
978
Romain Guy5a941392009-04-28 15:18:25 -0700979 // When the drawer is opened and we are saving the state because of a
980 // configuration change
981 if (mDrawer.isOpened() && getChangingConfigurations() != 0) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984
985 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
986 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
987 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
988
989 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
990 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
991 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
992 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
993 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
994 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
995 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
996 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
997 layout.getOccupiedCells());
998 }
999
1000 if (mFolderInfo != null && mWaitingForResult) {
1001 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1002 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1003 }
Romain Guy73b979d2009-06-09 12:57:21 -07001004
1005 if (mCurrentGesture != null && mWaitingForResult) {
1006 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 }
1009
1010 @Override
1011 public void onDestroy() {
1012 mDestroyed = true;
1013
1014 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001017 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001019 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 }
1021
1022 TextKeyListener.getInstance().release();
1023
1024 mAllAppsGrid.clearTextFilter();
1025 mAllAppsGrid.setAdapter(null);
1026 sModel.unbind();
1027 sModel.abortLoaders();
1028
1029 getContentResolver().unregisterContentObserver(mObserver);
1030 unregisterReceiver(mApplicationsReceiver);
1031 }
1032
1033 @Override
1034 public void startActivityForResult(Intent intent, int requestCode) {
1035 mWaitingForResult = true;
1036 super.startActivityForResult(intent, requestCode);
1037 }
1038
1039 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001040 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001042
1043 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001044
Karl Rosaen138a0412009-04-23 19:00:21 -07001045 // Slide the search widget to the top, if it's on the current screen,
1046 // otherwise show the search dialog immediately.
1047 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1048 if (searchWidget == null) {
1049 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1050 } else {
1051 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1052 // show the currently typed text in the search widget while sliding
1053 searchWidget.setQuery(getTypedText());
1054 }
1055 }
Romain Guycbb89e42009-06-08 15:52:54 -07001056
Karl Rosaen138a0412009-04-23 19:00:21 -07001057 /**
1058 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001059 *
1060 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001061 */
Romain Guycbb89e42009-06-08 15:52:54 -07001062 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001063 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001064
Karl Rosaen138a0412009-04-23 19:00:21 -07001065 if (initialQuery == null) {
1066 // Use any text typed in the launcher as the initial query
1067 initialQuery = getTypedText();
1068 clearTypedText();
1069 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 if (appSearchData == null) {
1071 appSearchData = new Bundle();
1072 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1073 }
Romain Guycbb89e42009-06-08 15:52:54 -07001074
Karl Rosaen138a0412009-04-23 19:00:21 -07001075 final SearchManager searchManager =
1076 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1077
1078 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1079 if (searchWidget != null) {
1080 // This gets called when the user leaves the search dialog to go back to
1081 // the Launcher.
1082 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1083 public void onCancel() {
1084 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001085 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001086 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001087 });
1088 }
Romain Guycbb89e42009-06-08 15:52:54 -07001089
Karl Rosaen138a0412009-04-23 19:00:21 -07001090 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001091 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 }
1093
Karl Rosaen138a0412009-04-23 19:00:21 -07001094 /**
1095 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001096 */
Romain Guy5a941392009-04-28 15:18:25 -07001097 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001098 // Close search dialog
1099 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1100 if (searchManager.isVisible()) {
1101 searchManager.stopSearch();
1102 }
1103 // Restore search widget to its normal position
1104 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1105 if (searchWidget != null) {
1106 searchWidget.stopSearch(false);
1107 }
1108 }
Romain Guycbb89e42009-06-08 15:52:54 -07001109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 @Override
1111 public boolean onCreateOptionsMenu(Menu menu) {
1112 if (mDesktopLocked) return false;
1113
1114 super.onCreateOptionsMenu(menu);
1115 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1116 .setIcon(android.R.drawable.ic_menu_add)
1117 .setAlphabeticShortcut('A');
1118 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1119 .setIcon(android.R.drawable.ic_menu_gallery)
1120 .setAlphabeticShortcut('W');
1121 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1122 .setIcon(android.R.drawable.ic_search_category_default)
1123 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1124 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1125 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1126 .setAlphabeticShortcut('N');
1127
Romain Guy73b979d2009-06-09 12:57:21 -07001128 final Intent gestures = new Intent(this, GesturesActivity.class);
1129 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1130 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1131 .setIntent(gestures);
1132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001134 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1135 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136
1137 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1138 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1139 .setIntent(settings);
1140
1141 return true;
1142 }
1143
1144 @Override
1145 public boolean onPrepareOptionsMenu(Menu menu) {
1146 super.onPrepareOptionsMenu(menu);
1147
1148 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1149 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1150
1151 return true;
1152 }
1153
1154 @Override
1155 public boolean onOptionsItemSelected(MenuItem item) {
1156 switch (item.getItemId()) {
1157 case MENU_ADD:
1158 addItems();
1159 return true;
1160 case MENU_WALLPAPER_SETTINGS:
1161 startWallpaper();
1162 return true;
1163 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001164 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 return true;
1166 case MENU_NOTIFICATIONS:
1167 showNotifications();
1168 return true;
1169 }
1170
1171 return super.onOptionsItemSelected(item);
1172 }
Romain Guycbb89e42009-06-08 15:52:54 -07001173
Karl Rosaen138a0412009-04-23 19:00:21 -07001174 /**
1175 * Indicates that we want global search for this activity by setting the globalSearch
1176 * argument for {@link #startSearch} to true.
1177 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001179 @Override
1180 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001181 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001182 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001183 }
1184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 private void addItems() {
1186 showAddDialog(mMenuAddInfo);
1187 }
1188
1189 private void removeShortcutsForPackage(String packageName) {
1190 if (packageName != null && packageName.length() > 0) {
1191 mWorkspace.removeShortcutsForPackage(packageName);
1192 }
1193 }
Romain Guycbb89e42009-06-08 15:52:54 -07001194
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001195 private void updateShortcutsForPackage(String packageName) {
1196 if (packageName != null && packageName.length() > 0) {
1197 mWorkspace.updateShortcutsForPackage(packageName);
1198 }
1199 }
1200
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001201 void addAppWidget(Intent data) {
1202 // TODO: catch bad widget exception when sent
1203 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001205 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1206 if (SEARCH_WIDGET.equals(customWidget)) {
1207 // We don't need this any more, since this isn't a real app widget.
1208 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1209 // add the search widget
1210 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001212 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1213
1214 if (appWidget.configure != null) {
1215 // Launch over to configure widget, if needed
1216 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1217 intent.setComponent(appWidget.configure);
1218 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1219
1220 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1221 } else {
1222 // Otherwise just add it
1223 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
1226 }
Romain Guycbb89e42009-06-08 15:52:54 -07001227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 void addSearch() {
1229 final Widget info = Widget.makeSearch();
1230 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 final int[] xy = mCellCoordinates;
1233 final int spanX = info.spanX;
1234 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 sModel.addDesktopItem(info);
1239 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1240 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 final View view = mInflater.inflate(info.layoutResource, null);
1243 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001244 Search search = (Search) view.findViewById(R.id.widget_search);
1245 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1248 }
1249
Romain Guy73b979d2009-06-09 12:57:21 -07001250 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001251 // Handle case where user selected "Applications"
1252 String applicationName = getResources().getString(R.string.group_applications);
1253 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001254
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001255 if (applicationName != null && applicationName.equals(shortcutName)) {
1256 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1257 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001258
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001259 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1260 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001261 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001262 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001263 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001264 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 }
1266
1267 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001268 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001269 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001270 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001271
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001272 if (folderName != null && folderName.equals(shortcutName)) {
1273 addFolder(!mDesktopLocked);
1274 } else {
1275 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1276 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
1278
1279 void addFolder(boolean insertAtFirst) {
1280 UserFolderInfo folderInfo = new UserFolderInfo();
1281 folderInfo.title = getText(R.string.folder_name);
1282
1283 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1284 cellInfo.screen = mWorkspace.getCurrentScreen();
1285 if (!findSingleSlot(cellInfo)) return;
1286
1287 // Update the model
1288 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1289 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1290 sModel.addDesktopItem(folderInfo);
1291 sModel.addFolder(folderInfo);
1292
1293 // Create the view
1294 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1295 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1296 mWorkspace.addInCurrentScreen(newFolder,
1297 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1298 }
Romain Guycbb89e42009-06-08 15:52:54 -07001299
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1301 boolean insertAtFirst) {
1302 cellInfo.screen = mWorkspace.getCurrentScreen();
1303 if (!findSingleSlot(cellInfo)) return;
1304
1305 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1306
1307 if (!mRestoring) {
1308 sModel.addDesktopItem(info);
1309
1310 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1311 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1312 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1313 } else if (sModel.isDesktopLoaded()) {
1314 sModel.addDesktopItem(info);
1315 }
1316 }
1317
1318 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1319 CellLayout.CellInfo cellInfo, boolean notify) {
1320
1321 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1322 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1323
1324 Drawable icon = null;
1325 boolean filtered = false;
1326 Intent.ShortcutIconResource iconResource = null;
1327
1328 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1329 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1330 try {
1331 iconResource = (Intent.ShortcutIconResource) extra;
1332 final PackageManager packageManager = context.getPackageManager();
1333 Resources resources = packageManager.getResourcesForApplication(
1334 iconResource.packageName);
1335 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1336 icon = resources.getDrawable(id);
1337 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001338 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 }
1340 }
1341
1342 if (icon == null) {
1343 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1344 }
1345
1346 final LiveFolderInfo info = new LiveFolderInfo();
1347 info.icon = icon;
1348 info.filtered = filtered;
1349 info.title = name;
1350 info.iconResource = iconResource;
1351 info.uri = data.getData();
1352 info.baseIntent = baseIntent;
1353 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1354 LiveFolders.DISPLAY_MODE_GRID);
1355
1356 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1357 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1358 sModel.addFolder(info);
1359
1360 return info;
1361 }
1362
1363 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1364 final int[] xy = new int[2];
1365 if (findSlot(cellInfo, xy, 1, 1)) {
1366 cellInfo.cellX = xy[0];
1367 cellInfo.cellY = xy[1];
1368 return true;
1369 }
1370 return false;
1371 }
1372
1373 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1374 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1375 boolean[] occupied = mSavedState != null ?
1376 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1377 cellInfo = mWorkspace.findAllVacantCells(occupied);
1378 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1379 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1380 return false;
1381 }
1382 }
1383 return true;
1384 }
1385
1386 private void showNotifications() {
1387 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1388 if (statusBar != null) {
1389 statusBar.expand();
1390 }
1391 }
1392
1393 private void startWallpaper() {
1394 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1395 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1396 }
1397
1398 /**
1399 * Registers various intent receivers. The current implementation registers
1400 * only a wallpaper intent receiver to let other applications change the
1401 * wallpaper.
1402 */
1403 private void registerIntentReceivers() {
1404 if (sWallpaperReceiver == null) {
1405 final Application application = getApplication();
1406
1407 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1408
1409 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1410 application.registerReceiver(sWallpaperReceiver, filter);
1411 } else {
1412 sWallpaperReceiver.setLauncher(this);
1413 }
1414
1415 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1416 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1417 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1418 filter.addDataScheme("package");
1419 registerReceiver(mApplicationsReceiver, filter);
1420 }
1421
1422 /**
1423 * Registers various content observers. The current implementation registers
1424 * only a favorites observer to keep track of the favorites applications.
1425 */
1426 private void registerContentObservers() {
1427 ContentResolver resolver = getContentResolver();
1428 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1429 }
1430
1431 @Override
1432 public boolean dispatchKeyEvent(KeyEvent event) {
1433 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1434 switch (event.getKeyCode()) {
1435 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001436 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001437 if (mDrawer.isOpened()) {
1438 closeDrawer();
1439 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001440 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001441 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 return true;
1443 case KeyEvent.KEYCODE_HOME:
1444 return true;
1445 }
1446 }
1447
1448 return super.dispatchKeyEvent(event);
1449 }
1450
1451 private void closeDrawer() {
1452 closeDrawer(true);
1453 }
1454
1455 private void closeDrawer(boolean animated) {
1456 if (mDrawer.isOpened()) {
1457 if (animated) {
1458 mDrawer.animateClose();
1459 } else {
1460 mDrawer.close();
1461 }
1462 if (mDrawer.hasFocus()) {
1463 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1464 }
1465 }
1466 }
1467
1468 private void closeFolder() {
1469 Folder folder = mWorkspace.getOpenFolder();
1470 if (folder != null) {
1471 closeFolder(folder);
1472 }
1473 }
1474
1475 void closeFolder(Folder folder) {
1476 folder.getInfo().opened = false;
1477 ViewGroup parent = (ViewGroup) folder.getParent();
1478 if (parent != null) {
1479 parent.removeView(folder);
1480 }
1481 folder.onClose();
1482 }
1483
1484 /**
1485 * When the notification that favorites have changed is received, requests
1486 * a favorites list refresh.
1487 */
1488 private void onFavoritesChanged() {
1489 mDesktopLocked = true;
1490 mDrawer.lock();
1491 sModel.loadUserItems(false, this, false, false);
1492 }
1493
1494 void onDesktopItemsLoaded() {
1495 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 bindDesktopItems();
1497 }
Romain Guycbb89e42009-06-08 15:52:54 -07001498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 /**
1500 * Refreshes the shortcuts shown on the workspace.
1501 */
1502 private void bindDesktopItems() {
1503 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001504 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001505 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1506 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 return;
1508 }
1509
1510 final Workspace workspace = mWorkspace;
1511 int count = workspace.getChildCount();
1512 for (int i = 0; i < count; i++) {
1513 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1514 }
Romain Guycbb89e42009-06-08 15:52:54 -07001515
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 if (DEBUG_USER_INTERFACE) {
1517 android.widget.Button finishButton = new android.widget.Button(this);
1518 finishButton.setText("Finish");
1519 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1520
1521 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1522 public void onClick(View v) {
1523 finish();
1524 }
1525 });
1526 }
Romain Guycbb89e42009-06-08 15:52:54 -07001527
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001528 // Flag any old binder to terminate early
1529 if (mBinder != null) {
1530 mBinder.mTerminate = true;
1531 }
Romain Guycbb89e42009-06-08 15:52:54 -07001532
Karl Rosaen138a0412009-04-23 19:00:21 -07001533 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001534 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 }
1536
1537 private void bindItems(Launcher.DesktopBinder binder,
1538 ArrayList<ItemInfo> shortcuts, int start, int count) {
1539
1540 final Workspace workspace = mWorkspace;
1541 final boolean desktopLocked = mDesktopLocked;
1542
1543 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1544 int i = start;
1545
1546 for ( ; i < end; i++) {
1547 final ItemInfo item = shortcuts.get(i);
1548 switch (item.itemType) {
1549 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1550 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1551 final View shortcut = createShortcut((ApplicationInfo) item);
1552 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1553 !desktopLocked);
1554 break;
1555 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1556 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1557 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1558 (UserFolderInfo) item);
1559 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1560 !desktopLocked);
1561 break;
1562 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1563 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1564 R.layout.live_folder_icon, this,
1565 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1566 (LiveFolderInfo) item);
1567 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1568 !desktopLocked);
1569 break;
1570 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1571 final int screen = workspace.getCurrentScreen();
1572 final View view = mInflater.inflate(R.layout.widget_search,
1573 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001574
Karl Rosaen138a0412009-04-23 19:00:21 -07001575 Search search = (Search) view.findViewById(R.id.widget_search);
1576 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 final Widget widget = (Widget) item;
1579 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001580
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 workspace.addWidget(view, widget, !desktopLocked);
1582 break;
1583 }
1584 }
1585
1586 workspace.requestLayout();
1587
1588 if (end >= count) {
1589 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001590 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 } else {
1592 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1593 }
1594 }
1595
1596 private void finishBindDesktopItems() {
1597 if (mSavedState != null) {
1598 if (!mWorkspace.hasFocus()) {
1599 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1600 }
1601
1602 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1603 if (userFolders != null) {
1604 for (long folderId : userFolders) {
1605 final FolderInfo info = sModel.findFolderById(folderId);
1606 if (info != null) {
1607 openFolder(info);
1608 }
1609 }
1610 final Folder openFolder = mWorkspace.getOpenFolder();
1611 if (openFolder != null) {
1612 openFolder.requestFocus();
1613 }
1614 }
1615
1616 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1617 if (allApps) {
1618 mDrawer.open();
1619 }
1620
1621 mSavedState = null;
1622 }
1623
1624 if (mSavedInstanceState != null) {
1625 super.onRestoreInstanceState(mSavedInstanceState);
1626 mSavedInstanceState = null;
1627 }
1628
1629 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1630 mDrawer.requestFocus();
1631 }
1632
1633 mDesktopLocked = false;
1634 mDrawer.unlock();
1635 }
Romain Guycbb89e42009-06-08 15:52:54 -07001636
Karl Rosaen138a0412009-04-23 19:00:21 -07001637 private void bindDrawer(Launcher.DesktopBinder binder,
1638 ApplicationsAdapter drawerAdapter) {
1639 mAllAppsGrid.setAdapter(drawerAdapter);
1640 binder.startBindingAppWidgetsWhenIdle();
1641 }
Romain Guycbb89e42009-06-08 15:52:54 -07001642
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001643 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001644 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001645
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 final Workspace workspace = mWorkspace;
1647 final boolean desktopLocked = mDesktopLocked;
1648
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001649 if (!appWidgets.isEmpty()) {
1650 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001651
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001652 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001653 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001654 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001655
Karl Rosaen138a0412009-04-23 19:00:21 -07001656 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001657
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001658 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1659 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001660
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001661 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1662 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001663
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001664 workspace.requestLayout();
1665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001667 if (appWidgets.isEmpty()) {
1668 if (PROFILE_ROTATE) {
1669 android.os.Debug.stopMethodTracing();
1670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001672 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
1674 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001675
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 DragController getDragController() {
1677 return mDragLayer;
1678 }
1679
1680 /**
1681 * Launches the intent referred by the clicked shortcut.
1682 *
1683 * @param v The view representing the clicked shortcut.
1684 */
1685 public void onClick(View v) {
1686 Object tag = v.getTag();
1687 if (tag instanceof ApplicationInfo) {
1688 // Open shortcut
1689 final Intent intent = ((ApplicationInfo) tag).intent;
1690 startActivitySafely(intent);
1691 } else if (tag instanceof FolderInfo) {
1692 handleFolderClick((FolderInfo) tag);
1693 }
1694 }
1695
1696 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001697 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1699 try {
1700 startActivity(intent);
1701 } catch (ActivityNotFoundException e) {
1702 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1703 } catch (SecurityException e) {
1704 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001705 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1707 "or use the exported attribute for this activity.", e);
1708 }
1709 }
1710
1711 private void handleFolderClick(FolderInfo folderInfo) {
1712 if (!folderInfo.opened) {
1713 // Close any open folder
1714 closeFolder();
1715 // Open the requested folder
1716 openFolder(folderInfo);
1717 } else {
1718 // Find the open folder...
1719 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1720 int folderScreen;
1721 if (openFolder != null) {
1722 folderScreen = mWorkspace.getScreenForView(openFolder);
1723 // .. and close it
1724 closeFolder(openFolder);
1725 if (folderScreen != mWorkspace.getCurrentScreen()) {
1726 // Close any folder open on the current screen
1727 closeFolder();
1728 // Pull the folder onto this screen
1729 openFolder(folderInfo);
1730 }
1731 }
1732 }
1733 }
1734
1735 private void loadWallpaper() {
1736 // The first time the application is started, we load the wallpaper from
1737 // the ApplicationContext
1738 if (sWallpaper == null) {
1739 final Drawable drawable = getWallpaper();
1740 if (drawable instanceof BitmapDrawable) {
1741 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1742 } else {
1743 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1744 }
1745 }
1746 mWorkspace.loadWallpaper(sWallpaper);
1747 }
1748
1749 /**
1750 * Opens the user fodler described by the specified tag. The opening of the folder
1751 * is animated relative to the specified View. If the View is null, no animation
1752 * is played.
1753 *
1754 * @param folderInfo The FolderInfo describing the folder to open.
1755 */
1756 private void openFolder(FolderInfo folderInfo) {
1757 Folder openFolder;
1758
1759 if (folderInfo instanceof UserFolderInfo) {
1760 openFolder = UserFolder.fromXml(this);
1761 } else if (folderInfo instanceof LiveFolderInfo) {
1762 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1763 } else {
1764 return;
1765 }
1766
1767 openFolder.setDragger(mDragLayer);
1768 openFolder.setLauncher(this);
1769
1770 openFolder.bind(folderInfo);
1771 folderInfo.opened = true;
1772
1773 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1774 openFolder.onOpen();
1775 }
1776
1777 /**
1778 * Returns true if the workspace is being loaded. When the workspace is loading,
1779 * no user interaction should be allowed to avoid any conflict.
1780 *
1781 * @return True if the workspace is locked, false otherwise.
1782 */
1783 boolean isWorkspaceLocked() {
1784 return mDesktopLocked;
1785 }
1786
1787 public boolean onLongClick(View v) {
1788 if (mDesktopLocked) {
1789 return false;
1790 }
1791
1792 if (!(v instanceof CellLayout)) {
1793 v = (View) v.getParent();
1794 }
1795
1796 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1797
1798 // This happens when long clicking an item with the dpad/trackball
1799 if (cellInfo == null) {
1800 return true;
1801 }
1802
1803 if (mWorkspace.allowLongPress()) {
1804 if (cellInfo.cell == null) {
1805 if (cellInfo.valid) {
1806 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001807 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 showAddDialog(cellInfo);
1809 }
1810 } else {
1811 if (!(cellInfo.cell instanceof Folder)) {
1812 // User long pressed on an item
1813 mWorkspace.startDrag(cellInfo);
1814 }
1815 }
1816 }
1817 return true;
1818 }
1819
1820 static LauncherModel getModel() {
1821 return sModel;
1822 }
1823
Romain Guy73b979d2009-06-09 12:57:21 -07001824 static GestureLibrary getGestureLibrary() {
1825 return sLibrary;
1826 }
1827
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 void closeAllApplications() {
1829 mDrawer.close();
1830 }
1831
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001832 View getDrawerHandle() {
1833 return mHandleView;
1834 }
1835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 boolean isDrawerDown() {
1837 return !mDrawer.isMoving() && !mDrawer.isOpened();
1838 }
1839
1840 boolean isDrawerUp() {
1841 return mDrawer.isOpened() && !mDrawer.isMoving();
1842 }
1843
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001844 boolean isDrawerMoving() {
1845 return mDrawer.isMoving();
1846 }
1847
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 Workspace getWorkspace() {
1849 return mWorkspace;
1850 }
1851
1852 GridView getApplicationsGrid() {
1853 return mAllAppsGrid;
1854 }
1855
1856 @Override
1857 protected Dialog onCreateDialog(int id) {
1858 switch (id) {
1859 case DIALOG_CREATE_SHORTCUT:
1860 return new CreateShortcut().createDialog();
1861 case DIALOG_RENAME_FOLDER:
1862 return new RenameFolder().createDialog();
1863 }
1864
1865 return super.onCreateDialog(id);
1866 }
1867
1868 @Override
1869 protected void onPrepareDialog(int id, Dialog dialog) {
1870 switch (id) {
1871 case DIALOG_CREATE_SHORTCUT:
1872 mWorkspace.lock();
1873 break;
1874 case DIALOG_RENAME_FOLDER:
1875 mWorkspace.lock();
1876 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1877 final CharSequence text = mFolderInfo.title;
1878 input.setText(text);
Romain Guycbb89e42009-06-08 15:52:54 -07001879 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 break;
1881 }
1882 }
1883
1884 void showRenameDialog(FolderInfo info) {
1885 mFolderInfo = info;
1886 mWaitingForResult = true;
1887 showDialog(DIALOG_RENAME_FOLDER);
1888 }
1889
1890 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1891 mAddItemCellInfo = cellInfo;
1892 mWaitingForResult = true;
1893 showDialog(DIALOG_CREATE_SHORTCUT);
1894 }
1895
Romain Guy73b979d2009-06-09 12:57:21 -07001896 private void pickShortcut(int requestCode, int title) {
1897 Bundle bundle = new Bundle();
1898
1899 ArrayList<String> shortcutNames = new ArrayList<String>();
1900 shortcutNames.add(getString(R.string.group_applications));
1901 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1902
1903 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1904 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1905 R.drawable.ic_launcher_application));
1906 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1907
1908 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1909 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1910 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1911 pickIntent.putExtras(bundle);
1912
1913 startActivityForResult(pickIntent, requestCode);
1914 }
1915
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 private class RenameFolder {
1917 private EditText mInput;
1918
1919 Dialog createDialog() {
1920 mWaitingForResult = true;
1921 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1922 mInput = (EditText) layout.findViewById(R.id.folder_name);
1923
1924 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1925 builder.setIcon(0);
1926 builder.setTitle(getString(R.string.rename_folder_title));
1927 builder.setCancelable(true);
1928 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1929 public void onCancel(DialogInterface dialog) {
1930 cleanup();
1931 }
1932 });
1933 builder.setNegativeButton(getString(R.string.cancel_action),
1934 new Dialog.OnClickListener() {
1935 public void onClick(DialogInterface dialog, int which) {
1936 cleanup();
1937 }
1938 }
1939 );
1940 builder.setPositiveButton(getString(R.string.rename_action),
1941 new Dialog.OnClickListener() {
1942 public void onClick(DialogInterface dialog, int which) {
1943 changeFolderName();
1944 }
1945 }
1946 );
1947 builder.setView(layout);
1948 return builder.create();
1949 }
1950
1951 private void changeFolderName() {
1952 final String name = mInput.getText().toString();
1953 if (!TextUtils.isEmpty(name)) {
1954 // Make sure we have the right folder info
1955 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1956 mFolderInfo.title = name;
1957 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1958
1959 if (mDesktopLocked) {
1960 mDrawer.lock();
1961 sModel.loadUserItems(false, Launcher.this, false, false);
1962 } else {
1963 final FolderIcon folderIcon = (FolderIcon)
1964 mWorkspace.getViewForTag(mFolderInfo);
1965 if (folderIcon != null) {
1966 folderIcon.setText(name);
1967 getWorkspace().requestLayout();
1968 } else {
1969 mDesktopLocked = true;
1970 mDrawer.lock();
1971 sModel.loadUserItems(false, Launcher.this, false, false);
1972 }
1973 }
1974 }
1975 cleanup();
1976 }
1977
1978 private void cleanup() {
1979 mWorkspace.unlock();
1980 dismissDialog(DIALOG_RENAME_FOLDER);
1981 mWaitingForResult = false;
1982 mFolderInfo = null;
1983 }
1984 }
1985
1986 /**
1987 * Displays the shortcut creation dialog and launches, if necessary, the
1988 * appropriate activity.
1989 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001990 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07001991 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001993
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 Dialog createDialog() {
1995 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001996
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2000 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002001 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 builder.setInverseBackgroundForced(true);
2004
2005 AlertDialog dialog = builder.create();
2006 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002007 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 return dialog;
2010 }
2011
2012 public void onCancel(DialogInterface dialog) {
2013 mWaitingForResult = false;
2014 cleanup();
2015 }
2016
Romain Guycbb89e42009-06-08 15:52:54 -07002017 public void onDismiss(DialogInterface dialog) {
2018 mWorkspace.unlock();
2019 }
2020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 private void cleanup() {
2022 mWorkspace.unlock();
2023 dismissDialog(DIALOG_CREATE_SHORTCUT);
2024 }
2025
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002026 /**
2027 * Handle the action clicked in the "Add to home" dialog.
2028 */
2029 public void onClick(DialogInterface dialog, int which) {
2030 Resources res = getResources();
2031 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002032
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002033 switch (which) {
2034 case AddAdapter.ITEM_SHORTCUT: {
2035 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002036 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002037 break;
2038 }
Romain Guycbb89e42009-06-08 15:52:54 -07002039
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002040 case AddAdapter.ITEM_APPWIDGET: {
2041 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002042
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002043 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2044 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2045 // add the search widget
2046 ArrayList<AppWidgetProviderInfo> customInfo =
2047 new ArrayList<AppWidgetProviderInfo>();
2048 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2049 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2050 info.label = getString(R.string.group_search);
2051 info.icon = R.drawable.ic_search_widget;
2052 customInfo.add(info);
2053 pickIntent.putParcelableArrayListExtra(
2054 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2055 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2056 Bundle b = new Bundle();
2057 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2058 customExtras.add(b);
2059 pickIntent.putParcelableArrayListExtra(
2060 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2061 // start the pick activity
2062 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2063 break;
2064 }
Romain Guycbb89e42009-06-08 15:52:54 -07002065
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002066 case AddAdapter.ITEM_LIVE_FOLDER: {
2067 // Insert extra item to handle inserting folder
2068 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002069
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002070 ArrayList<String> shortcutNames = new ArrayList<String>();
2071 shortcutNames.add(res.getString(R.string.group_folder));
2072 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002073
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002074 ArrayList<ShortcutIconResource> shortcutIcons =
2075 new ArrayList<ShortcutIconResource>();
2076 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2077 R.drawable.ic_launcher_folder));
2078 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2079
2080 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2081 pickIntent.putExtra(Intent.EXTRA_INTENT,
2082 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2083 pickIntent.putExtra(Intent.EXTRA_TITLE,
2084 getText(R.string.title_select_live_folder));
2085 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002086
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002087 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2088 break;
2089 }
2090
2091 case AddAdapter.ITEM_WALLPAPER: {
2092 startWallpaper();
2093 break;
2094 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 }
2096 }
2097 }
2098
2099 /**
2100 * Receives notifications when applications are added/removed.
2101 */
2102 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2103 @Override
2104 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002105 final String action = intent.getAction();
2106 final String packageName = intent.getData().getSchemeSpecificPart();
2107 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2108
2109 if (LauncherModel.DEBUG_LOADERS) {
2110 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2111 ", replacing=" + replacing);
2112 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002114
2115 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2116 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2117 if (!replacing) {
2118 removeShortcutsForPackage(packageName);
2119 if (LauncherModel.DEBUG_LOADERS) {
2120 d(LauncherModel.LOG_TAG, " --> remove package");
2121 }
2122 sModel.removePackage(Launcher.this, packageName);
2123 }
2124 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2125 // later, we will update the package at this time
2126 } else {
2127 if (!replacing) {
2128 if (LauncherModel.DEBUG_LOADERS) {
2129 d(LauncherModel.LOG_TAG, " --> add package");
2130 }
2131 sModel.addPackage(Launcher.this, packageName);
2132 } else {
2133 if (LauncherModel.DEBUG_LOADERS) {
2134 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2135 }
2136 sModel.updatePackage(Launcher.this, packageName);
2137 updateShortcutsForPackage(packageName);
2138 }
2139 }
2140 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002142 if (LauncherModel.DEBUG_LOADERS) {
2143 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2144 }
2145 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 }
2147 }
2148 }
2149
2150 /**
2151 * Receives notifications whenever the user favorites have changed.
2152 */
2153 private class FavoritesChangeObserver extends ContentObserver {
2154 public FavoritesChangeObserver() {
2155 super(new Handler());
2156 }
2157
2158 @Override
2159 public void onChange(boolean selfChange) {
2160 onFavoritesChanged();
2161 }
2162 }
2163
2164 /**
2165 * Receives intents from other applications to change the wallpaper.
2166 */
2167 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2168 private final Application mApplication;
2169 private WeakReference<Launcher> mLauncher;
2170
2171 WallpaperIntentReceiver(Application application, Launcher launcher) {
2172 mApplication = application;
2173 setLauncher(launcher);
2174 }
2175
2176 void setLauncher(Launcher launcher) {
2177 mLauncher = new WeakReference<Launcher>(launcher);
2178 }
2179
2180 @Override
2181 public void onReceive(Context context, Intent intent) {
2182 // Load the wallpaper from the ApplicationContext and store it locally
2183 // until the Launcher Activity is ready to use it
2184 final Drawable drawable = mApplication.getWallpaper();
2185 if (drawable instanceof BitmapDrawable) {
2186 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2187 } else {
2188 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2189 }
2190
2191 // If Launcher is alive, notify we have a new wallpaper
2192 if (mLauncher != null) {
2193 final Launcher launcher = mLauncher.get();
2194 if (launcher != null) {
2195 launcher.loadWallpaper();
2196 }
2197 }
2198 }
2199 }
2200
2201 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2202 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2203 private boolean mOpen;
2204
2205 public void onDrawerOpened() {
2206 if (!mOpen) {
2207 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002210 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211
2212 mOpen = true;
2213 }
2214 }
2215
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002216 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2217 view.getDrawingRect(bounds);
2218
2219 while (view != mDragLayer) {
2220 bounds.offset(view.getLeft(), view.getTop());
2221 view = (View) view.getParent();
2222 }
2223 }
2224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225 public void onDrawerClosed() {
2226 if (mOpen) {
2227 mHandleIcon.reverseTransition(150);
2228 mWorkspace.mDrawerBounds.setEmpty();
2229 mOpen = false;
2230 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 mAllAppsGrid.setSelection(0);
2233 mAllAppsGrid.clearTextFilter();
2234 }
2235
2236 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002237 if (PROFILE_DRAWER) {
2238 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2239 }
2240
2241 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2242 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 }
2244
2245 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002246 if (PROFILE_DRAWER) {
2247 android.os.Debug.stopMethodTracing();
2248 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 }
2250 }
2251
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002252 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002254 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002255 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 // Number of items to bind in every pass
2258 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259
2260 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002261 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002262 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002264
Karl Rosaen138a0412009-04-23 19:00:21 -07002265 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266
2267 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002268 ArrayList<LauncherAppWidgetInfo> appWidgets,
2269 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002270
2271 mLauncher = new WeakReference<Launcher>(launcher);
2272 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002273 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002274
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002275 // Sort widgets so active workspace is bound first
2276 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2277 final int size = appWidgets.size();
2278 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002279
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002280 for (int i = 0; i < size; i++) {
2281 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2282 if (appWidgetInfo.screen == currentScreen) {
2283 mAppWidgets.addFirst(appWidgetInfo);
2284 } else {
2285 mAppWidgets.addLast(appWidgetInfo);
2286 }
2287 }
2288 }
Romain Guycbb89e42009-06-08 15:52:54 -07002289
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 public void startBindingItems() {
2291 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2292 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002293
2294 public void startBindingDrawer() {
2295 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2296 }
Romain Guycbb89e42009-06-08 15:52:54 -07002297
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002298 public void startBindingAppWidgetsWhenIdle() {
2299 // Ask for notification when message queue becomes idle
2300 final MessageQueue messageQueue = Looper.myQueue();
2301 messageQueue.addIdleHandler(this);
2302 }
Romain Guycbb89e42009-06-08 15:52:54 -07002303
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002304 public boolean queueIdle() {
2305 // Queue is idle, so start binding items
2306 startBindingAppWidgets();
2307 return false;
2308 }
2309
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002310 public void startBindingAppWidgets() {
2311 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2312 }
2313
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002314 @Override
2315 public void handleMessage(Message msg) {
2316 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002317 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002318 return;
2319 }
Romain Guycbb89e42009-06-08 15:52:54 -07002320
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 switch (msg.what) {
2322 case MESSAGE_BIND_ITEMS: {
2323 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2324 break;
2325 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002326 case MESSAGE_BIND_DRAWER: {
2327 launcher.bindDrawer(this, mDrawerAdapter);
2328 break;
2329 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002330 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002331 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 break;
2333 }
2334 }
2335 }
2336 }
Romain Guy73b979d2009-06-09 12:57:21 -07002337
2338 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2339 GestureOverlayView.OnGesturePerformedListener {
2340
2341 private final GestureMatcher mMatcher = new GestureMatcher();
2342
2343 GesturesProcessor() {
2344 // TODO: Maybe the load should happen on a background thread?
2345 sLibrary.load();
2346 }
2347
2348 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
2349 overlay.removeCallbacks(mMatcher);
2350 resetGesturesNextPrompt();
2351
2352 mGesturesAdd.setAlpha(128);
2353 mGesturesAdd.setEnabled(false);
2354 }
2355
2356 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2357 }
2358
2359 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2360 }
2361
2362 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
2363 overlay.removeCallbacks(mMatcher);
2364
2365 mMatcher.gesture = overlay.getGesture();
2366 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2367 overlay.clear(false);
2368 } else {
2369 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2370 }
2371 }
2372
2373 private void matchGesture(Gesture gesture) {
2374 mGesturesAdd.setAlpha(255);
2375 mGesturesAdd.setEnabled(true);
2376
2377 if (gesture != null) {
2378 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2379
2380 if (DEBUG_GESTURES) {
2381 for (Prediction p : predictions) {
2382 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2383 }
2384 }
2385
2386 boolean match = false;
2387 if (predictions.size() > 0) {
2388 final Prediction prediction = predictions.get(0);
2389 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2390 match = true;
2391
2392 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2393 if (info != null) {
2394 updatePrompt(info);
2395 }
2396 }
2397 }
2398
2399 if (!match){
2400 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2401 }
2402 }
2403 }
2404
2405 private void updatePrompt(ApplicationInfo info) {
2406 setGesturesNextPrompt(info.icon, info.title);
2407 mGesturesAction.intent = info.intent;
2408 }
2409
2410 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
2411 overlay.removeCallbacks(mMatcher);
2412 }
2413
2414 void addGesture(String name, Gesture gesture) {
2415 sLibrary.addGesture(name, gesture);
2416 // TODO: On a background thread?
2417 sLibrary.save();
2418 }
2419
2420 void update(ApplicationInfo info, Gesture gesture) {
2421 mGesturesOverlay.setGesture(gesture);
2422 updatePrompt(info);
2423 }
2424
2425 class GestureMatcher implements Runnable {
2426 Gesture gesture;
2427
2428 public void run() {
2429 if (gesture != null) {
2430 matchGesture(gesture);
2431 }
2432 }
2433 }
2434 }
2435
2436 private class GesturesAction implements View.OnClickListener {
2437 Intent intent;
2438
2439 public void onClick(View v) {
2440 if (intent != null) {
2441 startActivitySafely(intent);
2442 }
2443 }
2444 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002445}
Karl Rosaen138a0412009-04-23 19:00:21 -07002446