blob: 2e728187495f5de905e52894fd2eef438e5fa9fa [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
Romain Guy6fefcf12009-06-11 13:07:43 -0700108 private static final boolean CONFIG_GESTURES_IMMEDIATE_MODE = true;
109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 private static final int WALLPAPER_SCREENS_SPAN = 2;
111
112 private static final int MENU_GROUP_ADD = 1;
113 private static final int MENU_ADD = Menu.FIRST + 1;
114 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
115 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
116 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy73b979d2009-06-09 12:57:21 -0700117 private static final int MENU_GESTURES = MENU_NOTIFICATIONS + 1;
118 private static final int MENU_SETTINGS = MENU_GESTURES + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
120 private static final int REQUEST_CREATE_SHORTCUT = 1;
121 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700122 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int REQUEST_PICK_APPLICATION = 6;
124 private static final int REQUEST_PICK_SHORTCUT = 7;
125 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700126 private static final int REQUEST_PICK_APPWIDGET = 9;
Romain Guy73b979d2009-06-09 12:57:21 -0700127 private static final int REQUEST_PICK_GESTURE_ACTION = 10;
128 private static final int REQUEST_CREATE_GESTURE_ACTION = 11;
129 private static final int REQUEST_CREATE_GESTURE_APPLICATION_ACTION = 12;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
131 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
132
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700133 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
134 static final String SEARCH_WIDGET = "search_widget";
135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 static final int SCREEN_COUNT = 3;
137 static final int DEFAULT_SCREN = 1;
138 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700139 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
141 private static final int DIALOG_CREATE_SHORTCUT = 1;
Romain Guycbb89e42009-06-08 15:52:54 -0700142 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
144 private static final String PREFERENCES = "launcher";
145 private static final String KEY_LOCALE = "locale";
146 private static final String KEY_MCC = "mcc";
147 private static final String KEY_MNC = "mnc";
148
149 // Type: int
150 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
151 // Type: boolean
152 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
153 // Type: long
154 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
169 // Type: int[]
170 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
171 // Type: boolean
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
173 // Type: long
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Romain Guy73b979d2009-06-09 12:57:21 -0700175 // Type: Gesture (Parcelable)
176 private static final String RUNTIME_STATE_PENDING_GESTURE = "launcher.gesture";
Romain Guy3cf604f2009-06-16 13:12:53 -0700177 // Type: boolean
178 private static final String RUNTIME_STATE_GESTURES_PANEL = "launcher.gesture_panel_showing";
179 // Type: Gesture (Parcelable)
180 private static final String RUNTIME_STATE_GESTURES_PANEL_GESTURE = "launcher.gesture_panel_gesture";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700182 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
184 private static Bitmap sWallpaper;
185
186 private static final Object sLock = new Object();
187 private static int sScreen = DEFAULT_SCREN;
188
189 private static WallpaperIntentReceiver sWallpaperReceiver;
190
Romain Guy73b979d2009-06-09 12:57:21 -0700191 private static GestureLibrary sLibrary;
192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
194 private final ContentObserver mObserver = new FavoritesChangeObserver();
195
196 private LayoutInflater mInflater;
197
198 private DragLayer mDragLayer;
199 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700200
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700201 private AppWidgetManager mAppWidgetManager;
202 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private CellLayout.CellInfo mAddItemCellInfo;
207 private CellLayout.CellInfo mMenuAddInfo;
208 private final int[] mCellCoordinates = new int[2];
209 private FolderInfo mFolderInfo;
210
211 private SlidingDrawer mDrawer;
212 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 private HandleView mHandleView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 private AllAppsGridView mAllAppsGrid;
215
216 private boolean mDesktopLocked = true;
217 private Bundle mSavedState;
218
219 private SpannableStringBuilder mDefaultKeySsb = null;
220
221 private boolean mDestroyed;
222
223 private boolean mRestoring;
224 private boolean mWaitingForResult;
225 private boolean mLocaleChanged;
226
227 private Bundle mSavedInstanceState;
228
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700229 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Romain Guy73b979d2009-06-09 12:57:21 -0700231 private View mGesturesPanel;
232 private GestureOverlayView mGesturesOverlay;
233 private ViewSwitcher mGesturesPrompt;
234 private ImageView mGesturesAdd;
235 private PopupWindow mGesturesWindow;
236 private Launcher.GesturesProcessor mGesturesProcessor;
237 private Gesture mCurrentGesture;
238 private GesturesAction mGesturesAction;
Romain Guyaad5ef42009-06-10 02:48:37 -0700239 private boolean mHideGesturesPanel;
Romain Guy73b979d2009-06-09 12:57:21 -0700240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 @Override
242 protected void onCreate(Bundle savedInstanceState) {
243 super.onCreate(savedInstanceState);
244 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700245
Romain Guy73b979d2009-06-09 12:57:21 -0700246 if (sLibrary == null) {
247 // The context is not kept by the library so it's safe to do this
248 sLibrary = GestureLibraries.fromPrivateFile(Launcher.this,
249 GesturesConstants.STORE_NAME);
250 }
251
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700252 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700253
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700254 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
255 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 if (PROFILE_STARTUP) {
258 android.os.Debug.startMethodTracing("/sdcard/launcher");
259 }
260
261 checkForLocaleChange();
262 setWallpaperDimension();
263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 setContentView(R.layout.launcher);
265 setupViews();
266
267 registerIntentReceivers();
268 registerContentObservers();
269
270 mSavedState = savedInstanceState;
271 restoreState(mSavedState);
272
273 if (PROFILE_STARTUP) {
274 android.os.Debug.stopMethodTracing();
275 }
276
277 if (!mRestoring) {
278 startLoaders();
279 }
280
281 // For handling default keys
282 mDefaultKeySsb = new SpannableStringBuilder();
283 Selection.setSelection(mDefaultKeySsb, 0);
284 }
Romain Guycbb89e42009-06-08 15:52:54 -0700285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 private void checkForLocaleChange() {
287 final SharedPreferences preferences = getSharedPreferences(PREFERENCES, MODE_PRIVATE);
288 final Configuration configuration = getResources().getConfiguration();
289
290 final String previousLocale = preferences.getString(KEY_LOCALE, null);
291 final String locale = configuration.locale.toString();
292
293 final int previousMcc = preferences.getInt(KEY_MCC, -1);
294 final int mcc = configuration.mcc;
295
296 final int previousMnc = preferences.getInt(KEY_MNC, -1);
297 final int mnc = configuration.mnc;
298
299 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
300
301 if (mLocaleChanged) {
302 final SharedPreferences.Editor editor = preferences.edit();
303 editor.putString(KEY_LOCALE, locale);
304 editor.putInt(KEY_MCC, mcc);
305 editor.putInt(KEY_MNC, mnc);
306 editor.commit();
307 }
308 }
309
310 static int getScreen() {
311 synchronized (sLock) {
312 return sScreen;
313 }
314 }
315
316 static void setScreen(int screen) {
317 synchronized (sLock) {
318 sScreen = screen;
319 }
320 }
321
322 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700323 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
324 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
325
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 mRestoring = false;
327 }
328
329 private void setWallpaperDimension() {
330 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
331 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
332
333 Display display = getWindowManager().getDefaultDisplay();
334 boolean isPortrait = display.getWidth() < display.getHeight();
335
336 final int width = isPortrait ? display.getWidth() : display.getHeight();
337 final int height = isPortrait ? display.getHeight() : display.getWidth();
338 try {
339 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
340 } catch (RemoteException e) {
341 // System is dead!
342 }
343 }
344
345 @Override
346 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700347 mWaitingForResult = false;
348
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 // The pattern used here is that a user PICKs a specific application,
350 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
353 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700354
Romain Guy73b979d2009-06-09 12:57:21 -0700355 if (resultCode == RESULT_OK && (mAddItemCellInfo != null ||
356 ((requestCode == REQUEST_PICK_GESTURE_ACTION ||
357 requestCode == REQUEST_CREATE_GESTURE_ACTION ||
358 requestCode == REQUEST_CREATE_GESTURE_APPLICATION_ACTION) && mCurrentGesture != null))) {
359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 switch (requestCode) {
361 case REQUEST_PICK_APPLICATION:
362 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
363 break;
364 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700365 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 break;
367 case REQUEST_CREATE_SHORTCUT:
368 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
369 break;
370 case REQUEST_PICK_LIVE_FOLDER:
371 addLiveFolder(data);
372 break;
373 case REQUEST_CREATE_LIVE_FOLDER:
374 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
375 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700376 case REQUEST_PICK_APPWIDGET:
377 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700379 case REQUEST_CREATE_APPWIDGET:
380 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 break;
Romain Guy73b979d2009-06-09 12:57:21 -0700382 case REQUEST_PICK_GESTURE_ACTION:
383 processShortcut(data, REQUEST_CREATE_GESTURE_APPLICATION_ACTION,
384 REQUEST_CREATE_GESTURE_ACTION);
385 break;
386 case REQUEST_CREATE_GESTURE_ACTION:
387 completeCreateGesture(data, true);
388 break;
389 case REQUEST_CREATE_GESTURE_APPLICATION_ACTION:
390 completeCreateGesture(data, false);
391 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700393 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700395 // Clean up the appWidgetId if we canceled
396 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
397 if (appWidgetId != -1) {
398 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 }
400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
402
403 @Override
404 protected void onResume() {
405 super.onResume();
406
407 if (mRestoring) {
408 startLoaders();
409 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700410
411 // Make sure that the search gadget (if any) is in its normal place.
Romain Guy5a941392009-04-28 15:18:25 -0700412 stopSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 }
414
415 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700416 protected void onPause() {
417 super.onPause();
Romain Guy73b979d2009-06-09 12:57:21 -0700418 if (mGesturesWindow != null) {
419 mGesturesWindow.setAnimationStyle(0);
420 mGesturesWindow.update();
421 }
Romain Guycbb89e42009-06-08 15:52:54 -0700422 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700423 }
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700425 @Override
Romain Guy73b979d2009-06-09 12:57:21 -0700426 protected void onStop() {
427 super.onStop();
Romain Guyaad5ef42009-06-10 02:48:37 -0700428 if (mHideGesturesPanel) {
429 mHideGesturesPanel = false;
430 hideGesturesPanel();
431 }
Romain Guy73b979d2009-06-09 12:57:21 -0700432 }
433
434 @Override
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700435 public Object onRetainNonConfigurationInstance() {
436 // Flag any binder to stop early before switching
437 if (mBinder != null) {
438 mBinder.mTerminate = true;
439 }
Romain Guycbb89e42009-06-08 15:52:54 -0700440
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700441 if (PROFILE_ROTATE) {
442 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
443 }
444 return null;
445 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 private boolean acceptFilter() {
448 final InputMethodManager inputManager = (InputMethodManager)
449 getSystemService(Context.INPUT_METHOD_SERVICE);
450 return !inputManager.isFullscreenMode();
451 }
452
453 @Override
454 public boolean onKeyDown(int keyCode, KeyEvent event) {
455 boolean handled = super.onKeyDown(keyCode, event);
456 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
457 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
458 keyCode, event);
459 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700460 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700461 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700462 // showSearchDialog()
463 // If there are multiple keystrokes before the search dialog takes focus,
464 // onSearchRequested() will be called for every keystroke,
465 // but it is idempotent, so it's fine.
466 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468 }
469
470 return handled;
471 }
472
Karl Rosaen138a0412009-04-23 19:00:21 -0700473 private String getTypedText() {
474 return mDefaultKeySsb.toString();
475 }
476
477 private void clearTypedText() {
478 mDefaultKeySsb.clear();
479 mDefaultKeySsb.clearSpans();
480 Selection.setSelection(mDefaultKeySsb, 0);
481 }
482
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 /**
484 * Restores the previous state, if it exists.
485 *
486 * @param savedState The previous state.
487 */
488 private void restoreState(Bundle savedState) {
489 if (savedState == null) {
490 return;
491 }
492
493 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
494 if (currentScreen > -1) {
495 mWorkspace.setCurrentScreen(currentScreen);
496 }
497
498 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
499 if (addScreen > -1) {
500 mAddItemCellInfo = new CellLayout.CellInfo();
501 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
502 addItemCellInfo.valid = true;
503 addItemCellInfo.screen = addScreen;
504 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
505 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
506 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
507 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
508 addItemCellInfo.findVacantCellsFromOccupied(
509 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
510 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
511 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
512 mRestoring = true;
513 }
514
515 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
516 if (renameFolder) {
517 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
518 mFolderInfo = sModel.getFolderById(this, id);
519 mRestoring = true;
520 }
Romain Guy73b979d2009-06-09 12:57:21 -0700521
Romain Guy3cf604f2009-06-16 13:12:53 -0700522 boolean gesturesShowing = savedState.getBoolean(RUNTIME_STATE_GESTURES_PANEL, false);
523 if (gesturesShowing) {
524 final Gesture gesture = (Gesture) savedState.get(RUNTIME_STATE_GESTURES_PANEL_GESTURE);
525 mWorkspace.post(new Runnable() {
526 public void run() {
527 showGesturesPanel(false);
528 mGesturesProcessor.matchGesture(gesture, false);
529 mWorkspace.post(new Runnable() {
530 public void run() {
531 if (gesture != null) {
532 mGesturesOverlay.setGesture(gesture);
533 }
534 }
535 });
536 }
537 });
538 }
539
Romain Guy73b979d2009-06-09 12:57:21 -0700540 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_PENDING_GESTURE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 }
542
543 /**
544 * Finds all the views we need and configure them properly.
545 */
546 private void setupViews() {
547 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
548 final DragLayer dragLayer = mDragLayer;
549
550 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
551 final Workspace workspace = mWorkspace;
552
553 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
554 final SlidingDrawer drawer = mDrawer;
555
556 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
557 final AllAppsGridView grid = mAllAppsGrid;
558
559 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
560
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700561 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
562 mHandleView.setLauncher(this);
563 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 mHandleIcon.setCrossFadeEnabled(true);
565
566 drawer.lock();
567 final DrawerManager drawerManager = new DrawerManager();
568 drawer.setOnDrawerOpenListener(drawerManager);
569 drawer.setOnDrawerCloseListener(drawerManager);
570 drawer.setOnDrawerScrollListener(drawerManager);
571
Karl Rosaen138a0412009-04-23 19:00:21 -0700572 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 grid.setDragger(dragLayer);
574 grid.setLauncher(this);
575
576 workspace.setOnLongClickListener(this);
577 workspace.setDragger(dragLayer);
578 workspace.setLauncher(this);
579 loadWallpaper();
580
581 deleteZone.setLauncher(this);
582 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700583 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584
585 dragLayer.setIgnoredDropTarget(grid);
586 dragLayer.setDragScoller(workspace);
587 dragLayer.setDragListener(deleteZone);
Romain Guy73b979d2009-06-09 12:57:21 -0700588
589 mGesturesPanel = mInflater.inflate(R.layout.gestures, mDragLayer, false);
590 final View gesturesPanel = mGesturesPanel;
591
592 mGesturesPrompt = (ViewSwitcher) gesturesPanel.findViewById(R.id.gestures_actions);
593 mGesturesAction = new GesturesAction();
594
595 mGesturesPrompt.getChildAt(0).setOnClickListener(mGesturesAction);
596 mGesturesPrompt.getChildAt(1).setOnClickListener(mGesturesAction);
597
598 mGesturesAdd = (ImageView) gesturesPanel.findViewById(R.id.gestures_add);
599 final ImageView gesturesAdd = mGesturesAdd;
600 gesturesAdd.setAlpha(128);
601 gesturesAdd.setEnabled(false);
602 gesturesAdd.setOnClickListener(new View.OnClickListener() {
603 public void onClick(View v) {
604 createGesture();
605 }
606 });
607
608 mGesturesOverlay = (GestureOverlayView) gesturesPanel.findViewById(R.id.gestures_overlay);
609 mGesturesProcessor = new GesturesProcessor();
610
611 final GestureOverlayView overlay = mGesturesOverlay;
Romain Guy73b979d2009-06-09 12:57:21 -0700612 overlay.addOnGestureListener(mGesturesProcessor);
Romain Guyb8734242009-06-10 11:53:57 -0700613 overlay.getGesturePaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
Romain Guy73b979d2009-06-09 12:57:21 -0700614 }
615
616 private void createGesture() {
617 mCurrentGesture = mGesturesOverlay.getGesture();
618 mWaitingForResult = true;
619 pickShortcut(REQUEST_PICK_GESTURE_ACTION, R.string.title_select_shortcut);
620 }
621
622 private void completeCreateGesture(Intent data, boolean isShortcut) {
623 ApplicationInfo info;
624
625 if (isShortcut) {
626 info = infoFromShortcutIntent(this, data);
627 } else {
628 info = infoFromApplicationIntent(this, data);
629 }
630
631 boolean success = false;
632 if (info != null) {
633 info.isGesture = true;
634
635 if (LauncherModel.addGestureToDatabase(this, info, false)) {
636 mGesturesProcessor.addGesture(String.valueOf(info.id), mCurrentGesture);
637 mGesturesProcessor.update(info, mCurrentGesture);
638 Toast.makeText(this, getString(R.string.gestures_created, info.title),
639 Toast.LENGTH_SHORT).show();
640 success = true;
641 }
642 }
643
644 if (!success) {
645 Toast.makeText(this, getString(R.string.gestures_failed), Toast.LENGTH_SHORT).show();
646 }
647
648 mCurrentGesture = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
650
651 /**
652 * Creates a view representing a shortcut.
653 *
654 * @param info The data structure describing the shortcut.
655 *
656 * @return A View inflated from R.layout.application.
657 */
658 View createShortcut(ApplicationInfo info) {
659 return createShortcut(R.layout.application,
660 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
661 }
662
663 /**
664 * Creates a view representing a shortcut inflated from the specified resource.
665 *
666 * @param layoutResId The id of the XML layout used to create the shortcut.
667 * @param parent The group the shortcut belongs to.
668 * @param info The data structure describing the shortcut.
669 *
670 * @return A View inflated from layoutResId.
671 */
672 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
673 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
674
675 if (!info.filtered) {
676 info.icon = Utilities.createIconThumbnail(info.icon, this);
677 info.filtered = true;
678 }
679
680 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
681 favorite.setText(info.title);
682 favorite.setTag(info);
683 favorite.setOnClickListener(this);
684
685 return favorite;
686 }
687
688 /**
689 * Add an application shortcut to the workspace.
690 *
691 * @param data The intent describing the application.
692 * @param cellInfo The position on screen where to create the shortcut.
693 */
694 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
695 boolean insertAtFirst) {
696 cellInfo.screen = mWorkspace.getCurrentScreen();
697 if (!findSingleSlot(cellInfo)) return;
698
Romain Guy73b979d2009-06-09 12:57:21 -0700699 final ApplicationInfo info = infoFromApplicationIntent(context, data);
700 if (info != null) {
701 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
702 }
703 }
704
705 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 ComponentName component = data.getComponent();
707 PackageManager packageManager = context.getPackageManager();
708 ActivityInfo activityInfo = null;
709 try {
710 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
711 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700712 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
Romain Guycbb89e42009-06-08 15:52:54 -0700714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 if (activityInfo != null) {
716 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700717
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 itemInfo.title = activityInfo.loadLabel(packageManager);
719 if (itemInfo.title == null) {
720 itemInfo.title = activityInfo.name;
721 }
Romain Guycbb89e42009-06-08 15:52:54 -0700722
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
724 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
725 itemInfo.icon = activityInfo.loadIcon(packageManager);
726 itemInfo.container = ItemInfo.NO_ID;
727
Romain Guy73b979d2009-06-09 12:57:21 -0700728 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 }
Romain Guy73b979d2009-06-09 12:57:21 -0700730
731 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 }
Romain Guycbb89e42009-06-08 15:52:54 -0700733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 /**
735 * Add a shortcut to the workspace.
736 *
737 * @param data The intent describing the shortcut.
738 * @param cellInfo The position on screen where to create the shortcut.
739 * @param insertAtFirst
740 */
741 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
742 boolean insertAtFirst) {
743 cellInfo.screen = mWorkspace.getCurrentScreen();
744 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700745
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
747
748 if (!mRestoring) {
749 sModel.addDesktopItem(info);
750
751 final View view = createShortcut(info);
752 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
753 } else if (sModel.isDesktopLoaded()) {
754 sModel.addDesktopItem(info);
755 }
756 }
757
Romain Guycbb89e42009-06-08 15:52:54 -0700758
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700760 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700762 * @param data The intent describing the appWidgetId.
763 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700765 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 boolean insertAtFirst) {
767
768 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700769 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700770
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700771 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700772
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700773 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700774
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700775 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700777 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700778
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 // Try finding open space on Launcher screen
780 final int[] xy = mCellCoordinates;
781 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
782
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700783 // Build Launcher-specific widget info and save to database
784 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 launcherInfo.spanX = spans[0];
786 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700787
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 LauncherModel.addItemToDatabase(this, launcherInfo,
789 LauncherSettings.Favorites.CONTAINER_DESKTOP,
790 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
791
792 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700793 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700794
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700796 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700797
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700798 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700800
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
802 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
803 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700804 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 }
806 }
Romain Guycbb89e42009-06-08 15:52:54 -0700807
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700808 public LauncherAppWidgetHost getAppWidgetHost() {
809 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 }
Romain Guycbb89e42009-06-08 15:52:54 -0700811
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 static ApplicationInfo addShortcut(Context context, Intent data,
813 CellLayout.CellInfo cellInfo, boolean notify) {
814
Romain Guy73b979d2009-06-09 12:57:21 -0700815 final ApplicationInfo info = infoFromShortcutIntent(context, data);
816 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
817 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
818
819 return info;
820 }
821
822 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
824 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
825 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
826
827 Drawable icon = null;
828 boolean filtered = false;
829 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700830 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831
832 if (bitmap != null) {
833 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
834 filtered = true;
835 customIcon = true;
836 } else {
837 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700838 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700840 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 final PackageManager packageManager = context.getPackageManager();
842 Resources resources = packageManager.getResourcesForApplication(
843 iconResource.packageName);
844 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
845 icon = resources.getDrawable(id);
846 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700847 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 }
849 }
850 }
851
852 if (icon == null) {
853 icon = context.getPackageManager().getDefaultActivityIcon();
854 }
855
856 final ApplicationInfo info = new ApplicationInfo();
857 info.icon = icon;
858 info.filtered = filtered;
859 info.title = name;
860 info.intent = intent;
861 info.customIcon = customIcon;
862 info.iconResource = iconResource;
863
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 return info;
865 }
866
867 @Override
868 protected void onNewIntent(Intent intent) {
869 super.onNewIntent(intent);
870
871 // Close the menu
872 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
873 getWindow().closeAllPanels();
874
875 try {
876 dismissDialog(DIALOG_CREATE_SHORTCUT);
877 // Unlock the workspace if the dialog was showing
878 mWorkspace.unlock();
879 } catch (Exception e) {
880 // An exception is thrown if the dialog is not visible, which is fine
881 }
882
883 try {
884 dismissDialog(DIALOG_RENAME_FOLDER);
885 // Unlock the workspace if the dialog was showing
886 mWorkspace.unlock();
887 } catch (Exception e) {
888 // An exception is thrown if the dialog is not visible, which is fine
889 }
890
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
892 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700893
Romain Guye3895ae2009-06-16 13:25:29 -0700894 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE &&
Romain Guy94406842009-06-17 16:18:58 -0700895 (mDragLayer.hasWindowFocus() ||
896 (mGesturesWindow != null && mGesturesWindow.isShowing()))) {
897
Romain Guyb8734242009-06-10 11:53:57 -0700898 SearchManager searchManager =
899 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Romain Guy94406842009-06-17 16:18:58 -0700900
Romain Guyb8734242009-06-10 11:53:57 -0700901 if (!searchManager.isVisible()) {
902 onHomeKeyPressed();
903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700906
907 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 if (v != null && v.getWindowToken() != null) {
909 InputMethodManager imm = (InputMethodManager)getSystemService(
910 INPUT_METHOD_SERVICE);
911 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
912 }
913 } else {
914 closeDrawer(false);
915 }
916 }
917 }
918
Romain Guy73b979d2009-06-09 12:57:21 -0700919 private void onHomeKeyPressed() {
920 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
921 showGesturesPanel();
922 } else {
923 hideGesturesPanel();
924 }
925 }
926
927 private void showGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -0700928 showGesturesPanel(true);
929 }
930
931 private void showGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -0700932 resetGesturesPrompt();
933
934 mGesturesAdd.setEnabled(false);
935 mGesturesAdd.setAlpha(128);
936
937 mGesturesOverlay.clear(false);
938
939 PopupWindow window;
940 if (mGesturesWindow == null) {
941 mGesturesWindow = new PopupWindow(this);
942 window = mGesturesWindow;
943 window.setFocusable(true);
944 window.setTouchable(true);
945 window.setBackgroundDrawable(null);
946 window.setContentView(mGesturesPanel);
947 } else {
948 window = mGesturesWindow;
949 }
Romain Guy3cf604f2009-06-16 13:12:53 -0700950 window.setAnimationStyle(animate ? com.android.internal.R.style.Animation_SlidingCard : 0);
Romain Guy73b979d2009-06-09 12:57:21 -0700951
952 final int[] xy = new int[2];
953 final DragLayer dragLayer = mDragLayer;
954 dragLayer.getLocationOnScreen(xy);
955
956 window.setWidth(dragLayer.getWidth());
957 window.setHeight(dragLayer.getHeight() - 1);
958 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
959 }
960
961 private void resetGesturesPrompt() {
962 mGesturesAction.intent = null;
963 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
964 prompt.setText(R.string.gestures_instructions);
965 prompt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
966 prompt.setClickable(false);
967 }
968
969 private void resetGesturesNextPrompt() {
970 mGesturesAction.intent = null;
971 setGesturesNextPrompt(null, getString(R.string.gestures_instructions));
972 mGesturesPrompt.getNextView().setClickable(false);
973 }
974
975 private void setGesturesNextPrompt(Drawable icon, CharSequence title) {
976 final TextView prompt = (TextView) mGesturesPrompt.getNextView();
977 prompt.setText(title);
978 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
979 prompt.setClickable(true);
980 mGesturesPrompt.showNext();
981 }
982
Romain Guy3cf604f2009-06-16 13:12:53 -0700983 private void setGesturesPrompt(Drawable icon, CharSequence title) {
984 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
985 prompt.setText(title);
986 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
987 prompt.setClickable(true);
988 }
989
Romain Guy73b979d2009-06-09 12:57:21 -0700990 void hideGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -0700991 hideGesturesPanel(true);
992 }
993
994 void hideGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -0700995 if (mGesturesWindow != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -0700996 final PopupWindow popupWindow = mGesturesWindow;
997 popupWindow.setAnimationStyle(animate ?
998 com.android.internal.R.style.Animation_SlidingCard : 0);
999 popupWindow.update();
1000 popupWindow.dismiss();
Romain Guy73b979d2009-06-09 12:57:21 -07001001 }
1002 }
1003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 @Override
1005 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1006 // Do not call super here
1007 mSavedInstanceState = savedInstanceState;
1008 }
1009
1010 @Override
1011 protected void onSaveInstanceState(Bundle outState) {
1012 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1013
1014 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1015 if (folders.size() > 0) {
1016 final int count = folders.size();
1017 long[] ids = new long[count];
1018 for (int i = 0; i < count; i++) {
1019 final FolderInfo info = folders.get(i).getInfo();
1020 ids[i] = info.id;
1021 }
1022 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1023 } else {
1024 super.onSaveInstanceState(outState);
1025 }
1026
Romain Guy3cf604f2009-06-16 13:12:53 -07001027 final boolean isConfigurationChange = getChangingConfigurations() != 0;
1028
Romain Guy5a941392009-04-28 15:18:25 -07001029 // When the drawer is opened and we are saving the state because of a
1030 // configuration change
Romain Guy3cf604f2009-06-16 13:12:53 -07001031 if (mDrawer.isOpened() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034
1035 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1036 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1037 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1038
1039 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1040 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1041 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1042 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1043 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1044 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1045 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1046 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1047 layout.getOccupiedCells());
1048 }
1049
1050 if (mFolderInfo != null && mWaitingForResult) {
1051 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1052 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1053 }
Romain Guy73b979d2009-06-09 12:57:21 -07001054
1055 if (mCurrentGesture != null && mWaitingForResult) {
1056 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1057 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001058
1059 if (mGesturesWindow != null && mGesturesWindow.isShowing() && isConfigurationChange) {
1060 outState.putBoolean(RUNTIME_STATE_GESTURES_PANEL, true);
1061
1062 final Gesture gesture = mGesturesOverlay.getGesture();
1063 if (gesture != null) {
1064 outState.putParcelable(RUNTIME_STATE_GESTURES_PANEL_GESTURE, gesture);
1065 }
1066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 }
1068
1069 @Override
1070 public void onDestroy() {
1071 mDestroyed = true;
1072
1073 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001074
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001076 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001078 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 }
1080
1081 TextKeyListener.getInstance().release();
1082
Romain Guy3cf604f2009-06-16 13:12:53 -07001083 hideGesturesPanel(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 mAllAppsGrid.clearTextFilter();
1085 mAllAppsGrid.setAdapter(null);
1086 sModel.unbind();
1087 sModel.abortLoaders();
1088
1089 getContentResolver().unregisterContentObserver(mObserver);
1090 unregisterReceiver(mApplicationsReceiver);
1091 }
1092
1093 @Override
1094 public void startActivityForResult(Intent intent, int requestCode) {
1095 mWaitingForResult = true;
1096 super.startActivityForResult(intent, requestCode);
1097 }
1098
1099 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001100 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001102
1103 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001104
Karl Rosaen138a0412009-04-23 19:00:21 -07001105 // Slide the search widget to the top, if it's on the current screen,
1106 // otherwise show the search dialog immediately.
1107 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1108 if (searchWidget == null) {
1109 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1110 } else {
1111 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1112 // show the currently typed text in the search widget while sliding
1113 searchWidget.setQuery(getTypedText());
1114 }
1115 }
Romain Guycbb89e42009-06-08 15:52:54 -07001116
Karl Rosaen138a0412009-04-23 19:00:21 -07001117 /**
1118 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001119 *
1120 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001121 */
Romain Guycbb89e42009-06-08 15:52:54 -07001122 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001123 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001124
Karl Rosaen138a0412009-04-23 19:00:21 -07001125 if (initialQuery == null) {
1126 // Use any text typed in the launcher as the initial query
1127 initialQuery = getTypedText();
1128 clearTypedText();
1129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 if (appSearchData == null) {
1131 appSearchData = new Bundle();
1132 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1133 }
Romain Guycbb89e42009-06-08 15:52:54 -07001134
Karl Rosaen138a0412009-04-23 19:00:21 -07001135 final SearchManager searchManager =
1136 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1137
1138 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1139 if (searchWidget != null) {
1140 // This gets called when the user leaves the search dialog to go back to
1141 // the Launcher.
1142 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1143 public void onCancel() {
1144 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001145 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001146 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001147 });
1148 }
Romain Guycbb89e42009-06-08 15:52:54 -07001149
Karl Rosaen138a0412009-04-23 19:00:21 -07001150 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001151 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 }
1153
Karl Rosaen138a0412009-04-23 19:00:21 -07001154 /**
1155 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001156 */
Romain Guy5a941392009-04-28 15:18:25 -07001157 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001158 // Close search dialog
1159 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1160 if (searchManager.isVisible()) {
1161 searchManager.stopSearch();
1162 }
1163 // Restore search widget to its normal position
1164 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1165 if (searchWidget != null) {
1166 searchWidget.stopSearch(false);
1167 }
1168 }
Romain Guycbb89e42009-06-08 15:52:54 -07001169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 @Override
1171 public boolean onCreateOptionsMenu(Menu menu) {
1172 if (mDesktopLocked) return false;
1173
1174 super.onCreateOptionsMenu(menu);
1175 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1176 .setIcon(android.R.drawable.ic_menu_add)
1177 .setAlphabeticShortcut('A');
1178 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1179 .setIcon(android.R.drawable.ic_menu_gallery)
1180 .setAlphabeticShortcut('W');
1181 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1182 .setIcon(android.R.drawable.ic_search_category_default)
1183 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1184 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1185 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1186 .setAlphabeticShortcut('N');
1187
Romain Guy73b979d2009-06-09 12:57:21 -07001188 final Intent gestures = new Intent(this, GesturesActivity.class);
1189 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1190 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1191 .setIntent(gestures);
1192
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001194 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1195 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196
1197 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1198 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1199 .setIntent(settings);
1200
1201 return true;
1202 }
1203
1204 @Override
1205 public boolean onPrepareOptionsMenu(Menu menu) {
1206 super.onPrepareOptionsMenu(menu);
1207
1208 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1209 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1210
1211 return true;
1212 }
1213
1214 @Override
1215 public boolean onOptionsItemSelected(MenuItem item) {
1216 switch (item.getItemId()) {
1217 case MENU_ADD:
1218 addItems();
1219 return true;
1220 case MENU_WALLPAPER_SETTINGS:
1221 startWallpaper();
1222 return true;
1223 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001224 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 return true;
1226 case MENU_NOTIFICATIONS:
1227 showNotifications();
1228 return true;
1229 }
1230
1231 return super.onOptionsItemSelected(item);
1232 }
Romain Guycbb89e42009-06-08 15:52:54 -07001233
Karl Rosaen138a0412009-04-23 19:00:21 -07001234 /**
1235 * Indicates that we want global search for this activity by setting the globalSearch
1236 * argument for {@link #startSearch} to true.
1237 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001239 @Override
1240 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001241 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001242 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001243 }
1244
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 private void addItems() {
1246 showAddDialog(mMenuAddInfo);
1247 }
1248
1249 private void removeShortcutsForPackage(String packageName) {
1250 if (packageName != null && packageName.length() > 0) {
1251 mWorkspace.removeShortcutsForPackage(packageName);
1252 }
1253 }
Romain Guycbb89e42009-06-08 15:52:54 -07001254
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001255 private void updateShortcutsForPackage(String packageName) {
1256 if (packageName != null && packageName.length() > 0) {
1257 mWorkspace.updateShortcutsForPackage(packageName);
1258 }
1259 }
1260
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001261 void addAppWidget(Intent data) {
1262 // TODO: catch bad widget exception when sent
1263 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001265 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1266 if (SEARCH_WIDGET.equals(customWidget)) {
1267 // We don't need this any more, since this isn't a real app widget.
1268 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1269 // add the search widget
1270 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001272 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1273
1274 if (appWidget.configure != null) {
1275 // Launch over to configure widget, if needed
1276 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1277 intent.setComponent(appWidget.configure);
1278 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1279
1280 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1281 } else {
1282 // Otherwise just add it
1283 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1284 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
1286 }
Romain Guycbb89e42009-06-08 15:52:54 -07001287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 void addSearch() {
1289 final Widget info = Widget.makeSearch();
1290 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001291
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 final int[] xy = mCellCoordinates;
1293 final int spanX = info.spanX;
1294 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001295
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001297
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 sModel.addDesktopItem(info);
1299 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1300 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001301
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 final View view = mInflater.inflate(info.layoutResource, null);
1303 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001304 Search search = (Search) view.findViewById(R.id.widget_search);
1305 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001306
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1308 }
1309
Romain Guy73b979d2009-06-09 12:57:21 -07001310 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001311 // Handle case where user selected "Applications"
1312 String applicationName = getResources().getString(R.string.group_applications);
1313 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001314
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001315 if (applicationName != null && applicationName.equals(shortcutName)) {
1316 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1317 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001318
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001319 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1320 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001321 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001322 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001323 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001324 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
1326
1327 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001328 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001329 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001330 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001331
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001332 if (folderName != null && folderName.equals(shortcutName)) {
1333 addFolder(!mDesktopLocked);
1334 } else {
1335 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
1339 void addFolder(boolean insertAtFirst) {
1340 UserFolderInfo folderInfo = new UserFolderInfo();
1341 folderInfo.title = getText(R.string.folder_name);
1342
1343 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1344 cellInfo.screen = mWorkspace.getCurrentScreen();
1345 if (!findSingleSlot(cellInfo)) return;
1346
1347 // Update the model
1348 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1349 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1350 sModel.addDesktopItem(folderInfo);
1351 sModel.addFolder(folderInfo);
1352
1353 // Create the view
1354 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1355 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1356 mWorkspace.addInCurrentScreen(newFolder,
1357 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1358 }
Romain Guycbb89e42009-06-08 15:52:54 -07001359
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1361 boolean insertAtFirst) {
1362 cellInfo.screen = mWorkspace.getCurrentScreen();
1363 if (!findSingleSlot(cellInfo)) return;
1364
1365 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1366
1367 if (!mRestoring) {
1368 sModel.addDesktopItem(info);
1369
1370 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1371 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1372 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1373 } else if (sModel.isDesktopLoaded()) {
1374 sModel.addDesktopItem(info);
1375 }
1376 }
1377
1378 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1379 CellLayout.CellInfo cellInfo, boolean notify) {
1380
1381 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1382 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1383
1384 Drawable icon = null;
1385 boolean filtered = false;
1386 Intent.ShortcutIconResource iconResource = null;
1387
1388 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1389 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1390 try {
1391 iconResource = (Intent.ShortcutIconResource) extra;
1392 final PackageManager packageManager = context.getPackageManager();
1393 Resources resources = packageManager.getResourcesForApplication(
1394 iconResource.packageName);
1395 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1396 icon = resources.getDrawable(id);
1397 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001398 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
1400 }
1401
1402 if (icon == null) {
1403 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1404 }
1405
1406 final LiveFolderInfo info = new LiveFolderInfo();
1407 info.icon = icon;
1408 info.filtered = filtered;
1409 info.title = name;
1410 info.iconResource = iconResource;
1411 info.uri = data.getData();
1412 info.baseIntent = baseIntent;
1413 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1414 LiveFolders.DISPLAY_MODE_GRID);
1415
1416 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1417 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1418 sModel.addFolder(info);
1419
1420 return info;
1421 }
1422
1423 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1424 final int[] xy = new int[2];
1425 if (findSlot(cellInfo, xy, 1, 1)) {
1426 cellInfo.cellX = xy[0];
1427 cellInfo.cellY = xy[1];
1428 return true;
1429 }
1430 return false;
1431 }
1432
1433 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1434 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1435 boolean[] occupied = mSavedState != null ?
1436 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1437 cellInfo = mWorkspace.findAllVacantCells(occupied);
1438 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1439 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1440 return false;
1441 }
1442 }
1443 return true;
1444 }
1445
1446 private void showNotifications() {
1447 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1448 if (statusBar != null) {
1449 statusBar.expand();
1450 }
1451 }
1452
1453 private void startWallpaper() {
1454 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1455 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1456 }
1457
1458 /**
1459 * Registers various intent receivers. The current implementation registers
1460 * only a wallpaper intent receiver to let other applications change the
1461 * wallpaper.
1462 */
1463 private void registerIntentReceivers() {
1464 if (sWallpaperReceiver == null) {
1465 final Application application = getApplication();
1466
1467 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1468
1469 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1470 application.registerReceiver(sWallpaperReceiver, filter);
1471 } else {
1472 sWallpaperReceiver.setLauncher(this);
1473 }
1474
1475 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1476 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1477 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1478 filter.addDataScheme("package");
1479 registerReceiver(mApplicationsReceiver, filter);
1480 }
1481
1482 /**
1483 * Registers various content observers. The current implementation registers
1484 * only a favorites observer to keep track of the favorites applications.
1485 */
1486 private void registerContentObservers() {
1487 ContentResolver resolver = getContentResolver();
1488 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1489 }
1490
1491 @Override
1492 public boolean dispatchKeyEvent(KeyEvent event) {
1493 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1494 switch (event.getKeyCode()) {
1495 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001496 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001497 if (mDrawer.isOpened()) {
1498 closeDrawer();
1499 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001500 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 return true;
1503 case KeyEvent.KEYCODE_HOME:
1504 return true;
1505 }
1506 }
1507
1508 return super.dispatchKeyEvent(event);
1509 }
1510
1511 private void closeDrawer() {
1512 closeDrawer(true);
1513 }
1514
1515 private void closeDrawer(boolean animated) {
1516 if (mDrawer.isOpened()) {
1517 if (animated) {
1518 mDrawer.animateClose();
1519 } else {
1520 mDrawer.close();
1521 }
1522 if (mDrawer.hasFocus()) {
1523 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1524 }
1525 }
1526 }
1527
1528 private void closeFolder() {
1529 Folder folder = mWorkspace.getOpenFolder();
1530 if (folder != null) {
1531 closeFolder(folder);
1532 }
1533 }
1534
1535 void closeFolder(Folder folder) {
1536 folder.getInfo().opened = false;
1537 ViewGroup parent = (ViewGroup) folder.getParent();
1538 if (parent != null) {
1539 parent.removeView(folder);
1540 }
1541 folder.onClose();
1542 }
1543
1544 /**
1545 * When the notification that favorites have changed is received, requests
1546 * a favorites list refresh.
1547 */
1548 private void onFavoritesChanged() {
1549 mDesktopLocked = true;
1550 mDrawer.lock();
1551 sModel.loadUserItems(false, this, false, false);
1552 }
1553
1554 void onDesktopItemsLoaded() {
1555 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 bindDesktopItems();
1557 }
Romain Guycbb89e42009-06-08 15:52:54 -07001558
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 /**
1560 * Refreshes the shortcuts shown on the workspace.
1561 */
1562 private void bindDesktopItems() {
1563 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001564 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001565 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1566 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 return;
1568 }
1569
1570 final Workspace workspace = mWorkspace;
1571 int count = workspace.getChildCount();
1572 for (int i = 0; i < count; i++) {
1573 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1574 }
Romain Guycbb89e42009-06-08 15:52:54 -07001575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 if (DEBUG_USER_INTERFACE) {
1577 android.widget.Button finishButton = new android.widget.Button(this);
1578 finishButton.setText("Finish");
1579 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1580
1581 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1582 public void onClick(View v) {
1583 finish();
1584 }
1585 });
1586 }
Romain Guycbb89e42009-06-08 15:52:54 -07001587
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001588 // Flag any old binder to terminate early
1589 if (mBinder != null) {
1590 mBinder.mTerminate = true;
1591 }
Romain Guycbb89e42009-06-08 15:52:54 -07001592
Karl Rosaen138a0412009-04-23 19:00:21 -07001593 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001594 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
1596
1597 private void bindItems(Launcher.DesktopBinder binder,
1598 ArrayList<ItemInfo> shortcuts, int start, int count) {
1599
1600 final Workspace workspace = mWorkspace;
1601 final boolean desktopLocked = mDesktopLocked;
1602
1603 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1604 int i = start;
1605
1606 for ( ; i < end; i++) {
1607 final ItemInfo item = shortcuts.get(i);
1608 switch (item.itemType) {
1609 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1610 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1611 final View shortcut = createShortcut((ApplicationInfo) item);
1612 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1613 !desktopLocked);
1614 break;
1615 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1616 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1617 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1618 (UserFolderInfo) item);
1619 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1620 !desktopLocked);
1621 break;
1622 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1623 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1624 R.layout.live_folder_icon, this,
1625 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1626 (LiveFolderInfo) item);
1627 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1628 !desktopLocked);
1629 break;
1630 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1631 final int screen = workspace.getCurrentScreen();
1632 final View view = mInflater.inflate(R.layout.widget_search,
1633 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001634
Karl Rosaen138a0412009-04-23 19:00:21 -07001635 Search search = (Search) view.findViewById(R.id.widget_search);
1636 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001637
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 final Widget widget = (Widget) item;
1639 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001640
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 workspace.addWidget(view, widget, !desktopLocked);
1642 break;
1643 }
1644 }
1645
1646 workspace.requestLayout();
1647
1648 if (end >= count) {
1649 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001650 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 } else {
1652 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1653 }
1654 }
1655
1656 private void finishBindDesktopItems() {
1657 if (mSavedState != null) {
1658 if (!mWorkspace.hasFocus()) {
1659 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1660 }
1661
1662 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1663 if (userFolders != null) {
1664 for (long folderId : userFolders) {
1665 final FolderInfo info = sModel.findFolderById(folderId);
1666 if (info != null) {
1667 openFolder(info);
1668 }
1669 }
1670 final Folder openFolder = mWorkspace.getOpenFolder();
1671 if (openFolder != null) {
1672 openFolder.requestFocus();
1673 }
1674 }
1675
1676 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1677 if (allApps) {
1678 mDrawer.open();
1679 }
1680
1681 mSavedState = null;
1682 }
1683
1684 if (mSavedInstanceState != null) {
1685 super.onRestoreInstanceState(mSavedInstanceState);
1686 mSavedInstanceState = null;
1687 }
1688
1689 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1690 mDrawer.requestFocus();
1691 }
1692
1693 mDesktopLocked = false;
1694 mDrawer.unlock();
1695 }
Romain Guycbb89e42009-06-08 15:52:54 -07001696
Karl Rosaen138a0412009-04-23 19:00:21 -07001697 private void bindDrawer(Launcher.DesktopBinder binder,
1698 ApplicationsAdapter drawerAdapter) {
1699 mAllAppsGrid.setAdapter(drawerAdapter);
1700 binder.startBindingAppWidgetsWhenIdle();
1701 }
Romain Guycbb89e42009-06-08 15:52:54 -07001702
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001703 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001704 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001705
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 final Workspace workspace = mWorkspace;
1707 final boolean desktopLocked = mDesktopLocked;
1708
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001709 if (!appWidgets.isEmpty()) {
1710 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001711
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001712 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001713 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001714 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001715
Karl Rosaen138a0412009-04-23 19:00:21 -07001716 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001717
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001718 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1719 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001720
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001721 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1722 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001723
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001724 workspace.requestLayout();
1725 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001727 if (appWidgets.isEmpty()) {
1728 if (PROFILE_ROTATE) {
1729 android.os.Debug.stopMethodTracing();
1730 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001732 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 }
1734 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001735
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 DragController getDragController() {
1737 return mDragLayer;
1738 }
1739
1740 /**
1741 * Launches the intent referred by the clicked shortcut.
1742 *
1743 * @param v The view representing the clicked shortcut.
1744 */
1745 public void onClick(View v) {
1746 Object tag = v.getTag();
1747 if (tag instanceof ApplicationInfo) {
1748 // Open shortcut
1749 final Intent intent = ((ApplicationInfo) tag).intent;
1750 startActivitySafely(intent);
1751 } else if (tag instanceof FolderInfo) {
1752 handleFolderClick((FolderInfo) tag);
1753 }
1754 }
1755
1756 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001757 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1759 try {
1760 startActivity(intent);
1761 } catch (ActivityNotFoundException e) {
1762 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1763 } catch (SecurityException e) {
1764 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001765 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1767 "or use the exported attribute for this activity.", e);
1768 }
1769 }
1770
1771 private void handleFolderClick(FolderInfo folderInfo) {
1772 if (!folderInfo.opened) {
1773 // Close any open folder
1774 closeFolder();
1775 // Open the requested folder
1776 openFolder(folderInfo);
1777 } else {
1778 // Find the open folder...
1779 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1780 int folderScreen;
1781 if (openFolder != null) {
1782 folderScreen = mWorkspace.getScreenForView(openFolder);
1783 // .. and close it
1784 closeFolder(openFolder);
1785 if (folderScreen != mWorkspace.getCurrentScreen()) {
1786 // Close any folder open on the current screen
1787 closeFolder();
1788 // Pull the folder onto this screen
1789 openFolder(folderInfo);
1790 }
1791 }
1792 }
1793 }
1794
1795 private void loadWallpaper() {
1796 // The first time the application is started, we load the wallpaper from
1797 // the ApplicationContext
1798 if (sWallpaper == null) {
1799 final Drawable drawable = getWallpaper();
1800 if (drawable instanceof BitmapDrawable) {
1801 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1802 } else {
1803 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1804 }
1805 }
1806 mWorkspace.loadWallpaper(sWallpaper);
1807 }
1808
1809 /**
1810 * Opens the user fodler described by the specified tag. The opening of the folder
1811 * is animated relative to the specified View. If the View is null, no animation
1812 * is played.
1813 *
1814 * @param folderInfo The FolderInfo describing the folder to open.
1815 */
1816 private void openFolder(FolderInfo folderInfo) {
1817 Folder openFolder;
1818
1819 if (folderInfo instanceof UserFolderInfo) {
1820 openFolder = UserFolder.fromXml(this);
1821 } else if (folderInfo instanceof LiveFolderInfo) {
1822 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1823 } else {
1824 return;
1825 }
1826
1827 openFolder.setDragger(mDragLayer);
1828 openFolder.setLauncher(this);
1829
1830 openFolder.bind(folderInfo);
1831 folderInfo.opened = true;
1832
1833 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1834 openFolder.onOpen();
1835 }
1836
1837 /**
1838 * Returns true if the workspace is being loaded. When the workspace is loading,
1839 * no user interaction should be allowed to avoid any conflict.
1840 *
1841 * @return True if the workspace is locked, false otherwise.
1842 */
1843 boolean isWorkspaceLocked() {
1844 return mDesktopLocked;
1845 }
1846
1847 public boolean onLongClick(View v) {
1848 if (mDesktopLocked) {
1849 return false;
1850 }
1851
1852 if (!(v instanceof CellLayout)) {
1853 v = (View) v.getParent();
1854 }
1855
1856 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1857
1858 // This happens when long clicking an item with the dpad/trackball
1859 if (cellInfo == null) {
1860 return true;
1861 }
1862
1863 if (mWorkspace.allowLongPress()) {
1864 if (cellInfo.cell == null) {
1865 if (cellInfo.valid) {
1866 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001867 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 showAddDialog(cellInfo);
1869 }
1870 } else {
1871 if (!(cellInfo.cell instanceof Folder)) {
1872 // User long pressed on an item
1873 mWorkspace.startDrag(cellInfo);
1874 }
1875 }
1876 }
1877 return true;
1878 }
1879
1880 static LauncherModel getModel() {
1881 return sModel;
1882 }
1883
Romain Guy73b979d2009-06-09 12:57:21 -07001884 static GestureLibrary getGestureLibrary() {
1885 return sLibrary;
1886 }
1887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 void closeAllApplications() {
1889 mDrawer.close();
1890 }
1891
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001892 View getDrawerHandle() {
1893 return mHandleView;
1894 }
1895
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 boolean isDrawerDown() {
1897 return !mDrawer.isMoving() && !mDrawer.isOpened();
1898 }
1899
1900 boolean isDrawerUp() {
1901 return mDrawer.isOpened() && !mDrawer.isMoving();
1902 }
1903
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001904 boolean isDrawerMoving() {
1905 return mDrawer.isMoving();
1906 }
1907
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 Workspace getWorkspace() {
1909 return mWorkspace;
1910 }
1911
1912 GridView getApplicationsGrid() {
1913 return mAllAppsGrid;
1914 }
1915
1916 @Override
1917 protected Dialog onCreateDialog(int id) {
1918 switch (id) {
1919 case DIALOG_CREATE_SHORTCUT:
1920 return new CreateShortcut().createDialog();
1921 case DIALOG_RENAME_FOLDER:
1922 return new RenameFolder().createDialog();
1923 }
1924
1925 return super.onCreateDialog(id);
1926 }
1927
1928 @Override
1929 protected void onPrepareDialog(int id, Dialog dialog) {
1930 switch (id) {
1931 case DIALOG_CREATE_SHORTCUT:
1932 mWorkspace.lock();
1933 break;
1934 case DIALOG_RENAME_FOLDER:
1935 mWorkspace.lock();
1936 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1937 final CharSequence text = mFolderInfo.title;
1938 input.setText(text);
Romain Guycbb89e42009-06-08 15:52:54 -07001939 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 break;
1941 }
1942 }
1943
1944 void showRenameDialog(FolderInfo info) {
1945 mFolderInfo = info;
1946 mWaitingForResult = true;
1947 showDialog(DIALOG_RENAME_FOLDER);
1948 }
1949
1950 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1951 mAddItemCellInfo = cellInfo;
1952 mWaitingForResult = true;
1953 showDialog(DIALOG_CREATE_SHORTCUT);
1954 }
1955
Romain Guy73b979d2009-06-09 12:57:21 -07001956 private void pickShortcut(int requestCode, int title) {
1957 Bundle bundle = new Bundle();
1958
1959 ArrayList<String> shortcutNames = new ArrayList<String>();
1960 shortcutNames.add(getString(R.string.group_applications));
1961 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1962
1963 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1964 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1965 R.drawable.ic_launcher_application));
1966 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1967
1968 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1969 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1970 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1971 pickIntent.putExtras(bundle);
1972
1973 startActivityForResult(pickIntent, requestCode);
1974 }
1975
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 private class RenameFolder {
1977 private EditText mInput;
1978
1979 Dialog createDialog() {
1980 mWaitingForResult = true;
1981 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1982 mInput = (EditText) layout.findViewById(R.id.folder_name);
1983
1984 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1985 builder.setIcon(0);
1986 builder.setTitle(getString(R.string.rename_folder_title));
1987 builder.setCancelable(true);
1988 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1989 public void onCancel(DialogInterface dialog) {
1990 cleanup();
1991 }
1992 });
1993 builder.setNegativeButton(getString(R.string.cancel_action),
1994 new Dialog.OnClickListener() {
1995 public void onClick(DialogInterface dialog, int which) {
1996 cleanup();
1997 }
1998 }
1999 );
2000 builder.setPositiveButton(getString(R.string.rename_action),
2001 new Dialog.OnClickListener() {
2002 public void onClick(DialogInterface dialog, int which) {
2003 changeFolderName();
2004 }
2005 }
2006 );
2007 builder.setView(layout);
2008 return builder.create();
2009 }
2010
2011 private void changeFolderName() {
2012 final String name = mInput.getText().toString();
2013 if (!TextUtils.isEmpty(name)) {
2014 // Make sure we have the right folder info
2015 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
2016 mFolderInfo.title = name;
2017 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2018
2019 if (mDesktopLocked) {
2020 mDrawer.lock();
2021 sModel.loadUserItems(false, Launcher.this, false, false);
2022 } else {
2023 final FolderIcon folderIcon = (FolderIcon)
2024 mWorkspace.getViewForTag(mFolderInfo);
2025 if (folderIcon != null) {
2026 folderIcon.setText(name);
2027 getWorkspace().requestLayout();
2028 } else {
2029 mDesktopLocked = true;
2030 mDrawer.lock();
2031 sModel.loadUserItems(false, Launcher.this, false, false);
2032 }
2033 }
2034 }
2035 cleanup();
2036 }
2037
2038 private void cleanup() {
2039 mWorkspace.unlock();
2040 dismissDialog(DIALOG_RENAME_FOLDER);
2041 mWaitingForResult = false;
2042 mFolderInfo = null;
2043 }
2044 }
2045
2046 /**
2047 * Displays the shortcut creation dialog and launches, if necessary, the
2048 * appropriate activity.
2049 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002050 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07002051 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 Dialog createDialog() {
2055 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2060 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002061 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 builder.setInverseBackgroundForced(true);
2064
2065 AlertDialog dialog = builder.create();
2066 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002067 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 return dialog;
2070 }
2071
2072 public void onCancel(DialogInterface dialog) {
2073 mWaitingForResult = false;
2074 cleanup();
2075 }
2076
Romain Guycbb89e42009-06-08 15:52:54 -07002077 public void onDismiss(DialogInterface dialog) {
2078 mWorkspace.unlock();
2079 }
2080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 private void cleanup() {
2082 mWorkspace.unlock();
2083 dismissDialog(DIALOG_CREATE_SHORTCUT);
2084 }
2085
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002086 /**
2087 * Handle the action clicked in the "Add to home" dialog.
2088 */
2089 public void onClick(DialogInterface dialog, int which) {
2090 Resources res = getResources();
2091 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002092
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002093 switch (which) {
2094 case AddAdapter.ITEM_SHORTCUT: {
2095 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002096 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002097 break;
2098 }
Romain Guycbb89e42009-06-08 15:52:54 -07002099
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002100 case AddAdapter.ITEM_APPWIDGET: {
2101 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002102
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002103 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2104 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2105 // add the search widget
2106 ArrayList<AppWidgetProviderInfo> customInfo =
2107 new ArrayList<AppWidgetProviderInfo>();
2108 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2109 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2110 info.label = getString(R.string.group_search);
2111 info.icon = R.drawable.ic_search_widget;
2112 customInfo.add(info);
2113 pickIntent.putParcelableArrayListExtra(
2114 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2115 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2116 Bundle b = new Bundle();
2117 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2118 customExtras.add(b);
2119 pickIntent.putParcelableArrayListExtra(
2120 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2121 // start the pick activity
2122 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2123 break;
2124 }
Romain Guycbb89e42009-06-08 15:52:54 -07002125
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002126 case AddAdapter.ITEM_LIVE_FOLDER: {
2127 // Insert extra item to handle inserting folder
2128 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002129
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002130 ArrayList<String> shortcutNames = new ArrayList<String>();
2131 shortcutNames.add(res.getString(R.string.group_folder));
2132 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002133
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002134 ArrayList<ShortcutIconResource> shortcutIcons =
2135 new ArrayList<ShortcutIconResource>();
2136 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2137 R.drawable.ic_launcher_folder));
2138 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2139
2140 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2141 pickIntent.putExtra(Intent.EXTRA_INTENT,
2142 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2143 pickIntent.putExtra(Intent.EXTRA_TITLE,
2144 getText(R.string.title_select_live_folder));
2145 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002146
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002147 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2148 break;
2149 }
2150
2151 case AddAdapter.ITEM_WALLPAPER: {
2152 startWallpaper();
2153 break;
2154 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 }
2156 }
2157 }
2158
2159 /**
2160 * Receives notifications when applications are added/removed.
2161 */
2162 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2163 @Override
2164 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002165 final String action = intent.getAction();
2166 final String packageName = intent.getData().getSchemeSpecificPart();
2167 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2168
2169 if (LauncherModel.DEBUG_LOADERS) {
2170 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2171 ", replacing=" + replacing);
2172 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002174
2175 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2176 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2177 if (!replacing) {
2178 removeShortcutsForPackage(packageName);
2179 if (LauncherModel.DEBUG_LOADERS) {
2180 d(LauncherModel.LOG_TAG, " --> remove package");
2181 }
2182 sModel.removePackage(Launcher.this, packageName);
2183 }
2184 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2185 // later, we will update the package at this time
2186 } else {
2187 if (!replacing) {
2188 if (LauncherModel.DEBUG_LOADERS) {
2189 d(LauncherModel.LOG_TAG, " --> add package");
2190 }
2191 sModel.addPackage(Launcher.this, packageName);
2192 } else {
2193 if (LauncherModel.DEBUG_LOADERS) {
2194 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2195 }
2196 sModel.updatePackage(Launcher.this, packageName);
2197 updateShortcutsForPackage(packageName);
2198 }
2199 }
2200 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002202 if (LauncherModel.DEBUG_LOADERS) {
2203 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2204 }
2205 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 }
2207 }
2208 }
2209
2210 /**
2211 * Receives notifications whenever the user favorites have changed.
2212 */
2213 private class FavoritesChangeObserver extends ContentObserver {
2214 public FavoritesChangeObserver() {
2215 super(new Handler());
2216 }
2217
2218 @Override
2219 public void onChange(boolean selfChange) {
2220 onFavoritesChanged();
2221 }
2222 }
2223
2224 /**
2225 * Receives intents from other applications to change the wallpaper.
2226 */
2227 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2228 private final Application mApplication;
2229 private WeakReference<Launcher> mLauncher;
2230
2231 WallpaperIntentReceiver(Application application, Launcher launcher) {
2232 mApplication = application;
2233 setLauncher(launcher);
2234 }
2235
2236 void setLauncher(Launcher launcher) {
2237 mLauncher = new WeakReference<Launcher>(launcher);
2238 }
2239
2240 @Override
2241 public void onReceive(Context context, Intent intent) {
2242 // Load the wallpaper from the ApplicationContext and store it locally
2243 // until the Launcher Activity is ready to use it
2244 final Drawable drawable = mApplication.getWallpaper();
2245 if (drawable instanceof BitmapDrawable) {
2246 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2247 } else {
2248 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2249 }
2250
2251 // If Launcher is alive, notify we have a new wallpaper
2252 if (mLauncher != null) {
2253 final Launcher launcher = mLauncher.get();
2254 if (launcher != null) {
2255 launcher.loadWallpaper();
2256 }
2257 }
2258 }
2259 }
2260
2261 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2262 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2263 private boolean mOpen;
2264
2265 public void onDrawerOpened() {
2266 if (!mOpen) {
2267 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002270 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271
2272 mOpen = true;
2273 }
2274 }
2275
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002276 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2277 view.getDrawingRect(bounds);
2278
2279 while (view != mDragLayer) {
2280 bounds.offset(view.getLeft(), view.getTop());
2281 view = (View) view.getParent();
2282 }
2283 }
2284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 public void onDrawerClosed() {
2286 if (mOpen) {
2287 mHandleIcon.reverseTransition(150);
2288 mWorkspace.mDrawerBounds.setEmpty();
2289 mOpen = false;
2290 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002291
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002292 mAllAppsGrid.setSelection(0);
2293 mAllAppsGrid.clearTextFilter();
2294 }
2295
2296 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002297 if (PROFILE_DRAWER) {
2298 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2299 }
2300
2301 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2302 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 }
2304
2305 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002306 if (PROFILE_DRAWER) {
2307 android.os.Debug.stopMethodTracing();
2308 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 }
2310 }
2311
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002312 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002314 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002315 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002316
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 // Number of items to bind in every pass
2318 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319
2320 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002321 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002322 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002324
Karl Rosaen138a0412009-04-23 19:00:21 -07002325 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326
2327 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002328 ArrayList<LauncherAppWidgetInfo> appWidgets,
2329 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330
2331 mLauncher = new WeakReference<Launcher>(launcher);
2332 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002333 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002334
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002335 // Sort widgets so active workspace is bound first
2336 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2337 final int size = appWidgets.size();
2338 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002339
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002340 for (int i = 0; i < size; i++) {
2341 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2342 if (appWidgetInfo.screen == currentScreen) {
2343 mAppWidgets.addFirst(appWidgetInfo);
2344 } else {
2345 mAppWidgets.addLast(appWidgetInfo);
2346 }
2347 }
2348 }
Romain Guycbb89e42009-06-08 15:52:54 -07002349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 public void startBindingItems() {
2351 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2352 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002353
2354 public void startBindingDrawer() {
2355 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2356 }
Romain Guycbb89e42009-06-08 15:52:54 -07002357
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002358 public void startBindingAppWidgetsWhenIdle() {
2359 // Ask for notification when message queue becomes idle
2360 final MessageQueue messageQueue = Looper.myQueue();
2361 messageQueue.addIdleHandler(this);
2362 }
Romain Guycbb89e42009-06-08 15:52:54 -07002363
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002364 public boolean queueIdle() {
2365 // Queue is idle, so start binding items
2366 startBindingAppWidgets();
2367 return false;
2368 }
2369
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002370 public void startBindingAppWidgets() {
2371 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2372 }
2373
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 @Override
2375 public void handleMessage(Message msg) {
2376 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002377 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 return;
2379 }
Romain Guycbb89e42009-06-08 15:52:54 -07002380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 switch (msg.what) {
2382 case MESSAGE_BIND_ITEMS: {
2383 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2384 break;
2385 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002386 case MESSAGE_BIND_DRAWER: {
2387 launcher.bindDrawer(this, mDrawerAdapter);
2388 break;
2389 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002390 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002391 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002392 break;
2393 }
2394 }
2395 }
2396 }
Romain Guy73b979d2009-06-09 12:57:21 -07002397
2398 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2399 GestureOverlayView.OnGesturePerformedListener {
2400
2401 private final GestureMatcher mMatcher = new GestureMatcher();
2402
2403 GesturesProcessor() {
2404 // TODO: Maybe the load should happen on a background thread?
2405 sLibrary.load();
2406 }
2407
2408 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002409 //noinspection PointlessBooleanExpression,ConstantConditions
2410 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2411 overlay.removeCallbacks(mMatcher);
2412 resetGesturesNextPrompt();
2413 }
Romain Guy73b979d2009-06-09 12:57:21 -07002414
2415 mGesturesAdd.setAlpha(128);
2416 mGesturesAdd.setEnabled(false);
2417 }
2418
2419 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2420 }
2421
2422 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2423 }
2424
2425 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002426 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2427 mMatcher.gesture = overlay.getGesture();
2428 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2429 overlay.clear(false);
2430 } else {
2431 mMatcher.run();
2432 }
Romain Guy73b979d2009-06-09 12:57:21 -07002433 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002434 overlay.removeCallbacks(mMatcher);
2435
2436 mMatcher.gesture = overlay.getGesture();
2437 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2438 overlay.clear(false);
2439 } else {
2440 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2441 }
Romain Guy73b979d2009-06-09 12:57:21 -07002442 }
2443 }
2444
Romain Guy3cf604f2009-06-16 13:12:53 -07002445 void matchGesture(Gesture gesture) {
2446 matchGesture(gesture, true);
2447 }
2448
2449 void matchGesture(Gesture gesture, boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07002450 mGesturesAdd.setAlpha(255);
2451 mGesturesAdd.setEnabled(true);
2452
2453 if (gesture != null) {
2454 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2455
2456 if (DEBUG_GESTURES) {
2457 for (Prediction p : predictions) {
2458 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2459 }
2460 }
2461
2462 boolean match = false;
2463 if (predictions.size() > 0) {
2464 final Prediction prediction = predictions.get(0);
2465 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2466 match = true;
2467
2468 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2469 if (info != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002470 updatePrompt(info, animate);
Romain Guy73b979d2009-06-09 12:57:21 -07002471 }
2472 }
2473 }
2474
2475 if (!match){
Romain Guy3cf604f2009-06-16 13:12:53 -07002476 if (animate) {
2477 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2478 } else {
2479 setGesturesPrompt(null, getString(R.string.gestures_unknown));
2480 }
Romain Guy73b979d2009-06-09 12:57:21 -07002481 }
2482 }
2483 }
2484
2485 private void updatePrompt(ApplicationInfo info) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002486 updatePrompt(info, true);
2487 }
2488
2489 private void updatePrompt(ApplicationInfo info, boolean animate) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002490 if (mGesturesAction.intent != null &&
2491 info.intent.toURI().equals(mGesturesAction.intent.toURI()) &&
2492 info.title.equals(((TextView) mGesturesPrompt.getCurrentView()).getText())) {
2493 return;
2494 }
Romain Guy3cf604f2009-06-16 13:12:53 -07002495
2496 if (animate) {
2497 setGesturesNextPrompt(info.icon, info.title);
2498 } else {
2499 setGesturesPrompt(info.icon, info.title);
2500 }
2501
Romain Guy73b979d2009-06-09 12:57:21 -07002502 mGesturesAction.intent = info.intent;
2503 }
2504
2505 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002506 //noinspection PointlessBooleanExpression,ConstantConditions
2507 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2508 overlay.removeCallbacks(mMatcher);
2509 }
Romain Guy73b979d2009-06-09 12:57:21 -07002510 }
2511
2512 void addGesture(String name, Gesture gesture) {
2513 sLibrary.addGesture(name, gesture);
2514 // TODO: On a background thread?
2515 sLibrary.save();
2516 }
2517
2518 void update(ApplicationInfo info, Gesture gesture) {
2519 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002520 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002521 }
2522
2523 class GestureMatcher implements Runnable {
2524 Gesture gesture;
2525
2526 public void run() {
2527 if (gesture != null) {
2528 matchGesture(gesture);
2529 }
2530 }
2531 }
2532 }
2533
2534 private class GesturesAction implements View.OnClickListener {
2535 Intent intent;
2536
2537 public void onClick(View v) {
2538 if (intent != null) {
2539 startActivitySafely(intent);
2540 }
2541 }
2542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543}
Karl Rosaen138a0412009-04-23 19:00:21 -07002544