blob: fb454c06137ef29450858bb8d2b422359b46b59f [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
Romain Guye3895ae2009-06-16 13:25:29 -0700872 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE &&
873 mDragLayer.hasWindowFocus()) {
Romain Guyb8734242009-06-10 11:53:57 -0700874 SearchManager searchManager =
875 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
876 if (!searchManager.isVisible()) {
877 onHomeKeyPressed();
878 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 }
880 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700881
882 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 if (v != null && v.getWindowToken() != null) {
884 InputMethodManager imm = (InputMethodManager)getSystemService(
885 INPUT_METHOD_SERVICE);
886 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
887 }
888 } else {
889 closeDrawer(false);
890 }
891 }
892 }
893
Romain Guy73b979d2009-06-09 12:57:21 -0700894 private void onHomeKeyPressed() {
895 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
896 showGesturesPanel();
897 } else {
898 hideGesturesPanel();
899 }
900 }
901
902 private void showGesturesPanel() {
903 resetGesturesPrompt();
904
905 mGesturesAdd.setEnabled(false);
906 mGesturesAdd.setAlpha(128);
907
908 mGesturesOverlay.clear(false);
909
910 PopupWindow window;
911 if (mGesturesWindow == null) {
912 mGesturesWindow = new PopupWindow(this);
913 window = mGesturesWindow;
914 window.setFocusable(true);
915 window.setTouchable(true);
916 window.setBackgroundDrawable(null);
917 window.setContentView(mGesturesPanel);
918 } else {
919 window = mGesturesWindow;
920 }
921 window.setAnimationStyle(com.android.internal.R.style.Animation_SlidingCard);
922
923 final int[] xy = new int[2];
924 final DragLayer dragLayer = mDragLayer;
925 dragLayer.getLocationOnScreen(xy);
926
927 window.setWidth(dragLayer.getWidth());
928 window.setHeight(dragLayer.getHeight() - 1);
929 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
930 }
931
932 private void resetGesturesPrompt() {
933 mGesturesAction.intent = null;
934 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
935 prompt.setText(R.string.gestures_instructions);
936 prompt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
937 prompt.setClickable(false);
938 }
939
940 private void resetGesturesNextPrompt() {
941 mGesturesAction.intent = null;
942 setGesturesNextPrompt(null, getString(R.string.gestures_instructions));
943 mGesturesPrompt.getNextView().setClickable(false);
944 }
945
946 private void setGesturesNextPrompt(Drawable icon, CharSequence title) {
947 final TextView prompt = (TextView) mGesturesPrompt.getNextView();
948 prompt.setText(title);
949 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
950 prompt.setClickable(true);
951 mGesturesPrompt.showNext();
952 }
953
954 void hideGesturesPanel() {
955 if (mGesturesWindow != null) {
956 mGesturesWindow.setAnimationStyle(com.android.internal.R.style.Animation_SlidingCard);
957 mGesturesWindow.update();
958 mGesturesWindow.dismiss();
959 }
960 }
961
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 @Override
963 protected void onRestoreInstanceState(Bundle savedInstanceState) {
964 // Do not call super here
965 mSavedInstanceState = savedInstanceState;
966 }
967
968 @Override
969 protected void onSaveInstanceState(Bundle outState) {
970 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
971
972 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
973 if (folders.size() > 0) {
974 final int count = folders.size();
975 long[] ids = new long[count];
976 for (int i = 0; i < count; i++) {
977 final FolderInfo info = folders.get(i).getInfo();
978 ids[i] = info.id;
979 }
980 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
981 } else {
982 super.onSaveInstanceState(outState);
983 }
984
Romain Guy5a941392009-04-28 15:18:25 -0700985 // When the drawer is opened and we are saving the state because of a
986 // configuration change
987 if (mDrawer.isOpened() && getChangingConfigurations() != 0) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990
991 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
992 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
993 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
994
995 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
996 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
997 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
998 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
999 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1000 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1001 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1002 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1003 layout.getOccupiedCells());
1004 }
1005
1006 if (mFolderInfo != null && mWaitingForResult) {
1007 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1008 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1009 }
Romain Guy73b979d2009-06-09 12:57:21 -07001010
1011 if (mCurrentGesture != null && mWaitingForResult) {
1012 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1013 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 }
1015
1016 @Override
1017 public void onDestroy() {
1018 mDestroyed = true;
1019
1020 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001021
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001023 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001025 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
1027
1028 TextKeyListener.getInstance().release();
1029
1030 mAllAppsGrid.clearTextFilter();
1031 mAllAppsGrid.setAdapter(null);
1032 sModel.unbind();
1033 sModel.abortLoaders();
1034
1035 getContentResolver().unregisterContentObserver(mObserver);
1036 unregisterReceiver(mApplicationsReceiver);
1037 }
1038
1039 @Override
1040 public void startActivityForResult(Intent intent, int requestCode) {
1041 mWaitingForResult = true;
1042 super.startActivityForResult(intent, requestCode);
1043 }
1044
1045 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001046 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001048
1049 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001050
Karl Rosaen138a0412009-04-23 19:00:21 -07001051 // Slide the search widget to the top, if it's on the current screen,
1052 // otherwise show the search dialog immediately.
1053 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1054 if (searchWidget == null) {
1055 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1056 } else {
1057 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1058 // show the currently typed text in the search widget while sliding
1059 searchWidget.setQuery(getTypedText());
1060 }
1061 }
Romain Guycbb89e42009-06-08 15:52:54 -07001062
Karl Rosaen138a0412009-04-23 19:00:21 -07001063 /**
1064 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001065 *
1066 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001067 */
Romain Guycbb89e42009-06-08 15:52:54 -07001068 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001069 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001070
Karl Rosaen138a0412009-04-23 19:00:21 -07001071 if (initialQuery == null) {
1072 // Use any text typed in the launcher as the initial query
1073 initialQuery = getTypedText();
1074 clearTypedText();
1075 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 if (appSearchData == null) {
1077 appSearchData = new Bundle();
1078 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1079 }
Romain Guycbb89e42009-06-08 15:52:54 -07001080
Karl Rosaen138a0412009-04-23 19:00:21 -07001081 final SearchManager searchManager =
1082 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1083
1084 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1085 if (searchWidget != null) {
1086 // This gets called when the user leaves the search dialog to go back to
1087 // the Launcher.
1088 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1089 public void onCancel() {
1090 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001091 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001092 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001093 });
1094 }
Romain Guycbb89e42009-06-08 15:52:54 -07001095
Karl Rosaen138a0412009-04-23 19:00:21 -07001096 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001097 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 }
1099
Karl Rosaen138a0412009-04-23 19:00:21 -07001100 /**
1101 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001102 */
Romain Guy5a941392009-04-28 15:18:25 -07001103 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001104 // Close search dialog
1105 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1106 if (searchManager.isVisible()) {
1107 searchManager.stopSearch();
1108 }
1109 // Restore search widget to its normal position
1110 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1111 if (searchWidget != null) {
1112 searchWidget.stopSearch(false);
1113 }
1114 }
Romain Guycbb89e42009-06-08 15:52:54 -07001115
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 @Override
1117 public boolean onCreateOptionsMenu(Menu menu) {
1118 if (mDesktopLocked) return false;
1119
1120 super.onCreateOptionsMenu(menu);
1121 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1122 .setIcon(android.R.drawable.ic_menu_add)
1123 .setAlphabeticShortcut('A');
1124 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1125 .setIcon(android.R.drawable.ic_menu_gallery)
1126 .setAlphabeticShortcut('W');
1127 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1128 .setIcon(android.R.drawable.ic_search_category_default)
1129 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1130 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1131 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1132 .setAlphabeticShortcut('N');
1133
Romain Guy73b979d2009-06-09 12:57:21 -07001134 final Intent gestures = new Intent(this, GesturesActivity.class);
1135 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1136 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1137 .setIntent(gestures);
1138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001140 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1141 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142
1143 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1144 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1145 .setIntent(settings);
1146
1147 return true;
1148 }
1149
1150 @Override
1151 public boolean onPrepareOptionsMenu(Menu menu) {
1152 super.onPrepareOptionsMenu(menu);
1153
1154 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1155 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1156
1157 return true;
1158 }
1159
1160 @Override
1161 public boolean onOptionsItemSelected(MenuItem item) {
1162 switch (item.getItemId()) {
1163 case MENU_ADD:
1164 addItems();
1165 return true;
1166 case MENU_WALLPAPER_SETTINGS:
1167 startWallpaper();
1168 return true;
1169 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001170 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 return true;
1172 case MENU_NOTIFICATIONS:
1173 showNotifications();
1174 return true;
1175 }
1176
1177 return super.onOptionsItemSelected(item);
1178 }
Romain Guycbb89e42009-06-08 15:52:54 -07001179
Karl Rosaen138a0412009-04-23 19:00:21 -07001180 /**
1181 * Indicates that we want global search for this activity by setting the globalSearch
1182 * argument for {@link #startSearch} to true.
1183 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001185 @Override
1186 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001187 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001188 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001189 }
1190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 private void addItems() {
1192 showAddDialog(mMenuAddInfo);
1193 }
1194
1195 private void removeShortcutsForPackage(String packageName) {
1196 if (packageName != null && packageName.length() > 0) {
1197 mWorkspace.removeShortcutsForPackage(packageName);
1198 }
1199 }
Romain Guycbb89e42009-06-08 15:52:54 -07001200
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001201 private void updateShortcutsForPackage(String packageName) {
1202 if (packageName != null && packageName.length() > 0) {
1203 mWorkspace.updateShortcutsForPackage(packageName);
1204 }
1205 }
1206
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001207 void addAppWidget(Intent data) {
1208 // TODO: catch bad widget exception when sent
1209 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001211 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1212 if (SEARCH_WIDGET.equals(customWidget)) {
1213 // We don't need this any more, since this isn't a real app widget.
1214 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1215 // add the search widget
1216 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001218 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1219
1220 if (appWidget.configure != null) {
1221 // Launch over to configure widget, if needed
1222 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1223 intent.setComponent(appWidget.configure);
1224 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1225
1226 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1227 } else {
1228 // Otherwise just add it
1229 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1230 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 }
1232 }
Romain Guycbb89e42009-06-08 15:52:54 -07001233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 void addSearch() {
1235 final Widget info = Widget.makeSearch();
1236 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 final int[] xy = mCellCoordinates;
1239 final int spanX = info.spanX;
1240 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 sModel.addDesktopItem(info);
1245 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1246 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 final View view = mInflater.inflate(info.layoutResource, null);
1249 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001250 Search search = (Search) view.findViewById(R.id.widget_search);
1251 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1254 }
1255
Romain Guy73b979d2009-06-09 12:57:21 -07001256 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001257 // Handle case where user selected "Applications"
1258 String applicationName = getResources().getString(R.string.group_applications);
1259 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001260
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001261 if (applicationName != null && applicationName.equals(shortcutName)) {
1262 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1263 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001264
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001265 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1266 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001267 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001268 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001269 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
1272
1273 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001274 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001275 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001276 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001277
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001278 if (folderName != null && folderName.equals(shortcutName)) {
1279 addFolder(!mDesktopLocked);
1280 } else {
1281 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1282 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284
1285 void addFolder(boolean insertAtFirst) {
1286 UserFolderInfo folderInfo = new UserFolderInfo();
1287 folderInfo.title = getText(R.string.folder_name);
1288
1289 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1290 cellInfo.screen = mWorkspace.getCurrentScreen();
1291 if (!findSingleSlot(cellInfo)) return;
1292
1293 // Update the model
1294 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1295 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1296 sModel.addDesktopItem(folderInfo);
1297 sModel.addFolder(folderInfo);
1298
1299 // Create the view
1300 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1301 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1302 mWorkspace.addInCurrentScreen(newFolder,
1303 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1304 }
Romain Guycbb89e42009-06-08 15:52:54 -07001305
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1307 boolean insertAtFirst) {
1308 cellInfo.screen = mWorkspace.getCurrentScreen();
1309 if (!findSingleSlot(cellInfo)) return;
1310
1311 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1312
1313 if (!mRestoring) {
1314 sModel.addDesktopItem(info);
1315
1316 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1317 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1318 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1319 } else if (sModel.isDesktopLoaded()) {
1320 sModel.addDesktopItem(info);
1321 }
1322 }
1323
1324 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1325 CellLayout.CellInfo cellInfo, boolean notify) {
1326
1327 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1328 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1329
1330 Drawable icon = null;
1331 boolean filtered = false;
1332 Intent.ShortcutIconResource iconResource = null;
1333
1334 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1335 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1336 try {
1337 iconResource = (Intent.ShortcutIconResource) extra;
1338 final PackageManager packageManager = context.getPackageManager();
1339 Resources resources = packageManager.getResourcesForApplication(
1340 iconResource.packageName);
1341 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1342 icon = resources.getDrawable(id);
1343 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001344 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346 }
1347
1348 if (icon == null) {
1349 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1350 }
1351
1352 final LiveFolderInfo info = new LiveFolderInfo();
1353 info.icon = icon;
1354 info.filtered = filtered;
1355 info.title = name;
1356 info.iconResource = iconResource;
1357 info.uri = data.getData();
1358 info.baseIntent = baseIntent;
1359 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1360 LiveFolders.DISPLAY_MODE_GRID);
1361
1362 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1363 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1364 sModel.addFolder(info);
1365
1366 return info;
1367 }
1368
1369 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1370 final int[] xy = new int[2];
1371 if (findSlot(cellInfo, xy, 1, 1)) {
1372 cellInfo.cellX = xy[0];
1373 cellInfo.cellY = xy[1];
1374 return true;
1375 }
1376 return false;
1377 }
1378
1379 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1380 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1381 boolean[] occupied = mSavedState != null ?
1382 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1383 cellInfo = mWorkspace.findAllVacantCells(occupied);
1384 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1385 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1386 return false;
1387 }
1388 }
1389 return true;
1390 }
1391
1392 private void showNotifications() {
1393 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1394 if (statusBar != null) {
1395 statusBar.expand();
1396 }
1397 }
1398
1399 private void startWallpaper() {
1400 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1401 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1402 }
1403
1404 /**
1405 * Registers various intent receivers. The current implementation registers
1406 * only a wallpaper intent receiver to let other applications change the
1407 * wallpaper.
1408 */
1409 private void registerIntentReceivers() {
1410 if (sWallpaperReceiver == null) {
1411 final Application application = getApplication();
1412
1413 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1414
1415 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1416 application.registerReceiver(sWallpaperReceiver, filter);
1417 } else {
1418 sWallpaperReceiver.setLauncher(this);
1419 }
1420
1421 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1422 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1423 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1424 filter.addDataScheme("package");
1425 registerReceiver(mApplicationsReceiver, filter);
1426 }
1427
1428 /**
1429 * Registers various content observers. The current implementation registers
1430 * only a favorites observer to keep track of the favorites applications.
1431 */
1432 private void registerContentObservers() {
1433 ContentResolver resolver = getContentResolver();
1434 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1435 }
1436
1437 @Override
1438 public boolean dispatchKeyEvent(KeyEvent event) {
1439 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1440 switch (event.getKeyCode()) {
1441 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001442 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001443 if (mDrawer.isOpened()) {
1444 closeDrawer();
1445 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001446 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001447 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 return true;
1449 case KeyEvent.KEYCODE_HOME:
1450 return true;
1451 }
1452 }
1453
1454 return super.dispatchKeyEvent(event);
1455 }
1456
1457 private void closeDrawer() {
1458 closeDrawer(true);
1459 }
1460
1461 private void closeDrawer(boolean animated) {
1462 if (mDrawer.isOpened()) {
1463 if (animated) {
1464 mDrawer.animateClose();
1465 } else {
1466 mDrawer.close();
1467 }
1468 if (mDrawer.hasFocus()) {
1469 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1470 }
1471 }
1472 }
1473
1474 private void closeFolder() {
1475 Folder folder = mWorkspace.getOpenFolder();
1476 if (folder != null) {
1477 closeFolder(folder);
1478 }
1479 }
1480
1481 void closeFolder(Folder folder) {
1482 folder.getInfo().opened = false;
1483 ViewGroup parent = (ViewGroup) folder.getParent();
1484 if (parent != null) {
1485 parent.removeView(folder);
1486 }
1487 folder.onClose();
1488 }
1489
1490 /**
1491 * When the notification that favorites have changed is received, requests
1492 * a favorites list refresh.
1493 */
1494 private void onFavoritesChanged() {
1495 mDesktopLocked = true;
1496 mDrawer.lock();
1497 sModel.loadUserItems(false, this, false, false);
1498 }
1499
1500 void onDesktopItemsLoaded() {
1501 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 bindDesktopItems();
1503 }
Romain Guycbb89e42009-06-08 15:52:54 -07001504
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 /**
1506 * Refreshes the shortcuts shown on the workspace.
1507 */
1508 private void bindDesktopItems() {
1509 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001510 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001511 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1512 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 return;
1514 }
1515
1516 final Workspace workspace = mWorkspace;
1517 int count = workspace.getChildCount();
1518 for (int i = 0; i < count; i++) {
1519 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1520 }
Romain Guycbb89e42009-06-08 15:52:54 -07001521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 if (DEBUG_USER_INTERFACE) {
1523 android.widget.Button finishButton = new android.widget.Button(this);
1524 finishButton.setText("Finish");
1525 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1526
1527 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1528 public void onClick(View v) {
1529 finish();
1530 }
1531 });
1532 }
Romain Guycbb89e42009-06-08 15:52:54 -07001533
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001534 // Flag any old binder to terminate early
1535 if (mBinder != null) {
1536 mBinder.mTerminate = true;
1537 }
Romain Guycbb89e42009-06-08 15:52:54 -07001538
Karl Rosaen138a0412009-04-23 19:00:21 -07001539 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001540 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
1542
1543 private void bindItems(Launcher.DesktopBinder binder,
1544 ArrayList<ItemInfo> shortcuts, int start, int count) {
1545
1546 final Workspace workspace = mWorkspace;
1547 final boolean desktopLocked = mDesktopLocked;
1548
1549 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1550 int i = start;
1551
1552 for ( ; i < end; i++) {
1553 final ItemInfo item = shortcuts.get(i);
1554 switch (item.itemType) {
1555 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1556 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1557 final View shortcut = createShortcut((ApplicationInfo) item);
1558 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1559 !desktopLocked);
1560 break;
1561 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1562 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1563 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1564 (UserFolderInfo) item);
1565 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1566 !desktopLocked);
1567 break;
1568 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1569 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1570 R.layout.live_folder_icon, this,
1571 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1572 (LiveFolderInfo) item);
1573 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1574 !desktopLocked);
1575 break;
1576 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1577 final int screen = workspace.getCurrentScreen();
1578 final View view = mInflater.inflate(R.layout.widget_search,
1579 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001580
Karl Rosaen138a0412009-04-23 19:00:21 -07001581 Search search = (Search) view.findViewById(R.id.widget_search);
1582 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001583
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 final Widget widget = (Widget) item;
1585 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 workspace.addWidget(view, widget, !desktopLocked);
1588 break;
1589 }
1590 }
1591
1592 workspace.requestLayout();
1593
1594 if (end >= count) {
1595 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001596 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 } else {
1598 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1599 }
1600 }
1601
1602 private void finishBindDesktopItems() {
1603 if (mSavedState != null) {
1604 if (!mWorkspace.hasFocus()) {
1605 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1606 }
1607
1608 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1609 if (userFolders != null) {
1610 for (long folderId : userFolders) {
1611 final FolderInfo info = sModel.findFolderById(folderId);
1612 if (info != null) {
1613 openFolder(info);
1614 }
1615 }
1616 final Folder openFolder = mWorkspace.getOpenFolder();
1617 if (openFolder != null) {
1618 openFolder.requestFocus();
1619 }
1620 }
1621
1622 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1623 if (allApps) {
1624 mDrawer.open();
1625 }
1626
1627 mSavedState = null;
1628 }
1629
1630 if (mSavedInstanceState != null) {
1631 super.onRestoreInstanceState(mSavedInstanceState);
1632 mSavedInstanceState = null;
1633 }
1634
1635 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1636 mDrawer.requestFocus();
1637 }
1638
1639 mDesktopLocked = false;
1640 mDrawer.unlock();
1641 }
Romain Guycbb89e42009-06-08 15:52:54 -07001642
Karl Rosaen138a0412009-04-23 19:00:21 -07001643 private void bindDrawer(Launcher.DesktopBinder binder,
1644 ApplicationsAdapter drawerAdapter) {
1645 mAllAppsGrid.setAdapter(drawerAdapter);
1646 binder.startBindingAppWidgetsWhenIdle();
1647 }
Romain Guycbb89e42009-06-08 15:52:54 -07001648
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001649 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001650 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 final Workspace workspace = mWorkspace;
1653 final boolean desktopLocked = mDesktopLocked;
1654
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001655 if (!appWidgets.isEmpty()) {
1656 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001657
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001658 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001659 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001660 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001661
Karl Rosaen138a0412009-04-23 19:00:21 -07001662 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001663
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001664 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1665 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001666
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001667 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1668 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001669
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001670 workspace.requestLayout();
1671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001673 if (appWidgets.isEmpty()) {
1674 if (PROFILE_ROTATE) {
1675 android.os.Debug.stopMethodTracing();
1676 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001678 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
1680 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 DragController getDragController() {
1683 return mDragLayer;
1684 }
1685
1686 /**
1687 * Launches the intent referred by the clicked shortcut.
1688 *
1689 * @param v The view representing the clicked shortcut.
1690 */
1691 public void onClick(View v) {
1692 Object tag = v.getTag();
1693 if (tag instanceof ApplicationInfo) {
1694 // Open shortcut
1695 final Intent intent = ((ApplicationInfo) tag).intent;
1696 startActivitySafely(intent);
1697 } else if (tag instanceof FolderInfo) {
1698 handleFolderClick((FolderInfo) tag);
1699 }
1700 }
1701
1702 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001703 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1705 try {
1706 startActivity(intent);
1707 } catch (ActivityNotFoundException e) {
1708 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1709 } catch (SecurityException e) {
1710 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001711 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1713 "or use the exported attribute for this activity.", e);
1714 }
1715 }
1716
1717 private void handleFolderClick(FolderInfo folderInfo) {
1718 if (!folderInfo.opened) {
1719 // Close any open folder
1720 closeFolder();
1721 // Open the requested folder
1722 openFolder(folderInfo);
1723 } else {
1724 // Find the open folder...
1725 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1726 int folderScreen;
1727 if (openFolder != null) {
1728 folderScreen = mWorkspace.getScreenForView(openFolder);
1729 // .. and close it
1730 closeFolder(openFolder);
1731 if (folderScreen != mWorkspace.getCurrentScreen()) {
1732 // Close any folder open on the current screen
1733 closeFolder();
1734 // Pull the folder onto this screen
1735 openFolder(folderInfo);
1736 }
1737 }
1738 }
1739 }
1740
1741 private void loadWallpaper() {
1742 // The first time the application is started, we load the wallpaper from
1743 // the ApplicationContext
1744 if (sWallpaper == null) {
1745 final Drawable drawable = getWallpaper();
1746 if (drawable instanceof BitmapDrawable) {
1747 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1748 } else {
1749 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1750 }
1751 }
1752 mWorkspace.loadWallpaper(sWallpaper);
1753 }
1754
1755 /**
1756 * Opens the user fodler described by the specified tag. The opening of the folder
1757 * is animated relative to the specified View. If the View is null, no animation
1758 * is played.
1759 *
1760 * @param folderInfo The FolderInfo describing the folder to open.
1761 */
1762 private void openFolder(FolderInfo folderInfo) {
1763 Folder openFolder;
1764
1765 if (folderInfo instanceof UserFolderInfo) {
1766 openFolder = UserFolder.fromXml(this);
1767 } else if (folderInfo instanceof LiveFolderInfo) {
1768 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1769 } else {
1770 return;
1771 }
1772
1773 openFolder.setDragger(mDragLayer);
1774 openFolder.setLauncher(this);
1775
1776 openFolder.bind(folderInfo);
1777 folderInfo.opened = true;
1778
1779 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1780 openFolder.onOpen();
1781 }
1782
1783 /**
1784 * Returns true if the workspace is being loaded. When the workspace is loading,
1785 * no user interaction should be allowed to avoid any conflict.
1786 *
1787 * @return True if the workspace is locked, false otherwise.
1788 */
1789 boolean isWorkspaceLocked() {
1790 return mDesktopLocked;
1791 }
1792
1793 public boolean onLongClick(View v) {
1794 if (mDesktopLocked) {
1795 return false;
1796 }
1797
1798 if (!(v instanceof CellLayout)) {
1799 v = (View) v.getParent();
1800 }
1801
1802 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1803
1804 // This happens when long clicking an item with the dpad/trackball
1805 if (cellInfo == null) {
1806 return true;
1807 }
1808
1809 if (mWorkspace.allowLongPress()) {
1810 if (cellInfo.cell == null) {
1811 if (cellInfo.valid) {
1812 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001813 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 showAddDialog(cellInfo);
1815 }
1816 } else {
1817 if (!(cellInfo.cell instanceof Folder)) {
1818 // User long pressed on an item
1819 mWorkspace.startDrag(cellInfo);
1820 }
1821 }
1822 }
1823 return true;
1824 }
1825
1826 static LauncherModel getModel() {
1827 return sModel;
1828 }
1829
Romain Guy73b979d2009-06-09 12:57:21 -07001830 static GestureLibrary getGestureLibrary() {
1831 return sLibrary;
1832 }
1833
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 void closeAllApplications() {
1835 mDrawer.close();
1836 }
1837
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001838 View getDrawerHandle() {
1839 return mHandleView;
1840 }
1841
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 boolean isDrawerDown() {
1843 return !mDrawer.isMoving() && !mDrawer.isOpened();
1844 }
1845
1846 boolean isDrawerUp() {
1847 return mDrawer.isOpened() && !mDrawer.isMoving();
1848 }
1849
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001850 boolean isDrawerMoving() {
1851 return mDrawer.isMoving();
1852 }
1853
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 Workspace getWorkspace() {
1855 return mWorkspace;
1856 }
1857
1858 GridView getApplicationsGrid() {
1859 return mAllAppsGrid;
1860 }
1861
1862 @Override
1863 protected Dialog onCreateDialog(int id) {
1864 switch (id) {
1865 case DIALOG_CREATE_SHORTCUT:
1866 return new CreateShortcut().createDialog();
1867 case DIALOG_RENAME_FOLDER:
1868 return new RenameFolder().createDialog();
1869 }
1870
1871 return super.onCreateDialog(id);
1872 }
1873
1874 @Override
1875 protected void onPrepareDialog(int id, Dialog dialog) {
1876 switch (id) {
1877 case DIALOG_CREATE_SHORTCUT:
1878 mWorkspace.lock();
1879 break;
1880 case DIALOG_RENAME_FOLDER:
1881 mWorkspace.lock();
1882 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1883 final CharSequence text = mFolderInfo.title;
1884 input.setText(text);
Romain Guycbb89e42009-06-08 15:52:54 -07001885 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 break;
1887 }
1888 }
1889
1890 void showRenameDialog(FolderInfo info) {
1891 mFolderInfo = info;
1892 mWaitingForResult = true;
1893 showDialog(DIALOG_RENAME_FOLDER);
1894 }
1895
1896 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1897 mAddItemCellInfo = cellInfo;
1898 mWaitingForResult = true;
1899 showDialog(DIALOG_CREATE_SHORTCUT);
1900 }
1901
Romain Guy73b979d2009-06-09 12:57:21 -07001902 private void pickShortcut(int requestCode, int title) {
1903 Bundle bundle = new Bundle();
1904
1905 ArrayList<String> shortcutNames = new ArrayList<String>();
1906 shortcutNames.add(getString(R.string.group_applications));
1907 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1908
1909 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1910 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1911 R.drawable.ic_launcher_application));
1912 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1913
1914 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1915 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1916 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1917 pickIntent.putExtras(bundle);
1918
1919 startActivityForResult(pickIntent, requestCode);
1920 }
1921
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 private class RenameFolder {
1923 private EditText mInput;
1924
1925 Dialog createDialog() {
1926 mWaitingForResult = true;
1927 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1928 mInput = (EditText) layout.findViewById(R.id.folder_name);
1929
1930 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1931 builder.setIcon(0);
1932 builder.setTitle(getString(R.string.rename_folder_title));
1933 builder.setCancelable(true);
1934 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1935 public void onCancel(DialogInterface dialog) {
1936 cleanup();
1937 }
1938 });
1939 builder.setNegativeButton(getString(R.string.cancel_action),
1940 new Dialog.OnClickListener() {
1941 public void onClick(DialogInterface dialog, int which) {
1942 cleanup();
1943 }
1944 }
1945 );
1946 builder.setPositiveButton(getString(R.string.rename_action),
1947 new Dialog.OnClickListener() {
1948 public void onClick(DialogInterface dialog, int which) {
1949 changeFolderName();
1950 }
1951 }
1952 );
1953 builder.setView(layout);
1954 return builder.create();
1955 }
1956
1957 private void changeFolderName() {
1958 final String name = mInput.getText().toString();
1959 if (!TextUtils.isEmpty(name)) {
1960 // Make sure we have the right folder info
1961 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1962 mFolderInfo.title = name;
1963 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1964
1965 if (mDesktopLocked) {
1966 mDrawer.lock();
1967 sModel.loadUserItems(false, Launcher.this, false, false);
1968 } else {
1969 final FolderIcon folderIcon = (FolderIcon)
1970 mWorkspace.getViewForTag(mFolderInfo);
1971 if (folderIcon != null) {
1972 folderIcon.setText(name);
1973 getWorkspace().requestLayout();
1974 } else {
1975 mDesktopLocked = true;
1976 mDrawer.lock();
1977 sModel.loadUserItems(false, Launcher.this, false, false);
1978 }
1979 }
1980 }
1981 cleanup();
1982 }
1983
1984 private void cleanup() {
1985 mWorkspace.unlock();
1986 dismissDialog(DIALOG_RENAME_FOLDER);
1987 mWaitingForResult = false;
1988 mFolderInfo = null;
1989 }
1990 }
1991
1992 /**
1993 * Displays the shortcut creation dialog and launches, if necessary, the
1994 * appropriate activity.
1995 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001996 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07001997 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 Dialog createDialog() {
2001 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2006 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002007 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 builder.setInverseBackgroundForced(true);
2010
2011 AlertDialog dialog = builder.create();
2012 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002013 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 return dialog;
2016 }
2017
2018 public void onCancel(DialogInterface dialog) {
2019 mWaitingForResult = false;
2020 cleanup();
2021 }
2022
Romain Guycbb89e42009-06-08 15:52:54 -07002023 public void onDismiss(DialogInterface dialog) {
2024 mWorkspace.unlock();
2025 }
2026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 private void cleanup() {
2028 mWorkspace.unlock();
2029 dismissDialog(DIALOG_CREATE_SHORTCUT);
2030 }
2031
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002032 /**
2033 * Handle the action clicked in the "Add to home" dialog.
2034 */
2035 public void onClick(DialogInterface dialog, int which) {
2036 Resources res = getResources();
2037 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002038
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002039 switch (which) {
2040 case AddAdapter.ITEM_SHORTCUT: {
2041 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002042 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002043 break;
2044 }
Romain Guycbb89e42009-06-08 15:52:54 -07002045
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002046 case AddAdapter.ITEM_APPWIDGET: {
2047 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002048
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002049 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2050 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2051 // add the search widget
2052 ArrayList<AppWidgetProviderInfo> customInfo =
2053 new ArrayList<AppWidgetProviderInfo>();
2054 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2055 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2056 info.label = getString(R.string.group_search);
2057 info.icon = R.drawable.ic_search_widget;
2058 customInfo.add(info);
2059 pickIntent.putParcelableArrayListExtra(
2060 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2061 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2062 Bundle b = new Bundle();
2063 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2064 customExtras.add(b);
2065 pickIntent.putParcelableArrayListExtra(
2066 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2067 // start the pick activity
2068 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2069 break;
2070 }
Romain Guycbb89e42009-06-08 15:52:54 -07002071
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002072 case AddAdapter.ITEM_LIVE_FOLDER: {
2073 // Insert extra item to handle inserting folder
2074 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002075
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002076 ArrayList<String> shortcutNames = new ArrayList<String>();
2077 shortcutNames.add(res.getString(R.string.group_folder));
2078 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002079
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002080 ArrayList<ShortcutIconResource> shortcutIcons =
2081 new ArrayList<ShortcutIconResource>();
2082 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2083 R.drawable.ic_launcher_folder));
2084 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2085
2086 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2087 pickIntent.putExtra(Intent.EXTRA_INTENT,
2088 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2089 pickIntent.putExtra(Intent.EXTRA_TITLE,
2090 getText(R.string.title_select_live_folder));
2091 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002092
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002093 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2094 break;
2095 }
2096
2097 case AddAdapter.ITEM_WALLPAPER: {
2098 startWallpaper();
2099 break;
2100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 }
2102 }
2103 }
2104
2105 /**
2106 * Receives notifications when applications are added/removed.
2107 */
2108 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2109 @Override
2110 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002111 final String action = intent.getAction();
2112 final String packageName = intent.getData().getSchemeSpecificPart();
2113 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2114
2115 if (LauncherModel.DEBUG_LOADERS) {
2116 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2117 ", replacing=" + replacing);
2118 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002120
2121 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2122 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2123 if (!replacing) {
2124 removeShortcutsForPackage(packageName);
2125 if (LauncherModel.DEBUG_LOADERS) {
2126 d(LauncherModel.LOG_TAG, " --> remove package");
2127 }
2128 sModel.removePackage(Launcher.this, packageName);
2129 }
2130 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2131 // later, we will update the package at this time
2132 } else {
2133 if (!replacing) {
2134 if (LauncherModel.DEBUG_LOADERS) {
2135 d(LauncherModel.LOG_TAG, " --> add package");
2136 }
2137 sModel.addPackage(Launcher.this, packageName);
2138 } else {
2139 if (LauncherModel.DEBUG_LOADERS) {
2140 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2141 }
2142 sModel.updatePackage(Launcher.this, packageName);
2143 updateShortcutsForPackage(packageName);
2144 }
2145 }
2146 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002148 if (LauncherModel.DEBUG_LOADERS) {
2149 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2150 }
2151 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152 }
2153 }
2154 }
2155
2156 /**
2157 * Receives notifications whenever the user favorites have changed.
2158 */
2159 private class FavoritesChangeObserver extends ContentObserver {
2160 public FavoritesChangeObserver() {
2161 super(new Handler());
2162 }
2163
2164 @Override
2165 public void onChange(boolean selfChange) {
2166 onFavoritesChanged();
2167 }
2168 }
2169
2170 /**
2171 * Receives intents from other applications to change the wallpaper.
2172 */
2173 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2174 private final Application mApplication;
2175 private WeakReference<Launcher> mLauncher;
2176
2177 WallpaperIntentReceiver(Application application, Launcher launcher) {
2178 mApplication = application;
2179 setLauncher(launcher);
2180 }
2181
2182 void setLauncher(Launcher launcher) {
2183 mLauncher = new WeakReference<Launcher>(launcher);
2184 }
2185
2186 @Override
2187 public void onReceive(Context context, Intent intent) {
2188 // Load the wallpaper from the ApplicationContext and store it locally
2189 // until the Launcher Activity is ready to use it
2190 final Drawable drawable = mApplication.getWallpaper();
2191 if (drawable instanceof BitmapDrawable) {
2192 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2193 } else {
2194 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2195 }
2196
2197 // If Launcher is alive, notify we have a new wallpaper
2198 if (mLauncher != null) {
2199 final Launcher launcher = mLauncher.get();
2200 if (launcher != null) {
2201 launcher.loadWallpaper();
2202 }
2203 }
2204 }
2205 }
2206
2207 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2208 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2209 private boolean mOpen;
2210
2211 public void onDrawerOpened() {
2212 if (!mOpen) {
2213 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002216 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217
2218 mOpen = true;
2219 }
2220 }
2221
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002222 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2223 view.getDrawingRect(bounds);
2224
2225 while (view != mDragLayer) {
2226 bounds.offset(view.getLeft(), view.getTop());
2227 view = (View) view.getParent();
2228 }
2229 }
2230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 public void onDrawerClosed() {
2232 if (mOpen) {
2233 mHandleIcon.reverseTransition(150);
2234 mWorkspace.mDrawerBounds.setEmpty();
2235 mOpen = false;
2236 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 mAllAppsGrid.setSelection(0);
2239 mAllAppsGrid.clearTextFilter();
2240 }
2241
2242 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002243 if (PROFILE_DRAWER) {
2244 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2245 }
2246
2247 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2248 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 }
2250
2251 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002252 if (PROFILE_DRAWER) {
2253 android.os.Debug.stopMethodTracing();
2254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255 }
2256 }
2257
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002258 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002260 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002261 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263 // Number of items to bind in every pass
2264 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265
2266 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002267 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002268 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002270
Karl Rosaen138a0412009-04-23 19:00:21 -07002271 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272
2273 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002274 ArrayList<LauncherAppWidgetInfo> appWidgets,
2275 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276
2277 mLauncher = new WeakReference<Launcher>(launcher);
2278 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002279 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002280
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002281 // Sort widgets so active workspace is bound first
2282 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2283 final int size = appWidgets.size();
2284 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002285
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002286 for (int i = 0; i < size; i++) {
2287 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2288 if (appWidgetInfo.screen == currentScreen) {
2289 mAppWidgets.addFirst(appWidgetInfo);
2290 } else {
2291 mAppWidgets.addLast(appWidgetInfo);
2292 }
2293 }
2294 }
Romain Guycbb89e42009-06-08 15:52:54 -07002295
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002296 public void startBindingItems() {
2297 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2298 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002299
2300 public void startBindingDrawer() {
2301 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2302 }
Romain Guycbb89e42009-06-08 15:52:54 -07002303
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002304 public void startBindingAppWidgetsWhenIdle() {
2305 // Ask for notification when message queue becomes idle
2306 final MessageQueue messageQueue = Looper.myQueue();
2307 messageQueue.addIdleHandler(this);
2308 }
Romain Guycbb89e42009-06-08 15:52:54 -07002309
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002310 public boolean queueIdle() {
2311 // Queue is idle, so start binding items
2312 startBindingAppWidgets();
2313 return false;
2314 }
2315
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002316 public void startBindingAppWidgets() {
2317 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2318 }
2319
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002320 @Override
2321 public void handleMessage(Message msg) {
2322 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002323 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324 return;
2325 }
Romain Guycbb89e42009-06-08 15:52:54 -07002326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 switch (msg.what) {
2328 case MESSAGE_BIND_ITEMS: {
2329 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2330 break;
2331 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002332 case MESSAGE_BIND_DRAWER: {
2333 launcher.bindDrawer(this, mDrawerAdapter);
2334 break;
2335 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002336 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002337 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338 break;
2339 }
2340 }
2341 }
2342 }
Romain Guy73b979d2009-06-09 12:57:21 -07002343
2344 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2345 GestureOverlayView.OnGesturePerformedListener {
2346
2347 private final GestureMatcher mMatcher = new GestureMatcher();
2348
2349 GesturesProcessor() {
2350 // TODO: Maybe the load should happen on a background thread?
2351 sLibrary.load();
2352 }
2353
2354 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002355 //noinspection PointlessBooleanExpression,ConstantConditions
2356 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2357 overlay.removeCallbacks(mMatcher);
2358 resetGesturesNextPrompt();
2359 }
Romain Guy73b979d2009-06-09 12:57:21 -07002360
2361 mGesturesAdd.setAlpha(128);
2362 mGesturesAdd.setEnabled(false);
2363 }
2364
2365 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2366 }
2367
2368 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2369 }
2370
2371 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002372 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2373 mMatcher.gesture = overlay.getGesture();
2374 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2375 overlay.clear(false);
2376 } else {
2377 mMatcher.run();
2378 }
Romain Guy73b979d2009-06-09 12:57:21 -07002379 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002380 overlay.removeCallbacks(mMatcher);
2381
2382 mMatcher.gesture = overlay.getGesture();
2383 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2384 overlay.clear(false);
2385 } else {
2386 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2387 }
Romain Guy73b979d2009-06-09 12:57:21 -07002388 }
2389 }
2390
2391 private void matchGesture(Gesture gesture) {
2392 mGesturesAdd.setAlpha(255);
2393 mGesturesAdd.setEnabled(true);
2394
2395 if (gesture != null) {
2396 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2397
2398 if (DEBUG_GESTURES) {
2399 for (Prediction p : predictions) {
2400 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2401 }
2402 }
2403
2404 boolean match = false;
2405 if (predictions.size() > 0) {
2406 final Prediction prediction = predictions.get(0);
2407 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2408 match = true;
2409
2410 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2411 if (info != null) {
2412 updatePrompt(info);
2413 }
2414 }
2415 }
2416
2417 if (!match){
2418 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2419 }
2420 }
2421 }
2422
2423 private void updatePrompt(ApplicationInfo info) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002424 if (mGesturesAction.intent != null &&
2425 info.intent.toURI().equals(mGesturesAction.intent.toURI()) &&
2426 info.title.equals(((TextView) mGesturesPrompt.getCurrentView()).getText())) {
2427 return;
2428 }
Romain Guy73b979d2009-06-09 12:57:21 -07002429 setGesturesNextPrompt(info.icon, info.title);
2430 mGesturesAction.intent = info.intent;
2431 }
2432
2433 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002434 //noinspection PointlessBooleanExpression,ConstantConditions
2435 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2436 overlay.removeCallbacks(mMatcher);
2437 }
Romain Guy73b979d2009-06-09 12:57:21 -07002438 }
2439
2440 void addGesture(String name, Gesture gesture) {
2441 sLibrary.addGesture(name, gesture);
2442 // TODO: On a background thread?
2443 sLibrary.save();
2444 }
2445
2446 void update(ApplicationInfo info, Gesture gesture) {
2447 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002448 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002449 }
2450
2451 class GestureMatcher implements Runnable {
2452 Gesture gesture;
2453
2454 public void run() {
2455 if (gesture != null) {
2456 matchGesture(gesture);
2457 }
2458 }
2459 }
2460 }
2461
2462 private class GesturesAction implements View.OnClickListener {
2463 Intent intent;
2464
2465 public void onClick(View v) {
2466 if (intent != null) {
2467 startActivitySafely(intent);
2468 }
2469 }
2470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002471}
Karl Rosaen138a0412009-04-23 19:00:21 -07002472