blob: 328a5ca2b0280149a4fc4acdf0ff0ea418a16c75 [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 Guy1ce1a242009-06-23 17:34:54 -0700522 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_PENDING_GESTURE);
523
Romain Guy3cf604f2009-06-16 13:12:53 -0700524 boolean gesturesShowing = savedState.getBoolean(RUNTIME_STATE_GESTURES_PANEL, false);
525 if (gesturesShowing) {
526 final Gesture gesture = (Gesture) savedState.get(RUNTIME_STATE_GESTURES_PANEL_GESTURE);
527 mWorkspace.post(new Runnable() {
528 public void run() {
529 showGesturesPanel(false);
530 mGesturesProcessor.matchGesture(gesture, false);
531 mWorkspace.post(new Runnable() {
532 public void run() {
533 if (gesture != null) {
534 mGesturesOverlay.setGesture(gesture);
535 }
536 }
537 });
538 }
539 });
540 }
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
Romain Guy1ce1a242009-06-23 17:34:54 -07001059 if (mGesturesWindow != null && mGesturesWindow.isShowing()) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001060 outState.putBoolean(RUNTIME_STATE_GESTURES_PANEL, true);
1061
Romain Guy1ce1a242009-06-23 17:34:54 -07001062 if (mCurrentGesture == null || !mWaitingForResult) {
1063 final Gesture gesture = mGesturesOverlay.getGesture();
1064 if (gesture != null) {
1065 outState.putParcelable(RUNTIME_STATE_GESTURES_PANEL_GESTURE, gesture);
1066 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001067 }
1068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 }
1070
1071 @Override
1072 public void onDestroy() {
1073 mDestroyed = true;
1074
1075 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001080 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 }
1082
1083 TextKeyListener.getInstance().release();
1084
Romain Guy3cf604f2009-06-16 13:12:53 -07001085 hideGesturesPanel(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 mAllAppsGrid.clearTextFilter();
1087 mAllAppsGrid.setAdapter(null);
1088 sModel.unbind();
1089 sModel.abortLoaders();
1090
1091 getContentResolver().unregisterContentObserver(mObserver);
1092 unregisterReceiver(mApplicationsReceiver);
1093 }
1094
1095 @Override
1096 public void startActivityForResult(Intent intent, int requestCode) {
1097 mWaitingForResult = true;
1098 super.startActivityForResult(intent, requestCode);
1099 }
1100
1101 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001102 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001104
1105 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001106
Karl Rosaen138a0412009-04-23 19:00:21 -07001107 // Slide the search widget to the top, if it's on the current screen,
1108 // otherwise show the search dialog immediately.
1109 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1110 if (searchWidget == null) {
1111 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1112 } else {
1113 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1114 // show the currently typed text in the search widget while sliding
1115 searchWidget.setQuery(getTypedText());
1116 }
1117 }
Romain Guycbb89e42009-06-08 15:52:54 -07001118
Karl Rosaen138a0412009-04-23 19:00:21 -07001119 /**
1120 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001121 *
1122 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001123 */
Romain Guycbb89e42009-06-08 15:52:54 -07001124 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001125 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001126
Karl Rosaen138a0412009-04-23 19:00:21 -07001127 if (initialQuery == null) {
1128 // Use any text typed in the launcher as the initial query
1129 initialQuery = getTypedText();
1130 clearTypedText();
1131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 if (appSearchData == null) {
1133 appSearchData = new Bundle();
1134 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1135 }
Romain Guycbb89e42009-06-08 15:52:54 -07001136
Karl Rosaen138a0412009-04-23 19:00:21 -07001137 final SearchManager searchManager =
1138 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1139
1140 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1141 if (searchWidget != null) {
1142 // This gets called when the user leaves the search dialog to go back to
1143 // the Launcher.
1144 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1145 public void onCancel() {
1146 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001147 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001148 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001149 });
1150 }
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Karl Rosaen138a0412009-04-23 19:00:21 -07001152 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001153 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
1155
Karl Rosaen138a0412009-04-23 19:00:21 -07001156 /**
1157 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001158 */
Romain Guy5a941392009-04-28 15:18:25 -07001159 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001160 // Close search dialog
1161 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1162 if (searchManager.isVisible()) {
1163 searchManager.stopSearch();
1164 }
1165 // Restore search widget to its normal position
1166 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1167 if (searchWidget != null) {
1168 searchWidget.stopSearch(false);
1169 }
1170 }
Romain Guycbb89e42009-06-08 15:52:54 -07001171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 @Override
1173 public boolean onCreateOptionsMenu(Menu menu) {
1174 if (mDesktopLocked) return false;
1175
1176 super.onCreateOptionsMenu(menu);
1177 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1178 .setIcon(android.R.drawable.ic_menu_add)
1179 .setAlphabeticShortcut('A');
1180 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1181 .setIcon(android.R.drawable.ic_menu_gallery)
1182 .setAlphabeticShortcut('W');
1183 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1184 .setIcon(android.R.drawable.ic_search_category_default)
1185 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1186 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1187 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1188 .setAlphabeticShortcut('N');
1189
Romain Guy73b979d2009-06-09 12:57:21 -07001190 final Intent gestures = new Intent(this, GesturesActivity.class);
1191 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1192 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1193 .setIntent(gestures);
1194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001196 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1197 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198
1199 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1200 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1201 .setIntent(settings);
1202
1203 return true;
1204 }
1205
1206 @Override
1207 public boolean onPrepareOptionsMenu(Menu menu) {
1208 super.onPrepareOptionsMenu(menu);
1209
1210 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1211 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1212
1213 return true;
1214 }
1215
1216 @Override
1217 public boolean onOptionsItemSelected(MenuItem item) {
1218 switch (item.getItemId()) {
1219 case MENU_ADD:
1220 addItems();
1221 return true;
1222 case MENU_WALLPAPER_SETTINGS:
1223 startWallpaper();
1224 return true;
1225 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001226 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 return true;
1228 case MENU_NOTIFICATIONS:
1229 showNotifications();
1230 return true;
1231 }
1232
1233 return super.onOptionsItemSelected(item);
1234 }
Romain Guycbb89e42009-06-08 15:52:54 -07001235
Karl Rosaen138a0412009-04-23 19:00:21 -07001236 /**
1237 * Indicates that we want global search for this activity by setting the globalSearch
1238 * argument for {@link #startSearch} to true.
1239 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001241 @Override
1242 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001243 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001244 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001245 }
1246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 private void addItems() {
1248 showAddDialog(mMenuAddInfo);
1249 }
1250
1251 private void removeShortcutsForPackage(String packageName) {
1252 if (packageName != null && packageName.length() > 0) {
1253 mWorkspace.removeShortcutsForPackage(packageName);
1254 }
1255 }
Romain Guycbb89e42009-06-08 15:52:54 -07001256
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001257 private void updateShortcutsForPackage(String packageName) {
1258 if (packageName != null && packageName.length() > 0) {
1259 mWorkspace.updateShortcutsForPackage(packageName);
1260 }
1261 }
1262
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001263 void addAppWidget(Intent data) {
1264 // TODO: catch bad widget exception when sent
1265 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001267 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1268 if (SEARCH_WIDGET.equals(customWidget)) {
1269 // We don't need this any more, since this isn't a real app widget.
1270 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1271 // add the search widget
1272 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001274 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1275
1276 if (appWidget.configure != null) {
1277 // Launch over to configure widget, if needed
1278 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1279 intent.setComponent(appWidget.configure);
1280 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1281
1282 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1283 } else {
1284 // Otherwise just add it
1285 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1286 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
1288 }
Romain Guycbb89e42009-06-08 15:52:54 -07001289
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 void addSearch() {
1291 final Widget info = Widget.makeSearch();
1292 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001293
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 final int[] xy = mCellCoordinates;
1295 final int spanX = info.spanX;
1296 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001297
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001299
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 sModel.addDesktopItem(info);
1301 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1302 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 final View view = mInflater.inflate(info.layoutResource, null);
1305 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001306 Search search = (Search) view.findViewById(R.id.widget_search);
1307 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001308
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1310 }
1311
Romain Guy73b979d2009-06-09 12:57:21 -07001312 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001313 // Handle case where user selected "Applications"
1314 String applicationName = getResources().getString(R.string.group_applications);
1315 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001316
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001317 if (applicationName != null && applicationName.equals(shortcutName)) {
1318 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1319 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001320
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001321 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1322 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001323 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001324 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001325 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001326 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328
1329 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001330 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001331 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001332 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001333
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001334 if (folderName != null && folderName.equals(shortcutName)) {
1335 addFolder(!mDesktopLocked);
1336 } else {
1337 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1338 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 }
1340
1341 void addFolder(boolean insertAtFirst) {
1342 UserFolderInfo folderInfo = new UserFolderInfo();
1343 folderInfo.title = getText(R.string.folder_name);
1344
1345 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1346 cellInfo.screen = mWorkspace.getCurrentScreen();
1347 if (!findSingleSlot(cellInfo)) return;
1348
1349 // Update the model
1350 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1351 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1352 sModel.addDesktopItem(folderInfo);
1353 sModel.addFolder(folderInfo);
1354
1355 // Create the view
1356 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1357 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1358 mWorkspace.addInCurrentScreen(newFolder,
1359 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1360 }
Romain Guycbb89e42009-06-08 15:52:54 -07001361
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1363 boolean insertAtFirst) {
1364 cellInfo.screen = mWorkspace.getCurrentScreen();
1365 if (!findSingleSlot(cellInfo)) return;
1366
1367 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1368
1369 if (!mRestoring) {
1370 sModel.addDesktopItem(info);
1371
1372 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1373 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1374 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1375 } else if (sModel.isDesktopLoaded()) {
1376 sModel.addDesktopItem(info);
1377 }
1378 }
1379
1380 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1381 CellLayout.CellInfo cellInfo, boolean notify) {
1382
1383 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1384 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1385
1386 Drawable icon = null;
1387 boolean filtered = false;
1388 Intent.ShortcutIconResource iconResource = null;
1389
1390 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1391 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1392 try {
1393 iconResource = (Intent.ShortcutIconResource) extra;
1394 final PackageManager packageManager = context.getPackageManager();
1395 Resources resources = packageManager.getResourcesForApplication(
1396 iconResource.packageName);
1397 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1398 icon = resources.getDrawable(id);
1399 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001400 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 }
1402 }
1403
1404 if (icon == null) {
1405 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1406 }
1407
1408 final LiveFolderInfo info = new LiveFolderInfo();
1409 info.icon = icon;
1410 info.filtered = filtered;
1411 info.title = name;
1412 info.iconResource = iconResource;
1413 info.uri = data.getData();
1414 info.baseIntent = baseIntent;
1415 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1416 LiveFolders.DISPLAY_MODE_GRID);
1417
1418 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1419 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1420 sModel.addFolder(info);
1421
1422 return info;
1423 }
1424
1425 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1426 final int[] xy = new int[2];
1427 if (findSlot(cellInfo, xy, 1, 1)) {
1428 cellInfo.cellX = xy[0];
1429 cellInfo.cellY = xy[1];
1430 return true;
1431 }
1432 return false;
1433 }
1434
1435 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1436 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1437 boolean[] occupied = mSavedState != null ?
1438 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1439 cellInfo = mWorkspace.findAllVacantCells(occupied);
1440 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1441 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1442 return false;
1443 }
1444 }
1445 return true;
1446 }
1447
1448 private void showNotifications() {
1449 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1450 if (statusBar != null) {
1451 statusBar.expand();
1452 }
1453 }
1454
1455 private void startWallpaper() {
1456 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1457 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1458 }
1459
1460 /**
1461 * Registers various intent receivers. The current implementation registers
1462 * only a wallpaper intent receiver to let other applications change the
1463 * wallpaper.
1464 */
1465 private void registerIntentReceivers() {
1466 if (sWallpaperReceiver == null) {
1467 final Application application = getApplication();
1468
1469 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1470
1471 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1472 application.registerReceiver(sWallpaperReceiver, filter);
1473 } else {
1474 sWallpaperReceiver.setLauncher(this);
1475 }
1476
1477 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1478 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1479 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1480 filter.addDataScheme("package");
1481 registerReceiver(mApplicationsReceiver, filter);
1482 }
1483
1484 /**
1485 * Registers various content observers. The current implementation registers
1486 * only a favorites observer to keep track of the favorites applications.
1487 */
1488 private void registerContentObservers() {
1489 ContentResolver resolver = getContentResolver();
1490 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1491 }
1492
1493 @Override
1494 public boolean dispatchKeyEvent(KeyEvent event) {
1495 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1496 switch (event.getKeyCode()) {
1497 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001498 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001499 if (mDrawer.isOpened()) {
1500 closeDrawer();
1501 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001502 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001503 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 return true;
1505 case KeyEvent.KEYCODE_HOME:
1506 return true;
1507 }
1508 }
1509
1510 return super.dispatchKeyEvent(event);
1511 }
1512
1513 private void closeDrawer() {
1514 closeDrawer(true);
1515 }
1516
1517 private void closeDrawer(boolean animated) {
1518 if (mDrawer.isOpened()) {
1519 if (animated) {
1520 mDrawer.animateClose();
1521 } else {
1522 mDrawer.close();
1523 }
1524 if (mDrawer.hasFocus()) {
1525 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1526 }
1527 }
1528 }
1529
1530 private void closeFolder() {
1531 Folder folder = mWorkspace.getOpenFolder();
1532 if (folder != null) {
1533 closeFolder(folder);
1534 }
1535 }
1536
1537 void closeFolder(Folder folder) {
1538 folder.getInfo().opened = false;
1539 ViewGroup parent = (ViewGroup) folder.getParent();
1540 if (parent != null) {
1541 parent.removeView(folder);
1542 }
1543 folder.onClose();
1544 }
1545
1546 /**
1547 * When the notification that favorites have changed is received, requests
1548 * a favorites list refresh.
1549 */
1550 private void onFavoritesChanged() {
1551 mDesktopLocked = true;
1552 mDrawer.lock();
1553 sModel.loadUserItems(false, this, false, false);
1554 }
1555
1556 void onDesktopItemsLoaded() {
1557 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 bindDesktopItems();
1559 }
Romain Guycbb89e42009-06-08 15:52:54 -07001560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 /**
1562 * Refreshes the shortcuts shown on the workspace.
1563 */
1564 private void bindDesktopItems() {
1565 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001566 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001567 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1568 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 return;
1570 }
1571
1572 final Workspace workspace = mWorkspace;
1573 int count = workspace.getChildCount();
1574 for (int i = 0; i < count; i++) {
1575 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1576 }
Romain Guycbb89e42009-06-08 15:52:54 -07001577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 if (DEBUG_USER_INTERFACE) {
1579 android.widget.Button finishButton = new android.widget.Button(this);
1580 finishButton.setText("Finish");
1581 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1582
1583 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1584 public void onClick(View v) {
1585 finish();
1586 }
1587 });
1588 }
Romain Guycbb89e42009-06-08 15:52:54 -07001589
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001590 // Flag any old binder to terminate early
1591 if (mBinder != null) {
1592 mBinder.mTerminate = true;
1593 }
Romain Guycbb89e42009-06-08 15:52:54 -07001594
Karl Rosaen138a0412009-04-23 19:00:21 -07001595 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001596 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 }
1598
1599 private void bindItems(Launcher.DesktopBinder binder,
1600 ArrayList<ItemInfo> shortcuts, int start, int count) {
1601
1602 final Workspace workspace = mWorkspace;
1603 final boolean desktopLocked = mDesktopLocked;
1604
1605 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1606 int i = start;
1607
1608 for ( ; i < end; i++) {
1609 final ItemInfo item = shortcuts.get(i);
1610 switch (item.itemType) {
1611 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1612 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1613 final View shortcut = createShortcut((ApplicationInfo) item);
1614 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1615 !desktopLocked);
1616 break;
1617 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1618 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1619 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1620 (UserFolderInfo) item);
1621 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1622 !desktopLocked);
1623 break;
1624 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1625 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1626 R.layout.live_folder_icon, this,
1627 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1628 (LiveFolderInfo) item);
1629 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1630 !desktopLocked);
1631 break;
1632 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1633 final int screen = workspace.getCurrentScreen();
1634 final View view = mInflater.inflate(R.layout.widget_search,
1635 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001636
Karl Rosaen138a0412009-04-23 19:00:21 -07001637 Search search = (Search) view.findViewById(R.id.widget_search);
1638 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001639
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 final Widget widget = (Widget) item;
1641 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001642
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 workspace.addWidget(view, widget, !desktopLocked);
1644 break;
1645 }
1646 }
1647
1648 workspace.requestLayout();
1649
1650 if (end >= count) {
1651 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001652 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 } else {
1654 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1655 }
1656 }
1657
1658 private void finishBindDesktopItems() {
1659 if (mSavedState != null) {
1660 if (!mWorkspace.hasFocus()) {
1661 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1662 }
1663
1664 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1665 if (userFolders != null) {
1666 for (long folderId : userFolders) {
1667 final FolderInfo info = sModel.findFolderById(folderId);
1668 if (info != null) {
1669 openFolder(info);
1670 }
1671 }
1672 final Folder openFolder = mWorkspace.getOpenFolder();
1673 if (openFolder != null) {
1674 openFolder.requestFocus();
1675 }
1676 }
1677
1678 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1679 if (allApps) {
1680 mDrawer.open();
1681 }
1682
1683 mSavedState = null;
1684 }
1685
1686 if (mSavedInstanceState != null) {
1687 super.onRestoreInstanceState(mSavedInstanceState);
1688 mSavedInstanceState = null;
1689 }
1690
1691 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1692 mDrawer.requestFocus();
1693 }
1694
1695 mDesktopLocked = false;
1696 mDrawer.unlock();
1697 }
Romain Guycbb89e42009-06-08 15:52:54 -07001698
Karl Rosaen138a0412009-04-23 19:00:21 -07001699 private void bindDrawer(Launcher.DesktopBinder binder,
1700 ApplicationsAdapter drawerAdapter) {
1701 mAllAppsGrid.setAdapter(drawerAdapter);
1702 binder.startBindingAppWidgetsWhenIdle();
1703 }
Romain Guycbb89e42009-06-08 15:52:54 -07001704
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001705 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001706 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001707
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 final Workspace workspace = mWorkspace;
1709 final boolean desktopLocked = mDesktopLocked;
1710
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001711 if (!appWidgets.isEmpty()) {
1712 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001713
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001714 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001715 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001716 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001717
Karl Rosaen138a0412009-04-23 19:00:21 -07001718 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001719
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001720 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1721 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001722
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001723 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1724 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001725
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001726 workspace.requestLayout();
1727 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001729 if (appWidgets.isEmpty()) {
1730 if (PROFILE_ROTATE) {
1731 android.os.Debug.stopMethodTracing();
1732 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001734 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 }
1736 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001737
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 DragController getDragController() {
1739 return mDragLayer;
1740 }
1741
1742 /**
1743 * Launches the intent referred by the clicked shortcut.
1744 *
1745 * @param v The view representing the clicked shortcut.
1746 */
1747 public void onClick(View v) {
1748 Object tag = v.getTag();
1749 if (tag instanceof ApplicationInfo) {
1750 // Open shortcut
1751 final Intent intent = ((ApplicationInfo) tag).intent;
1752 startActivitySafely(intent);
1753 } else if (tag instanceof FolderInfo) {
1754 handleFolderClick((FolderInfo) tag);
1755 }
1756 }
1757
1758 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001759 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1761 try {
1762 startActivity(intent);
1763 } catch (ActivityNotFoundException e) {
1764 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1765 } catch (SecurityException e) {
1766 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001767 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1769 "or use the exported attribute for this activity.", e);
1770 }
1771 }
1772
1773 private void handleFolderClick(FolderInfo folderInfo) {
1774 if (!folderInfo.opened) {
1775 // Close any open folder
1776 closeFolder();
1777 // Open the requested folder
1778 openFolder(folderInfo);
1779 } else {
1780 // Find the open folder...
1781 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1782 int folderScreen;
1783 if (openFolder != null) {
1784 folderScreen = mWorkspace.getScreenForView(openFolder);
1785 // .. and close it
1786 closeFolder(openFolder);
1787 if (folderScreen != mWorkspace.getCurrentScreen()) {
1788 // Close any folder open on the current screen
1789 closeFolder();
1790 // Pull the folder onto this screen
1791 openFolder(folderInfo);
1792 }
1793 }
1794 }
1795 }
1796
1797 private void loadWallpaper() {
1798 // The first time the application is started, we load the wallpaper from
1799 // the ApplicationContext
1800 if (sWallpaper == null) {
1801 final Drawable drawable = getWallpaper();
1802 if (drawable instanceof BitmapDrawable) {
1803 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1804 } else {
1805 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1806 }
1807 }
1808 mWorkspace.loadWallpaper(sWallpaper);
1809 }
1810
1811 /**
1812 * Opens the user fodler described by the specified tag. The opening of the folder
1813 * is animated relative to the specified View. If the View is null, no animation
1814 * is played.
1815 *
1816 * @param folderInfo The FolderInfo describing the folder to open.
1817 */
1818 private void openFolder(FolderInfo folderInfo) {
1819 Folder openFolder;
1820
1821 if (folderInfo instanceof UserFolderInfo) {
1822 openFolder = UserFolder.fromXml(this);
1823 } else if (folderInfo instanceof LiveFolderInfo) {
1824 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1825 } else {
1826 return;
1827 }
1828
1829 openFolder.setDragger(mDragLayer);
1830 openFolder.setLauncher(this);
1831
1832 openFolder.bind(folderInfo);
1833 folderInfo.opened = true;
1834
1835 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1836 openFolder.onOpen();
1837 }
1838
1839 /**
1840 * Returns true if the workspace is being loaded. When the workspace is loading,
1841 * no user interaction should be allowed to avoid any conflict.
1842 *
1843 * @return True if the workspace is locked, false otherwise.
1844 */
1845 boolean isWorkspaceLocked() {
1846 return mDesktopLocked;
1847 }
1848
1849 public boolean onLongClick(View v) {
1850 if (mDesktopLocked) {
1851 return false;
1852 }
1853
1854 if (!(v instanceof CellLayout)) {
1855 v = (View) v.getParent();
1856 }
1857
1858 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1859
1860 // This happens when long clicking an item with the dpad/trackball
1861 if (cellInfo == null) {
1862 return true;
1863 }
1864
1865 if (mWorkspace.allowLongPress()) {
1866 if (cellInfo.cell == null) {
1867 if (cellInfo.valid) {
1868 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001869 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 showAddDialog(cellInfo);
1871 }
1872 } else {
1873 if (!(cellInfo.cell instanceof Folder)) {
1874 // User long pressed on an item
1875 mWorkspace.startDrag(cellInfo);
1876 }
1877 }
1878 }
1879 return true;
1880 }
1881
1882 static LauncherModel getModel() {
1883 return sModel;
1884 }
1885
Romain Guy73b979d2009-06-09 12:57:21 -07001886 static GestureLibrary getGestureLibrary() {
1887 return sLibrary;
1888 }
1889
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 void closeAllApplications() {
1891 mDrawer.close();
1892 }
1893
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001894 View getDrawerHandle() {
1895 return mHandleView;
1896 }
1897
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 boolean isDrawerDown() {
1899 return !mDrawer.isMoving() && !mDrawer.isOpened();
1900 }
1901
1902 boolean isDrawerUp() {
1903 return mDrawer.isOpened() && !mDrawer.isMoving();
1904 }
1905
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001906 boolean isDrawerMoving() {
1907 return mDrawer.isMoving();
1908 }
1909
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 Workspace getWorkspace() {
1911 return mWorkspace;
1912 }
1913
1914 GridView getApplicationsGrid() {
1915 return mAllAppsGrid;
1916 }
1917
1918 @Override
1919 protected Dialog onCreateDialog(int id) {
1920 switch (id) {
1921 case DIALOG_CREATE_SHORTCUT:
1922 return new CreateShortcut().createDialog();
1923 case DIALOG_RENAME_FOLDER:
1924 return new RenameFolder().createDialog();
1925 }
1926
1927 return super.onCreateDialog(id);
1928 }
1929
1930 @Override
1931 protected void onPrepareDialog(int id, Dialog dialog) {
1932 switch (id) {
1933 case DIALOG_CREATE_SHORTCUT:
1934 mWorkspace.lock();
1935 break;
1936 case DIALOG_RENAME_FOLDER:
1937 mWorkspace.lock();
1938 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1939 final CharSequence text = mFolderInfo.title;
1940 input.setText(text);
Romain Guycbb89e42009-06-08 15:52:54 -07001941 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 break;
1943 }
1944 }
1945
1946 void showRenameDialog(FolderInfo info) {
1947 mFolderInfo = info;
1948 mWaitingForResult = true;
1949 showDialog(DIALOG_RENAME_FOLDER);
1950 }
1951
1952 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1953 mAddItemCellInfo = cellInfo;
1954 mWaitingForResult = true;
1955 showDialog(DIALOG_CREATE_SHORTCUT);
1956 }
1957
Romain Guy73b979d2009-06-09 12:57:21 -07001958 private void pickShortcut(int requestCode, int title) {
1959 Bundle bundle = new Bundle();
1960
1961 ArrayList<String> shortcutNames = new ArrayList<String>();
1962 shortcutNames.add(getString(R.string.group_applications));
1963 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1964
1965 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1966 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1967 R.drawable.ic_launcher_application));
1968 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1969
1970 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1971 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1972 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1973 pickIntent.putExtras(bundle);
1974
1975 startActivityForResult(pickIntent, requestCode);
1976 }
1977
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 private class RenameFolder {
1979 private EditText mInput;
1980
1981 Dialog createDialog() {
1982 mWaitingForResult = true;
1983 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1984 mInput = (EditText) layout.findViewById(R.id.folder_name);
1985
1986 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1987 builder.setIcon(0);
1988 builder.setTitle(getString(R.string.rename_folder_title));
1989 builder.setCancelable(true);
1990 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1991 public void onCancel(DialogInterface dialog) {
1992 cleanup();
1993 }
1994 });
1995 builder.setNegativeButton(getString(R.string.cancel_action),
1996 new Dialog.OnClickListener() {
1997 public void onClick(DialogInterface dialog, int which) {
1998 cleanup();
1999 }
2000 }
2001 );
2002 builder.setPositiveButton(getString(R.string.rename_action),
2003 new Dialog.OnClickListener() {
2004 public void onClick(DialogInterface dialog, int which) {
2005 changeFolderName();
2006 }
2007 }
2008 );
2009 builder.setView(layout);
2010 return builder.create();
2011 }
2012
2013 private void changeFolderName() {
2014 final String name = mInput.getText().toString();
2015 if (!TextUtils.isEmpty(name)) {
2016 // Make sure we have the right folder info
2017 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
2018 mFolderInfo.title = name;
2019 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2020
2021 if (mDesktopLocked) {
2022 mDrawer.lock();
2023 sModel.loadUserItems(false, Launcher.this, false, false);
2024 } else {
2025 final FolderIcon folderIcon = (FolderIcon)
2026 mWorkspace.getViewForTag(mFolderInfo);
2027 if (folderIcon != null) {
2028 folderIcon.setText(name);
2029 getWorkspace().requestLayout();
2030 } else {
2031 mDesktopLocked = true;
2032 mDrawer.lock();
2033 sModel.loadUserItems(false, Launcher.this, false, false);
2034 }
2035 }
2036 }
2037 cleanup();
2038 }
2039
2040 private void cleanup() {
2041 mWorkspace.unlock();
2042 dismissDialog(DIALOG_RENAME_FOLDER);
2043 mWaitingForResult = false;
2044 mFolderInfo = null;
2045 }
2046 }
2047
2048 /**
2049 * Displays the shortcut creation dialog and launches, if necessary, the
2050 * appropriate activity.
2051 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002052 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07002053 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002055
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 Dialog createDialog() {
2057 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002060
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2062 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002063 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 builder.setInverseBackgroundForced(true);
2066
2067 AlertDialog dialog = builder.create();
2068 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002069 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 return dialog;
2072 }
2073
2074 public void onCancel(DialogInterface dialog) {
2075 mWaitingForResult = false;
2076 cleanup();
2077 }
2078
Romain Guycbb89e42009-06-08 15:52:54 -07002079 public void onDismiss(DialogInterface dialog) {
2080 mWorkspace.unlock();
2081 }
2082
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 private void cleanup() {
2084 mWorkspace.unlock();
2085 dismissDialog(DIALOG_CREATE_SHORTCUT);
2086 }
2087
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002088 /**
2089 * Handle the action clicked in the "Add to home" dialog.
2090 */
2091 public void onClick(DialogInterface dialog, int which) {
2092 Resources res = getResources();
2093 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002094
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002095 switch (which) {
2096 case AddAdapter.ITEM_SHORTCUT: {
2097 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002098 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002099 break;
2100 }
Romain Guycbb89e42009-06-08 15:52:54 -07002101
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002102 case AddAdapter.ITEM_APPWIDGET: {
2103 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002104
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002105 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2106 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2107 // add the search widget
2108 ArrayList<AppWidgetProviderInfo> customInfo =
2109 new ArrayList<AppWidgetProviderInfo>();
2110 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2111 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2112 info.label = getString(R.string.group_search);
2113 info.icon = R.drawable.ic_search_widget;
2114 customInfo.add(info);
2115 pickIntent.putParcelableArrayListExtra(
2116 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2117 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2118 Bundle b = new Bundle();
2119 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2120 customExtras.add(b);
2121 pickIntent.putParcelableArrayListExtra(
2122 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2123 // start the pick activity
2124 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2125 break;
2126 }
Romain Guycbb89e42009-06-08 15:52:54 -07002127
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002128 case AddAdapter.ITEM_LIVE_FOLDER: {
2129 // Insert extra item to handle inserting folder
2130 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002131
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002132 ArrayList<String> shortcutNames = new ArrayList<String>();
2133 shortcutNames.add(res.getString(R.string.group_folder));
2134 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002135
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002136 ArrayList<ShortcutIconResource> shortcutIcons =
2137 new ArrayList<ShortcutIconResource>();
2138 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2139 R.drawable.ic_launcher_folder));
2140 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2141
2142 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2143 pickIntent.putExtra(Intent.EXTRA_INTENT,
2144 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2145 pickIntent.putExtra(Intent.EXTRA_TITLE,
2146 getText(R.string.title_select_live_folder));
2147 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002148
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002149 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2150 break;
2151 }
2152
2153 case AddAdapter.ITEM_WALLPAPER: {
2154 startWallpaper();
2155 break;
2156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157 }
2158 }
2159 }
2160
2161 /**
2162 * Receives notifications when applications are added/removed.
2163 */
2164 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2165 @Override
2166 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002167 final String action = intent.getAction();
2168 final String packageName = intent.getData().getSchemeSpecificPart();
2169 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2170
2171 if (LauncherModel.DEBUG_LOADERS) {
2172 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2173 ", replacing=" + replacing);
2174 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002176
2177 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2178 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2179 if (!replacing) {
2180 removeShortcutsForPackage(packageName);
2181 if (LauncherModel.DEBUG_LOADERS) {
2182 d(LauncherModel.LOG_TAG, " --> remove package");
2183 }
2184 sModel.removePackage(Launcher.this, packageName);
2185 }
2186 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2187 // later, we will update the package at this time
2188 } else {
2189 if (!replacing) {
2190 if (LauncherModel.DEBUG_LOADERS) {
2191 d(LauncherModel.LOG_TAG, " --> add package");
2192 }
2193 sModel.addPackage(Launcher.this, packageName);
2194 } else {
2195 if (LauncherModel.DEBUG_LOADERS) {
2196 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2197 }
2198 sModel.updatePackage(Launcher.this, packageName);
2199 updateShortcutsForPackage(packageName);
2200 }
2201 }
2202 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002204 if (LauncherModel.DEBUG_LOADERS) {
2205 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2206 }
2207 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 }
2209 }
2210 }
2211
2212 /**
2213 * Receives notifications whenever the user favorites have changed.
2214 */
2215 private class FavoritesChangeObserver extends ContentObserver {
2216 public FavoritesChangeObserver() {
2217 super(new Handler());
2218 }
2219
2220 @Override
2221 public void onChange(boolean selfChange) {
2222 onFavoritesChanged();
2223 }
2224 }
2225
2226 /**
2227 * Receives intents from other applications to change the wallpaper.
2228 */
2229 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2230 private final Application mApplication;
2231 private WeakReference<Launcher> mLauncher;
2232
2233 WallpaperIntentReceiver(Application application, Launcher launcher) {
2234 mApplication = application;
2235 setLauncher(launcher);
2236 }
2237
2238 void setLauncher(Launcher launcher) {
2239 mLauncher = new WeakReference<Launcher>(launcher);
2240 }
2241
2242 @Override
2243 public void onReceive(Context context, Intent intent) {
2244 // Load the wallpaper from the ApplicationContext and store it locally
2245 // until the Launcher Activity is ready to use it
2246 final Drawable drawable = mApplication.getWallpaper();
2247 if (drawable instanceof BitmapDrawable) {
2248 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2249 } else {
2250 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2251 }
2252
2253 // If Launcher is alive, notify we have a new wallpaper
2254 if (mLauncher != null) {
2255 final Launcher launcher = mLauncher.get();
2256 if (launcher != null) {
2257 launcher.loadWallpaper();
2258 }
2259 }
2260 }
2261 }
2262
2263 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2264 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2265 private boolean mOpen;
2266
2267 public void onDrawerOpened() {
2268 if (!mOpen) {
2269 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002272 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273
2274 mOpen = true;
2275 }
2276 }
2277
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002278 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2279 view.getDrawingRect(bounds);
2280
2281 while (view != mDragLayer) {
2282 bounds.offset(view.getLeft(), view.getTop());
2283 view = (View) view.getParent();
2284 }
2285 }
2286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 public void onDrawerClosed() {
2288 if (mOpen) {
2289 mHandleIcon.reverseTransition(150);
2290 mWorkspace.mDrawerBounds.setEmpty();
2291 mOpen = false;
2292 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002293
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002294 mAllAppsGrid.setSelection(0);
2295 mAllAppsGrid.clearTextFilter();
2296 }
2297
2298 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002299 if (PROFILE_DRAWER) {
2300 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2301 }
2302
2303 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2304 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305 }
2306
2307 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002308 if (PROFILE_DRAWER) {
2309 android.os.Debug.stopMethodTracing();
2310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 }
2312 }
2313
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002314 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002316 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002317 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 // Number of items to bind in every pass
2320 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321
2322 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002323 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002324 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002326
Karl Rosaen138a0412009-04-23 19:00:21 -07002327 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328
2329 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002330 ArrayList<LauncherAppWidgetInfo> appWidgets,
2331 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332
2333 mLauncher = new WeakReference<Launcher>(launcher);
2334 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002335 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002336
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002337 // Sort widgets so active workspace is bound first
2338 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2339 final int size = appWidgets.size();
2340 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002341
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002342 for (int i = 0; i < size; i++) {
2343 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2344 if (appWidgetInfo.screen == currentScreen) {
2345 mAppWidgets.addFirst(appWidgetInfo);
2346 } else {
2347 mAppWidgets.addLast(appWidgetInfo);
2348 }
2349 }
2350 }
Romain Guycbb89e42009-06-08 15:52:54 -07002351
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 public void startBindingItems() {
2353 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2354 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002355
2356 public void startBindingDrawer() {
2357 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2358 }
Romain Guycbb89e42009-06-08 15:52:54 -07002359
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002360 public void startBindingAppWidgetsWhenIdle() {
2361 // Ask for notification when message queue becomes idle
2362 final MessageQueue messageQueue = Looper.myQueue();
2363 messageQueue.addIdleHandler(this);
2364 }
Romain Guycbb89e42009-06-08 15:52:54 -07002365
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002366 public boolean queueIdle() {
2367 // Queue is idle, so start binding items
2368 startBindingAppWidgets();
2369 return false;
2370 }
2371
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002372 public void startBindingAppWidgets() {
2373 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2374 }
2375
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 @Override
2377 public void handleMessage(Message msg) {
2378 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002379 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 return;
2381 }
Romain Guycbb89e42009-06-08 15:52:54 -07002382
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 switch (msg.what) {
2384 case MESSAGE_BIND_ITEMS: {
2385 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2386 break;
2387 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002388 case MESSAGE_BIND_DRAWER: {
2389 launcher.bindDrawer(this, mDrawerAdapter);
2390 break;
2391 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002392 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002393 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 break;
2395 }
2396 }
2397 }
2398 }
Romain Guy73b979d2009-06-09 12:57:21 -07002399
2400 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2401 GestureOverlayView.OnGesturePerformedListener {
2402
2403 private final GestureMatcher mMatcher = new GestureMatcher();
2404
2405 GesturesProcessor() {
2406 // TODO: Maybe the load should happen on a background thread?
2407 sLibrary.load();
2408 }
2409
2410 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002411 //noinspection PointlessBooleanExpression,ConstantConditions
2412 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2413 overlay.removeCallbacks(mMatcher);
2414 resetGesturesNextPrompt();
2415 }
Romain Guy73b979d2009-06-09 12:57:21 -07002416
2417 mGesturesAdd.setAlpha(128);
2418 mGesturesAdd.setEnabled(false);
2419 }
2420
2421 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2422 }
2423
2424 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2425 }
2426
2427 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002428 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2429 mMatcher.gesture = overlay.getGesture();
2430 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2431 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002432 if (mGesturesAction.intent != null) {
2433 mGesturesAction.intent = null;
2434 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2435 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002436 } else {
2437 mMatcher.run();
2438 }
Romain Guy73b979d2009-06-09 12:57:21 -07002439 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002440 overlay.removeCallbacks(mMatcher);
2441
2442 mMatcher.gesture = overlay.getGesture();
2443 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2444 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002445 if (mGesturesAction.intent != null) {
2446 mGesturesAction.intent = null;
2447 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2448 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002449 } else {
2450 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2451 }
Romain Guy73b979d2009-06-09 12:57:21 -07002452 }
2453 }
2454
Romain Guy3cf604f2009-06-16 13:12:53 -07002455 void matchGesture(Gesture gesture) {
2456 matchGesture(gesture, true);
2457 }
2458
2459 void matchGesture(Gesture gesture, boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07002460 mGesturesAdd.setAlpha(255);
2461 mGesturesAdd.setEnabled(true);
2462
2463 if (gesture != null) {
2464 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2465
2466 if (DEBUG_GESTURES) {
2467 for (Prediction p : predictions) {
2468 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2469 }
2470 }
2471
2472 boolean match = false;
2473 if (predictions.size() > 0) {
2474 final Prediction prediction = predictions.get(0);
2475 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2476 match = true;
2477
2478 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2479 if (info != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002480 updatePrompt(info, animate);
Romain Guy73b979d2009-06-09 12:57:21 -07002481 }
2482 }
2483 }
2484
2485 if (!match){
Romain Guy1ce1a242009-06-23 17:34:54 -07002486 mGesturesAction.intent = null;
Romain Guy3cf604f2009-06-16 13:12:53 -07002487 if (animate) {
2488 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2489 } else {
2490 setGesturesPrompt(null, getString(R.string.gestures_unknown));
2491 }
Romain Guy73b979d2009-06-09 12:57:21 -07002492 }
2493 }
2494 }
2495
2496 private void updatePrompt(ApplicationInfo info) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002497 updatePrompt(info, true);
2498 }
2499
2500 private void updatePrompt(ApplicationInfo info, boolean animate) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002501 if (mGesturesAction.intent != null &&
Romain Guy1ce1a242009-06-23 17:34:54 -07002502 info.intent.toUri(0).equals(mGesturesAction.intent.toUri(0)) &&
Romain Guy6fefcf12009-06-11 13:07:43 -07002503 info.title.equals(((TextView) mGesturesPrompt.getCurrentView()).getText())) {
2504 return;
2505 }
Romain Guy3cf604f2009-06-16 13:12:53 -07002506
2507 if (animate) {
2508 setGesturesNextPrompt(info.icon, info.title);
2509 } else {
2510 setGesturesPrompt(info.icon, info.title);
2511 }
2512
Romain Guy73b979d2009-06-09 12:57:21 -07002513 mGesturesAction.intent = info.intent;
2514 }
2515
2516 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002517 //noinspection PointlessBooleanExpression,ConstantConditions
2518 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2519 overlay.removeCallbacks(mMatcher);
2520 }
Romain Guy73b979d2009-06-09 12:57:21 -07002521 }
2522
2523 void addGesture(String name, Gesture gesture) {
2524 sLibrary.addGesture(name, gesture);
2525 // TODO: On a background thread?
2526 sLibrary.save();
2527 }
2528
2529 void update(ApplicationInfo info, Gesture gesture) {
2530 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002531 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002532 }
2533
2534 class GestureMatcher implements Runnable {
2535 Gesture gesture;
2536
2537 public void run() {
2538 if (gesture != null) {
2539 matchGesture(gesture);
2540 }
2541 }
2542 }
2543 }
2544
2545 private class GesturesAction implements View.OnClickListener {
2546 Intent intent;
2547
2548 public void onClick(View v) {
2549 if (intent != null) {
2550 startActivitySafely(intent);
2551 }
2552 }
2553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554}
Karl Rosaen138a0412009-04-23 19:00:21 -07002555