blob: 0e5be6da5d147049210b11949f6445171a337ab6 [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() {
Romain Guyf280f202009-06-26 10:31:07 -0700617 if (!mWaitingForResult) {
618 mCurrentGesture = mGesturesOverlay.getGesture();
619 mWaitingForResult = true;
620 pickShortcut(REQUEST_PICK_GESTURE_ACTION, R.string.title_select_shortcut);
621 }
Romain Guy73b979d2009-06-09 12:57:21 -0700622 }
623
624 private void completeCreateGesture(Intent data, boolean isShortcut) {
625 ApplicationInfo info;
626
627 if (isShortcut) {
628 info = infoFromShortcutIntent(this, data);
629 } else {
630 info = infoFromApplicationIntent(this, data);
631 }
632
633 boolean success = false;
634 if (info != null) {
635 info.isGesture = true;
636
637 if (LauncherModel.addGestureToDatabase(this, info, false)) {
638 mGesturesProcessor.addGesture(String.valueOf(info.id), mCurrentGesture);
639 mGesturesProcessor.update(info, mCurrentGesture);
640 Toast.makeText(this, getString(R.string.gestures_created, info.title),
641 Toast.LENGTH_SHORT).show();
642 success = true;
643 }
644 }
645
646 if (!success) {
647 Toast.makeText(this, getString(R.string.gestures_failed), Toast.LENGTH_SHORT).show();
648 }
649
650 mCurrentGesture = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
652
653 /**
654 * Creates a view representing a shortcut.
655 *
656 * @param info The data structure describing the shortcut.
657 *
658 * @return A View inflated from R.layout.application.
659 */
660 View createShortcut(ApplicationInfo info) {
661 return createShortcut(R.layout.application,
662 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
663 }
664
665 /**
666 * Creates a view representing a shortcut inflated from the specified resource.
667 *
668 * @param layoutResId The id of the XML layout used to create the shortcut.
669 * @param parent The group the shortcut belongs to.
670 * @param info The data structure describing the shortcut.
671 *
672 * @return A View inflated from layoutResId.
673 */
674 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
675 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
676
677 if (!info.filtered) {
678 info.icon = Utilities.createIconThumbnail(info.icon, this);
679 info.filtered = true;
680 }
681
682 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
683 favorite.setText(info.title);
684 favorite.setTag(info);
685 favorite.setOnClickListener(this);
686
687 return favorite;
688 }
689
690 /**
691 * Add an application shortcut to the workspace.
692 *
693 * @param data The intent describing the application.
694 * @param cellInfo The position on screen where to create the shortcut.
695 */
696 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
697 boolean insertAtFirst) {
698 cellInfo.screen = mWorkspace.getCurrentScreen();
699 if (!findSingleSlot(cellInfo)) return;
700
Romain Guy73b979d2009-06-09 12:57:21 -0700701 final ApplicationInfo info = infoFromApplicationIntent(context, data);
702 if (info != null) {
703 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
704 }
705 }
706
707 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 ComponentName component = data.getComponent();
709 PackageManager packageManager = context.getPackageManager();
710 ActivityInfo activityInfo = null;
711 try {
712 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
713 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700714 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 }
Romain Guycbb89e42009-06-08 15:52:54 -0700716
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 if (activityInfo != null) {
718 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 itemInfo.title = activityInfo.loadLabel(packageManager);
721 if (itemInfo.title == null) {
722 itemInfo.title = activityInfo.name;
723 }
Romain Guycbb89e42009-06-08 15:52:54 -0700724
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
726 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
727 itemInfo.icon = activityInfo.loadIcon(packageManager);
728 itemInfo.container = ItemInfo.NO_ID;
729
Romain Guy73b979d2009-06-09 12:57:21 -0700730 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 }
Romain Guy73b979d2009-06-09 12:57:21 -0700732
733 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 }
Romain Guycbb89e42009-06-08 15:52:54 -0700735
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 /**
737 * Add a shortcut to the workspace.
738 *
739 * @param data The intent describing the shortcut.
740 * @param cellInfo The position on screen where to create the shortcut.
741 * @param insertAtFirst
742 */
743 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
744 boolean insertAtFirst) {
745 cellInfo.screen = mWorkspace.getCurrentScreen();
746 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700747
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
749
750 if (!mRestoring) {
751 sModel.addDesktopItem(info);
752
753 final View view = createShortcut(info);
754 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
755 } else if (sModel.isDesktopLoaded()) {
756 sModel.addDesktopItem(info);
757 }
758 }
759
Romain Guycbb89e42009-06-08 15:52:54 -0700760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700762 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700764 * @param data The intent describing the appWidgetId.
765 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700767 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 boolean insertAtFirst) {
769
770 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700771 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700772
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700773 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700774
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700775 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700776
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700777 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700779 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700780
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 // Try finding open space on Launcher screen
782 final int[] xy = mCellCoordinates;
783 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
784
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700785 // Build Launcher-specific widget info and save to database
786 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 launcherInfo.spanX = spans[0];
788 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700789
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 LauncherModel.addItemToDatabase(this, launcherInfo,
791 LauncherSettings.Favorites.CONTAINER_DESKTOP,
792 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
793
794 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700795 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700796
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700798 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700799
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700800 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700802
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
804 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
805 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700806 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 }
808 }
Romain Guycbb89e42009-06-08 15:52:54 -0700809
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700810 public LauncherAppWidgetHost getAppWidgetHost() {
811 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
Romain Guycbb89e42009-06-08 15:52:54 -0700813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 static ApplicationInfo addShortcut(Context context, Intent data,
815 CellLayout.CellInfo cellInfo, boolean notify) {
816
Romain Guy73b979d2009-06-09 12:57:21 -0700817 final ApplicationInfo info = infoFromShortcutIntent(context, data);
818 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
819 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
820
821 return info;
822 }
823
824 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
826 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
827 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
828
829 Drawable icon = null;
830 boolean filtered = false;
831 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700832 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833
834 if (bitmap != null) {
835 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
836 filtered = true;
837 customIcon = true;
838 } else {
839 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700840 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700842 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 final PackageManager packageManager = context.getPackageManager();
844 Resources resources = packageManager.getResourcesForApplication(
845 iconResource.packageName);
846 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
847 icon = resources.getDrawable(id);
848 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700849 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 }
851 }
852 }
853
854 if (icon == null) {
855 icon = context.getPackageManager().getDefaultActivityIcon();
856 }
857
858 final ApplicationInfo info = new ApplicationInfo();
859 info.icon = icon;
860 info.filtered = filtered;
861 info.title = name;
862 info.intent = intent;
863 info.customIcon = customIcon;
864 info.iconResource = iconResource;
865
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 return info;
867 }
868
869 @Override
870 protected void onNewIntent(Intent intent) {
871 super.onNewIntent(intent);
872
873 // Close the menu
874 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
875 getWindow().closeAllPanels();
876
877 try {
878 dismissDialog(DIALOG_CREATE_SHORTCUT);
879 // Unlock the workspace if the dialog was showing
880 mWorkspace.unlock();
881 } catch (Exception e) {
882 // An exception is thrown if the dialog is not visible, which is fine
883 }
884
885 try {
886 dismissDialog(DIALOG_RENAME_FOLDER);
887 // Unlock the workspace if the dialog was showing
888 mWorkspace.unlock();
889 } catch (Exception e) {
890 // An exception is thrown if the dialog is not visible, which is fine
891 }
892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
894 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700895
Romain Guye3895ae2009-06-16 13:25:29 -0700896 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE &&
Romain Guy94406842009-06-17 16:18:58 -0700897 (mDragLayer.hasWindowFocus() ||
898 (mGesturesWindow != null && mGesturesWindow.isShowing()))) {
899
Romain Guyb8734242009-06-10 11:53:57 -0700900 SearchManager searchManager =
901 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Romain Guy94406842009-06-17 16:18:58 -0700902
Romain Guyb8734242009-06-10 11:53:57 -0700903 if (!searchManager.isVisible()) {
904 onHomeKeyPressed();
905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 }
907 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700908
909 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 if (v != null && v.getWindowToken() != null) {
911 InputMethodManager imm = (InputMethodManager)getSystemService(
912 INPUT_METHOD_SERVICE);
913 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
914 }
915 } else {
916 closeDrawer(false);
917 }
918 }
919 }
920
Romain Guy73b979d2009-06-09 12:57:21 -0700921 private void onHomeKeyPressed() {
922 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
923 showGesturesPanel();
924 } else {
925 hideGesturesPanel();
926 }
927 }
928
929 private void showGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -0700930 showGesturesPanel(true);
931 }
932
933 private void showGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -0700934 resetGesturesPrompt();
935
936 mGesturesAdd.setEnabled(false);
937 mGesturesAdd.setAlpha(128);
938
939 mGesturesOverlay.clear(false);
940
941 PopupWindow window;
942 if (mGesturesWindow == null) {
943 mGesturesWindow = new PopupWindow(this);
944 window = mGesturesWindow;
945 window.setFocusable(true);
946 window.setTouchable(true);
947 window.setBackgroundDrawable(null);
948 window.setContentView(mGesturesPanel);
949 } else {
950 window = mGesturesWindow;
951 }
Romain Guy3cf604f2009-06-16 13:12:53 -0700952 window.setAnimationStyle(animate ? com.android.internal.R.style.Animation_SlidingCard : 0);
Romain Guy73b979d2009-06-09 12:57:21 -0700953
954 final int[] xy = new int[2];
955 final DragLayer dragLayer = mDragLayer;
956 dragLayer.getLocationOnScreen(xy);
957
958 window.setWidth(dragLayer.getWidth());
959 window.setHeight(dragLayer.getHeight() - 1);
960 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
961 }
962
963 private void resetGesturesPrompt() {
964 mGesturesAction.intent = null;
965 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
966 prompt.setText(R.string.gestures_instructions);
967 prompt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
968 prompt.setClickable(false);
969 }
970
971 private void resetGesturesNextPrompt() {
972 mGesturesAction.intent = null;
973 setGesturesNextPrompt(null, getString(R.string.gestures_instructions));
974 mGesturesPrompt.getNextView().setClickable(false);
975 }
976
977 private void setGesturesNextPrompt(Drawable icon, CharSequence title) {
978 final TextView prompt = (TextView) mGesturesPrompt.getNextView();
979 prompt.setText(title);
980 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
981 prompt.setClickable(true);
982 mGesturesPrompt.showNext();
983 }
984
Romain Guy3cf604f2009-06-16 13:12:53 -0700985 private void setGesturesPrompt(Drawable icon, CharSequence title) {
986 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
987 prompt.setText(title);
988 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
989 prompt.setClickable(true);
990 }
991
Romain Guy73b979d2009-06-09 12:57:21 -0700992 void hideGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -0700993 hideGesturesPanel(true);
994 }
995
996 void hideGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -0700997 if (mGesturesWindow != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -0700998 final PopupWindow popupWindow = mGesturesWindow;
999 popupWindow.setAnimationStyle(animate ?
1000 com.android.internal.R.style.Animation_SlidingCard : 0);
1001 popupWindow.update();
1002 popupWindow.dismiss();
Romain Guy73b979d2009-06-09 12:57:21 -07001003 }
1004 }
1005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 @Override
1007 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1008 // Do not call super here
1009 mSavedInstanceState = savedInstanceState;
1010 }
1011
1012 @Override
1013 protected void onSaveInstanceState(Bundle outState) {
1014 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1015
1016 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1017 if (folders.size() > 0) {
1018 final int count = folders.size();
1019 long[] ids = new long[count];
1020 for (int i = 0; i < count; i++) {
1021 final FolderInfo info = folders.get(i).getInfo();
1022 ids[i] = info.id;
1023 }
1024 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1025 } else {
1026 super.onSaveInstanceState(outState);
1027 }
1028
Romain Guy3cf604f2009-06-16 13:12:53 -07001029 final boolean isConfigurationChange = getChangingConfigurations() != 0;
1030
Romain Guy5a941392009-04-28 15:18:25 -07001031 // When the drawer is opened and we are saving the state because of a
1032 // configuration change
Romain Guy3cf604f2009-06-16 13:12:53 -07001033 if (mDrawer.isOpened() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036
1037 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1038 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1039 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1040
1041 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1042 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1043 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1044 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1045 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1046 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1047 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1048 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1049 layout.getOccupiedCells());
1050 }
1051
1052 if (mFolderInfo != null && mWaitingForResult) {
1053 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1054 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1055 }
Romain Guy73b979d2009-06-09 12:57:21 -07001056
1057 if (mCurrentGesture != null && mWaitingForResult) {
1058 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1059 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001060
Romain Guy1ce1a242009-06-23 17:34:54 -07001061 if (mGesturesWindow != null && mGesturesWindow.isShowing()) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001062 outState.putBoolean(RUNTIME_STATE_GESTURES_PANEL, true);
1063
Romain Guy1ce1a242009-06-23 17:34:54 -07001064 if (mCurrentGesture == null || !mWaitingForResult) {
1065 final Gesture gesture = mGesturesOverlay.getGesture();
1066 if (gesture != null) {
1067 outState.putParcelable(RUNTIME_STATE_GESTURES_PANEL_GESTURE, gesture);
1068 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001069 }
1070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 }
1072
1073 @Override
1074 public void onDestroy() {
1075 mDestroyed = true;
1076
1077 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001080 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001082 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 }
1084
1085 TextKeyListener.getInstance().release();
1086
Romain Guy3cf604f2009-06-16 13:12:53 -07001087 hideGesturesPanel(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 mAllAppsGrid.clearTextFilter();
1089 mAllAppsGrid.setAdapter(null);
1090 sModel.unbind();
1091 sModel.abortLoaders();
1092
1093 getContentResolver().unregisterContentObserver(mObserver);
1094 unregisterReceiver(mApplicationsReceiver);
1095 }
1096
1097 @Override
1098 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001099 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 super.startActivityForResult(intent, requestCode);
1101 }
1102
1103 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001104 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001106
1107 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001108
Karl Rosaen138a0412009-04-23 19:00:21 -07001109 // Slide the search widget to the top, if it's on the current screen,
1110 // otherwise show the search dialog immediately.
1111 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1112 if (searchWidget == null) {
1113 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1114 } else {
1115 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1116 // show the currently typed text in the search widget while sliding
1117 searchWidget.setQuery(getTypedText());
1118 }
1119 }
Romain Guycbb89e42009-06-08 15:52:54 -07001120
Karl Rosaen138a0412009-04-23 19:00:21 -07001121 /**
1122 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001123 *
1124 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001125 */
Romain Guycbb89e42009-06-08 15:52:54 -07001126 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001127 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001128
Karl Rosaen138a0412009-04-23 19:00:21 -07001129 if (initialQuery == null) {
1130 // Use any text typed in the launcher as the initial query
1131 initialQuery = getTypedText();
1132 clearTypedText();
1133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 if (appSearchData == null) {
1135 appSearchData = new Bundle();
1136 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1137 }
Romain Guycbb89e42009-06-08 15:52:54 -07001138
Karl Rosaen138a0412009-04-23 19:00:21 -07001139 final SearchManager searchManager =
1140 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1141
1142 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1143 if (searchWidget != null) {
1144 // This gets called when the user leaves the search dialog to go back to
1145 // the Launcher.
1146 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1147 public void onCancel() {
1148 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001149 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001150 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001151 });
1152 }
Romain Guycbb89e42009-06-08 15:52:54 -07001153
Karl Rosaen138a0412009-04-23 19:00:21 -07001154 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001155 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 }
1157
Karl Rosaen138a0412009-04-23 19:00:21 -07001158 /**
1159 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001160 */
Romain Guy5a941392009-04-28 15:18:25 -07001161 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001162 // Close search dialog
1163 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1164 if (searchManager.isVisible()) {
1165 searchManager.stopSearch();
1166 }
1167 // Restore search widget to its normal position
1168 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1169 if (searchWidget != null) {
1170 searchWidget.stopSearch(false);
1171 }
1172 }
Romain Guycbb89e42009-06-08 15:52:54 -07001173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 @Override
1175 public boolean onCreateOptionsMenu(Menu menu) {
1176 if (mDesktopLocked) return false;
1177
1178 super.onCreateOptionsMenu(menu);
1179 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1180 .setIcon(android.R.drawable.ic_menu_add)
1181 .setAlphabeticShortcut('A');
1182 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1183 .setIcon(android.R.drawable.ic_menu_gallery)
1184 .setAlphabeticShortcut('W');
1185 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1186 .setIcon(android.R.drawable.ic_search_category_default)
1187 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1188 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1189 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1190 .setAlphabeticShortcut('N');
1191
Romain Guy73b979d2009-06-09 12:57:21 -07001192 final Intent gestures = new Intent(this, GesturesActivity.class);
1193 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1194 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1195 .setIntent(gestures);
1196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001198 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1199 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200
1201 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1202 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1203 .setIntent(settings);
1204
1205 return true;
1206 }
1207
1208 @Override
1209 public boolean onPrepareOptionsMenu(Menu menu) {
1210 super.onPrepareOptionsMenu(menu);
1211
1212 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1213 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1214
1215 return true;
1216 }
1217
1218 @Override
1219 public boolean onOptionsItemSelected(MenuItem item) {
1220 switch (item.getItemId()) {
1221 case MENU_ADD:
1222 addItems();
1223 return true;
1224 case MENU_WALLPAPER_SETTINGS:
1225 startWallpaper();
1226 return true;
1227 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001228 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 return true;
1230 case MENU_NOTIFICATIONS:
1231 showNotifications();
1232 return true;
1233 }
1234
1235 return super.onOptionsItemSelected(item);
1236 }
Romain Guycbb89e42009-06-08 15:52:54 -07001237
Karl Rosaen138a0412009-04-23 19:00:21 -07001238 /**
1239 * Indicates that we want global search for this activity by setting the globalSearch
1240 * argument for {@link #startSearch} to true.
1241 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001243 @Override
1244 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001245 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001246 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001247 }
1248
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 private void addItems() {
1250 showAddDialog(mMenuAddInfo);
1251 }
1252
1253 private void removeShortcutsForPackage(String packageName) {
1254 if (packageName != null && packageName.length() > 0) {
1255 mWorkspace.removeShortcutsForPackage(packageName);
1256 }
1257 }
Romain Guycbb89e42009-06-08 15:52:54 -07001258
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001259 private void updateShortcutsForPackage(String packageName) {
1260 if (packageName != null && packageName.length() > 0) {
1261 mWorkspace.updateShortcutsForPackage(packageName);
1262 }
1263 }
1264
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001265 void addAppWidget(Intent data) {
1266 // TODO: catch bad widget exception when sent
1267 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001269 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1270 if (SEARCH_WIDGET.equals(customWidget)) {
1271 // We don't need this any more, since this isn't a real app widget.
1272 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1273 // add the search widget
1274 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001276 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1277
1278 if (appWidget.configure != null) {
1279 // Launch over to configure widget, if needed
1280 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1281 intent.setComponent(appWidget.configure);
1282 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1283
1284 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1285 } else {
1286 // Otherwise just add it
1287 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1288 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 }
1290 }
Romain Guycbb89e42009-06-08 15:52:54 -07001291
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 void addSearch() {
1293 final Widget info = Widget.makeSearch();
1294 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001295
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 final int[] xy = mCellCoordinates;
1297 final int spanX = info.spanX;
1298 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001299
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001301
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 sModel.addDesktopItem(info);
1303 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1304 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001305
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 final View view = mInflater.inflate(info.layoutResource, null);
1307 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001308 Search search = (Search) view.findViewById(R.id.widget_search);
1309 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001310
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1312 }
1313
Romain Guy73b979d2009-06-09 12:57:21 -07001314 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001315 // Handle case where user selected "Applications"
1316 String applicationName = getResources().getString(R.string.group_applications);
1317 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001318
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001319 if (applicationName != null && applicationName.equals(shortcutName)) {
1320 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1321 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001322
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001323 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1324 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001325 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001326 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001327 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 }
1330
1331 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001332 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001333 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001334 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001335
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001336 if (folderName != null && folderName.equals(shortcutName)) {
1337 addFolder(!mDesktopLocked);
1338 } else {
1339 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 }
1342
1343 void addFolder(boolean insertAtFirst) {
1344 UserFolderInfo folderInfo = new UserFolderInfo();
1345 folderInfo.title = getText(R.string.folder_name);
1346
1347 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1348 cellInfo.screen = mWorkspace.getCurrentScreen();
1349 if (!findSingleSlot(cellInfo)) return;
1350
1351 // Update the model
1352 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1353 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1354 sModel.addDesktopItem(folderInfo);
1355 sModel.addFolder(folderInfo);
1356
1357 // Create the view
1358 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1359 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1360 mWorkspace.addInCurrentScreen(newFolder,
1361 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1362 }
Romain Guycbb89e42009-06-08 15:52:54 -07001363
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1365 boolean insertAtFirst) {
1366 cellInfo.screen = mWorkspace.getCurrentScreen();
1367 if (!findSingleSlot(cellInfo)) return;
1368
1369 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1370
1371 if (!mRestoring) {
1372 sModel.addDesktopItem(info);
1373
1374 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1375 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1376 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1377 } else if (sModel.isDesktopLoaded()) {
1378 sModel.addDesktopItem(info);
1379 }
1380 }
1381
1382 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1383 CellLayout.CellInfo cellInfo, boolean notify) {
1384
1385 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1386 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1387
1388 Drawable icon = null;
1389 boolean filtered = false;
1390 Intent.ShortcutIconResource iconResource = null;
1391
1392 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1393 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1394 try {
1395 iconResource = (Intent.ShortcutIconResource) extra;
1396 final PackageManager packageManager = context.getPackageManager();
1397 Resources resources = packageManager.getResourcesForApplication(
1398 iconResource.packageName);
1399 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1400 icon = resources.getDrawable(id);
1401 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001402 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
1404 }
1405
1406 if (icon == null) {
1407 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1408 }
1409
1410 final LiveFolderInfo info = new LiveFolderInfo();
1411 info.icon = icon;
1412 info.filtered = filtered;
1413 info.title = name;
1414 info.iconResource = iconResource;
1415 info.uri = data.getData();
1416 info.baseIntent = baseIntent;
1417 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1418 LiveFolders.DISPLAY_MODE_GRID);
1419
1420 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1421 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1422 sModel.addFolder(info);
1423
1424 return info;
1425 }
1426
1427 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1428 final int[] xy = new int[2];
1429 if (findSlot(cellInfo, xy, 1, 1)) {
1430 cellInfo.cellX = xy[0];
1431 cellInfo.cellY = xy[1];
1432 return true;
1433 }
1434 return false;
1435 }
1436
1437 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1438 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1439 boolean[] occupied = mSavedState != null ?
1440 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1441 cellInfo = mWorkspace.findAllVacantCells(occupied);
1442 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1443 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1444 return false;
1445 }
1446 }
1447 return true;
1448 }
1449
1450 private void showNotifications() {
1451 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1452 if (statusBar != null) {
1453 statusBar.expand();
1454 }
1455 }
1456
1457 private void startWallpaper() {
1458 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1459 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1460 }
1461
1462 /**
1463 * Registers various intent receivers. The current implementation registers
1464 * only a wallpaper intent receiver to let other applications change the
1465 * wallpaper.
1466 */
1467 private void registerIntentReceivers() {
1468 if (sWallpaperReceiver == null) {
1469 final Application application = getApplication();
1470
1471 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1472
1473 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1474 application.registerReceiver(sWallpaperReceiver, filter);
1475 } else {
1476 sWallpaperReceiver.setLauncher(this);
1477 }
1478
1479 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1480 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1481 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1482 filter.addDataScheme("package");
1483 registerReceiver(mApplicationsReceiver, filter);
1484 }
1485
1486 /**
1487 * Registers various content observers. The current implementation registers
1488 * only a favorites observer to keep track of the favorites applications.
1489 */
1490 private void registerContentObservers() {
1491 ContentResolver resolver = getContentResolver();
1492 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1493 }
1494
1495 @Override
1496 public boolean dispatchKeyEvent(KeyEvent event) {
1497 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1498 switch (event.getKeyCode()) {
1499 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001500 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001501 if (mDrawer.isOpened()) {
1502 closeDrawer();
1503 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001504 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001505 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 return true;
1507 case KeyEvent.KEYCODE_HOME:
1508 return true;
1509 }
1510 }
1511
1512 return super.dispatchKeyEvent(event);
1513 }
1514
1515 private void closeDrawer() {
1516 closeDrawer(true);
1517 }
1518
1519 private void closeDrawer(boolean animated) {
1520 if (mDrawer.isOpened()) {
1521 if (animated) {
1522 mDrawer.animateClose();
1523 } else {
1524 mDrawer.close();
1525 }
1526 if (mDrawer.hasFocus()) {
1527 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1528 }
1529 }
1530 }
1531
1532 private void closeFolder() {
1533 Folder folder = mWorkspace.getOpenFolder();
1534 if (folder != null) {
1535 closeFolder(folder);
1536 }
1537 }
1538
1539 void closeFolder(Folder folder) {
1540 folder.getInfo().opened = false;
1541 ViewGroup parent = (ViewGroup) folder.getParent();
1542 if (parent != null) {
1543 parent.removeView(folder);
1544 }
1545 folder.onClose();
1546 }
1547
1548 /**
1549 * When the notification that favorites have changed is received, requests
1550 * a favorites list refresh.
1551 */
1552 private void onFavoritesChanged() {
1553 mDesktopLocked = true;
1554 mDrawer.lock();
1555 sModel.loadUserItems(false, this, false, false);
1556 }
1557
1558 void onDesktopItemsLoaded() {
1559 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 bindDesktopItems();
1561 }
Romain Guycbb89e42009-06-08 15:52:54 -07001562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 /**
1564 * Refreshes the shortcuts shown on the workspace.
1565 */
1566 private void bindDesktopItems() {
1567 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001568 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001569 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1570 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 return;
1572 }
1573
1574 final Workspace workspace = mWorkspace;
1575 int count = workspace.getChildCount();
1576 for (int i = 0; i < count; i++) {
1577 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1578 }
Romain Guycbb89e42009-06-08 15:52:54 -07001579
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 if (DEBUG_USER_INTERFACE) {
1581 android.widget.Button finishButton = new android.widget.Button(this);
1582 finishButton.setText("Finish");
1583 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1584
1585 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1586 public void onClick(View v) {
1587 finish();
1588 }
1589 });
1590 }
Romain Guycbb89e42009-06-08 15:52:54 -07001591
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001592 // Flag any old binder to terminate early
1593 if (mBinder != null) {
1594 mBinder.mTerminate = true;
1595 }
Romain Guycbb89e42009-06-08 15:52:54 -07001596
Karl Rosaen138a0412009-04-23 19:00:21 -07001597 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001598 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
1600
1601 private void bindItems(Launcher.DesktopBinder binder,
1602 ArrayList<ItemInfo> shortcuts, int start, int count) {
1603
1604 final Workspace workspace = mWorkspace;
1605 final boolean desktopLocked = mDesktopLocked;
1606
1607 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1608 int i = start;
1609
1610 for ( ; i < end; i++) {
1611 final ItemInfo item = shortcuts.get(i);
1612 switch (item.itemType) {
1613 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1614 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1615 final View shortcut = createShortcut((ApplicationInfo) item);
1616 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1617 !desktopLocked);
1618 break;
1619 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1620 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1621 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1622 (UserFolderInfo) item);
1623 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1624 !desktopLocked);
1625 break;
1626 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1627 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1628 R.layout.live_folder_icon, this,
1629 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1630 (LiveFolderInfo) item);
1631 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1632 !desktopLocked);
1633 break;
1634 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1635 final int screen = workspace.getCurrentScreen();
1636 final View view = mInflater.inflate(R.layout.widget_search,
1637 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001638
Karl Rosaen138a0412009-04-23 19:00:21 -07001639 Search search = (Search) view.findViewById(R.id.widget_search);
1640 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001641
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 final Widget widget = (Widget) item;
1643 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 workspace.addWidget(view, widget, !desktopLocked);
1646 break;
1647 }
1648 }
1649
1650 workspace.requestLayout();
1651
1652 if (end >= count) {
1653 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001654 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 } else {
1656 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1657 }
1658 }
1659
1660 private void finishBindDesktopItems() {
1661 if (mSavedState != null) {
1662 if (!mWorkspace.hasFocus()) {
1663 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1664 }
1665
1666 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1667 if (userFolders != null) {
1668 for (long folderId : userFolders) {
1669 final FolderInfo info = sModel.findFolderById(folderId);
1670 if (info != null) {
1671 openFolder(info);
1672 }
1673 }
1674 final Folder openFolder = mWorkspace.getOpenFolder();
1675 if (openFolder != null) {
1676 openFolder.requestFocus();
1677 }
1678 }
1679
1680 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1681 if (allApps) {
1682 mDrawer.open();
1683 }
1684
1685 mSavedState = null;
1686 }
1687
1688 if (mSavedInstanceState != null) {
1689 super.onRestoreInstanceState(mSavedInstanceState);
1690 mSavedInstanceState = null;
1691 }
1692
1693 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1694 mDrawer.requestFocus();
1695 }
1696
1697 mDesktopLocked = false;
1698 mDrawer.unlock();
1699 }
Romain Guycbb89e42009-06-08 15:52:54 -07001700
Karl Rosaen138a0412009-04-23 19:00:21 -07001701 private void bindDrawer(Launcher.DesktopBinder binder,
1702 ApplicationsAdapter drawerAdapter) {
1703 mAllAppsGrid.setAdapter(drawerAdapter);
1704 binder.startBindingAppWidgetsWhenIdle();
1705 }
Romain Guycbb89e42009-06-08 15:52:54 -07001706
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001707 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001708 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001709
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 final Workspace workspace = mWorkspace;
1711 final boolean desktopLocked = mDesktopLocked;
1712
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001713 if (!appWidgets.isEmpty()) {
1714 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001715
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001716 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001717 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001718 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001719
Karl Rosaen138a0412009-04-23 19:00:21 -07001720 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001721
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001722 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1723 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001724
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001725 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1726 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001727
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001728 workspace.requestLayout();
1729 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001731 if (appWidgets.isEmpty()) {
1732 if (PROFILE_ROTATE) {
1733 android.os.Debug.stopMethodTracing();
1734 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001736 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 }
1738 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001739
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 DragController getDragController() {
1741 return mDragLayer;
1742 }
1743
1744 /**
1745 * Launches the intent referred by the clicked shortcut.
1746 *
1747 * @param v The view representing the clicked shortcut.
1748 */
1749 public void onClick(View v) {
1750 Object tag = v.getTag();
1751 if (tag instanceof ApplicationInfo) {
1752 // Open shortcut
1753 final Intent intent = ((ApplicationInfo) tag).intent;
1754 startActivitySafely(intent);
1755 } else if (tag instanceof FolderInfo) {
1756 handleFolderClick((FolderInfo) tag);
1757 }
1758 }
1759
1760 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001761 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1763 try {
1764 startActivity(intent);
1765 } catch (ActivityNotFoundException e) {
1766 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1767 } catch (SecurityException e) {
1768 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001769 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1771 "or use the exported attribute for this activity.", e);
1772 }
1773 }
1774
1775 private void handleFolderClick(FolderInfo folderInfo) {
1776 if (!folderInfo.opened) {
1777 // Close any open folder
1778 closeFolder();
1779 // Open the requested folder
1780 openFolder(folderInfo);
1781 } else {
1782 // Find the open folder...
1783 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1784 int folderScreen;
1785 if (openFolder != null) {
1786 folderScreen = mWorkspace.getScreenForView(openFolder);
1787 // .. and close it
1788 closeFolder(openFolder);
1789 if (folderScreen != mWorkspace.getCurrentScreen()) {
1790 // Close any folder open on the current screen
1791 closeFolder();
1792 // Pull the folder onto this screen
1793 openFolder(folderInfo);
1794 }
1795 }
1796 }
1797 }
1798
1799 private void loadWallpaper() {
1800 // The first time the application is started, we load the wallpaper from
1801 // the ApplicationContext
1802 if (sWallpaper == null) {
1803 final Drawable drawable = getWallpaper();
1804 if (drawable instanceof BitmapDrawable) {
1805 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1806 } else {
1807 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1808 }
1809 }
1810 mWorkspace.loadWallpaper(sWallpaper);
1811 }
1812
1813 /**
1814 * Opens the user fodler described by the specified tag. The opening of the folder
1815 * is animated relative to the specified View. If the View is null, no animation
1816 * is played.
1817 *
1818 * @param folderInfo The FolderInfo describing the folder to open.
1819 */
1820 private void openFolder(FolderInfo folderInfo) {
1821 Folder openFolder;
1822
1823 if (folderInfo instanceof UserFolderInfo) {
1824 openFolder = UserFolder.fromXml(this);
1825 } else if (folderInfo instanceof LiveFolderInfo) {
1826 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1827 } else {
1828 return;
1829 }
1830
1831 openFolder.setDragger(mDragLayer);
1832 openFolder.setLauncher(this);
1833
1834 openFolder.bind(folderInfo);
1835 folderInfo.opened = true;
1836
1837 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1838 openFolder.onOpen();
1839 }
1840
1841 /**
1842 * Returns true if the workspace is being loaded. When the workspace is loading,
1843 * no user interaction should be allowed to avoid any conflict.
1844 *
1845 * @return True if the workspace is locked, false otherwise.
1846 */
1847 boolean isWorkspaceLocked() {
1848 return mDesktopLocked;
1849 }
1850
1851 public boolean onLongClick(View v) {
1852 if (mDesktopLocked) {
1853 return false;
1854 }
1855
1856 if (!(v instanceof CellLayout)) {
1857 v = (View) v.getParent();
1858 }
1859
1860 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1861
1862 // This happens when long clicking an item with the dpad/trackball
1863 if (cellInfo == null) {
1864 return true;
1865 }
1866
1867 if (mWorkspace.allowLongPress()) {
1868 if (cellInfo.cell == null) {
1869 if (cellInfo.valid) {
1870 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001871 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 showAddDialog(cellInfo);
1873 }
1874 } else {
1875 if (!(cellInfo.cell instanceof Folder)) {
1876 // User long pressed on an item
1877 mWorkspace.startDrag(cellInfo);
1878 }
1879 }
1880 }
1881 return true;
1882 }
1883
1884 static LauncherModel getModel() {
1885 return sModel;
1886 }
1887
Romain Guy73b979d2009-06-09 12:57:21 -07001888 static GestureLibrary getGestureLibrary() {
1889 return sLibrary;
1890 }
1891
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 void closeAllApplications() {
1893 mDrawer.close();
1894 }
1895
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001896 View getDrawerHandle() {
1897 return mHandleView;
1898 }
1899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 boolean isDrawerDown() {
1901 return !mDrawer.isMoving() && !mDrawer.isOpened();
1902 }
1903
1904 boolean isDrawerUp() {
1905 return mDrawer.isOpened() && !mDrawer.isMoving();
1906 }
1907
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001908 boolean isDrawerMoving() {
1909 return mDrawer.isMoving();
1910 }
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 Workspace getWorkspace() {
1913 return mWorkspace;
1914 }
1915
1916 GridView getApplicationsGrid() {
1917 return mAllAppsGrid;
1918 }
1919
1920 @Override
1921 protected Dialog onCreateDialog(int id) {
1922 switch (id) {
1923 case DIALOG_CREATE_SHORTCUT:
1924 return new CreateShortcut().createDialog();
1925 case DIALOG_RENAME_FOLDER:
1926 return new RenameFolder().createDialog();
1927 }
1928
1929 return super.onCreateDialog(id);
1930 }
1931
1932 @Override
1933 protected void onPrepareDialog(int id, Dialog dialog) {
1934 switch (id) {
1935 case DIALOG_CREATE_SHORTCUT:
1936 mWorkspace.lock();
1937 break;
1938 case DIALOG_RENAME_FOLDER:
1939 mWorkspace.lock();
1940 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1941 final CharSequence text = mFolderInfo.title;
1942 input.setText(text);
Romain Guycbb89e42009-06-08 15:52:54 -07001943 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 break;
1945 }
1946 }
1947
1948 void showRenameDialog(FolderInfo info) {
1949 mFolderInfo = info;
1950 mWaitingForResult = true;
1951 showDialog(DIALOG_RENAME_FOLDER);
1952 }
1953
1954 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1955 mAddItemCellInfo = cellInfo;
1956 mWaitingForResult = true;
1957 showDialog(DIALOG_CREATE_SHORTCUT);
1958 }
1959
Romain Guy73b979d2009-06-09 12:57:21 -07001960 private void pickShortcut(int requestCode, int title) {
1961 Bundle bundle = new Bundle();
1962
1963 ArrayList<String> shortcutNames = new ArrayList<String>();
1964 shortcutNames.add(getString(R.string.group_applications));
1965 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1966
1967 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1968 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1969 R.drawable.ic_launcher_application));
1970 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1971
1972 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1973 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1974 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1975 pickIntent.putExtras(bundle);
1976
1977 startActivityForResult(pickIntent, requestCode);
1978 }
1979
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 private class RenameFolder {
1981 private EditText mInput;
1982
1983 Dialog createDialog() {
1984 mWaitingForResult = true;
1985 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1986 mInput = (EditText) layout.findViewById(R.id.folder_name);
1987
1988 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1989 builder.setIcon(0);
1990 builder.setTitle(getString(R.string.rename_folder_title));
1991 builder.setCancelable(true);
1992 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1993 public void onCancel(DialogInterface dialog) {
1994 cleanup();
1995 }
1996 });
1997 builder.setNegativeButton(getString(R.string.cancel_action),
1998 new Dialog.OnClickListener() {
1999 public void onClick(DialogInterface dialog, int which) {
2000 cleanup();
2001 }
2002 }
2003 );
2004 builder.setPositiveButton(getString(R.string.rename_action),
2005 new Dialog.OnClickListener() {
2006 public void onClick(DialogInterface dialog, int which) {
2007 changeFolderName();
2008 }
2009 }
2010 );
2011 builder.setView(layout);
2012 return builder.create();
2013 }
2014
2015 private void changeFolderName() {
2016 final String name = mInput.getText().toString();
2017 if (!TextUtils.isEmpty(name)) {
2018 // Make sure we have the right folder info
2019 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
2020 mFolderInfo.title = name;
2021 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2022
2023 if (mDesktopLocked) {
2024 mDrawer.lock();
2025 sModel.loadUserItems(false, Launcher.this, false, false);
2026 } else {
2027 final FolderIcon folderIcon = (FolderIcon)
2028 mWorkspace.getViewForTag(mFolderInfo);
2029 if (folderIcon != null) {
2030 folderIcon.setText(name);
2031 getWorkspace().requestLayout();
2032 } else {
2033 mDesktopLocked = true;
2034 mDrawer.lock();
2035 sModel.loadUserItems(false, Launcher.this, false, false);
2036 }
2037 }
2038 }
2039 cleanup();
2040 }
2041
2042 private void cleanup() {
2043 mWorkspace.unlock();
2044 dismissDialog(DIALOG_RENAME_FOLDER);
2045 mWaitingForResult = false;
2046 mFolderInfo = null;
2047 }
2048 }
2049
2050 /**
2051 * Displays the shortcut creation dialog and launches, if necessary, the
2052 * appropriate activity.
2053 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002054 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07002055 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 Dialog createDialog() {
2059 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002060
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2064 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002065 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 builder.setInverseBackgroundForced(true);
2068
2069 AlertDialog dialog = builder.create();
2070 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002071 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 return dialog;
2074 }
2075
2076 public void onCancel(DialogInterface dialog) {
2077 mWaitingForResult = false;
2078 cleanup();
2079 }
2080
Romain Guycbb89e42009-06-08 15:52:54 -07002081 public void onDismiss(DialogInterface dialog) {
2082 mWorkspace.unlock();
2083 }
2084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 private void cleanup() {
2086 mWorkspace.unlock();
2087 dismissDialog(DIALOG_CREATE_SHORTCUT);
2088 }
2089
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002090 /**
2091 * Handle the action clicked in the "Add to home" dialog.
2092 */
2093 public void onClick(DialogInterface dialog, int which) {
2094 Resources res = getResources();
2095 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002096
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002097 switch (which) {
2098 case AddAdapter.ITEM_SHORTCUT: {
2099 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002100 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002101 break;
2102 }
Romain Guycbb89e42009-06-08 15:52:54 -07002103
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002104 case AddAdapter.ITEM_APPWIDGET: {
2105 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002106
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002107 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2108 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2109 // add the search widget
2110 ArrayList<AppWidgetProviderInfo> customInfo =
2111 new ArrayList<AppWidgetProviderInfo>();
2112 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2113 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2114 info.label = getString(R.string.group_search);
2115 info.icon = R.drawable.ic_search_widget;
2116 customInfo.add(info);
2117 pickIntent.putParcelableArrayListExtra(
2118 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2119 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2120 Bundle b = new Bundle();
2121 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2122 customExtras.add(b);
2123 pickIntent.putParcelableArrayListExtra(
2124 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2125 // start the pick activity
2126 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2127 break;
2128 }
Romain Guycbb89e42009-06-08 15:52:54 -07002129
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002130 case AddAdapter.ITEM_LIVE_FOLDER: {
2131 // Insert extra item to handle inserting folder
2132 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002133
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002134 ArrayList<String> shortcutNames = new ArrayList<String>();
2135 shortcutNames.add(res.getString(R.string.group_folder));
2136 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002137
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002138 ArrayList<ShortcutIconResource> shortcutIcons =
2139 new ArrayList<ShortcutIconResource>();
2140 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2141 R.drawable.ic_launcher_folder));
2142 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2143
2144 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2145 pickIntent.putExtra(Intent.EXTRA_INTENT,
2146 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2147 pickIntent.putExtra(Intent.EXTRA_TITLE,
2148 getText(R.string.title_select_live_folder));
2149 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002150
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002151 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2152 break;
2153 }
2154
2155 case AddAdapter.ITEM_WALLPAPER: {
2156 startWallpaper();
2157 break;
2158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 }
2160 }
2161 }
2162
2163 /**
2164 * Receives notifications when applications are added/removed.
2165 */
2166 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2167 @Override
2168 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002169 final String action = intent.getAction();
2170 final String packageName = intent.getData().getSchemeSpecificPart();
2171 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2172
2173 if (LauncherModel.DEBUG_LOADERS) {
2174 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2175 ", replacing=" + replacing);
2176 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002178
2179 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2180 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2181 if (!replacing) {
2182 removeShortcutsForPackage(packageName);
2183 if (LauncherModel.DEBUG_LOADERS) {
2184 d(LauncherModel.LOG_TAG, " --> remove package");
2185 }
2186 sModel.removePackage(Launcher.this, packageName);
2187 }
2188 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2189 // later, we will update the package at this time
2190 } else {
2191 if (!replacing) {
2192 if (LauncherModel.DEBUG_LOADERS) {
2193 d(LauncherModel.LOG_TAG, " --> add package");
2194 }
2195 sModel.addPackage(Launcher.this, packageName);
2196 } else {
2197 if (LauncherModel.DEBUG_LOADERS) {
2198 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2199 }
2200 sModel.updatePackage(Launcher.this, packageName);
2201 updateShortcutsForPackage(packageName);
2202 }
2203 }
2204 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002206 if (LauncherModel.DEBUG_LOADERS) {
2207 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2208 }
2209 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 }
2211 }
2212 }
2213
2214 /**
2215 * Receives notifications whenever the user favorites have changed.
2216 */
2217 private class FavoritesChangeObserver extends ContentObserver {
2218 public FavoritesChangeObserver() {
2219 super(new Handler());
2220 }
2221
2222 @Override
2223 public void onChange(boolean selfChange) {
2224 onFavoritesChanged();
2225 }
2226 }
2227
2228 /**
2229 * Receives intents from other applications to change the wallpaper.
2230 */
2231 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2232 private final Application mApplication;
2233 private WeakReference<Launcher> mLauncher;
2234
2235 WallpaperIntentReceiver(Application application, Launcher launcher) {
2236 mApplication = application;
2237 setLauncher(launcher);
2238 }
2239
2240 void setLauncher(Launcher launcher) {
2241 mLauncher = new WeakReference<Launcher>(launcher);
2242 }
2243
2244 @Override
2245 public void onReceive(Context context, Intent intent) {
2246 // Load the wallpaper from the ApplicationContext and store it locally
2247 // until the Launcher Activity is ready to use it
2248 final Drawable drawable = mApplication.getWallpaper();
2249 if (drawable instanceof BitmapDrawable) {
2250 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2251 } else {
2252 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2253 }
2254
2255 // If Launcher is alive, notify we have a new wallpaper
2256 if (mLauncher != null) {
2257 final Launcher launcher = mLauncher.get();
2258 if (launcher != null) {
2259 launcher.loadWallpaper();
2260 }
2261 }
2262 }
2263 }
2264
2265 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2266 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2267 private boolean mOpen;
2268
2269 public void onDrawerOpened() {
2270 if (!mOpen) {
2271 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002274 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275
2276 mOpen = true;
2277 }
2278 }
2279
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002280 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2281 view.getDrawingRect(bounds);
2282
2283 while (view != mDragLayer) {
2284 bounds.offset(view.getLeft(), view.getTop());
2285 view = (View) view.getParent();
2286 }
2287 }
2288
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002289 public void onDrawerClosed() {
2290 if (mOpen) {
2291 mHandleIcon.reverseTransition(150);
2292 mWorkspace.mDrawerBounds.setEmpty();
2293 mOpen = false;
2294 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002295
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002296 mAllAppsGrid.setSelection(0);
2297 mAllAppsGrid.clearTextFilter();
2298 }
2299
2300 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002301 if (PROFILE_DRAWER) {
2302 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2303 }
2304
2305 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2306 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002307 }
2308
2309 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002310 if (PROFILE_DRAWER) {
2311 android.os.Debug.stopMethodTracing();
2312 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 }
2314 }
2315
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002316 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002318 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002319 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002320
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 // Number of items to bind in every pass
2322 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323
2324 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002325 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002326 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002328
Karl Rosaen138a0412009-04-23 19:00:21 -07002329 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330
2331 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002332 ArrayList<LauncherAppWidgetInfo> appWidgets,
2333 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334
2335 mLauncher = new WeakReference<Launcher>(launcher);
2336 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002337 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002338
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002339 // Sort widgets so active workspace is bound first
2340 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2341 final int size = appWidgets.size();
2342 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002343
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002344 for (int i = 0; i < size; i++) {
2345 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2346 if (appWidgetInfo.screen == currentScreen) {
2347 mAppWidgets.addFirst(appWidgetInfo);
2348 } else {
2349 mAppWidgets.addLast(appWidgetInfo);
2350 }
2351 }
2352 }
Romain Guycbb89e42009-06-08 15:52:54 -07002353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354 public void startBindingItems() {
2355 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2356 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002357
2358 public void startBindingDrawer() {
2359 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2360 }
Romain Guycbb89e42009-06-08 15:52:54 -07002361
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002362 public void startBindingAppWidgetsWhenIdle() {
2363 // Ask for notification when message queue becomes idle
2364 final MessageQueue messageQueue = Looper.myQueue();
2365 messageQueue.addIdleHandler(this);
2366 }
Romain Guycbb89e42009-06-08 15:52:54 -07002367
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002368 public boolean queueIdle() {
2369 // Queue is idle, so start binding items
2370 startBindingAppWidgets();
2371 return false;
2372 }
2373
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002374 public void startBindingAppWidgets() {
2375 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2376 }
2377
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 @Override
2379 public void handleMessage(Message msg) {
2380 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002381 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 return;
2383 }
Romain Guycbb89e42009-06-08 15:52:54 -07002384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 switch (msg.what) {
2386 case MESSAGE_BIND_ITEMS: {
2387 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2388 break;
2389 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002390 case MESSAGE_BIND_DRAWER: {
2391 launcher.bindDrawer(this, mDrawerAdapter);
2392 break;
2393 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002394 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002395 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 break;
2397 }
2398 }
2399 }
2400 }
Romain Guy73b979d2009-06-09 12:57:21 -07002401
2402 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2403 GestureOverlayView.OnGesturePerformedListener {
2404
2405 private final GestureMatcher mMatcher = new GestureMatcher();
2406
2407 GesturesProcessor() {
2408 // TODO: Maybe the load should happen on a background thread?
2409 sLibrary.load();
2410 }
2411
2412 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002413 //noinspection PointlessBooleanExpression,ConstantConditions
2414 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2415 overlay.removeCallbacks(mMatcher);
2416 resetGesturesNextPrompt();
2417 }
Romain Guy73b979d2009-06-09 12:57:21 -07002418
2419 mGesturesAdd.setAlpha(128);
2420 mGesturesAdd.setEnabled(false);
2421 }
2422
2423 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2424 }
2425
2426 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2427 }
2428
2429 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002430 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2431 mMatcher.gesture = overlay.getGesture();
2432 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2433 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002434 if (mGesturesAction.intent != null) {
2435 mGesturesAction.intent = null;
2436 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2437 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002438 } else {
2439 mMatcher.run();
2440 }
Romain Guy73b979d2009-06-09 12:57:21 -07002441 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002442 overlay.removeCallbacks(mMatcher);
2443
2444 mMatcher.gesture = overlay.getGesture();
2445 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2446 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002447 if (mGesturesAction.intent != null) {
2448 mGesturesAction.intent = null;
2449 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2450 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002451 } else {
2452 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2453 }
Romain Guy73b979d2009-06-09 12:57:21 -07002454 }
2455 }
2456
Romain Guy3cf604f2009-06-16 13:12:53 -07002457 void matchGesture(Gesture gesture) {
2458 matchGesture(gesture, true);
2459 }
2460
2461 void matchGesture(Gesture gesture, boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07002462 mGesturesAdd.setAlpha(255);
2463 mGesturesAdd.setEnabled(true);
2464
2465 if (gesture != null) {
2466 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2467
2468 if (DEBUG_GESTURES) {
2469 for (Prediction p : predictions) {
2470 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2471 }
2472 }
2473
2474 boolean match = false;
2475 if (predictions.size() > 0) {
2476 final Prediction prediction = predictions.get(0);
2477 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2478 match = true;
2479
2480 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2481 if (info != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002482 updatePrompt(info, animate);
Romain Guy73b979d2009-06-09 12:57:21 -07002483 }
2484 }
2485 }
2486
2487 if (!match){
Romain Guy1ce1a242009-06-23 17:34:54 -07002488 mGesturesAction.intent = null;
Romain Guy3cf604f2009-06-16 13:12:53 -07002489 if (animate) {
2490 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2491 } else {
2492 setGesturesPrompt(null, getString(R.string.gestures_unknown));
2493 }
Romain Guy73b979d2009-06-09 12:57:21 -07002494 }
2495 }
2496 }
2497
2498 private void updatePrompt(ApplicationInfo info) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002499 updatePrompt(info, true);
2500 }
2501
2502 private void updatePrompt(ApplicationInfo info, boolean animate) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002503 if (mGesturesAction.intent != null &&
Romain Guy1ce1a242009-06-23 17:34:54 -07002504 info.intent.toUri(0).equals(mGesturesAction.intent.toUri(0)) &&
Romain Guy6fefcf12009-06-11 13:07:43 -07002505 info.title.equals(((TextView) mGesturesPrompt.getCurrentView()).getText())) {
2506 return;
2507 }
Romain Guy3cf604f2009-06-16 13:12:53 -07002508
2509 if (animate) {
2510 setGesturesNextPrompt(info.icon, info.title);
2511 } else {
2512 setGesturesPrompt(info.icon, info.title);
2513 }
2514
Romain Guy73b979d2009-06-09 12:57:21 -07002515 mGesturesAction.intent = info.intent;
2516 }
2517
2518 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002519 //noinspection PointlessBooleanExpression,ConstantConditions
2520 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2521 overlay.removeCallbacks(mMatcher);
2522 }
Romain Guy73b979d2009-06-09 12:57:21 -07002523 }
2524
2525 void addGesture(String name, Gesture gesture) {
2526 sLibrary.addGesture(name, gesture);
2527 // TODO: On a background thread?
2528 sLibrary.save();
2529 }
2530
2531 void update(ApplicationInfo info, Gesture gesture) {
2532 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002533 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002534 }
2535
2536 class GestureMatcher implements Runnable {
2537 Gesture gesture;
2538
2539 public void run() {
2540 if (gesture != null) {
2541 matchGesture(gesture);
2542 }
2543 }
2544 }
2545 }
2546
2547 private class GesturesAction implements View.OnClickListener {
2548 Intent intent;
2549
2550 public void onClick(View v) {
2551 if (intent != null) {
2552 startActivitySafely(intent);
2553 }
2554 }
2555 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556}
Karl Rosaen138a0412009-04-23 19:00:21 -07002557