blob: 02bb19d2c7bf1798f421af9ae7ec4230056e1239 [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 &&
Romain Guy94406842009-06-17 16:18:58 -0700873 (mDragLayer.hasWindowFocus() ||
874 (mGesturesWindow != null && mGesturesWindow.isShowing()))) {
875
Romain Guyb8734242009-06-10 11:53:57 -0700876 SearchManager searchManager =
877 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Romain Guy94406842009-06-17 16:18:58 -0700878
Romain Guyb8734242009-06-10 11:53:57 -0700879 if (!searchManager.isVisible()) {
880 onHomeKeyPressed();
881 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 }
883 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700884
885 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 if (v != null && v.getWindowToken() != null) {
887 InputMethodManager imm = (InputMethodManager)getSystemService(
888 INPUT_METHOD_SERVICE);
889 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
890 }
891 } else {
892 closeDrawer(false);
893 }
894 }
895 }
896
Romain Guy73b979d2009-06-09 12:57:21 -0700897 private void onHomeKeyPressed() {
898 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
899 showGesturesPanel();
900 } else {
901 hideGesturesPanel();
902 }
903 }
904
905 private void showGesturesPanel() {
906 resetGesturesPrompt();
907
908 mGesturesAdd.setEnabled(false);
909 mGesturesAdd.setAlpha(128);
910
911 mGesturesOverlay.clear(false);
912
913 PopupWindow window;
914 if (mGesturesWindow == null) {
915 mGesturesWindow = new PopupWindow(this);
916 window = mGesturesWindow;
917 window.setFocusable(true);
918 window.setTouchable(true);
919 window.setBackgroundDrawable(null);
920 window.setContentView(mGesturesPanel);
921 } else {
922 window = mGesturesWindow;
923 }
924 window.setAnimationStyle(com.android.internal.R.style.Animation_SlidingCard);
925
926 final int[] xy = new int[2];
927 final DragLayer dragLayer = mDragLayer;
928 dragLayer.getLocationOnScreen(xy);
929
930 window.setWidth(dragLayer.getWidth());
931 window.setHeight(dragLayer.getHeight() - 1);
932 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
933 }
934
935 private void resetGesturesPrompt() {
936 mGesturesAction.intent = null;
937 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
938 prompt.setText(R.string.gestures_instructions);
939 prompt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
940 prompt.setClickable(false);
941 }
942
943 private void resetGesturesNextPrompt() {
944 mGesturesAction.intent = null;
945 setGesturesNextPrompt(null, getString(R.string.gestures_instructions));
946 mGesturesPrompt.getNextView().setClickable(false);
947 }
948
949 private void setGesturesNextPrompt(Drawable icon, CharSequence title) {
950 final TextView prompt = (TextView) mGesturesPrompt.getNextView();
951 prompt.setText(title);
952 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
953 prompt.setClickable(true);
954 mGesturesPrompt.showNext();
955 }
956
957 void hideGesturesPanel() {
958 if (mGesturesWindow != null) {
959 mGesturesWindow.setAnimationStyle(com.android.internal.R.style.Animation_SlidingCard);
960 mGesturesWindow.update();
961 mGesturesWindow.dismiss();
962 }
963 }
964
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 @Override
966 protected void onRestoreInstanceState(Bundle savedInstanceState) {
967 // Do not call super here
968 mSavedInstanceState = savedInstanceState;
969 }
970
971 @Override
972 protected void onSaveInstanceState(Bundle outState) {
973 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
974
975 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
976 if (folders.size() > 0) {
977 final int count = folders.size();
978 long[] ids = new long[count];
979 for (int i = 0; i < count; i++) {
980 final FolderInfo info = folders.get(i).getInfo();
981 ids[i] = info.id;
982 }
983 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
984 } else {
985 super.onSaveInstanceState(outState);
986 }
987
Romain Guy5a941392009-04-28 15:18:25 -0700988 // When the drawer is opened and we are saving the state because of a
989 // configuration change
990 if (mDrawer.isOpened() && getChangingConfigurations() != 0) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700992 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993
994 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
995 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
996 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
997
998 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
999 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1000 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1001 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1002 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1003 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1004 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1005 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1006 layout.getOccupiedCells());
1007 }
1008
1009 if (mFolderInfo != null && mWaitingForResult) {
1010 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1011 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1012 }
Romain Guy73b979d2009-06-09 12:57:21 -07001013
1014 if (mCurrentGesture != null && mWaitingForResult) {
1015 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 }
1018
1019 @Override
1020 public void onDestroy() {
1021 mDestroyed = true;
1022
1023 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001026 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001028 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 }
1030
1031 TextKeyListener.getInstance().release();
1032
1033 mAllAppsGrid.clearTextFilter();
1034 mAllAppsGrid.setAdapter(null);
1035 sModel.unbind();
1036 sModel.abortLoaders();
1037
1038 getContentResolver().unregisterContentObserver(mObserver);
1039 unregisterReceiver(mApplicationsReceiver);
1040 }
1041
1042 @Override
1043 public void startActivityForResult(Intent intent, int requestCode) {
1044 mWaitingForResult = true;
1045 super.startActivityForResult(intent, requestCode);
1046 }
1047
1048 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001049 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001051
1052 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001053
Karl Rosaen138a0412009-04-23 19:00:21 -07001054 // Slide the search widget to the top, if it's on the current screen,
1055 // otherwise show the search dialog immediately.
1056 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1057 if (searchWidget == null) {
1058 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1059 } else {
1060 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1061 // show the currently typed text in the search widget while sliding
1062 searchWidget.setQuery(getTypedText());
1063 }
1064 }
Romain Guycbb89e42009-06-08 15:52:54 -07001065
Karl Rosaen138a0412009-04-23 19:00:21 -07001066 /**
1067 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001068 *
1069 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001070 */
Romain Guycbb89e42009-06-08 15:52:54 -07001071 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001072 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001073
Karl Rosaen138a0412009-04-23 19:00:21 -07001074 if (initialQuery == null) {
1075 // Use any text typed in the launcher as the initial query
1076 initialQuery = getTypedText();
1077 clearTypedText();
1078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 if (appSearchData == null) {
1080 appSearchData = new Bundle();
1081 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1082 }
Romain Guycbb89e42009-06-08 15:52:54 -07001083
Karl Rosaen138a0412009-04-23 19:00:21 -07001084 final SearchManager searchManager =
1085 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1086
1087 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1088 if (searchWidget != null) {
1089 // This gets called when the user leaves the search dialog to go back to
1090 // the Launcher.
1091 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1092 public void onCancel() {
1093 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001094 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001095 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001096 });
1097 }
Romain Guycbb89e42009-06-08 15:52:54 -07001098
Karl Rosaen138a0412009-04-23 19:00:21 -07001099 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001100 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 }
1102
Karl Rosaen138a0412009-04-23 19:00:21 -07001103 /**
1104 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001105 */
Romain Guy5a941392009-04-28 15:18:25 -07001106 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001107 // Close search dialog
1108 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1109 if (searchManager.isVisible()) {
1110 searchManager.stopSearch();
1111 }
1112 // Restore search widget to its normal position
1113 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1114 if (searchWidget != null) {
1115 searchWidget.stopSearch(false);
1116 }
1117 }
Romain Guycbb89e42009-06-08 15:52:54 -07001118
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 @Override
1120 public boolean onCreateOptionsMenu(Menu menu) {
1121 if (mDesktopLocked) return false;
1122
1123 super.onCreateOptionsMenu(menu);
1124 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1125 .setIcon(android.R.drawable.ic_menu_add)
1126 .setAlphabeticShortcut('A');
1127 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1128 .setIcon(android.R.drawable.ic_menu_gallery)
1129 .setAlphabeticShortcut('W');
1130 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1131 .setIcon(android.R.drawable.ic_search_category_default)
1132 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1133 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1134 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1135 .setAlphabeticShortcut('N');
1136
Romain Guy73b979d2009-06-09 12:57:21 -07001137 final Intent gestures = new Intent(this, GesturesActivity.class);
1138 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1139 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1140 .setIntent(gestures);
1141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001143 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1144 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145
1146 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1147 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1148 .setIntent(settings);
1149
1150 return true;
1151 }
1152
1153 @Override
1154 public boolean onPrepareOptionsMenu(Menu menu) {
1155 super.onPrepareOptionsMenu(menu);
1156
1157 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1158 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1159
1160 return true;
1161 }
1162
1163 @Override
1164 public boolean onOptionsItemSelected(MenuItem item) {
1165 switch (item.getItemId()) {
1166 case MENU_ADD:
1167 addItems();
1168 return true;
1169 case MENU_WALLPAPER_SETTINGS:
1170 startWallpaper();
1171 return true;
1172 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001173 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 return true;
1175 case MENU_NOTIFICATIONS:
1176 showNotifications();
1177 return true;
1178 }
1179
1180 return super.onOptionsItemSelected(item);
1181 }
Romain Guycbb89e42009-06-08 15:52:54 -07001182
Karl Rosaen138a0412009-04-23 19:00:21 -07001183 /**
1184 * Indicates that we want global search for this activity by setting the globalSearch
1185 * argument for {@link #startSearch} to true.
1186 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001188 @Override
1189 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001190 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001191 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001192 }
1193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 private void addItems() {
1195 showAddDialog(mMenuAddInfo);
1196 }
1197
1198 private void removeShortcutsForPackage(String packageName) {
1199 if (packageName != null && packageName.length() > 0) {
1200 mWorkspace.removeShortcutsForPackage(packageName);
1201 }
1202 }
Romain Guycbb89e42009-06-08 15:52:54 -07001203
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001204 private void updateShortcutsForPackage(String packageName) {
1205 if (packageName != null && packageName.length() > 0) {
1206 mWorkspace.updateShortcutsForPackage(packageName);
1207 }
1208 }
1209
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001210 void addAppWidget(Intent data) {
1211 // TODO: catch bad widget exception when sent
1212 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001214 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1215 if (SEARCH_WIDGET.equals(customWidget)) {
1216 // We don't need this any more, since this isn't a real app widget.
1217 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1218 // add the search widget
1219 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001221 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1222
1223 if (appWidget.configure != null) {
1224 // Launch over to configure widget, if needed
1225 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1226 intent.setComponent(appWidget.configure);
1227 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1228
1229 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1230 } else {
1231 // Otherwise just add it
1232 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1233 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 }
1235 }
Romain Guycbb89e42009-06-08 15:52:54 -07001236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 void addSearch() {
1238 final Widget info = Widget.makeSearch();
1239 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 final int[] xy = mCellCoordinates;
1242 final int spanX = info.spanX;
1243 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001244
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 sModel.addDesktopItem(info);
1248 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1249 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 final View view = mInflater.inflate(info.layoutResource, null);
1252 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001253 Search search = (Search) view.findViewById(R.id.widget_search);
1254 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1257 }
1258
Romain Guy73b979d2009-06-09 12:57:21 -07001259 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001260 // Handle case where user selected "Applications"
1261 String applicationName = getResources().getString(R.string.group_applications);
1262 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001263
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001264 if (applicationName != null && applicationName.equals(shortcutName)) {
1265 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1266 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001267
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001268 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1269 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001270 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001271 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001272 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
1275
1276 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001277 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001278 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001279 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001280
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001281 if (folderName != null && folderName.equals(shortcutName)) {
1282 addFolder(!mDesktopLocked);
1283 } else {
1284 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1285 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 }
1287
1288 void addFolder(boolean insertAtFirst) {
1289 UserFolderInfo folderInfo = new UserFolderInfo();
1290 folderInfo.title = getText(R.string.folder_name);
1291
1292 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1293 cellInfo.screen = mWorkspace.getCurrentScreen();
1294 if (!findSingleSlot(cellInfo)) return;
1295
1296 // Update the model
1297 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1298 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1299 sModel.addDesktopItem(folderInfo);
1300 sModel.addFolder(folderInfo);
1301
1302 // Create the view
1303 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1304 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1305 mWorkspace.addInCurrentScreen(newFolder,
1306 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1307 }
Romain Guycbb89e42009-06-08 15:52:54 -07001308
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1310 boolean insertAtFirst) {
1311 cellInfo.screen = mWorkspace.getCurrentScreen();
1312 if (!findSingleSlot(cellInfo)) return;
1313
1314 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1315
1316 if (!mRestoring) {
1317 sModel.addDesktopItem(info);
1318
1319 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1320 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1321 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1322 } else if (sModel.isDesktopLoaded()) {
1323 sModel.addDesktopItem(info);
1324 }
1325 }
1326
1327 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1328 CellLayout.CellInfo cellInfo, boolean notify) {
1329
1330 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1331 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1332
1333 Drawable icon = null;
1334 boolean filtered = false;
1335 Intent.ShortcutIconResource iconResource = null;
1336
1337 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1338 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1339 try {
1340 iconResource = (Intent.ShortcutIconResource) extra;
1341 final PackageManager packageManager = context.getPackageManager();
1342 Resources resources = packageManager.getResourcesForApplication(
1343 iconResource.packageName);
1344 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1345 icon = resources.getDrawable(id);
1346 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001347 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 }
1349 }
1350
1351 if (icon == null) {
1352 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1353 }
1354
1355 final LiveFolderInfo info = new LiveFolderInfo();
1356 info.icon = icon;
1357 info.filtered = filtered;
1358 info.title = name;
1359 info.iconResource = iconResource;
1360 info.uri = data.getData();
1361 info.baseIntent = baseIntent;
1362 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1363 LiveFolders.DISPLAY_MODE_GRID);
1364
1365 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1366 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1367 sModel.addFolder(info);
1368
1369 return info;
1370 }
1371
1372 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1373 final int[] xy = new int[2];
1374 if (findSlot(cellInfo, xy, 1, 1)) {
1375 cellInfo.cellX = xy[0];
1376 cellInfo.cellY = xy[1];
1377 return true;
1378 }
1379 return false;
1380 }
1381
1382 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1383 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1384 boolean[] occupied = mSavedState != null ?
1385 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1386 cellInfo = mWorkspace.findAllVacantCells(occupied);
1387 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1388 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1389 return false;
1390 }
1391 }
1392 return true;
1393 }
1394
1395 private void showNotifications() {
1396 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1397 if (statusBar != null) {
1398 statusBar.expand();
1399 }
1400 }
1401
1402 private void startWallpaper() {
1403 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1404 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1405 }
1406
1407 /**
1408 * Registers various intent receivers. The current implementation registers
1409 * only a wallpaper intent receiver to let other applications change the
1410 * wallpaper.
1411 */
1412 private void registerIntentReceivers() {
1413 if (sWallpaperReceiver == null) {
1414 final Application application = getApplication();
1415
1416 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1417
1418 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1419 application.registerReceiver(sWallpaperReceiver, filter);
1420 } else {
1421 sWallpaperReceiver.setLauncher(this);
1422 }
1423
1424 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1425 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1426 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1427 filter.addDataScheme("package");
1428 registerReceiver(mApplicationsReceiver, filter);
1429 }
1430
1431 /**
1432 * Registers various content observers. The current implementation registers
1433 * only a favorites observer to keep track of the favorites applications.
1434 */
1435 private void registerContentObservers() {
1436 ContentResolver resolver = getContentResolver();
1437 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1438 }
1439
1440 @Override
1441 public boolean dispatchKeyEvent(KeyEvent event) {
1442 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1443 switch (event.getKeyCode()) {
1444 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001445 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001446 if (mDrawer.isOpened()) {
1447 closeDrawer();
1448 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001449 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001450 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 return true;
1452 case KeyEvent.KEYCODE_HOME:
1453 return true;
1454 }
1455 }
1456
1457 return super.dispatchKeyEvent(event);
1458 }
1459
1460 private void closeDrawer() {
1461 closeDrawer(true);
1462 }
1463
1464 private void closeDrawer(boolean animated) {
1465 if (mDrawer.isOpened()) {
1466 if (animated) {
1467 mDrawer.animateClose();
1468 } else {
1469 mDrawer.close();
1470 }
1471 if (mDrawer.hasFocus()) {
1472 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1473 }
1474 }
1475 }
1476
1477 private void closeFolder() {
1478 Folder folder = mWorkspace.getOpenFolder();
1479 if (folder != null) {
1480 closeFolder(folder);
1481 }
1482 }
1483
1484 void closeFolder(Folder folder) {
1485 folder.getInfo().opened = false;
1486 ViewGroup parent = (ViewGroup) folder.getParent();
1487 if (parent != null) {
1488 parent.removeView(folder);
1489 }
1490 folder.onClose();
1491 }
1492
1493 /**
1494 * When the notification that favorites have changed is received, requests
1495 * a favorites list refresh.
1496 */
1497 private void onFavoritesChanged() {
1498 mDesktopLocked = true;
1499 mDrawer.lock();
1500 sModel.loadUserItems(false, this, false, false);
1501 }
1502
1503 void onDesktopItemsLoaded() {
1504 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 bindDesktopItems();
1506 }
Romain Guycbb89e42009-06-08 15:52:54 -07001507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 /**
1509 * Refreshes the shortcuts shown on the workspace.
1510 */
1511 private void bindDesktopItems() {
1512 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001513 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001514 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1515 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 return;
1517 }
1518
1519 final Workspace workspace = mWorkspace;
1520 int count = workspace.getChildCount();
1521 for (int i = 0; i < count; i++) {
1522 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1523 }
Romain Guycbb89e42009-06-08 15:52:54 -07001524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 if (DEBUG_USER_INTERFACE) {
1526 android.widget.Button finishButton = new android.widget.Button(this);
1527 finishButton.setText("Finish");
1528 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1529
1530 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1531 public void onClick(View v) {
1532 finish();
1533 }
1534 });
1535 }
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001537 // Flag any old binder to terminate early
1538 if (mBinder != null) {
1539 mBinder.mTerminate = true;
1540 }
Romain Guycbb89e42009-06-08 15:52:54 -07001541
Karl Rosaen138a0412009-04-23 19:00:21 -07001542 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001543 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 }
1545
1546 private void bindItems(Launcher.DesktopBinder binder,
1547 ArrayList<ItemInfo> shortcuts, int start, int count) {
1548
1549 final Workspace workspace = mWorkspace;
1550 final boolean desktopLocked = mDesktopLocked;
1551
1552 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1553 int i = start;
1554
1555 for ( ; i < end; i++) {
1556 final ItemInfo item = shortcuts.get(i);
1557 switch (item.itemType) {
1558 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1559 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1560 final View shortcut = createShortcut((ApplicationInfo) item);
1561 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1562 !desktopLocked);
1563 break;
1564 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1565 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1566 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1567 (UserFolderInfo) item);
1568 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1569 !desktopLocked);
1570 break;
1571 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1572 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1573 R.layout.live_folder_icon, this,
1574 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1575 (LiveFolderInfo) item);
1576 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1577 !desktopLocked);
1578 break;
1579 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1580 final int screen = workspace.getCurrentScreen();
1581 final View view = mInflater.inflate(R.layout.widget_search,
1582 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001583
Karl Rosaen138a0412009-04-23 19:00:21 -07001584 Search search = (Search) view.findViewById(R.id.widget_search);
1585 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 final Widget widget = (Widget) item;
1588 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001589
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 workspace.addWidget(view, widget, !desktopLocked);
1591 break;
1592 }
1593 }
1594
1595 workspace.requestLayout();
1596
1597 if (end >= count) {
1598 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001599 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 } else {
1601 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1602 }
1603 }
1604
1605 private void finishBindDesktopItems() {
1606 if (mSavedState != null) {
1607 if (!mWorkspace.hasFocus()) {
1608 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1609 }
1610
1611 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1612 if (userFolders != null) {
1613 for (long folderId : userFolders) {
1614 final FolderInfo info = sModel.findFolderById(folderId);
1615 if (info != null) {
1616 openFolder(info);
1617 }
1618 }
1619 final Folder openFolder = mWorkspace.getOpenFolder();
1620 if (openFolder != null) {
1621 openFolder.requestFocus();
1622 }
1623 }
1624
1625 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1626 if (allApps) {
1627 mDrawer.open();
1628 }
1629
1630 mSavedState = null;
1631 }
1632
1633 if (mSavedInstanceState != null) {
1634 super.onRestoreInstanceState(mSavedInstanceState);
1635 mSavedInstanceState = null;
1636 }
1637
1638 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1639 mDrawer.requestFocus();
1640 }
1641
1642 mDesktopLocked = false;
1643 mDrawer.unlock();
1644 }
Romain Guycbb89e42009-06-08 15:52:54 -07001645
Karl Rosaen138a0412009-04-23 19:00:21 -07001646 private void bindDrawer(Launcher.DesktopBinder binder,
1647 ApplicationsAdapter drawerAdapter) {
1648 mAllAppsGrid.setAdapter(drawerAdapter);
1649 binder.startBindingAppWidgetsWhenIdle();
1650 }
Romain Guycbb89e42009-06-08 15:52:54 -07001651
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001652 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001653 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 final Workspace workspace = mWorkspace;
1656 final boolean desktopLocked = mDesktopLocked;
1657
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001658 if (!appWidgets.isEmpty()) {
1659 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001660
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001661 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001662 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001663 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001664
Karl Rosaen138a0412009-04-23 19:00:21 -07001665 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001666
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001667 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1668 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001669
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001670 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1671 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001672
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001673 workspace.requestLayout();
1674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001676 if (appWidgets.isEmpty()) {
1677 if (PROFILE_ROTATE) {
1678 android.os.Debug.stopMethodTracing();
1679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001681 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 }
1683 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001684
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 DragController getDragController() {
1686 return mDragLayer;
1687 }
1688
1689 /**
1690 * Launches the intent referred by the clicked shortcut.
1691 *
1692 * @param v The view representing the clicked shortcut.
1693 */
1694 public void onClick(View v) {
1695 Object tag = v.getTag();
1696 if (tag instanceof ApplicationInfo) {
1697 // Open shortcut
1698 final Intent intent = ((ApplicationInfo) tag).intent;
1699 startActivitySafely(intent);
1700 } else if (tag instanceof FolderInfo) {
1701 handleFolderClick((FolderInfo) tag);
1702 }
1703 }
1704
1705 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001706 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1708 try {
1709 startActivity(intent);
1710 } catch (ActivityNotFoundException e) {
1711 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1712 } catch (SecurityException e) {
1713 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001714 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1716 "or use the exported attribute for this activity.", e);
1717 }
1718 }
1719
1720 private void handleFolderClick(FolderInfo folderInfo) {
1721 if (!folderInfo.opened) {
1722 // Close any open folder
1723 closeFolder();
1724 // Open the requested folder
1725 openFolder(folderInfo);
1726 } else {
1727 // Find the open folder...
1728 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1729 int folderScreen;
1730 if (openFolder != null) {
1731 folderScreen = mWorkspace.getScreenForView(openFolder);
1732 // .. and close it
1733 closeFolder(openFolder);
1734 if (folderScreen != mWorkspace.getCurrentScreen()) {
1735 // Close any folder open on the current screen
1736 closeFolder();
1737 // Pull the folder onto this screen
1738 openFolder(folderInfo);
1739 }
1740 }
1741 }
1742 }
1743
1744 private void loadWallpaper() {
1745 // The first time the application is started, we load the wallpaper from
1746 // the ApplicationContext
1747 if (sWallpaper == null) {
1748 final Drawable drawable = getWallpaper();
1749 if (drawable instanceof BitmapDrawable) {
1750 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1751 } else {
1752 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1753 }
1754 }
1755 mWorkspace.loadWallpaper(sWallpaper);
1756 }
1757
1758 /**
1759 * Opens the user fodler described by the specified tag. The opening of the folder
1760 * is animated relative to the specified View. If the View is null, no animation
1761 * is played.
1762 *
1763 * @param folderInfo The FolderInfo describing the folder to open.
1764 */
1765 private void openFolder(FolderInfo folderInfo) {
1766 Folder openFolder;
1767
1768 if (folderInfo instanceof UserFolderInfo) {
1769 openFolder = UserFolder.fromXml(this);
1770 } else if (folderInfo instanceof LiveFolderInfo) {
1771 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1772 } else {
1773 return;
1774 }
1775
1776 openFolder.setDragger(mDragLayer);
1777 openFolder.setLauncher(this);
1778
1779 openFolder.bind(folderInfo);
1780 folderInfo.opened = true;
1781
1782 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1783 openFolder.onOpen();
1784 }
1785
1786 /**
1787 * Returns true if the workspace is being loaded. When the workspace is loading,
1788 * no user interaction should be allowed to avoid any conflict.
1789 *
1790 * @return True if the workspace is locked, false otherwise.
1791 */
1792 boolean isWorkspaceLocked() {
1793 return mDesktopLocked;
1794 }
1795
1796 public boolean onLongClick(View v) {
1797 if (mDesktopLocked) {
1798 return false;
1799 }
1800
1801 if (!(v instanceof CellLayout)) {
1802 v = (View) v.getParent();
1803 }
1804
1805 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1806
1807 // This happens when long clicking an item with the dpad/trackball
1808 if (cellInfo == null) {
1809 return true;
1810 }
1811
1812 if (mWorkspace.allowLongPress()) {
1813 if (cellInfo.cell == null) {
1814 if (cellInfo.valid) {
1815 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001816 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 showAddDialog(cellInfo);
1818 }
1819 } else {
1820 if (!(cellInfo.cell instanceof Folder)) {
1821 // User long pressed on an item
1822 mWorkspace.startDrag(cellInfo);
1823 }
1824 }
1825 }
1826 return true;
1827 }
1828
1829 static LauncherModel getModel() {
1830 return sModel;
1831 }
1832
Romain Guy73b979d2009-06-09 12:57:21 -07001833 static GestureLibrary getGestureLibrary() {
1834 return sLibrary;
1835 }
1836
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 void closeAllApplications() {
1838 mDrawer.close();
1839 }
1840
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001841 View getDrawerHandle() {
1842 return mHandleView;
1843 }
1844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 boolean isDrawerDown() {
1846 return !mDrawer.isMoving() && !mDrawer.isOpened();
1847 }
1848
1849 boolean isDrawerUp() {
1850 return mDrawer.isOpened() && !mDrawer.isMoving();
1851 }
1852
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001853 boolean isDrawerMoving() {
1854 return mDrawer.isMoving();
1855 }
1856
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 Workspace getWorkspace() {
1858 return mWorkspace;
1859 }
1860
1861 GridView getApplicationsGrid() {
1862 return mAllAppsGrid;
1863 }
1864
1865 @Override
1866 protected Dialog onCreateDialog(int id) {
1867 switch (id) {
1868 case DIALOG_CREATE_SHORTCUT:
1869 return new CreateShortcut().createDialog();
1870 case DIALOG_RENAME_FOLDER:
1871 return new RenameFolder().createDialog();
1872 }
1873
1874 return super.onCreateDialog(id);
1875 }
1876
1877 @Override
1878 protected void onPrepareDialog(int id, Dialog dialog) {
1879 switch (id) {
1880 case DIALOG_CREATE_SHORTCUT:
1881 mWorkspace.lock();
1882 break;
1883 case DIALOG_RENAME_FOLDER:
1884 mWorkspace.lock();
1885 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1886 final CharSequence text = mFolderInfo.title;
1887 input.setText(text);
Romain Guycbb89e42009-06-08 15:52:54 -07001888 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 break;
1890 }
1891 }
1892
1893 void showRenameDialog(FolderInfo info) {
1894 mFolderInfo = info;
1895 mWaitingForResult = true;
1896 showDialog(DIALOG_RENAME_FOLDER);
1897 }
1898
1899 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1900 mAddItemCellInfo = cellInfo;
1901 mWaitingForResult = true;
1902 showDialog(DIALOG_CREATE_SHORTCUT);
1903 }
1904
Romain Guy73b979d2009-06-09 12:57:21 -07001905 private void pickShortcut(int requestCode, int title) {
1906 Bundle bundle = new Bundle();
1907
1908 ArrayList<String> shortcutNames = new ArrayList<String>();
1909 shortcutNames.add(getString(R.string.group_applications));
1910 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1911
1912 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1913 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1914 R.drawable.ic_launcher_application));
1915 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1916
1917 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1918 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1919 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1920 pickIntent.putExtras(bundle);
1921
1922 startActivityForResult(pickIntent, requestCode);
1923 }
1924
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 private class RenameFolder {
1926 private EditText mInput;
1927
1928 Dialog createDialog() {
1929 mWaitingForResult = true;
1930 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1931 mInput = (EditText) layout.findViewById(R.id.folder_name);
1932
1933 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1934 builder.setIcon(0);
1935 builder.setTitle(getString(R.string.rename_folder_title));
1936 builder.setCancelable(true);
1937 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1938 public void onCancel(DialogInterface dialog) {
1939 cleanup();
1940 }
1941 });
1942 builder.setNegativeButton(getString(R.string.cancel_action),
1943 new Dialog.OnClickListener() {
1944 public void onClick(DialogInterface dialog, int which) {
1945 cleanup();
1946 }
1947 }
1948 );
1949 builder.setPositiveButton(getString(R.string.rename_action),
1950 new Dialog.OnClickListener() {
1951 public void onClick(DialogInterface dialog, int which) {
1952 changeFolderName();
1953 }
1954 }
1955 );
1956 builder.setView(layout);
1957 return builder.create();
1958 }
1959
1960 private void changeFolderName() {
1961 final String name = mInput.getText().toString();
1962 if (!TextUtils.isEmpty(name)) {
1963 // Make sure we have the right folder info
1964 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
1965 mFolderInfo.title = name;
1966 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1967
1968 if (mDesktopLocked) {
1969 mDrawer.lock();
1970 sModel.loadUserItems(false, Launcher.this, false, false);
1971 } else {
1972 final FolderIcon folderIcon = (FolderIcon)
1973 mWorkspace.getViewForTag(mFolderInfo);
1974 if (folderIcon != null) {
1975 folderIcon.setText(name);
1976 getWorkspace().requestLayout();
1977 } else {
1978 mDesktopLocked = true;
1979 mDrawer.lock();
1980 sModel.loadUserItems(false, Launcher.this, false, false);
1981 }
1982 }
1983 }
1984 cleanup();
1985 }
1986
1987 private void cleanup() {
1988 mWorkspace.unlock();
1989 dismissDialog(DIALOG_RENAME_FOLDER);
1990 mWaitingForResult = false;
1991 mFolderInfo = null;
1992 }
1993 }
1994
1995 /**
1996 * Displays the shortcut creation dialog and launches, if necessary, the
1997 * appropriate activity.
1998 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001999 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07002000 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 Dialog createDialog() {
2004 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2009 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002010 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 builder.setInverseBackgroundForced(true);
2013
2014 AlertDialog dialog = builder.create();
2015 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002016 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 return dialog;
2019 }
2020
2021 public void onCancel(DialogInterface dialog) {
2022 mWaitingForResult = false;
2023 cleanup();
2024 }
2025
Romain Guycbb89e42009-06-08 15:52:54 -07002026 public void onDismiss(DialogInterface dialog) {
2027 mWorkspace.unlock();
2028 }
2029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 private void cleanup() {
2031 mWorkspace.unlock();
2032 dismissDialog(DIALOG_CREATE_SHORTCUT);
2033 }
2034
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002035 /**
2036 * Handle the action clicked in the "Add to home" dialog.
2037 */
2038 public void onClick(DialogInterface dialog, int which) {
2039 Resources res = getResources();
2040 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002041
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002042 switch (which) {
2043 case AddAdapter.ITEM_SHORTCUT: {
2044 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002045 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002046 break;
2047 }
Romain Guycbb89e42009-06-08 15:52:54 -07002048
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002049 case AddAdapter.ITEM_APPWIDGET: {
2050 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002051
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002052 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2053 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2054 // add the search widget
2055 ArrayList<AppWidgetProviderInfo> customInfo =
2056 new ArrayList<AppWidgetProviderInfo>();
2057 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2058 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2059 info.label = getString(R.string.group_search);
2060 info.icon = R.drawable.ic_search_widget;
2061 customInfo.add(info);
2062 pickIntent.putParcelableArrayListExtra(
2063 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2064 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2065 Bundle b = new Bundle();
2066 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2067 customExtras.add(b);
2068 pickIntent.putParcelableArrayListExtra(
2069 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2070 // start the pick activity
2071 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2072 break;
2073 }
Romain Guycbb89e42009-06-08 15:52:54 -07002074
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002075 case AddAdapter.ITEM_LIVE_FOLDER: {
2076 // Insert extra item to handle inserting folder
2077 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002078
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002079 ArrayList<String> shortcutNames = new ArrayList<String>();
2080 shortcutNames.add(res.getString(R.string.group_folder));
2081 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002082
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002083 ArrayList<ShortcutIconResource> shortcutIcons =
2084 new ArrayList<ShortcutIconResource>();
2085 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2086 R.drawable.ic_launcher_folder));
2087 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2088
2089 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2090 pickIntent.putExtra(Intent.EXTRA_INTENT,
2091 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2092 pickIntent.putExtra(Intent.EXTRA_TITLE,
2093 getText(R.string.title_select_live_folder));
2094 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002095
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002096 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2097 break;
2098 }
2099
2100 case AddAdapter.ITEM_WALLPAPER: {
2101 startWallpaper();
2102 break;
2103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 }
2105 }
2106 }
2107
2108 /**
2109 * Receives notifications when applications are added/removed.
2110 */
2111 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2112 @Override
2113 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002114 final String action = intent.getAction();
2115 final String packageName = intent.getData().getSchemeSpecificPart();
2116 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2117
2118 if (LauncherModel.DEBUG_LOADERS) {
2119 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2120 ", replacing=" + replacing);
2121 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002123
2124 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2125 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2126 if (!replacing) {
2127 removeShortcutsForPackage(packageName);
2128 if (LauncherModel.DEBUG_LOADERS) {
2129 d(LauncherModel.LOG_TAG, " --> remove package");
2130 }
2131 sModel.removePackage(Launcher.this, packageName);
2132 }
2133 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2134 // later, we will update the package at this time
2135 } else {
2136 if (!replacing) {
2137 if (LauncherModel.DEBUG_LOADERS) {
2138 d(LauncherModel.LOG_TAG, " --> add package");
2139 }
2140 sModel.addPackage(Launcher.this, packageName);
2141 } else {
2142 if (LauncherModel.DEBUG_LOADERS) {
2143 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2144 }
2145 sModel.updatePackage(Launcher.this, packageName);
2146 updateShortcutsForPackage(packageName);
2147 }
2148 }
2149 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002151 if (LauncherModel.DEBUG_LOADERS) {
2152 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2153 }
2154 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 }
2156 }
2157 }
2158
2159 /**
2160 * Receives notifications whenever the user favorites have changed.
2161 */
2162 private class FavoritesChangeObserver extends ContentObserver {
2163 public FavoritesChangeObserver() {
2164 super(new Handler());
2165 }
2166
2167 @Override
2168 public void onChange(boolean selfChange) {
2169 onFavoritesChanged();
2170 }
2171 }
2172
2173 /**
2174 * Receives intents from other applications to change the wallpaper.
2175 */
2176 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2177 private final Application mApplication;
2178 private WeakReference<Launcher> mLauncher;
2179
2180 WallpaperIntentReceiver(Application application, Launcher launcher) {
2181 mApplication = application;
2182 setLauncher(launcher);
2183 }
2184
2185 void setLauncher(Launcher launcher) {
2186 mLauncher = new WeakReference<Launcher>(launcher);
2187 }
2188
2189 @Override
2190 public void onReceive(Context context, Intent intent) {
2191 // Load the wallpaper from the ApplicationContext and store it locally
2192 // until the Launcher Activity is ready to use it
2193 final Drawable drawable = mApplication.getWallpaper();
2194 if (drawable instanceof BitmapDrawable) {
2195 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2196 } else {
2197 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2198 }
2199
2200 // If Launcher is alive, notify we have a new wallpaper
2201 if (mLauncher != null) {
2202 final Launcher launcher = mLauncher.get();
2203 if (launcher != null) {
2204 launcher.loadWallpaper();
2205 }
2206 }
2207 }
2208 }
2209
2210 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2211 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2212 private boolean mOpen;
2213
2214 public void onDrawerOpened() {
2215 if (!mOpen) {
2216 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002219 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220
2221 mOpen = true;
2222 }
2223 }
2224
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002225 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2226 view.getDrawingRect(bounds);
2227
2228 while (view != mDragLayer) {
2229 bounds.offset(view.getLeft(), view.getTop());
2230 view = (View) view.getParent();
2231 }
2232 }
2233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 public void onDrawerClosed() {
2235 if (mOpen) {
2236 mHandleIcon.reverseTransition(150);
2237 mWorkspace.mDrawerBounds.setEmpty();
2238 mOpen = false;
2239 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 mAllAppsGrid.setSelection(0);
2242 mAllAppsGrid.clearTextFilter();
2243 }
2244
2245 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002246 if (PROFILE_DRAWER) {
2247 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2248 }
2249
2250 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2251 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 }
2253
2254 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002255 if (PROFILE_DRAWER) {
2256 android.os.Debug.stopMethodTracing();
2257 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 }
2259 }
2260
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002261 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002263 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002264 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 // Number of items to bind in every pass
2267 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268
2269 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002270 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002271 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002273
Karl Rosaen138a0412009-04-23 19:00:21 -07002274 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275
2276 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002277 ArrayList<LauncherAppWidgetInfo> appWidgets,
2278 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279
2280 mLauncher = new WeakReference<Launcher>(launcher);
2281 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002282 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002283
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002284 // Sort widgets so active workspace is bound first
2285 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2286 final int size = appWidgets.size();
2287 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002288
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002289 for (int i = 0; i < size; i++) {
2290 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2291 if (appWidgetInfo.screen == currentScreen) {
2292 mAppWidgets.addFirst(appWidgetInfo);
2293 } else {
2294 mAppWidgets.addLast(appWidgetInfo);
2295 }
2296 }
2297 }
Romain Guycbb89e42009-06-08 15:52:54 -07002298
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 public void startBindingItems() {
2300 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2301 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002302
2303 public void startBindingDrawer() {
2304 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2305 }
Romain Guycbb89e42009-06-08 15:52:54 -07002306
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002307 public void startBindingAppWidgetsWhenIdle() {
2308 // Ask for notification when message queue becomes idle
2309 final MessageQueue messageQueue = Looper.myQueue();
2310 messageQueue.addIdleHandler(this);
2311 }
Romain Guycbb89e42009-06-08 15:52:54 -07002312
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002313 public boolean queueIdle() {
2314 // Queue is idle, so start binding items
2315 startBindingAppWidgets();
2316 return false;
2317 }
2318
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002319 public void startBindingAppWidgets() {
2320 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2321 }
2322
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 @Override
2324 public void handleMessage(Message msg) {
2325 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002326 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 return;
2328 }
Romain Guycbb89e42009-06-08 15:52:54 -07002329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 switch (msg.what) {
2331 case MESSAGE_BIND_ITEMS: {
2332 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2333 break;
2334 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002335 case MESSAGE_BIND_DRAWER: {
2336 launcher.bindDrawer(this, mDrawerAdapter);
2337 break;
2338 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002339 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002340 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002341 break;
2342 }
2343 }
2344 }
2345 }
Romain Guy73b979d2009-06-09 12:57:21 -07002346
2347 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2348 GestureOverlayView.OnGesturePerformedListener {
2349
2350 private final GestureMatcher mMatcher = new GestureMatcher();
2351
2352 GesturesProcessor() {
2353 // TODO: Maybe the load should happen on a background thread?
2354 sLibrary.load();
2355 }
2356
2357 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002358 //noinspection PointlessBooleanExpression,ConstantConditions
2359 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2360 overlay.removeCallbacks(mMatcher);
2361 resetGesturesNextPrompt();
2362 }
Romain Guy73b979d2009-06-09 12:57:21 -07002363
2364 mGesturesAdd.setAlpha(128);
2365 mGesturesAdd.setEnabled(false);
2366 }
2367
2368 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2369 }
2370
2371 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2372 }
2373
2374 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002375 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2376 mMatcher.gesture = overlay.getGesture();
2377 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2378 overlay.clear(false);
2379 } else {
2380 mMatcher.run();
2381 }
Romain Guy73b979d2009-06-09 12:57:21 -07002382 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002383 overlay.removeCallbacks(mMatcher);
2384
2385 mMatcher.gesture = overlay.getGesture();
2386 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2387 overlay.clear(false);
2388 } else {
2389 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2390 }
Romain Guy73b979d2009-06-09 12:57:21 -07002391 }
2392 }
2393
2394 private void matchGesture(Gesture gesture) {
2395 mGesturesAdd.setAlpha(255);
2396 mGesturesAdd.setEnabled(true);
2397
2398 if (gesture != null) {
2399 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2400
2401 if (DEBUG_GESTURES) {
2402 for (Prediction p : predictions) {
2403 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2404 }
2405 }
2406
2407 boolean match = false;
2408 if (predictions.size() > 0) {
2409 final Prediction prediction = predictions.get(0);
2410 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2411 match = true;
2412
2413 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2414 if (info != null) {
2415 updatePrompt(info);
2416 }
2417 }
2418 }
2419
2420 if (!match){
2421 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2422 }
2423 }
2424 }
2425
2426 private void updatePrompt(ApplicationInfo info) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002427 if (mGesturesAction.intent != null &&
2428 info.intent.toURI().equals(mGesturesAction.intent.toURI()) &&
2429 info.title.equals(((TextView) mGesturesPrompt.getCurrentView()).getText())) {
2430 return;
2431 }
Romain Guy73b979d2009-06-09 12:57:21 -07002432 setGesturesNextPrompt(info.icon, info.title);
2433 mGesturesAction.intent = info.intent;
2434 }
2435
2436 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002437 //noinspection PointlessBooleanExpression,ConstantConditions
2438 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2439 overlay.removeCallbacks(mMatcher);
2440 }
Romain Guy73b979d2009-06-09 12:57:21 -07002441 }
2442
2443 void addGesture(String name, Gesture gesture) {
2444 sLibrary.addGesture(name, gesture);
2445 // TODO: On a background thread?
2446 sLibrary.save();
2447 }
2448
2449 void update(ApplicationInfo info, Gesture gesture) {
2450 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002451 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002452 }
2453
2454 class GestureMatcher implements Runnable {
2455 Gesture gesture;
2456
2457 public void run() {
2458 if (gesture != null) {
2459 matchGesture(gesture);
2460 }
2461 }
2462 }
2463 }
2464
2465 private class GesturesAction implements View.OnClickListener {
2466 Intent intent;
2467
2468 public void onClick(View v) {
2469 if (intent != null) {
2470 startActivitySafely(intent);
2471 }
2472 }
2473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474}
Karl Rosaen138a0412009-04-23 19:00:21 -07002475