blob: 513108181952270d403ccc1ed1d5ce362d87dcbf [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";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700178 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
180 private static Bitmap sWallpaper;
181
182 private static final Object sLock = new Object();
183 private static int sScreen = DEFAULT_SCREN;
184
185 private static WallpaperIntentReceiver sWallpaperReceiver;
186
Romain Guy73b979d2009-06-09 12:57:21 -0700187 private static GestureLibrary sLibrary;
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
190 private final ContentObserver mObserver = new FavoritesChangeObserver();
191
192 private LayoutInflater mInflater;
193
194 private DragLayer mDragLayer;
195 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700196
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700197 private AppWidgetManager mAppWidgetManager;
198 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700200 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private CellLayout.CellInfo mAddItemCellInfo;
203 private CellLayout.CellInfo mMenuAddInfo;
204 private final int[] mCellCoordinates = new int[2];
205 private FolderInfo mFolderInfo;
206
207 private SlidingDrawer mDrawer;
208 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700209 private HandleView mHandleView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private AllAppsGridView mAllAppsGrid;
211
212 private boolean mDesktopLocked = true;
213 private Bundle mSavedState;
214
215 private SpannableStringBuilder mDefaultKeySsb = null;
216
217 private boolean mDestroyed;
218
219 private boolean mRestoring;
220 private boolean mWaitingForResult;
221 private boolean mLocaleChanged;
222
223 private Bundle mSavedInstanceState;
224
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700225 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700226
Romain Guy73b979d2009-06-09 12:57:21 -0700227 private View mGesturesPanel;
228 private GestureOverlayView mGesturesOverlay;
229 private ViewSwitcher mGesturesPrompt;
230 private ImageView mGesturesAdd;
231 private PopupWindow mGesturesWindow;
232 private Launcher.GesturesProcessor mGesturesProcessor;
233 private Gesture mCurrentGesture;
234 private GesturesAction mGesturesAction;
Romain Guyaad5ef42009-06-10 02:48:37 -0700235 private boolean mHideGesturesPanel;
Romain Guy73b979d2009-06-09 12:57:21 -0700236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 @Override
238 protected void onCreate(Bundle savedInstanceState) {
239 super.onCreate(savedInstanceState);
240 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Romain Guy73b979d2009-06-09 12:57:21 -0700242 if (sLibrary == null) {
243 // The context is not kept by the library so it's safe to do this
244 sLibrary = GestureLibraries.fromPrivateFile(Launcher.this,
245 GesturesConstants.STORE_NAME);
246 }
247
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700248 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700249
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700250 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
251 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 if (PROFILE_STARTUP) {
254 android.os.Debug.startMethodTracing("/sdcard/launcher");
255 }
256
257 checkForLocaleChange();
258 setWallpaperDimension();
259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 setContentView(R.layout.launcher);
261 setupViews();
262
263 registerIntentReceivers();
264 registerContentObservers();
265
266 mSavedState = savedInstanceState;
267 restoreState(mSavedState);
268
269 if (PROFILE_STARTUP) {
270 android.os.Debug.stopMethodTracing();
271 }
272
273 if (!mRestoring) {
274 startLoaders();
275 }
276
277 // For handling default keys
278 mDefaultKeySsb = new SpannableStringBuilder();
279 Selection.setSelection(mDefaultKeySsb, 0);
280 }
Romain Guycbb89e42009-06-08 15:52:54 -0700281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private void checkForLocaleChange() {
283 final SharedPreferences preferences = getSharedPreferences(PREFERENCES, MODE_PRIVATE);
284 final Configuration configuration = getResources().getConfiguration();
285
286 final String previousLocale = preferences.getString(KEY_LOCALE, null);
287 final String locale = configuration.locale.toString();
288
289 final int previousMcc = preferences.getInt(KEY_MCC, -1);
290 final int mcc = configuration.mcc;
291
292 final int previousMnc = preferences.getInt(KEY_MNC, -1);
293 final int mnc = configuration.mnc;
294
295 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
296
297 if (mLocaleChanged) {
298 final SharedPreferences.Editor editor = preferences.edit();
299 editor.putString(KEY_LOCALE, locale);
300 editor.putInt(KEY_MCC, mcc);
301 editor.putInt(KEY_MNC, mnc);
302 editor.commit();
303 }
304 }
305
306 static int getScreen() {
307 synchronized (sLock) {
308 return sScreen;
309 }
310 }
311
312 static void setScreen(int screen) {
313 synchronized (sLock) {
314 sScreen = screen;
315 }
316 }
317
318 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700319 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
320 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
321
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 mRestoring = false;
323 }
324
325 private void setWallpaperDimension() {
326 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
327 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
328
329 Display display = getWindowManager().getDefaultDisplay();
330 boolean isPortrait = display.getWidth() < display.getHeight();
331
332 final int width = isPortrait ? display.getWidth() : display.getHeight();
333 final int height = isPortrait ? display.getHeight() : display.getWidth();
334 try {
335 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
336 } catch (RemoteException e) {
337 // System is dead!
338 }
339 }
340
341 @Override
342 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700343 mWaitingForResult = false;
344
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 // The pattern used here is that a user PICKs a specific application,
346 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
349 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700350
Romain Guy73b979d2009-06-09 12:57:21 -0700351 if (resultCode == RESULT_OK && (mAddItemCellInfo != null ||
352 ((requestCode == REQUEST_PICK_GESTURE_ACTION ||
353 requestCode == REQUEST_CREATE_GESTURE_ACTION ||
354 requestCode == REQUEST_CREATE_GESTURE_APPLICATION_ACTION) && mCurrentGesture != null))) {
355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 switch (requestCode) {
357 case REQUEST_PICK_APPLICATION:
358 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
359 break;
360 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700361 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 break;
363 case REQUEST_CREATE_SHORTCUT:
364 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
365 break;
366 case REQUEST_PICK_LIVE_FOLDER:
367 addLiveFolder(data);
368 break;
369 case REQUEST_CREATE_LIVE_FOLDER:
370 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
371 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700372 case REQUEST_PICK_APPWIDGET:
373 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700375 case REQUEST_CREATE_APPWIDGET:
376 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 break;
Romain Guy73b979d2009-06-09 12:57:21 -0700378 case REQUEST_PICK_GESTURE_ACTION:
379 processShortcut(data, REQUEST_CREATE_GESTURE_APPLICATION_ACTION,
380 REQUEST_CREATE_GESTURE_ACTION);
381 break;
382 case REQUEST_CREATE_GESTURE_ACTION:
383 completeCreateGesture(data, true);
384 break;
385 case REQUEST_CREATE_GESTURE_APPLICATION_ACTION:
386 completeCreateGesture(data, false);
387 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700389 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700391 // Clean up the appWidgetId if we canceled
392 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
393 if (appWidgetId != -1) {
394 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 }
396 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 }
398
399 @Override
400 protected void onResume() {
401 super.onResume();
402
403 if (mRestoring) {
404 startLoaders();
405 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700406
407 // Make sure that the search gadget (if any) is in its normal place.
Romain Guy5a941392009-04-28 15:18:25 -0700408 stopSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
410
411 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700412 protected void onPause() {
413 super.onPause();
Romain Guy73b979d2009-06-09 12:57:21 -0700414 if (mGesturesWindow != null) {
415 mGesturesWindow.setAnimationStyle(0);
416 mGesturesWindow.update();
417 }
Romain Guycbb89e42009-06-08 15:52:54 -0700418 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700419 }
Romain Guycbb89e42009-06-08 15:52:54 -0700420
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700421 @Override
Romain Guy73b979d2009-06-09 12:57:21 -0700422 protected void onStop() {
423 super.onStop();
Romain Guyaad5ef42009-06-10 02:48:37 -0700424 if (mHideGesturesPanel) {
425 mHideGesturesPanel = false;
426 hideGesturesPanel();
427 }
Romain Guy73b979d2009-06-09 12:57:21 -0700428 }
429
430 @Override
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700431 public Object onRetainNonConfigurationInstance() {
432 // Flag any binder to stop early before switching
433 if (mBinder != null) {
434 mBinder.mTerminate = true;
435 }
Romain Guycbb89e42009-06-08 15:52:54 -0700436
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700437 if (PROFILE_ROTATE) {
438 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
439 }
440 return null;
441 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 private boolean acceptFilter() {
444 final InputMethodManager inputManager = (InputMethodManager)
445 getSystemService(Context.INPUT_METHOD_SERVICE);
446 return !inputManager.isFullscreenMode();
447 }
448
449 @Override
450 public boolean onKeyDown(int keyCode, KeyEvent event) {
451 boolean handled = super.onKeyDown(keyCode, event);
452 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
453 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
454 keyCode, event);
455 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700456 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700457 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700458 // showSearchDialog()
459 // If there are multiple keystrokes before the search dialog takes focus,
460 // onSearchRequested() will be called for every keystroke,
461 // but it is idempotent, so it's fine.
462 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 }
464 }
465
466 return handled;
467 }
468
Karl Rosaen138a0412009-04-23 19:00:21 -0700469 private String getTypedText() {
470 return mDefaultKeySsb.toString();
471 }
472
473 private void clearTypedText() {
474 mDefaultKeySsb.clear();
475 mDefaultKeySsb.clearSpans();
476 Selection.setSelection(mDefaultKeySsb, 0);
477 }
478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 /**
480 * Restores the previous state, if it exists.
481 *
482 * @param savedState The previous state.
483 */
484 private void restoreState(Bundle savedState) {
485 if (savedState == null) {
486 return;
487 }
488
489 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
490 if (currentScreen > -1) {
491 mWorkspace.setCurrentScreen(currentScreen);
492 }
493
494 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
495 if (addScreen > -1) {
496 mAddItemCellInfo = new CellLayout.CellInfo();
497 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
498 addItemCellInfo.valid = true;
499 addItemCellInfo.screen = addScreen;
500 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
501 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
502 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
503 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
504 addItemCellInfo.findVacantCellsFromOccupied(
505 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
506 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
507 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
508 mRestoring = true;
509 }
510
511 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
512 if (renameFolder) {
513 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
514 mFolderInfo = sModel.getFolderById(this, id);
515 mRestoring = true;
516 }
Romain Guy73b979d2009-06-09 12:57:21 -0700517
518 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_PENDING_GESTURE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800519 }
520
521 /**
522 * Finds all the views we need and configure them properly.
523 */
524 private void setupViews() {
525 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
526 final DragLayer dragLayer = mDragLayer;
527
528 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
529 final Workspace workspace = mWorkspace;
530
531 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
532 final SlidingDrawer drawer = mDrawer;
533
534 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
535 final AllAppsGridView grid = mAllAppsGrid;
536
537 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
538
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700539 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
540 mHandleView.setLauncher(this);
541 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 mHandleIcon.setCrossFadeEnabled(true);
543
544 drawer.lock();
545 final DrawerManager drawerManager = new DrawerManager();
546 drawer.setOnDrawerOpenListener(drawerManager);
547 drawer.setOnDrawerCloseListener(drawerManager);
548 drawer.setOnDrawerScrollListener(drawerManager);
549
Karl Rosaen138a0412009-04-23 19:00:21 -0700550 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 grid.setDragger(dragLayer);
552 grid.setLauncher(this);
553
554 workspace.setOnLongClickListener(this);
555 workspace.setDragger(dragLayer);
556 workspace.setLauncher(this);
557 loadWallpaper();
558
559 deleteZone.setLauncher(this);
560 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700561 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562
563 dragLayer.setIgnoredDropTarget(grid);
564 dragLayer.setDragScoller(workspace);
565 dragLayer.setDragListener(deleteZone);
Romain Guy73b979d2009-06-09 12:57:21 -0700566
567 mGesturesPanel = mInflater.inflate(R.layout.gestures, mDragLayer, false);
568 final View gesturesPanel = mGesturesPanel;
569
570 mGesturesPrompt = (ViewSwitcher) gesturesPanel.findViewById(R.id.gestures_actions);
571 mGesturesAction = new GesturesAction();
572
573 mGesturesPrompt.getChildAt(0).setOnClickListener(mGesturesAction);
574 mGesturesPrompt.getChildAt(1).setOnClickListener(mGesturesAction);
575
576 mGesturesAdd = (ImageView) gesturesPanel.findViewById(R.id.gestures_add);
577 final ImageView gesturesAdd = mGesturesAdd;
578 gesturesAdd.setAlpha(128);
579 gesturesAdd.setEnabled(false);
580 gesturesAdd.setOnClickListener(new View.OnClickListener() {
581 public void onClick(View v) {
582 createGesture();
583 }
584 });
585
586 mGesturesOverlay = (GestureOverlayView) gesturesPanel.findViewById(R.id.gestures_overlay);
587 mGesturesProcessor = new GesturesProcessor();
588
589 final GestureOverlayView overlay = mGesturesOverlay;
Romain Guy73b979d2009-06-09 12:57:21 -0700590 overlay.addOnGestureListener(mGesturesProcessor);
Romain Guyb8734242009-06-10 11:53:57 -0700591 overlay.getGesturePaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
Romain Guy73b979d2009-06-09 12:57:21 -0700592 }
593
594 private void createGesture() {
595 mCurrentGesture = mGesturesOverlay.getGesture();
596 mWaitingForResult = true;
597 pickShortcut(REQUEST_PICK_GESTURE_ACTION, R.string.title_select_shortcut);
598 }
599
600 private void completeCreateGesture(Intent data, boolean isShortcut) {
601 ApplicationInfo info;
602
603 if (isShortcut) {
604 info = infoFromShortcutIntent(this, data);
605 } else {
606 info = infoFromApplicationIntent(this, data);
607 }
608
609 boolean success = false;
610 if (info != null) {
611 info.isGesture = true;
612
613 if (LauncherModel.addGestureToDatabase(this, info, false)) {
614 mGesturesProcessor.addGesture(String.valueOf(info.id), mCurrentGesture);
615 mGesturesProcessor.update(info, mCurrentGesture);
616 Toast.makeText(this, getString(R.string.gestures_created, info.title),
617 Toast.LENGTH_SHORT).show();
618 success = true;
619 }
620 }
621
622 if (!success) {
623 Toast.makeText(this, getString(R.string.gestures_failed), Toast.LENGTH_SHORT).show();
624 }
625
626 mCurrentGesture = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 }
628
629 /**
630 * Creates a view representing a shortcut.
631 *
632 * @param info The data structure describing the shortcut.
633 *
634 * @return A View inflated from R.layout.application.
635 */
636 View createShortcut(ApplicationInfo info) {
637 return createShortcut(R.layout.application,
638 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
639 }
640
641 /**
642 * Creates a view representing a shortcut inflated from the specified resource.
643 *
644 * @param layoutResId The id of the XML layout used to create the shortcut.
645 * @param parent The group the shortcut belongs to.
646 * @param info The data structure describing the shortcut.
647 *
648 * @return A View inflated from layoutResId.
649 */
650 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
651 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
652
653 if (!info.filtered) {
654 info.icon = Utilities.createIconThumbnail(info.icon, this);
655 info.filtered = true;
656 }
657
658 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
659 favorite.setText(info.title);
660 favorite.setTag(info);
661 favorite.setOnClickListener(this);
662
663 return favorite;
664 }
665
666 /**
667 * Add an application shortcut to the workspace.
668 *
669 * @param data The intent describing the application.
670 * @param cellInfo The position on screen where to create the shortcut.
671 */
672 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
673 boolean insertAtFirst) {
674 cellInfo.screen = mWorkspace.getCurrentScreen();
675 if (!findSingleSlot(cellInfo)) return;
676
Romain Guy73b979d2009-06-09 12:57:21 -0700677 final ApplicationInfo info = infoFromApplicationIntent(context, data);
678 if (info != null) {
679 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
680 }
681 }
682
683 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 ComponentName component = data.getComponent();
685 PackageManager packageManager = context.getPackageManager();
686 ActivityInfo activityInfo = null;
687 try {
688 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
689 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700690 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 }
Romain Guycbb89e42009-06-08 15:52:54 -0700692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 if (activityInfo != null) {
694 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700695
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 itemInfo.title = activityInfo.loadLabel(packageManager);
697 if (itemInfo.title == null) {
698 itemInfo.title = activityInfo.name;
699 }
Romain Guycbb89e42009-06-08 15:52:54 -0700700
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
702 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
703 itemInfo.icon = activityInfo.loadIcon(packageManager);
704 itemInfo.container = ItemInfo.NO_ID;
705
Romain Guy73b979d2009-06-09 12:57:21 -0700706 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 }
Romain Guy73b979d2009-06-09 12:57:21 -0700708
709 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 }
Romain Guycbb89e42009-06-08 15:52:54 -0700711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 /**
713 * Add a shortcut to the workspace.
714 *
715 * @param data The intent describing the shortcut.
716 * @param cellInfo The position on screen where to create the shortcut.
717 * @param insertAtFirst
718 */
719 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
720 boolean insertAtFirst) {
721 cellInfo.screen = mWorkspace.getCurrentScreen();
722 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700723
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
725
726 if (!mRestoring) {
727 sModel.addDesktopItem(info);
728
729 final View view = createShortcut(info);
730 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
731 } else if (sModel.isDesktopLoaded()) {
732 sModel.addDesktopItem(info);
733 }
734 }
735
Romain Guycbb89e42009-06-08 15:52:54 -0700736
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700738 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700740 * @param data The intent describing the appWidgetId.
741 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700743 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 boolean insertAtFirst) {
745
746 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700747 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700748
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700749 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700750
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700751 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700752
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700753 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700755 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700756
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 // Try finding open space on Launcher screen
758 final int[] xy = mCellCoordinates;
759 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
760
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700761 // Build Launcher-specific widget info and save to database
762 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 launcherInfo.spanX = spans[0];
764 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 LauncherModel.addItemToDatabase(this, launcherInfo,
767 LauncherSettings.Favorites.CONTAINER_DESKTOP,
768 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
769
770 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700771 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700772
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700774 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700775
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700776 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700778
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
780 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
781 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700782 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 }
784 }
Romain Guycbb89e42009-06-08 15:52:54 -0700785
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700786 public LauncherAppWidgetHost getAppWidgetHost() {
787 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 }
Romain Guycbb89e42009-06-08 15:52:54 -0700789
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 static ApplicationInfo addShortcut(Context context, Intent data,
791 CellLayout.CellInfo cellInfo, boolean notify) {
792
Romain Guy73b979d2009-06-09 12:57:21 -0700793 final ApplicationInfo info = infoFromShortcutIntent(context, data);
794 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
795 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
796
797 return info;
798 }
799
800 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
802 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
803 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
804
805 Drawable icon = null;
806 boolean filtered = false;
807 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700808 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809
810 if (bitmap != null) {
811 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
812 filtered = true;
813 customIcon = true;
814 } else {
815 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700816 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700818 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 final PackageManager packageManager = context.getPackageManager();
820 Resources resources = packageManager.getResourcesForApplication(
821 iconResource.packageName);
822 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
823 icon = resources.getDrawable(id);
824 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700825 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 }
827 }
828 }
829
830 if (icon == null) {
831 icon = context.getPackageManager().getDefaultActivityIcon();
832 }
833
834 final ApplicationInfo info = new ApplicationInfo();
835 info.icon = icon;
836 info.filtered = filtered;
837 info.title = name;
838 info.intent = intent;
839 info.customIcon = customIcon;
840 info.iconResource = iconResource;
841
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 return info;
843 }
844
845 @Override
846 protected void onNewIntent(Intent intent) {
847 super.onNewIntent(intent);
848
849 // Close the menu
850 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
851 getWindow().closeAllPanels();
852
853 try {
854 dismissDialog(DIALOG_CREATE_SHORTCUT);
855 // Unlock the workspace if the dialog was showing
856 mWorkspace.unlock();
857 } catch (Exception e) {
858 // An exception is thrown if the dialog is not visible, which is fine
859 }
860
861 try {
862 dismissDialog(DIALOG_RENAME_FOLDER);
863 // Unlock the workspace if the dialog was showing
864 mWorkspace.unlock();
865 } catch (Exception e) {
866 // An exception is thrown if the dialog is not visible, which is fine
867 }
868
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
870 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700871
872 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE) {
Romain Guyb8734242009-06-10 11:53:57 -0700873 SearchManager searchManager =
874 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
875 if (!searchManager.isVisible()) {
876 onHomeKeyPressed();
877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 }
879 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700880
881 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 if (v != null && v.getWindowToken() != null) {
883 InputMethodManager imm = (InputMethodManager)getSystemService(
884 INPUT_METHOD_SERVICE);
885 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
886 }
887 } else {
888 closeDrawer(false);
889 }
890 }
891 }
892
Romain Guy73b979d2009-06-09 12:57:21 -0700893 private void onHomeKeyPressed() {
894 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
895 showGesturesPanel();
896 } else {
897 hideGesturesPanel();
898 }
899 }
900
901 private void showGesturesPanel() {
902 resetGesturesPrompt();
903
904 mGesturesAdd.setEnabled(false);
905 mGesturesAdd.setAlpha(128);
906
907 mGesturesOverlay.clear(false);
908
909 PopupWindow window;
910 if (mGesturesWindow == null) {
911 mGesturesWindow = new PopupWindow(this);
912 window = mGesturesWindow;
913 window.setFocusable(true);
914 window.setTouchable(true);
915 window.setBackgroundDrawable(null);
916 window.setContentView(mGesturesPanel);
917 } else {
918 window = mGesturesWindow;
919 }
920 window.setAnimationStyle(com.android.internal.R.style.Animation_SlidingCard);
921
922 final int[] xy = new int[2];
923 final DragLayer dragLayer = mDragLayer;
924 dragLayer.getLocationOnScreen(xy);
925
926 window.setWidth(dragLayer.getWidth());
927 window.setHeight(dragLayer.getHeight() - 1);
928 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
929 }
930
931 private void resetGesturesPrompt() {
932 mGesturesAction.intent = null;
933 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
934 prompt.setText(R.string.gestures_instructions);
935 prompt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
936 prompt.setClickable(false);
937 }
938
939 private void resetGesturesNextPrompt() {
940 mGesturesAction.intent = null;
941 setGesturesNextPrompt(null, getString(R.string.gestures_instructions));
942 mGesturesPrompt.getNextView().setClickable(false);
943 }
944
945 private void setGesturesNextPrompt(Drawable icon, CharSequence title) {
946 final TextView prompt = (TextView) mGesturesPrompt.getNextView();
947 prompt.setText(title);
948 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
949 prompt.setClickable(true);
950 mGesturesPrompt.showNext();
951 }
952
953 void hideGesturesPanel() {
954 if (mGesturesWindow != null) {
955 mGesturesWindow.setAnimationStyle(com.android.internal.R.style.Animation_SlidingCard);
956 mGesturesWindow.update();
957 mGesturesWindow.dismiss();
958 }
959 }
960
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 @Override
962 protected void onRestoreInstanceState(Bundle savedInstanceState) {
963 // Do not call super here
964 mSavedInstanceState = savedInstanceState;
965 }
966
967 @Override
968 protected void onSaveInstanceState(Bundle outState) {
969 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
970
971 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
972 if (folders.size() > 0) {
973 final int count = folders.size();
974 long[] ids = new long[count];
975 for (int i = 0; i < count; i++) {
976 final FolderInfo info = folders.get(i).getInfo();
977 ids[i] = info.id;
978 }
979 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
980 } else {
981 super.onSaveInstanceState(outState);
982 }
983
Romain Guy5a941392009-04-28 15:18:25 -0700984 // When the drawer is opened and we are saving the state because of a
985 // configuration change
986 if (mDrawer.isOpened() && getChangingConfigurations() != 0) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989
990 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
991 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
992 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
993
994 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
995 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
996 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
997 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
998 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
999 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1000 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1001 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1002 layout.getOccupiedCells());
1003 }
1004
1005 if (mFolderInfo != null && mWaitingForResult) {
1006 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1007 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1008 }
Romain Guy73b979d2009-06-09 12:57:21 -07001009
1010 if (mCurrentGesture != null && mWaitingForResult) {
1011 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1012 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
1014
1015 @Override
1016 public void onDestroy() {
1017 mDestroyed = true;
1018
1019 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001022 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001024 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 }
1026
1027 TextKeyListener.getInstance().release();
1028
1029 mAllAppsGrid.clearTextFilter();
1030 mAllAppsGrid.setAdapter(null);
1031 sModel.unbind();
1032 sModel.abortLoaders();
1033
1034 getContentResolver().unregisterContentObserver(mObserver);
1035 unregisterReceiver(mApplicationsReceiver);
1036 }
1037
1038 @Override
1039 public void startActivityForResult(Intent intent, int requestCode) {
1040 mWaitingForResult = true;
1041 super.startActivityForResult(intent, requestCode);
1042 }
1043
1044 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001045 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001047
1048 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001049
Karl Rosaen138a0412009-04-23 19:00:21 -07001050 // Slide the search widget to the top, if it's on the current screen,
1051 // otherwise show the search dialog immediately.
1052 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1053 if (searchWidget == null) {
1054 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1055 } else {
1056 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1057 // show the currently typed text in the search widget while sliding
1058 searchWidget.setQuery(getTypedText());
1059 }
1060 }
Romain Guycbb89e42009-06-08 15:52:54 -07001061
Karl Rosaen138a0412009-04-23 19:00:21 -07001062 /**
1063 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001064 *
1065 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001066 */
Romain Guycbb89e42009-06-08 15:52:54 -07001067 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001068 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001069
Karl Rosaen138a0412009-04-23 19:00:21 -07001070 if (initialQuery == null) {
1071 // Use any text typed in the launcher as the initial query
1072 initialQuery = getTypedText();
1073 clearTypedText();
1074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 if (appSearchData == null) {
1076 appSearchData = new Bundle();
1077 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1078 }
Romain Guycbb89e42009-06-08 15:52:54 -07001079
Karl Rosaen138a0412009-04-23 19:00:21 -07001080 final SearchManager searchManager =
1081 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1082
1083 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1084 if (searchWidget != null) {
1085 // This gets called when the user leaves the search dialog to go back to
1086 // the Launcher.
1087 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1088 public void onCancel() {
1089 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001090 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001091 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001092 });
1093 }
Romain Guycbb89e42009-06-08 15:52:54 -07001094
Karl Rosaen138a0412009-04-23 19:00:21 -07001095 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001096 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
1098
Karl Rosaen138a0412009-04-23 19:00:21 -07001099 /**
1100 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001101 */
Romain Guy5a941392009-04-28 15:18:25 -07001102 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001103 // Close search dialog
1104 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1105 if (searchManager.isVisible()) {
1106 searchManager.stopSearch();
1107 }
1108 // Restore search widget to its normal position
1109 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1110 if (searchWidget != null) {
1111 searchWidget.stopSearch(false);
1112 }
1113 }
Romain Guycbb89e42009-06-08 15:52:54 -07001114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 @Override
1116 public boolean onCreateOptionsMenu(Menu menu) {
1117 if (mDesktopLocked) return false;
1118
1119 super.onCreateOptionsMenu(menu);
1120 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1121 .setIcon(android.R.drawable.ic_menu_add)
1122 .setAlphabeticShortcut('A');
1123 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1124 .setIcon(android.R.drawable.ic_menu_gallery)
1125 .setAlphabeticShortcut('W');
1126 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1127 .setIcon(android.R.drawable.ic_search_category_default)
1128 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1129 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1130 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1131 .setAlphabeticShortcut('N');
1132
Romain Guy73b979d2009-06-09 12:57:21 -07001133 final Intent gestures = new Intent(this, GesturesActivity.class);
1134 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1135 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1136 .setIntent(gestures);
1137
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001139 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1140 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141
1142 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1143 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1144 .setIntent(settings);
1145
1146 return true;
1147 }
1148
1149 @Override
1150 public boolean onPrepareOptionsMenu(Menu menu) {
1151 super.onPrepareOptionsMenu(menu);
1152
1153 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1154 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1155
1156 return true;
1157 }
1158
1159 @Override
1160 public boolean onOptionsItemSelected(MenuItem item) {
1161 switch (item.getItemId()) {
1162 case MENU_ADD:
1163 addItems();
1164 return true;
1165 case MENU_WALLPAPER_SETTINGS:
1166 startWallpaper();
1167 return true;
1168 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001169 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 return true;
1171 case MENU_NOTIFICATIONS:
1172 showNotifications();
1173 return true;
1174 }
1175
1176 return super.onOptionsItemSelected(item);
1177 }
Romain Guycbb89e42009-06-08 15:52:54 -07001178
Karl Rosaen138a0412009-04-23 19:00:21 -07001179 /**
1180 * Indicates that we want global search for this activity by setting the globalSearch
1181 * argument for {@link #startSearch} to true.
1182 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001184 @Override
1185 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001186 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001187 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001188 }
1189
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 private void addItems() {
1191 showAddDialog(mMenuAddInfo);
1192 }
1193
1194 private void removeShortcutsForPackage(String packageName) {
1195 if (packageName != null && packageName.length() > 0) {
1196 mWorkspace.removeShortcutsForPackage(packageName);
1197 }
1198 }
Romain Guycbb89e42009-06-08 15:52:54 -07001199
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001200 private void updateShortcutsForPackage(String packageName) {
1201 if (packageName != null && packageName.length() > 0) {
1202 mWorkspace.updateShortcutsForPackage(packageName);
1203 }
1204 }
1205
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001206 void addAppWidget(Intent data) {
1207 // TODO: catch bad widget exception when sent
1208 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001210 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1211 if (SEARCH_WIDGET.equals(customWidget)) {
1212 // We don't need this any more, since this isn't a real app widget.
1213 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1214 // add the search widget
1215 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001217 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1218
1219 if (appWidget.configure != null) {
1220 // Launch over to configure widget, if needed
1221 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1222 intent.setComponent(appWidget.configure);
1223 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1224
1225 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1226 } else {
1227 // Otherwise just add it
1228 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
1231 }
Romain Guycbb89e42009-06-08 15:52:54 -07001232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 void addSearch() {
1234 final Widget info = Widget.makeSearch();
1235 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 final int[] xy = mCellCoordinates;
1238 final int spanX = info.spanX;
1239 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 sModel.addDesktopItem(info);
1244 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1245 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 final View view = mInflater.inflate(info.layoutResource, null);
1248 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001249 Search search = (Search) view.findViewById(R.id.widget_search);
1250 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1253 }
1254
Romain Guy73b979d2009-06-09 12:57:21 -07001255 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001256 // Handle case where user selected "Applications"
1257 String applicationName = getResources().getString(R.string.group_applications);
1258 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001259
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001260 if (applicationName != null && applicationName.equals(shortcutName)) {
1261 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1262 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001263
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001264 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1265 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001266 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001267 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001268 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 }
1271
1272 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001273 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001274 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001275 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001276
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001277 if (folderName != null && folderName.equals(shortcutName)) {
1278 addFolder(!mDesktopLocked);
1279 } else {
1280 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1281 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 }
1283
1284 void addFolder(boolean insertAtFirst) {
1285 UserFolderInfo folderInfo = new UserFolderInfo();
1286 folderInfo.title = getText(R.string.folder_name);
1287
1288 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1289 cellInfo.screen = mWorkspace.getCurrentScreen();
1290 if (!findSingleSlot(cellInfo)) return;
1291
1292 // Update the model
1293 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1294 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1295 sModel.addDesktopItem(folderInfo);
1296 sModel.addFolder(folderInfo);
1297
1298 // Create the view
1299 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1300 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1301 mWorkspace.addInCurrentScreen(newFolder,
1302 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1303 }
Romain Guycbb89e42009-06-08 15:52:54 -07001304
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1306 boolean insertAtFirst) {
1307 cellInfo.screen = mWorkspace.getCurrentScreen();
1308 if (!findSingleSlot(cellInfo)) return;
1309
1310 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1311
1312 if (!mRestoring) {
1313 sModel.addDesktopItem(info);
1314
1315 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1316 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1317 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1318 } else if (sModel.isDesktopLoaded()) {
1319 sModel.addDesktopItem(info);
1320 }
1321 }
1322
1323 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1324 CellLayout.CellInfo cellInfo, boolean notify) {
1325
1326 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1327 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1328
1329 Drawable icon = null;
1330 boolean filtered = false;
1331 Intent.ShortcutIconResource iconResource = null;
1332
1333 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1334 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1335 try {
1336 iconResource = (Intent.ShortcutIconResource) extra;
1337 final PackageManager packageManager = context.getPackageManager();
1338 Resources resources = packageManager.getResourcesForApplication(
1339 iconResource.packageName);
1340 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1341 icon = resources.getDrawable(id);
1342 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001343 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
1345 }
1346
1347 if (icon == null) {
1348 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1349 }
1350
1351 final LiveFolderInfo info = new LiveFolderInfo();
1352 info.icon = icon;
1353 info.filtered = filtered;
1354 info.title = name;
1355 info.iconResource = iconResource;
1356 info.uri = data.getData();
1357 info.baseIntent = baseIntent;
1358 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1359 LiveFolders.DISPLAY_MODE_GRID);
1360
1361 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1362 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1363 sModel.addFolder(info);
1364
1365 return info;
1366 }
1367
1368 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1369 final int[] xy = new int[2];
1370 if (findSlot(cellInfo, xy, 1, 1)) {
1371 cellInfo.cellX = xy[0];
1372 cellInfo.cellY = xy[1];
1373 return true;
1374 }
1375 return false;
1376 }
1377
1378 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1379 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1380 boolean[] occupied = mSavedState != null ?
1381 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1382 cellInfo = mWorkspace.findAllVacantCells(occupied);
1383 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1384 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1385 return false;
1386 }
1387 }
1388 return true;
1389 }
1390
1391 private void showNotifications() {
1392 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1393 if (statusBar != null) {
1394 statusBar.expand();
1395 }
1396 }
1397
1398 private void startWallpaper() {
1399 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1400 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1401 }
1402
1403 /**
1404 * Registers various intent receivers. The current implementation registers
1405 * only a wallpaper intent receiver to let other applications change the
1406 * wallpaper.
1407 */
1408 private void registerIntentReceivers() {
1409 if (sWallpaperReceiver == null) {
1410 final Application application = getApplication();
1411
1412 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1413
1414 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1415 application.registerReceiver(sWallpaperReceiver, filter);
1416 } else {
1417 sWallpaperReceiver.setLauncher(this);
1418 }
1419
1420 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1421 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1422 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1423 filter.addDataScheme("package");
1424 registerReceiver(mApplicationsReceiver, filter);
1425 }
1426
1427 /**
1428 * Registers various content observers. The current implementation registers
1429 * only a favorites observer to keep track of the favorites applications.
1430 */
1431 private void registerContentObservers() {
1432 ContentResolver resolver = getContentResolver();
1433 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1434 }
1435
1436 @Override
1437 public boolean dispatchKeyEvent(KeyEvent event) {
1438 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1439 switch (event.getKeyCode()) {
1440 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001441 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001442 if (mDrawer.isOpened()) {
1443 closeDrawer();
1444 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001445 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 return true;
1448 case KeyEvent.KEYCODE_HOME:
1449 return true;
1450 }
1451 }
1452
1453 return super.dispatchKeyEvent(event);
1454 }
1455
1456 private void closeDrawer() {
1457 closeDrawer(true);
1458 }
1459
1460 private void closeDrawer(boolean animated) {
1461 if (mDrawer.isOpened()) {
1462 if (animated) {
1463 mDrawer.animateClose();
1464 } else {
1465 mDrawer.close();
1466 }
1467 if (mDrawer.hasFocus()) {
1468 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1469 }
1470 }
1471 }
1472
1473 private void closeFolder() {
1474 Folder folder = mWorkspace.getOpenFolder();
1475 if (folder != null) {
1476 closeFolder(folder);
1477 }
1478 }
1479
1480 void closeFolder(Folder folder) {
1481 folder.getInfo().opened = false;
1482 ViewGroup parent = (ViewGroup) folder.getParent();
1483 if (parent != null) {
1484 parent.removeView(folder);
1485 }
1486 folder.onClose();
1487 }
1488
1489 /**
1490 * When the notification that favorites have changed is received, requests
1491 * a favorites list refresh.
1492 */
1493 private void onFavoritesChanged() {
1494 mDesktopLocked = true;
1495 mDrawer.lock();
1496 sModel.loadUserItems(false, this, false, false);
1497 }
1498
1499 void onDesktopItemsLoaded() {
1500 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 bindDesktopItems();
1502 }
Romain Guycbb89e42009-06-08 15:52:54 -07001503
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 /**
1505 * Refreshes the shortcuts shown on the workspace.
1506 */
1507 private void bindDesktopItems() {
1508 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001509 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001510 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1511 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 return;
1513 }
1514
1515 final Workspace workspace = mWorkspace;
1516 int count = workspace.getChildCount();
1517 for (int i = 0; i < count; i++) {
1518 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1519 }
Romain Guycbb89e42009-06-08 15:52:54 -07001520
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 if (DEBUG_USER_INTERFACE) {
1522 android.widget.Button finishButton = new android.widget.Button(this);
1523 finishButton.setText("Finish");
1524 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1525
1526 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1527 public void onClick(View v) {
1528 finish();
1529 }
1530 });
1531 }
Romain Guycbb89e42009-06-08 15:52:54 -07001532
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001533 // Flag any old binder to terminate early
1534 if (mBinder != null) {
1535 mBinder.mTerminate = true;
1536 }
Romain Guycbb89e42009-06-08 15:52:54 -07001537
Karl Rosaen138a0412009-04-23 19:00:21 -07001538 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001539 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541
1542 private void bindItems(Launcher.DesktopBinder binder,
1543 ArrayList<ItemInfo> shortcuts, int start, int count) {
1544
1545 final Workspace workspace = mWorkspace;
1546 final boolean desktopLocked = mDesktopLocked;
1547
1548 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1549 int i = start;
1550
1551 for ( ; i < end; i++) {
1552 final ItemInfo item = shortcuts.get(i);
1553 switch (item.itemType) {
1554 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1555 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1556 final View shortcut = createShortcut((ApplicationInfo) item);
1557 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1558 !desktopLocked);
1559 break;
1560 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1561 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1562 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1563 (UserFolderInfo) item);
1564 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1565 !desktopLocked);
1566 break;
1567 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1568 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1569 R.layout.live_folder_icon, this,
1570 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1571 (LiveFolderInfo) item);
1572 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1573 !desktopLocked);
1574 break;
1575 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1576 final int screen = workspace.getCurrentScreen();
1577 final View view = mInflater.inflate(R.layout.widget_search,
1578 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001579
Karl Rosaen138a0412009-04-23 19:00:21 -07001580 Search search = (Search) view.findViewById(R.id.widget_search);
1581 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 final Widget widget = (Widget) item;
1584 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001585
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 workspace.addWidget(view, widget, !desktopLocked);
1587 break;
1588 }
1589 }
1590
1591 workspace.requestLayout();
1592
1593 if (end >= count) {
1594 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001595 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 } else {
1597 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1598 }
1599 }
1600
1601 private void finishBindDesktopItems() {
1602 if (mSavedState != null) {
1603 if (!mWorkspace.hasFocus()) {
1604 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1605 }
1606
1607 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1608 if (userFolders != null) {
1609 for (long folderId : userFolders) {
1610 final FolderInfo info = sModel.findFolderById(folderId);
1611 if (info != null) {
1612 openFolder(info);
1613 }
1614 }
1615 final Folder openFolder = mWorkspace.getOpenFolder();
1616 if (openFolder != null) {
1617 openFolder.requestFocus();
1618 }
1619 }
1620
1621 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1622 if (allApps) {
1623 mDrawer.open();
1624 }
1625
1626 mSavedState = null;
1627 }
1628
1629 if (mSavedInstanceState != null) {
1630 super.onRestoreInstanceState(mSavedInstanceState);
1631 mSavedInstanceState = null;
1632 }
1633
1634 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1635 mDrawer.requestFocus();
1636 }
1637
1638 mDesktopLocked = false;
1639 mDrawer.unlock();
1640 }
Romain Guycbb89e42009-06-08 15:52:54 -07001641
Karl Rosaen138a0412009-04-23 19:00:21 -07001642 private void bindDrawer(Launcher.DesktopBinder binder,
1643 ApplicationsAdapter drawerAdapter) {
1644 mAllAppsGrid.setAdapter(drawerAdapter);
1645 binder.startBindingAppWidgetsWhenIdle();
1646 }
Romain Guycbb89e42009-06-08 15:52:54 -07001647
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001648 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001649 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001650
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 final Workspace workspace = mWorkspace;
1652 final boolean desktopLocked = mDesktopLocked;
1653
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001654 if (!appWidgets.isEmpty()) {
1655 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001656
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001657 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001658 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001659 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001660
Karl Rosaen138a0412009-04-23 19:00:21 -07001661 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001662
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001663 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1664 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001665
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001666 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1667 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001668
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001669 workspace.requestLayout();
1670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001672 if (appWidgets.isEmpty()) {
1673 if (PROFILE_ROTATE) {
1674 android.os.Debug.stopMethodTracing();
1675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001677 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 }
1679 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001680
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 DragController getDragController() {
1682 return mDragLayer;
1683 }
1684
1685 /**
1686 * Launches the intent referred by the clicked shortcut.
1687 *
1688 * @param v The view representing the clicked shortcut.
1689 */
1690 public void onClick(View v) {
1691 Object tag = v.getTag();
1692 if (tag instanceof ApplicationInfo) {
1693 // Open shortcut
1694 final Intent intent = ((ApplicationInfo) tag).intent;
1695 startActivitySafely(intent);
1696 } else if (tag instanceof FolderInfo) {
1697 handleFolderClick((FolderInfo) tag);
1698 }
1699 }
1700
1701 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001702 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1704 try {
1705 startActivity(intent);
1706 } catch (ActivityNotFoundException e) {
1707 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1708 } catch (SecurityException e) {
1709 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001710 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1712 "or use the exported attribute for this activity.", e);
1713 }
1714 }
1715
1716 private void handleFolderClick(FolderInfo folderInfo) {
1717 if (!folderInfo.opened) {
1718 // Close any open folder
1719 closeFolder();
1720 // Open the requested folder
1721 openFolder(folderInfo);
1722 } else {
1723 // Find the open folder...
1724 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1725 int folderScreen;
1726 if (openFolder != null) {
1727 folderScreen = mWorkspace.getScreenForView(openFolder);
1728 // .. and close it
1729 closeFolder(openFolder);
1730 if (folderScreen != mWorkspace.getCurrentScreen()) {
1731 // Close any folder open on the current screen
1732 closeFolder();
1733 // Pull the folder onto this screen
1734 openFolder(folderInfo);
1735 }
1736 }
1737 }
1738 }
1739
1740 private void loadWallpaper() {
1741 // The first time the application is started, we load the wallpaper from
1742 // the ApplicationContext
1743 if (sWallpaper == null) {
1744 final Drawable drawable = getWallpaper();
1745 if (drawable instanceof BitmapDrawable) {
1746 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1747 } else {
1748 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1749 }
1750 }
1751 mWorkspace.loadWallpaper(sWallpaper);
1752 }
1753
1754 /**
1755 * Opens the user fodler described by the specified tag. The opening of the folder
1756 * is animated relative to the specified View. If the View is null, no animation
1757 * is played.
1758 *
1759 * @param folderInfo The FolderInfo describing the folder to open.
1760 */
1761 private void openFolder(FolderInfo folderInfo) {
1762 Folder openFolder;
1763
1764 if (folderInfo instanceof UserFolderInfo) {
1765 openFolder = UserFolder.fromXml(this);
1766 } else if (folderInfo instanceof LiveFolderInfo) {
1767 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1768 } else {
1769 return;
1770 }
1771
1772 openFolder.setDragger(mDragLayer);
1773 openFolder.setLauncher(this);
1774
1775 openFolder.bind(folderInfo);
1776 folderInfo.opened = true;
1777
1778 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1779 openFolder.onOpen();
1780 }
1781
1782 /**
1783 * Returns true if the workspace is being loaded. When the workspace is loading,
1784 * no user interaction should be allowed to avoid any conflict.
1785 *
1786 * @return True if the workspace is locked, false otherwise.
1787 */
1788 boolean isWorkspaceLocked() {
1789 return mDesktopLocked;
1790 }
1791
1792 public boolean onLongClick(View v) {
1793 if (mDesktopLocked) {
1794 return false;
1795 }
1796
1797 if (!(v instanceof CellLayout)) {
1798 v = (View) v.getParent();
1799 }
1800
1801 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1802
1803 // This happens when long clicking an item with the dpad/trackball
1804 if (cellInfo == null) {
1805 return true;
1806 }
1807
1808 if (mWorkspace.allowLongPress()) {
1809 if (cellInfo.cell == null) {
1810 if (cellInfo.valid) {
1811 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001812 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 showAddDialog(cellInfo);
1814 }
1815 } else {
1816 if (!(cellInfo.cell instanceof Folder)) {
1817 // User long pressed on an item
1818 mWorkspace.startDrag(cellInfo);
1819 }
1820 }
1821 }
1822 return true;
1823 }
1824
1825 static LauncherModel getModel() {
1826 return sModel;
1827 }
1828
Romain Guy73b979d2009-06-09 12:57:21 -07001829 static GestureLibrary getGestureLibrary() {
1830 return sLibrary;
1831 }
1832
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 void closeAllApplications() {
1834 mDrawer.close();
1835 }
1836
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001837 View getDrawerHandle() {
1838 return mHandleView;
1839 }
1840
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 boolean isDrawerDown() {
1842 return !mDrawer.isMoving() && !mDrawer.isOpened();
1843 }
1844
1845 boolean isDrawerUp() {
1846 return mDrawer.isOpened() && !mDrawer.isMoving();
1847 }
1848
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001849 boolean isDrawerMoving() {
1850 return mDrawer.isMoving();
1851 }
1852
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 Workspace getWorkspace() {
1854 return mWorkspace;
1855 }
1856
1857 GridView getApplicationsGrid() {
1858 return mAllAppsGrid;
1859 }
1860
1861 @Override
1862 protected Dialog onCreateDialog(int id) {
1863 switch (id) {
1864 case DIALOG_CREATE_SHORTCUT:
1865 return new CreateShortcut().createDialog();
1866 case DIALOG_RENAME_FOLDER:
1867 return new RenameFolder().createDialog();
1868 }
1869
1870 return super.onCreateDialog(id);
1871 }
1872
1873 @Override
1874 protected void onPrepareDialog(int id, Dialog dialog) {
1875 switch (id) {
1876 case DIALOG_CREATE_SHORTCUT:
1877 mWorkspace.lock();
1878 break;
1879 case DIALOG_RENAME_FOLDER:
1880 mWorkspace.lock();
1881 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1882 final CharSequence text = mFolderInfo.title;
1883 input.setText(text);
Romain Guycbb89e42009-06-08 15:52:54 -07001884 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 break;
1886 }
1887 }
1888
1889 void showRenameDialog(FolderInfo info) {
1890 mFolderInfo = info;
1891 mWaitingForResult = true;
1892 showDialog(DIALOG_RENAME_FOLDER);
1893 }
1894
1895 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1896 mAddItemCellInfo = cellInfo;
1897 mWaitingForResult = true;
1898 showDialog(DIALOG_CREATE_SHORTCUT);
1899 }
1900
Romain Guy73b979d2009-06-09 12:57:21 -07001901 private void pickShortcut(int requestCode, int title) {
1902 Bundle bundle = new Bundle();
1903
1904 ArrayList<String> shortcutNames = new ArrayList<String>();
1905 shortcutNames.add(getString(R.string.group_applications));
1906 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1907
1908 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1909 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1910 R.drawable.ic_launcher_application));
1911 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1912
1913 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1914 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1915 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1916 pickIntent.putExtras(bundle);
1917
1918 startActivityForResult(pickIntent, requestCode);
1919 }
1920
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 private class RenameFolder {
1922 private EditText mInput;
1923
1924 Dialog createDialog() {
1925 mWaitingForResult = true;
1926 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1927 mInput = (EditText) layout.findViewById(R.id.folder_name);
1928
1929 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1930 builder.setIcon(0);
1931 builder.setTitle(getString(R.string.rename_folder_title));
1932 builder.setCancelable(true);
1933 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1934 public void onCancel(DialogInterface dialog) {
1935 cleanup();
1936 }
1937 });
1938 builder.setNegativeButton(getString(R.string.cancel_action),
1939 new Dialog.OnClickListener() {
1940 public void onClick(DialogInterface dialog, int which) {
1941 cleanup();
1942 }
1943 }
1944 );
1945 builder.setPositiveButton(getString(R.string.rename_action),
1946 new Dialog.OnClickListener() {
1947 public void onClick(DialogInterface dialog, int which) {
1948 changeFolderName();
1949 }
1950 }
1951 );
1952 builder.setView(layout);
1953 return builder.create();
1954 }
1955
1956 private void changeFolderName() {
1957 final String name = mInput.getText().toString();
1958 if (!TextUtils.isEmpty(name)) {
1959 // Make sure we have the right folder info
1960 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1961 mFolderInfo.title = name;
1962 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1963
1964 if (mDesktopLocked) {
1965 mDrawer.lock();
1966 sModel.loadUserItems(false, Launcher.this, false, false);
1967 } else {
1968 final FolderIcon folderIcon = (FolderIcon)
1969 mWorkspace.getViewForTag(mFolderInfo);
1970 if (folderIcon != null) {
1971 folderIcon.setText(name);
1972 getWorkspace().requestLayout();
1973 } else {
1974 mDesktopLocked = true;
1975 mDrawer.lock();
1976 sModel.loadUserItems(false, Launcher.this, false, false);
1977 }
1978 }
1979 }
1980 cleanup();
1981 }
1982
1983 private void cleanup() {
1984 mWorkspace.unlock();
1985 dismissDialog(DIALOG_RENAME_FOLDER);
1986 mWaitingForResult = false;
1987 mFolderInfo = null;
1988 }
1989 }
1990
1991 /**
1992 * Displays the shortcut creation dialog and launches, if necessary, the
1993 * appropriate activity.
1994 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001995 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07001996 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 Dialog createDialog() {
2000 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2005 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002006 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 builder.setInverseBackgroundForced(true);
2009
2010 AlertDialog dialog = builder.create();
2011 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002012 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 return dialog;
2015 }
2016
2017 public void onCancel(DialogInterface dialog) {
2018 mWaitingForResult = false;
2019 cleanup();
2020 }
2021
Romain Guycbb89e42009-06-08 15:52:54 -07002022 public void onDismiss(DialogInterface dialog) {
2023 mWorkspace.unlock();
2024 }
2025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 private void cleanup() {
2027 mWorkspace.unlock();
2028 dismissDialog(DIALOG_CREATE_SHORTCUT);
2029 }
2030
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002031 /**
2032 * Handle the action clicked in the "Add to home" dialog.
2033 */
2034 public void onClick(DialogInterface dialog, int which) {
2035 Resources res = getResources();
2036 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002037
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002038 switch (which) {
2039 case AddAdapter.ITEM_SHORTCUT: {
2040 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002041 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002042 break;
2043 }
Romain Guycbb89e42009-06-08 15:52:54 -07002044
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002045 case AddAdapter.ITEM_APPWIDGET: {
2046 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002047
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002048 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2049 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2050 // add the search widget
2051 ArrayList<AppWidgetProviderInfo> customInfo =
2052 new ArrayList<AppWidgetProviderInfo>();
2053 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2054 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2055 info.label = getString(R.string.group_search);
2056 info.icon = R.drawable.ic_search_widget;
2057 customInfo.add(info);
2058 pickIntent.putParcelableArrayListExtra(
2059 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2060 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2061 Bundle b = new Bundle();
2062 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2063 customExtras.add(b);
2064 pickIntent.putParcelableArrayListExtra(
2065 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2066 // start the pick activity
2067 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2068 break;
2069 }
Romain Guycbb89e42009-06-08 15:52:54 -07002070
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002071 case AddAdapter.ITEM_LIVE_FOLDER: {
2072 // Insert extra item to handle inserting folder
2073 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002074
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002075 ArrayList<String> shortcutNames = new ArrayList<String>();
2076 shortcutNames.add(res.getString(R.string.group_folder));
2077 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002078
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002079 ArrayList<ShortcutIconResource> shortcutIcons =
2080 new ArrayList<ShortcutIconResource>();
2081 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2082 R.drawable.ic_launcher_folder));
2083 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2084
2085 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2086 pickIntent.putExtra(Intent.EXTRA_INTENT,
2087 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2088 pickIntent.putExtra(Intent.EXTRA_TITLE,
2089 getText(R.string.title_select_live_folder));
2090 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002091
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002092 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2093 break;
2094 }
2095
2096 case AddAdapter.ITEM_WALLPAPER: {
2097 startWallpaper();
2098 break;
2099 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 }
2101 }
2102 }
2103
2104 /**
2105 * Receives notifications when applications are added/removed.
2106 */
2107 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2108 @Override
2109 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002110 final String action = intent.getAction();
2111 final String packageName = intent.getData().getSchemeSpecificPart();
2112 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2113
2114 if (LauncherModel.DEBUG_LOADERS) {
2115 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2116 ", replacing=" + replacing);
2117 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002119
2120 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2121 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2122 if (!replacing) {
2123 removeShortcutsForPackage(packageName);
2124 if (LauncherModel.DEBUG_LOADERS) {
2125 d(LauncherModel.LOG_TAG, " --> remove package");
2126 }
2127 sModel.removePackage(Launcher.this, packageName);
2128 }
2129 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2130 // later, we will update the package at this time
2131 } else {
2132 if (!replacing) {
2133 if (LauncherModel.DEBUG_LOADERS) {
2134 d(LauncherModel.LOG_TAG, " --> add package");
2135 }
2136 sModel.addPackage(Launcher.this, packageName);
2137 } else {
2138 if (LauncherModel.DEBUG_LOADERS) {
2139 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2140 }
2141 sModel.updatePackage(Launcher.this, packageName);
2142 updateShortcutsForPackage(packageName);
2143 }
2144 }
2145 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002147 if (LauncherModel.DEBUG_LOADERS) {
2148 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2149 }
2150 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 }
2152 }
2153 }
2154
2155 /**
2156 * Receives notifications whenever the user favorites have changed.
2157 */
2158 private class FavoritesChangeObserver extends ContentObserver {
2159 public FavoritesChangeObserver() {
2160 super(new Handler());
2161 }
2162
2163 @Override
2164 public void onChange(boolean selfChange) {
2165 onFavoritesChanged();
2166 }
2167 }
2168
2169 /**
2170 * Receives intents from other applications to change the wallpaper.
2171 */
2172 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2173 private final Application mApplication;
2174 private WeakReference<Launcher> mLauncher;
2175
2176 WallpaperIntentReceiver(Application application, Launcher launcher) {
2177 mApplication = application;
2178 setLauncher(launcher);
2179 }
2180
2181 void setLauncher(Launcher launcher) {
2182 mLauncher = new WeakReference<Launcher>(launcher);
2183 }
2184
2185 @Override
2186 public void onReceive(Context context, Intent intent) {
2187 // Load the wallpaper from the ApplicationContext and store it locally
2188 // until the Launcher Activity is ready to use it
2189 final Drawable drawable = mApplication.getWallpaper();
2190 if (drawable instanceof BitmapDrawable) {
2191 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2192 } else {
2193 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2194 }
2195
2196 // If Launcher is alive, notify we have a new wallpaper
2197 if (mLauncher != null) {
2198 final Launcher launcher = mLauncher.get();
2199 if (launcher != null) {
2200 launcher.loadWallpaper();
2201 }
2202 }
2203 }
2204 }
2205
2206 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2207 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2208 private boolean mOpen;
2209
2210 public void onDrawerOpened() {
2211 if (!mOpen) {
2212 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002215 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216
2217 mOpen = true;
2218 }
2219 }
2220
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002221 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2222 view.getDrawingRect(bounds);
2223
2224 while (view != mDragLayer) {
2225 bounds.offset(view.getLeft(), view.getTop());
2226 view = (View) view.getParent();
2227 }
2228 }
2229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 public void onDrawerClosed() {
2231 if (mOpen) {
2232 mHandleIcon.reverseTransition(150);
2233 mWorkspace.mDrawerBounds.setEmpty();
2234 mOpen = false;
2235 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 mAllAppsGrid.setSelection(0);
2238 mAllAppsGrid.clearTextFilter();
2239 }
2240
2241 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002242 if (PROFILE_DRAWER) {
2243 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2244 }
2245
2246 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2247 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 }
2249
2250 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002251 if (PROFILE_DRAWER) {
2252 android.os.Debug.stopMethodTracing();
2253 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002254 }
2255 }
2256
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002257 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002259 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002260 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 // Number of items to bind in every pass
2263 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264
2265 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002266 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002267 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002269
Karl Rosaen138a0412009-04-23 19:00:21 -07002270 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271
2272 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002273 ArrayList<LauncherAppWidgetInfo> appWidgets,
2274 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275
2276 mLauncher = new WeakReference<Launcher>(launcher);
2277 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002278 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002279
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002280 // Sort widgets so active workspace is bound first
2281 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2282 final int size = appWidgets.size();
2283 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002284
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002285 for (int i = 0; i < size; i++) {
2286 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2287 if (appWidgetInfo.screen == currentScreen) {
2288 mAppWidgets.addFirst(appWidgetInfo);
2289 } else {
2290 mAppWidgets.addLast(appWidgetInfo);
2291 }
2292 }
2293 }
Romain Guycbb89e42009-06-08 15:52:54 -07002294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 public void startBindingItems() {
2296 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2297 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002298
2299 public void startBindingDrawer() {
2300 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2301 }
Romain Guycbb89e42009-06-08 15:52:54 -07002302
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002303 public void startBindingAppWidgetsWhenIdle() {
2304 // Ask for notification when message queue becomes idle
2305 final MessageQueue messageQueue = Looper.myQueue();
2306 messageQueue.addIdleHandler(this);
2307 }
Romain Guycbb89e42009-06-08 15:52:54 -07002308
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002309 public boolean queueIdle() {
2310 // Queue is idle, so start binding items
2311 startBindingAppWidgets();
2312 return false;
2313 }
2314
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002315 public void startBindingAppWidgets() {
2316 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2317 }
2318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 @Override
2320 public void handleMessage(Message msg) {
2321 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002322 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 return;
2324 }
Romain Guycbb89e42009-06-08 15:52:54 -07002325
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 switch (msg.what) {
2327 case MESSAGE_BIND_ITEMS: {
2328 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2329 break;
2330 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002331 case MESSAGE_BIND_DRAWER: {
2332 launcher.bindDrawer(this, mDrawerAdapter);
2333 break;
2334 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002335 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002336 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337 break;
2338 }
2339 }
2340 }
2341 }
Romain Guy73b979d2009-06-09 12:57:21 -07002342
2343 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2344 GestureOverlayView.OnGesturePerformedListener {
2345
2346 private final GestureMatcher mMatcher = new GestureMatcher();
2347
2348 GesturesProcessor() {
2349 // TODO: Maybe the load should happen on a background thread?
2350 sLibrary.load();
2351 }
2352
2353 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002354 //noinspection PointlessBooleanExpression,ConstantConditions
2355 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2356 overlay.removeCallbacks(mMatcher);
2357 resetGesturesNextPrompt();
2358 }
Romain Guy73b979d2009-06-09 12:57:21 -07002359
2360 mGesturesAdd.setAlpha(128);
2361 mGesturesAdd.setEnabled(false);
2362 }
2363
2364 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2365 }
2366
2367 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2368 }
2369
2370 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002371 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2372 mMatcher.gesture = overlay.getGesture();
2373 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2374 overlay.clear(false);
2375 } else {
2376 mMatcher.run();
2377 }
Romain Guy73b979d2009-06-09 12:57:21 -07002378 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002379 overlay.removeCallbacks(mMatcher);
2380
2381 mMatcher.gesture = overlay.getGesture();
2382 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2383 overlay.clear(false);
2384 } else {
2385 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2386 }
Romain Guy73b979d2009-06-09 12:57:21 -07002387 }
2388 }
2389
2390 private void matchGesture(Gesture gesture) {
2391 mGesturesAdd.setAlpha(255);
2392 mGesturesAdd.setEnabled(true);
2393
2394 if (gesture != null) {
2395 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2396
2397 if (DEBUG_GESTURES) {
2398 for (Prediction p : predictions) {
2399 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2400 }
2401 }
2402
2403 boolean match = false;
2404 if (predictions.size() > 0) {
2405 final Prediction prediction = predictions.get(0);
2406 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2407 match = true;
2408
2409 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2410 if (info != null) {
2411 updatePrompt(info);
2412 }
2413 }
2414 }
2415
2416 if (!match){
2417 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2418 }
2419 }
2420 }
2421
2422 private void updatePrompt(ApplicationInfo info) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002423 if (mGesturesAction.intent != null &&
2424 info.intent.toURI().equals(mGesturesAction.intent.toURI()) &&
2425 info.title.equals(((TextView) mGesturesPrompt.getCurrentView()).getText())) {
2426 return;
2427 }
Romain Guy73b979d2009-06-09 12:57:21 -07002428 setGesturesNextPrompt(info.icon, info.title);
2429 mGesturesAction.intent = info.intent;
2430 }
2431
2432 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002433 //noinspection PointlessBooleanExpression,ConstantConditions
2434 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2435 overlay.removeCallbacks(mMatcher);
2436 }
Romain Guy73b979d2009-06-09 12:57:21 -07002437 }
2438
2439 void addGesture(String name, Gesture gesture) {
2440 sLibrary.addGesture(name, gesture);
2441 // TODO: On a background thread?
2442 sLibrary.save();
2443 }
2444
2445 void update(ApplicationInfo info, Gesture gesture) {
2446 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002447 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002448 }
2449
2450 class GestureMatcher implements Runnable {
2451 Gesture gesture;
2452
2453 public void run() {
2454 if (gesture != null) {
2455 matchGesture(gesture);
2456 }
2457 }
2458 }
2459 }
2460
2461 private class GesturesAction implements View.OnClickListener {
2462 Intent intent;
2463
2464 public void onClick(View v) {
2465 if (intent != null) {
2466 startActivitySafely(intent);
2467 }
2468 }
2469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470}
Karl Rosaen138a0412009-04-23 19:00:21 -07002471