blob: d5c738c75e19326fe75529b3b30db7792da8c529 [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;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070034import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.ActivityInfo;
36import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070039import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.Bitmap;
42import android.graphics.Rect;
Romain Guy73b979d2009-06-09 12:57:21 -070043import android.graphics.PorterDuffXfermode;
44import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.graphics.drawable.BitmapDrawable;
46import android.graphics.drawable.Drawable;
47import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.os.Bundle;
49import android.os.Handler;
50import android.os.IBinder;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070051import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070052import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070053import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.os.Parcelable;
55import android.os.RemoteException;
56import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070057import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.text.Selection;
59import android.text.SpannableStringBuilder;
60import android.text.TextUtils;
61import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070062import static android.util.Log.*;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.view.KeyEvent;
65import android.view.LayoutInflater;
66import android.view.Menu;
67import android.view.MenuItem;
68import android.view.View;
69import android.view.ViewGroup;
Romain Guy73b979d2009-06-09 12:57:21 -070070import android.view.MotionEvent;
71import android.view.Gravity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.view.View.OnLongClickListener;
73import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.widget.EditText;
Karl Rosaen138a0412009-04-23 19:00:21 -070075import android.widget.GridView;
Karl Rosaen138a0412009-04-23 19:00:21 -070076import android.widget.SlidingDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.widget.TextView;
78import android.widget.Toast;
Romain Guy73b979d2009-06-09 12:57:21 -070079import android.widget.ImageView;
80import android.widget.PopupWindow;
81import android.widget.ViewSwitcher;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070082import android.appwidget.AppWidgetManager;
83import android.appwidget.AppWidgetProviderInfo;
Romain Guy73b979d2009-06-09 12:57:21 -070084import android.gesture.GestureOverlayView;
85import android.gesture.GestureLibraries;
86import android.gesture.GestureLibrary;
87import android.gesture.Gesture;
88import android.gesture.Prediction;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
90import java.lang.ref.WeakReference;
91import java.util.ArrayList;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070092import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070093import java.io.DataOutputStream;
94import java.io.FileNotFoundException;
95import java.io.IOException;
96import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
98/**
99 * Default launcher application.
100 */
101public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener {
102 static final String LOG_TAG = "Launcher";
103 static final boolean LOGD = false;
104
105 private static final boolean PROFILE_STARTUP = false;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700106 private static final boolean PROFILE_DRAWER = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700107 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 private static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700109 private static final boolean DEBUG_GESTURES = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Romain Guy6fefcf12009-06-11 13:07:43 -0700111 private static final boolean CONFIG_GESTURES_IMMEDIATE_MODE = true;
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 private static final int WALLPAPER_SCREENS_SPAN = 2;
114
115 private static final int MENU_GROUP_ADD = 1;
116 private static final int MENU_ADD = Menu.FIRST + 1;
117 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
118 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
119 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy73b979d2009-06-09 12:57:21 -0700120 private static final int MENU_GESTURES = MENU_NOTIFICATIONS + 1;
121 private static final int MENU_SETTINGS = MENU_GESTURES + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
123 private static final int REQUEST_CREATE_SHORTCUT = 1;
124 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700125 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int REQUEST_PICK_APPLICATION = 6;
127 private static final int REQUEST_PICK_SHORTCUT = 7;
128 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700129 private static final int REQUEST_PICK_APPWIDGET = 9;
Romain Guy73b979d2009-06-09 12:57:21 -0700130 private static final int REQUEST_PICK_GESTURE_ACTION = 10;
131 private static final int REQUEST_CREATE_GESTURE_ACTION = 11;
132 private static final int REQUEST_CREATE_GESTURE_APPLICATION_ACTION = 12;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
135
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700136 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
137 static final String SEARCH_WIDGET = "search_widget";
138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 static final int SCREEN_COUNT = 3;
140 static final int DEFAULT_SCREN = 1;
141 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700142 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
144 private static final int DIALOG_CREATE_SHORTCUT = 1;
Romain Guycbb89e42009-06-08 15:52:54 -0700145 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Romain Guy98d01652009-06-30 16:21:04 -0700147 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
149 // Type: int
150 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
151 // Type: boolean
152 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
153 // Type: long
154 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
169 // Type: int[]
170 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
171 // Type: boolean
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
173 // Type: long
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Romain Guy73b979d2009-06-09 12:57:21 -0700175 // Type: Gesture (Parcelable)
176 private static final String RUNTIME_STATE_PENDING_GESTURE = "launcher.gesture";
Romain Guy3cf604f2009-06-16 13:12:53 -0700177 // Type: boolean
178 private static final String RUNTIME_STATE_GESTURES_PANEL = "launcher.gesture_panel_showing";
179 // Type: Gesture (Parcelable)
180 private static final String RUNTIME_STATE_GESTURES_PANEL_GESTURE = "launcher.gesture_panel_gesture";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700182 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
184 private static Bitmap sWallpaper;
185
186 private static final Object sLock = new Object();
187 private static int sScreen = DEFAULT_SCREN;
188
189 private static WallpaperIntentReceiver sWallpaperReceiver;
190
Romain Guy73b979d2009-06-09 12:57:21 -0700191 private static GestureLibrary sLibrary;
192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
194 private final ContentObserver mObserver = new FavoritesChangeObserver();
195
196 private LayoutInflater mInflater;
197
198 private DragLayer mDragLayer;
199 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700200
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700201 private AppWidgetManager mAppWidgetManager;
202 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private CellLayout.CellInfo mAddItemCellInfo;
207 private CellLayout.CellInfo mMenuAddInfo;
208 private final int[] mCellCoordinates = new int[2];
209 private FolderInfo mFolderInfo;
210
211 private SlidingDrawer mDrawer;
212 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 private HandleView mHandleView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 private AllAppsGridView mAllAppsGrid;
215
216 private boolean mDesktopLocked = true;
217 private Bundle mSavedState;
218
219 private SpannableStringBuilder mDefaultKeySsb = null;
220
221 private boolean mDestroyed;
222
223 private boolean mRestoring;
224 private boolean mWaitingForResult;
225 private boolean mLocaleChanged;
226
227 private Bundle mSavedInstanceState;
228
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700229 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Romain Guy73b979d2009-06-09 12:57:21 -0700231 private View mGesturesPanel;
232 private GestureOverlayView mGesturesOverlay;
233 private ViewSwitcher mGesturesPrompt;
234 private ImageView mGesturesAdd;
235 private PopupWindow mGesturesWindow;
236 private Launcher.GesturesProcessor mGesturesProcessor;
237 private Gesture mCurrentGesture;
238 private GesturesAction mGesturesAction;
Romain Guyaad5ef42009-06-10 02:48:37 -0700239 private boolean mHideGesturesPanel;
Romain Guy73b979d2009-06-09 12:57:21 -0700240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 @Override
242 protected void onCreate(Bundle savedInstanceState) {
243 super.onCreate(savedInstanceState);
244 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700245
Romain Guy73b979d2009-06-09 12:57:21 -0700246 if (sLibrary == null) {
247 // The context is not kept by the library so it's safe to do this
248 sLibrary = GestureLibraries.fromPrivateFile(Launcher.this,
249 GesturesConstants.STORE_NAME);
250 }
251
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700252 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700253
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700254 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
255 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 if (PROFILE_STARTUP) {
258 android.os.Debug.startMethodTracing("/sdcard/launcher");
259 }
260
261 checkForLocaleChange();
262 setWallpaperDimension();
263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 setContentView(R.layout.launcher);
265 setupViews();
266
267 registerIntentReceivers();
268 registerContentObservers();
269
270 mSavedState = savedInstanceState;
271 restoreState(mSavedState);
272
273 if (PROFILE_STARTUP) {
274 android.os.Debug.stopMethodTracing();
275 }
276
277 if (!mRestoring) {
278 startLoaders();
279 }
280
281 // For handling default keys
282 mDefaultKeySsb = new SpannableStringBuilder();
283 Selection.setSelection(mDefaultKeySsb, 0);
284 }
Romain Guycbb89e42009-06-08 15:52:54 -0700285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700287 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
288 readConfiguration(this, localeConfiguration);
289
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 final Configuration configuration = getResources().getConfiguration();
291
Romain Guy98d01652009-06-30 16:21:04 -0700292 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 final String locale = configuration.locale.toString();
294
Romain Guy98d01652009-06-30 16:21:04 -0700295 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 final int mcc = configuration.mcc;
297
Romain Guy98d01652009-06-30 16:21:04 -0700298 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 final int mnc = configuration.mnc;
300
301 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
302
303 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700304 localeConfiguration.locale = locale;
305 localeConfiguration.mcc = mcc;
306 localeConfiguration.mnc = mnc;
307
308 writeConfiguration(this, localeConfiguration);
309 }
310 }
311
312 private static class LocaleConfiguration {
313 public String locale;
314 public int mcc = -1;
315 public int mnc = -1;
316 }
317
318 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
319 DataInputStream in = null;
320 try {
321 in = new DataInputStream(context.openFileInput(PREFERENCES));
322 configuration.locale = in.readUTF();
323 configuration.mcc = in.readInt();
324 configuration.mnc = in.readInt();
325 } catch (FileNotFoundException e) {
326 // Ignore
327 } catch (IOException e) {
328 // Ignore
329 } finally {
330 if (in != null) {
331 try {
332 in.close();
333 } catch (IOException e) {
334 // Ignore
335 }
336 }
337 }
338 }
339
340 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
341 DataOutputStream out = null;
342 try {
343 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
344 out.writeUTF(configuration.locale);
345 out.writeInt(configuration.mcc);
346 out.writeInt(configuration.mnc);
347 out.flush();
348 } catch (FileNotFoundException e) {
349 // Ignore
350 } catch (IOException e) {
351 //noinspection ResultOfMethodCallIgnored
352 context.getFileStreamPath(PREFERENCES).delete();
353 } finally {
354 if (out != null) {
355 try {
356 out.close();
357 } catch (IOException e) {
358 // Ignore
359 }
360 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 }
362 }
363
364 static int getScreen() {
365 synchronized (sLock) {
366 return sScreen;
367 }
368 }
369
370 static void setScreen(int screen) {
371 synchronized (sLock) {
372 sScreen = screen;
373 }
374 }
375
376 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700377 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
378 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
379
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 mRestoring = false;
381 }
382
383 private void setWallpaperDimension() {
384 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
385 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
386
387 Display display = getWindowManager().getDefaultDisplay();
388 boolean isPortrait = display.getWidth() < display.getHeight();
389
390 final int width = isPortrait ? display.getWidth() : display.getHeight();
391 final int height = isPortrait ? display.getHeight() : display.getWidth();
392 try {
393 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
394 } catch (RemoteException e) {
395 // System is dead!
396 }
397 }
398
399 @Override
400 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700401 mWaitingForResult = false;
402
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 // The pattern used here is that a user PICKs a specific application,
404 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
407 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700408
Romain Guy73b979d2009-06-09 12:57:21 -0700409 if (resultCode == RESULT_OK && (mAddItemCellInfo != null ||
410 ((requestCode == REQUEST_PICK_GESTURE_ACTION ||
411 requestCode == REQUEST_CREATE_GESTURE_ACTION ||
412 requestCode == REQUEST_CREATE_GESTURE_APPLICATION_ACTION) && mCurrentGesture != null))) {
413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 switch (requestCode) {
415 case REQUEST_PICK_APPLICATION:
416 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
417 break;
418 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700419 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 break;
421 case REQUEST_CREATE_SHORTCUT:
422 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
423 break;
424 case REQUEST_PICK_LIVE_FOLDER:
425 addLiveFolder(data);
426 break;
427 case REQUEST_CREATE_LIVE_FOLDER:
428 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
429 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 case REQUEST_PICK_APPWIDGET:
431 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 case REQUEST_CREATE_APPWIDGET:
434 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 break;
Romain Guy73b979d2009-06-09 12:57:21 -0700436 case REQUEST_PICK_GESTURE_ACTION:
437 processShortcut(data, REQUEST_CREATE_GESTURE_APPLICATION_ACTION,
438 REQUEST_CREATE_GESTURE_ACTION);
439 break;
440 case REQUEST_CREATE_GESTURE_ACTION:
441 completeCreateGesture(data, true);
442 break;
443 case REQUEST_CREATE_GESTURE_APPLICATION_ACTION:
444 completeCreateGesture(data, false);
445 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700447 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700449 // Clean up the appWidgetId if we canceled
450 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
451 if (appWidgetId != -1) {
452 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
456
457 @Override
458 protected void onResume() {
459 super.onResume();
460
461 if (mRestoring) {
462 startLoaders();
463 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700464
465 // Make sure that the search gadget (if any) is in its normal place.
Romain Guy5a941392009-04-28 15:18:25 -0700466 stopSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468
469 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700470 protected void onPause() {
471 super.onPause();
Romain Guy73b979d2009-06-09 12:57:21 -0700472 if (mGesturesWindow != null) {
473 mGesturesWindow.setAnimationStyle(0);
474 mGesturesWindow.update();
475 }
Romain Guycbb89e42009-06-08 15:52:54 -0700476 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700477 }
Romain Guycbb89e42009-06-08 15:52:54 -0700478
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700479 @Override
Romain Guy73b979d2009-06-09 12:57:21 -0700480 protected void onStop() {
481 super.onStop();
Romain Guyaad5ef42009-06-10 02:48:37 -0700482 if (mHideGesturesPanel) {
483 mHideGesturesPanel = false;
484 hideGesturesPanel();
485 }
Romain Guy73b979d2009-06-09 12:57:21 -0700486 }
487
488 @Override
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700489 public Object onRetainNonConfigurationInstance() {
490 // Flag any binder to stop early before switching
491 if (mBinder != null) {
492 mBinder.mTerminate = true;
493 }
Romain Guycbb89e42009-06-08 15:52:54 -0700494
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700495 if (PROFILE_ROTATE) {
496 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
497 }
498 return null;
499 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700500
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 private boolean acceptFilter() {
502 final InputMethodManager inputManager = (InputMethodManager)
503 getSystemService(Context.INPUT_METHOD_SERVICE);
504 return !inputManager.isFullscreenMode();
505 }
506
507 @Override
508 public boolean onKeyDown(int keyCode, KeyEvent event) {
509 boolean handled = super.onKeyDown(keyCode, event);
510 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
511 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
512 keyCode, event);
513 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700514 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700515 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700516 // showSearchDialog()
517 // If there are multiple keystrokes before the search dialog takes focus,
518 // onSearchRequested() will be called for every keystroke,
519 // but it is idempotent, so it's fine.
520 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800521 }
522 }
523
524 return handled;
525 }
526
Karl Rosaen138a0412009-04-23 19:00:21 -0700527 private String getTypedText() {
528 return mDefaultKeySsb.toString();
529 }
530
531 private void clearTypedText() {
532 mDefaultKeySsb.clear();
533 mDefaultKeySsb.clearSpans();
534 Selection.setSelection(mDefaultKeySsb, 0);
535 }
536
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 /**
538 * Restores the previous state, if it exists.
539 *
540 * @param savedState The previous state.
541 */
542 private void restoreState(Bundle savedState) {
543 if (savedState == null) {
544 return;
545 }
546
547 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
548 if (currentScreen > -1) {
549 mWorkspace.setCurrentScreen(currentScreen);
550 }
551
552 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
553 if (addScreen > -1) {
554 mAddItemCellInfo = new CellLayout.CellInfo();
555 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
556 addItemCellInfo.valid = true;
557 addItemCellInfo.screen = addScreen;
558 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
559 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
560 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
561 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
562 addItemCellInfo.findVacantCellsFromOccupied(
563 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
564 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
565 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
566 mRestoring = true;
567 }
568
569 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
570 if (renameFolder) {
571 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
572 mFolderInfo = sModel.getFolderById(this, id);
573 mRestoring = true;
574 }
Romain Guy73b979d2009-06-09 12:57:21 -0700575
Romain Guy1ce1a242009-06-23 17:34:54 -0700576 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_PENDING_GESTURE);
577
Romain Guy3cf604f2009-06-16 13:12:53 -0700578 boolean gesturesShowing = savedState.getBoolean(RUNTIME_STATE_GESTURES_PANEL, false);
579 if (gesturesShowing) {
580 final Gesture gesture = (Gesture) savedState.get(RUNTIME_STATE_GESTURES_PANEL_GESTURE);
581 mWorkspace.post(new Runnable() {
582 public void run() {
583 showGesturesPanel(false);
584 mGesturesProcessor.matchGesture(gesture, false);
585 mWorkspace.post(new Runnable() {
586 public void run() {
587 if (gesture != null) {
588 mGesturesOverlay.setGesture(gesture);
589 }
590 }
591 });
592 }
593 });
594 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 }
596
597 /**
598 * Finds all the views we need and configure them properly.
599 */
600 private void setupViews() {
601 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
602 final DragLayer dragLayer = mDragLayer;
603
604 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
605 final Workspace workspace = mWorkspace;
606
607 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
608 final SlidingDrawer drawer = mDrawer;
609
610 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
611 final AllAppsGridView grid = mAllAppsGrid;
612
613 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
614
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700615 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
616 mHandleView.setLauncher(this);
617 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 mHandleIcon.setCrossFadeEnabled(true);
619
620 drawer.lock();
621 final DrawerManager drawerManager = new DrawerManager();
622 drawer.setOnDrawerOpenListener(drawerManager);
623 drawer.setOnDrawerCloseListener(drawerManager);
624 drawer.setOnDrawerScrollListener(drawerManager);
625
Karl Rosaen138a0412009-04-23 19:00:21 -0700626 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 grid.setDragger(dragLayer);
628 grid.setLauncher(this);
629
630 workspace.setOnLongClickListener(this);
631 workspace.setDragger(dragLayer);
632 workspace.setLauncher(this);
633 loadWallpaper();
634
635 deleteZone.setLauncher(this);
636 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700637 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638
639 dragLayer.setIgnoredDropTarget(grid);
640 dragLayer.setDragScoller(workspace);
641 dragLayer.setDragListener(deleteZone);
Romain Guy73b979d2009-06-09 12:57:21 -0700642
643 mGesturesPanel = mInflater.inflate(R.layout.gestures, mDragLayer, false);
644 final View gesturesPanel = mGesturesPanel;
645
646 mGesturesPrompt = (ViewSwitcher) gesturesPanel.findViewById(R.id.gestures_actions);
647 mGesturesAction = new GesturesAction();
648
649 mGesturesPrompt.getChildAt(0).setOnClickListener(mGesturesAction);
650 mGesturesPrompt.getChildAt(1).setOnClickListener(mGesturesAction);
651
652 mGesturesAdd = (ImageView) gesturesPanel.findViewById(R.id.gestures_add);
653 final ImageView gesturesAdd = mGesturesAdd;
654 gesturesAdd.setAlpha(128);
655 gesturesAdd.setEnabled(false);
656 gesturesAdd.setOnClickListener(new View.OnClickListener() {
657 public void onClick(View v) {
658 createGesture();
659 }
660 });
661
662 mGesturesOverlay = (GestureOverlayView) gesturesPanel.findViewById(R.id.gestures_overlay);
663 mGesturesProcessor = new GesturesProcessor();
664
665 final GestureOverlayView overlay = mGesturesOverlay;
Romain Guy73b979d2009-06-09 12:57:21 -0700666 overlay.addOnGestureListener(mGesturesProcessor);
Romain Guyb8734242009-06-10 11:53:57 -0700667 overlay.getGesturePaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
Romain Guy73b979d2009-06-09 12:57:21 -0700668 }
669
670 private void createGesture() {
Romain Guyf280f202009-06-26 10:31:07 -0700671 if (!mWaitingForResult) {
672 mCurrentGesture = mGesturesOverlay.getGesture();
673 mWaitingForResult = true;
674 pickShortcut(REQUEST_PICK_GESTURE_ACTION, R.string.title_select_shortcut);
675 }
Romain Guy73b979d2009-06-09 12:57:21 -0700676 }
677
678 private void completeCreateGesture(Intent data, boolean isShortcut) {
679 ApplicationInfo info;
680
681 if (isShortcut) {
682 info = infoFromShortcutIntent(this, data);
683 } else {
684 info = infoFromApplicationIntent(this, data);
685 }
686
687 boolean success = false;
688 if (info != null) {
689 info.isGesture = true;
690
691 if (LauncherModel.addGestureToDatabase(this, info, false)) {
692 mGesturesProcessor.addGesture(String.valueOf(info.id), mCurrentGesture);
693 mGesturesProcessor.update(info, mCurrentGesture);
694 Toast.makeText(this, getString(R.string.gestures_created, info.title),
695 Toast.LENGTH_SHORT).show();
696 success = true;
697 }
698 }
699
700 if (!success) {
701 Toast.makeText(this, getString(R.string.gestures_failed), Toast.LENGTH_SHORT).show();
702 }
703
704 mCurrentGesture = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 }
706
707 /**
708 * Creates a view representing a shortcut.
709 *
710 * @param info The data structure describing the shortcut.
711 *
712 * @return A View inflated from R.layout.application.
713 */
714 View createShortcut(ApplicationInfo info) {
715 return createShortcut(R.layout.application,
716 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
717 }
718
719 /**
720 * Creates a view representing a shortcut inflated from the specified resource.
721 *
722 * @param layoutResId The id of the XML layout used to create the shortcut.
723 * @param parent The group the shortcut belongs to.
724 * @param info The data structure describing the shortcut.
725 *
726 * @return A View inflated from layoutResId.
727 */
728 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
729 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
730
731 if (!info.filtered) {
732 info.icon = Utilities.createIconThumbnail(info.icon, this);
733 info.filtered = true;
734 }
735
736 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
737 favorite.setText(info.title);
738 favorite.setTag(info);
739 favorite.setOnClickListener(this);
740
741 return favorite;
742 }
743
744 /**
745 * Add an application shortcut to the workspace.
746 *
747 * @param data The intent describing the application.
748 * @param cellInfo The position on screen where to create the shortcut.
749 */
750 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
751 boolean insertAtFirst) {
752 cellInfo.screen = mWorkspace.getCurrentScreen();
753 if (!findSingleSlot(cellInfo)) return;
754
Romain Guy73b979d2009-06-09 12:57:21 -0700755 final ApplicationInfo info = infoFromApplicationIntent(context, data);
756 if (info != null) {
757 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
758 }
759 }
760
761 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 ComponentName component = data.getComponent();
763 PackageManager packageManager = context.getPackageManager();
764 ActivityInfo activityInfo = null;
765 try {
766 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
767 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700768 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 }
Romain Guycbb89e42009-06-08 15:52:54 -0700770
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 if (activityInfo != null) {
772 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700773
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 itemInfo.title = activityInfo.loadLabel(packageManager);
775 if (itemInfo.title == null) {
776 itemInfo.title = activityInfo.name;
777 }
Romain Guycbb89e42009-06-08 15:52:54 -0700778
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
780 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
781 itemInfo.icon = activityInfo.loadIcon(packageManager);
782 itemInfo.container = ItemInfo.NO_ID;
783
Romain Guy73b979d2009-06-09 12:57:21 -0700784 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 }
Romain Guy73b979d2009-06-09 12:57:21 -0700786
787 return null;
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 /**
791 * Add a shortcut to the workspace.
792 *
793 * @param data The intent describing the shortcut.
794 * @param cellInfo The position on screen where to create the shortcut.
795 * @param insertAtFirst
796 */
797 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
798 boolean insertAtFirst) {
799 cellInfo.screen = mWorkspace.getCurrentScreen();
800 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
803
804 if (!mRestoring) {
805 sModel.addDesktopItem(info);
806
807 final View view = createShortcut(info);
808 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
809 } else if (sModel.isDesktopLoaded()) {
810 sModel.addDesktopItem(info);
811 }
812 }
813
Romain Guycbb89e42009-06-08 15:52:54 -0700814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700816 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700818 * @param data The intent describing the appWidgetId.
819 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700821 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 boolean insertAtFirst) {
823
824 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700825 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700826
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700827 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700828
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700829 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700830
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700831 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700833 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700834
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 // Try finding open space on Launcher screen
836 final int[] xy = mCellCoordinates;
837 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
838
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700839 // Build Launcher-specific widget info and save to database
840 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 launcherInfo.spanX = spans[0];
842 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700843
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 LauncherModel.addItemToDatabase(this, launcherInfo,
845 LauncherSettings.Favorites.CONTAINER_DESKTOP,
846 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
847
848 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700849 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700850
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700852 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700853
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700854 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700856
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
858 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
859 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700860 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 }
862 }
Romain Guycbb89e42009-06-08 15:52:54 -0700863
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700864 public LauncherAppWidgetHost getAppWidgetHost() {
865 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 }
Romain Guycbb89e42009-06-08 15:52:54 -0700867
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 static ApplicationInfo addShortcut(Context context, Intent data,
869 CellLayout.CellInfo cellInfo, boolean notify) {
870
Romain Guy73b979d2009-06-09 12:57:21 -0700871 final ApplicationInfo info = infoFromShortcutIntent(context, data);
872 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
873 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
874
875 return info;
876 }
877
878 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
880 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
881 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
882
883 Drawable icon = null;
884 boolean filtered = false;
885 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700886 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887
888 if (bitmap != null) {
889 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
890 filtered = true;
891 customIcon = true;
892 } else {
893 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700894 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700896 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 final PackageManager packageManager = context.getPackageManager();
898 Resources resources = packageManager.getResourcesForApplication(
899 iconResource.packageName);
900 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
901 icon = resources.getDrawable(id);
902 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700903 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905 }
906 }
907
908 if (icon == null) {
909 icon = context.getPackageManager().getDefaultActivityIcon();
910 }
911
912 final ApplicationInfo info = new ApplicationInfo();
913 info.icon = icon;
914 info.filtered = filtered;
915 info.title = name;
916 info.intent = intent;
917 info.customIcon = customIcon;
918 info.iconResource = iconResource;
919
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 return info;
921 }
922
923 @Override
924 protected void onNewIntent(Intent intent) {
925 super.onNewIntent(intent);
926
927 // Close the menu
928 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
929 getWindow().closeAllPanels();
930
931 try {
932 dismissDialog(DIALOG_CREATE_SHORTCUT);
933 // Unlock the workspace if the dialog was showing
934 mWorkspace.unlock();
935 } catch (Exception e) {
936 // An exception is thrown if the dialog is not visible, which is fine
937 }
938
939 try {
940 dismissDialog(DIALOG_RENAME_FOLDER);
941 // Unlock the workspace if the dialog was showing
942 mWorkspace.unlock();
943 } catch (Exception e) {
944 // An exception is thrown if the dialog is not visible, which is fine
945 }
946
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
948 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700949
Romain Guye3895ae2009-06-16 13:25:29 -0700950 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE &&
Romain Guy94406842009-06-17 16:18:58 -0700951 (mDragLayer.hasWindowFocus() ||
952 (mGesturesWindow != null && mGesturesWindow.isShowing()))) {
953
Romain Guyb8734242009-06-10 11:53:57 -0700954 SearchManager searchManager =
955 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Romain Guy94406842009-06-17 16:18:58 -0700956
Romain Guyb8734242009-06-10 11:53:57 -0700957 if (!searchManager.isVisible()) {
958 onHomeKeyPressed();
959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 }
961 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700962
963 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 if (v != null && v.getWindowToken() != null) {
965 InputMethodManager imm = (InputMethodManager)getSystemService(
966 INPUT_METHOD_SERVICE);
967 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
968 }
969 } else {
970 closeDrawer(false);
971 }
972 }
973 }
974
Romain Guy73b979d2009-06-09 12:57:21 -0700975 private void onHomeKeyPressed() {
976 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
977 showGesturesPanel();
978 } else {
979 hideGesturesPanel();
980 }
981 }
982
983 private void showGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -0700984 showGesturesPanel(true);
985 }
986
987 private void showGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -0700988 resetGesturesPrompt();
989
990 mGesturesAdd.setEnabled(false);
991 mGesturesAdd.setAlpha(128);
992
993 mGesturesOverlay.clear(false);
994
995 PopupWindow window;
996 if (mGesturesWindow == null) {
997 mGesturesWindow = new PopupWindow(this);
998 window = mGesturesWindow;
999 window.setFocusable(true);
1000 window.setTouchable(true);
1001 window.setBackgroundDrawable(null);
1002 window.setContentView(mGesturesPanel);
1003 } else {
1004 window = mGesturesWindow;
1005 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001006 window.setAnimationStyle(animate ? com.android.internal.R.style.Animation_SlidingCard : 0);
Romain Guy73b979d2009-06-09 12:57:21 -07001007
1008 final int[] xy = new int[2];
1009 final DragLayer dragLayer = mDragLayer;
1010 dragLayer.getLocationOnScreen(xy);
1011
1012 window.setWidth(dragLayer.getWidth());
1013 window.setHeight(dragLayer.getHeight() - 1);
1014 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
1015 }
1016
1017 private void resetGesturesPrompt() {
1018 mGesturesAction.intent = null;
1019 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
1020 prompt.setText(R.string.gestures_instructions);
1021 prompt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
1022 prompt.setClickable(false);
1023 }
1024
1025 private void resetGesturesNextPrompt() {
1026 mGesturesAction.intent = null;
1027 setGesturesNextPrompt(null, getString(R.string.gestures_instructions));
1028 mGesturesPrompt.getNextView().setClickable(false);
1029 }
1030
1031 private void setGesturesNextPrompt(Drawable icon, CharSequence title) {
1032 final TextView prompt = (TextView) mGesturesPrompt.getNextView();
1033 prompt.setText(title);
1034 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
1035 prompt.setClickable(true);
1036 mGesturesPrompt.showNext();
1037 }
1038
Romain Guy3cf604f2009-06-16 13:12:53 -07001039 private void setGesturesPrompt(Drawable icon, CharSequence title) {
1040 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
1041 prompt.setText(title);
1042 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
1043 prompt.setClickable(true);
1044 }
1045
Romain Guy73b979d2009-06-09 12:57:21 -07001046 void hideGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -07001047 hideGesturesPanel(true);
1048 }
1049
1050 void hideGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07001051 if (mGesturesWindow != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001052 final PopupWindow popupWindow = mGesturesWindow;
1053 popupWindow.setAnimationStyle(animate ?
1054 com.android.internal.R.style.Animation_SlidingCard : 0);
1055 popupWindow.update();
1056 popupWindow.dismiss();
Romain Guy73b979d2009-06-09 12:57:21 -07001057 }
1058 }
1059
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 @Override
1061 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1062 // Do not call super here
1063 mSavedInstanceState = savedInstanceState;
1064 }
1065
1066 @Override
1067 protected void onSaveInstanceState(Bundle outState) {
1068 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1069
1070 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1071 if (folders.size() > 0) {
1072 final int count = folders.size();
1073 long[] ids = new long[count];
1074 for (int i = 0; i < count; i++) {
1075 final FolderInfo info = folders.get(i).getInfo();
1076 ids[i] = info.id;
1077 }
1078 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1079 } else {
1080 super.onSaveInstanceState(outState);
1081 }
1082
Romain Guy3cf604f2009-06-16 13:12:53 -07001083 final boolean isConfigurationChange = getChangingConfigurations() != 0;
1084
Romain Guy5a941392009-04-28 15:18:25 -07001085 // When the drawer is opened and we are saving the state because of a
1086 // configuration change
Romain Guy3cf604f2009-06-16 13:12:53 -07001087 if (mDrawer.isOpened() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
1091 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1092 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1093 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1094
1095 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1096 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1097 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1098 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1099 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1100 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1101 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1102 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1103 layout.getOccupiedCells());
1104 }
1105
1106 if (mFolderInfo != null && mWaitingForResult) {
1107 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1108 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1109 }
Romain Guy73b979d2009-06-09 12:57:21 -07001110
1111 if (mCurrentGesture != null && mWaitingForResult) {
1112 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1113 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001114
Romain Guy1ce1a242009-06-23 17:34:54 -07001115 if (mGesturesWindow != null && mGesturesWindow.isShowing()) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001116 outState.putBoolean(RUNTIME_STATE_GESTURES_PANEL, true);
1117
Romain Guy1ce1a242009-06-23 17:34:54 -07001118 if (mCurrentGesture == null || !mWaitingForResult) {
1119 final Gesture gesture = mGesturesOverlay.getGesture();
1120 if (gesture != null) {
1121 outState.putParcelable(RUNTIME_STATE_GESTURES_PANEL_GESTURE, gesture);
1122 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001123 }
1124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
1126
1127 @Override
1128 public void onDestroy() {
1129 mDestroyed = true;
1130
1131 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001134 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001136 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 }
1138
1139 TextKeyListener.getInstance().release();
1140
Romain Guy3cf604f2009-06-16 13:12:53 -07001141 hideGesturesPanel(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 mAllAppsGrid.clearTextFilter();
1143 mAllAppsGrid.setAdapter(null);
1144 sModel.unbind();
1145 sModel.abortLoaders();
1146
1147 getContentResolver().unregisterContentObserver(mObserver);
1148 unregisterReceiver(mApplicationsReceiver);
1149 }
1150
1151 @Override
1152 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001153 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 super.startActivityForResult(intent, requestCode);
1155 }
1156
1157 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001158 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001160
1161 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001162
Karl Rosaen138a0412009-04-23 19:00:21 -07001163 // Slide the search widget to the top, if it's on the current screen,
1164 // otherwise show the search dialog immediately.
1165 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1166 if (searchWidget == null) {
1167 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1168 } else {
1169 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1170 // show the currently typed text in the search widget while sliding
1171 searchWidget.setQuery(getTypedText());
1172 }
1173 }
Romain Guycbb89e42009-06-08 15:52:54 -07001174
Karl Rosaen138a0412009-04-23 19:00:21 -07001175 /**
1176 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001177 *
1178 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001179 */
Romain Guycbb89e42009-06-08 15:52:54 -07001180 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001181 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001182
Karl Rosaen138a0412009-04-23 19:00:21 -07001183 if (initialQuery == null) {
1184 // Use any text typed in the launcher as the initial query
1185 initialQuery = getTypedText();
1186 clearTypedText();
1187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 if (appSearchData == null) {
1189 appSearchData = new Bundle();
1190 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1191 }
Romain Guycbb89e42009-06-08 15:52:54 -07001192
Karl Rosaen138a0412009-04-23 19:00:21 -07001193 final SearchManager searchManager =
1194 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1195
1196 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1197 if (searchWidget != null) {
1198 // This gets called when the user leaves the search dialog to go back to
1199 // the Launcher.
1200 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1201 public void onCancel() {
1202 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001203 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001204 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001205 });
1206 }
Romain Guycbb89e42009-06-08 15:52:54 -07001207
Karl Rosaen138a0412009-04-23 19:00:21 -07001208 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001209 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 }
1211
Karl Rosaen138a0412009-04-23 19:00:21 -07001212 /**
1213 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001214 */
Romain Guy5a941392009-04-28 15:18:25 -07001215 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001216 // Close search dialog
1217 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1218 if (searchManager.isVisible()) {
1219 searchManager.stopSearch();
1220 }
1221 // Restore search widget to its normal position
1222 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1223 if (searchWidget != null) {
1224 searchWidget.stopSearch(false);
1225 }
1226 }
Romain Guycbb89e42009-06-08 15:52:54 -07001227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 @Override
1229 public boolean onCreateOptionsMenu(Menu menu) {
1230 if (mDesktopLocked) return false;
1231
1232 super.onCreateOptionsMenu(menu);
1233 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1234 .setIcon(android.R.drawable.ic_menu_add)
1235 .setAlphabeticShortcut('A');
1236 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1237 .setIcon(android.R.drawable.ic_menu_gallery)
1238 .setAlphabeticShortcut('W');
1239 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1240 .setIcon(android.R.drawable.ic_search_category_default)
1241 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1242 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1243 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1244 .setAlphabeticShortcut('N');
1245
Romain Guy73b979d2009-06-09 12:57:21 -07001246 final Intent gestures = new Intent(this, GesturesActivity.class);
1247 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1248 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1249 .setIntent(gestures);
1250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001252 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1253 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254
1255 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1256 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1257 .setIntent(settings);
1258
1259 return true;
1260 }
1261
1262 @Override
1263 public boolean onPrepareOptionsMenu(Menu menu) {
1264 super.onPrepareOptionsMenu(menu);
1265
1266 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1267 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1268
1269 return true;
1270 }
1271
1272 @Override
1273 public boolean onOptionsItemSelected(MenuItem item) {
1274 switch (item.getItemId()) {
1275 case MENU_ADD:
1276 addItems();
1277 return true;
1278 case MENU_WALLPAPER_SETTINGS:
1279 startWallpaper();
1280 return true;
1281 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001282 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 return true;
1284 case MENU_NOTIFICATIONS:
1285 showNotifications();
1286 return true;
1287 }
1288
1289 return super.onOptionsItemSelected(item);
1290 }
Romain Guycbb89e42009-06-08 15:52:54 -07001291
Karl Rosaen138a0412009-04-23 19:00:21 -07001292 /**
1293 * Indicates that we want global search for this activity by setting the globalSearch
1294 * argument for {@link #startSearch} to true.
1295 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001297 @Override
1298 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001299 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001300 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001301 }
1302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 private void addItems() {
1304 showAddDialog(mMenuAddInfo);
1305 }
1306
1307 private void removeShortcutsForPackage(String packageName) {
1308 if (packageName != null && packageName.length() > 0) {
1309 mWorkspace.removeShortcutsForPackage(packageName);
1310 }
1311 }
Romain Guycbb89e42009-06-08 15:52:54 -07001312
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001313 private void updateShortcutsForPackage(String packageName) {
1314 if (packageName != null && packageName.length() > 0) {
1315 mWorkspace.updateShortcutsForPackage(packageName);
1316 }
1317 }
1318
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001319 void addAppWidget(Intent data) {
1320 // TODO: catch bad widget exception when sent
1321 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001323 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1324 if (SEARCH_WIDGET.equals(customWidget)) {
1325 // We don't need this any more, since this isn't a real app widget.
1326 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1327 // add the search widget
1328 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001330 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1331
1332 if (appWidget.configure != null) {
1333 // Launch over to configure widget, if needed
1334 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1335 intent.setComponent(appWidget.configure);
1336 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1337
1338 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1339 } else {
1340 // Otherwise just add it
1341 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344 }
Romain Guycbb89e42009-06-08 15:52:54 -07001345
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 void addSearch() {
1347 final Widget info = Widget.makeSearch();
1348 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 final int[] xy = mCellCoordinates;
1351 final int spanX = info.spanX;
1352 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 sModel.addDesktopItem(info);
1357 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1358 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001359
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 final View view = mInflater.inflate(info.layoutResource, null);
1361 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001362 Search search = (Search) view.findViewById(R.id.widget_search);
1363 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1366 }
1367
Romain Guy73b979d2009-06-09 12:57:21 -07001368 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001369 // Handle case where user selected "Applications"
1370 String applicationName = getResources().getString(R.string.group_applications);
1371 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001372
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001373 if (applicationName != null && applicationName.equals(shortcutName)) {
1374 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1375 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001376
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001377 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1378 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001379 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001380 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001381 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001382 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 }
1384
1385 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001386 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001387 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001388 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001389
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001390 if (folderName != null && folderName.equals(shortcutName)) {
1391 addFolder(!mDesktopLocked);
1392 } else {
1393 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 }
1396
1397 void addFolder(boolean insertAtFirst) {
1398 UserFolderInfo folderInfo = new UserFolderInfo();
1399 folderInfo.title = getText(R.string.folder_name);
1400
1401 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1402 cellInfo.screen = mWorkspace.getCurrentScreen();
1403 if (!findSingleSlot(cellInfo)) return;
1404
1405 // Update the model
1406 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1407 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1408 sModel.addDesktopItem(folderInfo);
1409 sModel.addFolder(folderInfo);
1410
1411 // Create the view
1412 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1413 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1414 mWorkspace.addInCurrentScreen(newFolder,
1415 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1416 }
Romain Guycbb89e42009-06-08 15:52:54 -07001417
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1419 boolean insertAtFirst) {
1420 cellInfo.screen = mWorkspace.getCurrentScreen();
1421 if (!findSingleSlot(cellInfo)) return;
1422
1423 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1424
1425 if (!mRestoring) {
1426 sModel.addDesktopItem(info);
1427
1428 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1429 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1430 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1431 } else if (sModel.isDesktopLoaded()) {
1432 sModel.addDesktopItem(info);
1433 }
1434 }
1435
1436 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1437 CellLayout.CellInfo cellInfo, boolean notify) {
1438
1439 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1440 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1441
1442 Drawable icon = null;
1443 boolean filtered = false;
1444 Intent.ShortcutIconResource iconResource = null;
1445
1446 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1447 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1448 try {
1449 iconResource = (Intent.ShortcutIconResource) extra;
1450 final PackageManager packageManager = context.getPackageManager();
1451 Resources resources = packageManager.getResourcesForApplication(
1452 iconResource.packageName);
1453 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1454 icon = resources.getDrawable(id);
1455 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001456 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 }
1458 }
1459
1460 if (icon == null) {
1461 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1462 }
1463
1464 final LiveFolderInfo info = new LiveFolderInfo();
1465 info.icon = icon;
1466 info.filtered = filtered;
1467 info.title = name;
1468 info.iconResource = iconResource;
1469 info.uri = data.getData();
1470 info.baseIntent = baseIntent;
1471 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1472 LiveFolders.DISPLAY_MODE_GRID);
1473
1474 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1475 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1476 sModel.addFolder(info);
1477
1478 return info;
1479 }
1480
1481 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1482 final int[] xy = new int[2];
1483 if (findSlot(cellInfo, xy, 1, 1)) {
1484 cellInfo.cellX = xy[0];
1485 cellInfo.cellY = xy[1];
1486 return true;
1487 }
1488 return false;
1489 }
1490
1491 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1492 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1493 boolean[] occupied = mSavedState != null ?
1494 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1495 cellInfo = mWorkspace.findAllVacantCells(occupied);
1496 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1497 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1498 return false;
1499 }
1500 }
1501 return true;
1502 }
1503
1504 private void showNotifications() {
1505 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1506 if (statusBar != null) {
1507 statusBar.expand();
1508 }
1509 }
1510
1511 private void startWallpaper() {
1512 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1513 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1514 }
1515
1516 /**
1517 * Registers various intent receivers. The current implementation registers
1518 * only a wallpaper intent receiver to let other applications change the
1519 * wallpaper.
1520 */
1521 private void registerIntentReceivers() {
1522 if (sWallpaperReceiver == null) {
1523 final Application application = getApplication();
1524
1525 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1526
1527 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1528 application.registerReceiver(sWallpaperReceiver, filter);
1529 } else {
1530 sWallpaperReceiver.setLauncher(this);
1531 }
1532
1533 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1534 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1535 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1536 filter.addDataScheme("package");
1537 registerReceiver(mApplicationsReceiver, filter);
1538 }
1539
1540 /**
1541 * Registers various content observers. The current implementation registers
1542 * only a favorites observer to keep track of the favorites applications.
1543 */
1544 private void registerContentObservers() {
1545 ContentResolver resolver = getContentResolver();
1546 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1547 }
1548
1549 @Override
1550 public boolean dispatchKeyEvent(KeyEvent event) {
1551 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1552 switch (event.getKeyCode()) {
1553 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001554 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001555 if (mDrawer.isOpened()) {
1556 closeDrawer();
1557 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001558 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 return true;
1561 case KeyEvent.KEYCODE_HOME:
1562 return true;
1563 }
1564 }
1565
1566 return super.dispatchKeyEvent(event);
1567 }
1568
1569 private void closeDrawer() {
1570 closeDrawer(true);
1571 }
1572
1573 private void closeDrawer(boolean animated) {
1574 if (mDrawer.isOpened()) {
1575 if (animated) {
1576 mDrawer.animateClose();
1577 } else {
1578 mDrawer.close();
1579 }
1580 if (mDrawer.hasFocus()) {
1581 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1582 }
1583 }
1584 }
1585
1586 private void closeFolder() {
1587 Folder folder = mWorkspace.getOpenFolder();
1588 if (folder != null) {
1589 closeFolder(folder);
1590 }
1591 }
1592
1593 void closeFolder(Folder folder) {
1594 folder.getInfo().opened = false;
1595 ViewGroup parent = (ViewGroup) folder.getParent();
1596 if (parent != null) {
1597 parent.removeView(folder);
1598 }
1599 folder.onClose();
1600 }
1601
1602 /**
1603 * When the notification that favorites have changed is received, requests
1604 * a favorites list refresh.
1605 */
1606 private void onFavoritesChanged() {
1607 mDesktopLocked = true;
1608 mDrawer.lock();
1609 sModel.loadUserItems(false, this, false, false);
1610 }
1611
1612 void onDesktopItemsLoaded() {
1613 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 bindDesktopItems();
1615 }
Romain Guycbb89e42009-06-08 15:52:54 -07001616
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 /**
1618 * Refreshes the shortcuts shown on the workspace.
1619 */
1620 private void bindDesktopItems() {
1621 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001622 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001623 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1624 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 return;
1626 }
1627
1628 final Workspace workspace = mWorkspace;
1629 int count = workspace.getChildCount();
1630 for (int i = 0; i < count; i++) {
1631 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1632 }
Romain Guycbb89e42009-06-08 15:52:54 -07001633
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 if (DEBUG_USER_INTERFACE) {
1635 android.widget.Button finishButton = new android.widget.Button(this);
1636 finishButton.setText("Finish");
1637 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1638
1639 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1640 public void onClick(View v) {
1641 finish();
1642 }
1643 });
1644 }
Romain Guycbb89e42009-06-08 15:52:54 -07001645
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001646 // Flag any old binder to terminate early
1647 if (mBinder != null) {
1648 mBinder.mTerminate = true;
1649 }
Romain Guycbb89e42009-06-08 15:52:54 -07001650
Karl Rosaen138a0412009-04-23 19:00:21 -07001651 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001652 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 }
1654
1655 private void bindItems(Launcher.DesktopBinder binder,
1656 ArrayList<ItemInfo> shortcuts, int start, int count) {
1657
1658 final Workspace workspace = mWorkspace;
1659 final boolean desktopLocked = mDesktopLocked;
1660
1661 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1662 int i = start;
1663
1664 for ( ; i < end; i++) {
1665 final ItemInfo item = shortcuts.get(i);
1666 switch (item.itemType) {
1667 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1668 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1669 final View shortcut = createShortcut((ApplicationInfo) item);
1670 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1671 !desktopLocked);
1672 break;
1673 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1674 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1675 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1676 (UserFolderInfo) item);
1677 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1678 !desktopLocked);
1679 break;
1680 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1681 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1682 R.layout.live_folder_icon, this,
1683 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1684 (LiveFolderInfo) item);
1685 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1686 !desktopLocked);
1687 break;
1688 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1689 final int screen = workspace.getCurrentScreen();
1690 final View view = mInflater.inflate(R.layout.widget_search,
1691 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001692
Karl Rosaen138a0412009-04-23 19:00:21 -07001693 Search search = (Search) view.findViewById(R.id.widget_search);
1694 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 final Widget widget = (Widget) item;
1697 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001698
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 workspace.addWidget(view, widget, !desktopLocked);
1700 break;
1701 }
1702 }
1703
1704 workspace.requestLayout();
1705
1706 if (end >= count) {
1707 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001708 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 } else {
1710 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1711 }
1712 }
1713
1714 private void finishBindDesktopItems() {
1715 if (mSavedState != null) {
1716 if (!mWorkspace.hasFocus()) {
1717 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1718 }
1719
1720 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1721 if (userFolders != null) {
1722 for (long folderId : userFolders) {
1723 final FolderInfo info = sModel.findFolderById(folderId);
1724 if (info != null) {
1725 openFolder(info);
1726 }
1727 }
1728 final Folder openFolder = mWorkspace.getOpenFolder();
1729 if (openFolder != null) {
1730 openFolder.requestFocus();
1731 }
1732 }
1733
1734 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1735 if (allApps) {
1736 mDrawer.open();
1737 }
1738
1739 mSavedState = null;
1740 }
1741
1742 if (mSavedInstanceState != null) {
1743 super.onRestoreInstanceState(mSavedInstanceState);
1744 mSavedInstanceState = null;
1745 }
1746
1747 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1748 mDrawer.requestFocus();
1749 }
1750
1751 mDesktopLocked = false;
1752 mDrawer.unlock();
1753 }
Romain Guycbb89e42009-06-08 15:52:54 -07001754
Karl Rosaen138a0412009-04-23 19:00:21 -07001755 private void bindDrawer(Launcher.DesktopBinder binder,
1756 ApplicationsAdapter drawerAdapter) {
1757 mAllAppsGrid.setAdapter(drawerAdapter);
1758 binder.startBindingAppWidgetsWhenIdle();
1759 }
Romain Guycbb89e42009-06-08 15:52:54 -07001760
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001761 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001762 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001763
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 final Workspace workspace = mWorkspace;
1765 final boolean desktopLocked = mDesktopLocked;
1766
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001767 if (!appWidgets.isEmpty()) {
1768 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001769
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001770 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001771 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001772 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001773
Karl Rosaen138a0412009-04-23 19:00:21 -07001774 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001775
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001776 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1777 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001778
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001779 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1780 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001781
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001782 workspace.requestLayout();
1783 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001785 if (appWidgets.isEmpty()) {
1786 if (PROFILE_ROTATE) {
1787 android.os.Debug.stopMethodTracing();
1788 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001790 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 }
1792 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001793
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 DragController getDragController() {
1795 return mDragLayer;
1796 }
1797
1798 /**
1799 * Launches the intent referred by the clicked shortcut.
1800 *
1801 * @param v The view representing the clicked shortcut.
1802 */
1803 public void onClick(View v) {
1804 Object tag = v.getTag();
1805 if (tag instanceof ApplicationInfo) {
1806 // Open shortcut
1807 final Intent intent = ((ApplicationInfo) tag).intent;
1808 startActivitySafely(intent);
1809 } else if (tag instanceof FolderInfo) {
1810 handleFolderClick((FolderInfo) tag);
1811 }
1812 }
1813
1814 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001815 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1817 try {
1818 startActivity(intent);
1819 } catch (ActivityNotFoundException e) {
1820 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1821 } catch (SecurityException e) {
1822 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001823 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1825 "or use the exported attribute for this activity.", e);
1826 }
1827 }
1828
1829 private void handleFolderClick(FolderInfo folderInfo) {
1830 if (!folderInfo.opened) {
1831 // Close any open folder
1832 closeFolder();
1833 // Open the requested folder
1834 openFolder(folderInfo);
1835 } else {
1836 // Find the open folder...
1837 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1838 int folderScreen;
1839 if (openFolder != null) {
1840 folderScreen = mWorkspace.getScreenForView(openFolder);
1841 // .. and close it
1842 closeFolder(openFolder);
1843 if (folderScreen != mWorkspace.getCurrentScreen()) {
1844 // Close any folder open on the current screen
1845 closeFolder();
1846 // Pull the folder onto this screen
1847 openFolder(folderInfo);
1848 }
1849 }
1850 }
1851 }
1852
1853 private void loadWallpaper() {
1854 // The first time the application is started, we load the wallpaper from
1855 // the ApplicationContext
1856 if (sWallpaper == null) {
1857 final Drawable drawable = getWallpaper();
1858 if (drawable instanceof BitmapDrawable) {
1859 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1860 } else {
1861 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1862 }
1863 }
1864 mWorkspace.loadWallpaper(sWallpaper);
1865 }
1866
1867 /**
1868 * Opens the user fodler described by the specified tag. The opening of the folder
1869 * is animated relative to the specified View. If the View is null, no animation
1870 * is played.
1871 *
1872 * @param folderInfo The FolderInfo describing the folder to open.
1873 */
1874 private void openFolder(FolderInfo folderInfo) {
1875 Folder openFolder;
1876
1877 if (folderInfo instanceof UserFolderInfo) {
1878 openFolder = UserFolder.fromXml(this);
1879 } else if (folderInfo instanceof LiveFolderInfo) {
1880 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1881 } else {
1882 return;
1883 }
1884
1885 openFolder.setDragger(mDragLayer);
1886 openFolder.setLauncher(this);
1887
1888 openFolder.bind(folderInfo);
1889 folderInfo.opened = true;
1890
1891 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1892 openFolder.onOpen();
1893 }
1894
1895 /**
1896 * Returns true if the workspace is being loaded. When the workspace is loading,
1897 * no user interaction should be allowed to avoid any conflict.
1898 *
1899 * @return True if the workspace is locked, false otherwise.
1900 */
1901 boolean isWorkspaceLocked() {
1902 return mDesktopLocked;
1903 }
1904
1905 public boolean onLongClick(View v) {
1906 if (mDesktopLocked) {
1907 return false;
1908 }
1909
1910 if (!(v instanceof CellLayout)) {
1911 v = (View) v.getParent();
1912 }
1913
1914 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1915
1916 // This happens when long clicking an item with the dpad/trackball
1917 if (cellInfo == null) {
1918 return true;
1919 }
1920
1921 if (mWorkspace.allowLongPress()) {
1922 if (cellInfo.cell == null) {
1923 if (cellInfo.valid) {
1924 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001925 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 showAddDialog(cellInfo);
1927 }
1928 } else {
1929 if (!(cellInfo.cell instanceof Folder)) {
1930 // User long pressed on an item
1931 mWorkspace.startDrag(cellInfo);
1932 }
1933 }
1934 }
1935 return true;
1936 }
1937
1938 static LauncherModel getModel() {
1939 return sModel;
1940 }
1941
Romain Guy73b979d2009-06-09 12:57:21 -07001942 static GestureLibrary getGestureLibrary() {
1943 return sLibrary;
1944 }
1945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 void closeAllApplications() {
1947 mDrawer.close();
1948 }
1949
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001950 View getDrawerHandle() {
1951 return mHandleView;
1952 }
1953
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 boolean isDrawerDown() {
1955 return !mDrawer.isMoving() && !mDrawer.isOpened();
1956 }
1957
1958 boolean isDrawerUp() {
1959 return mDrawer.isOpened() && !mDrawer.isMoving();
1960 }
1961
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001962 boolean isDrawerMoving() {
1963 return mDrawer.isMoving();
1964 }
1965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 Workspace getWorkspace() {
1967 return mWorkspace;
1968 }
1969
1970 GridView getApplicationsGrid() {
1971 return mAllAppsGrid;
1972 }
1973
1974 @Override
1975 protected Dialog onCreateDialog(int id) {
1976 switch (id) {
1977 case DIALOG_CREATE_SHORTCUT:
1978 return new CreateShortcut().createDialog();
1979 case DIALOG_RENAME_FOLDER:
1980 return new RenameFolder().createDialog();
1981 }
1982
1983 return super.onCreateDialog(id);
1984 }
1985
1986 @Override
1987 protected void onPrepareDialog(int id, Dialog dialog) {
1988 switch (id) {
1989 case DIALOG_CREATE_SHORTCUT:
1990 mWorkspace.lock();
1991 break;
1992 case DIALOG_RENAME_FOLDER:
1993 mWorkspace.lock();
1994 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1995 final CharSequence text = mFolderInfo.title;
1996 input.setText(text);
Romain Guycbb89e42009-06-08 15:52:54 -07001997 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 break;
1999 }
2000 }
2001
2002 void showRenameDialog(FolderInfo info) {
2003 mFolderInfo = info;
2004 mWaitingForResult = true;
2005 showDialog(DIALOG_RENAME_FOLDER);
2006 }
2007
2008 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2009 mAddItemCellInfo = cellInfo;
2010 mWaitingForResult = true;
2011 showDialog(DIALOG_CREATE_SHORTCUT);
2012 }
2013
Romain Guy73b979d2009-06-09 12:57:21 -07002014 private void pickShortcut(int requestCode, int title) {
2015 Bundle bundle = new Bundle();
2016
2017 ArrayList<String> shortcutNames = new ArrayList<String>();
2018 shortcutNames.add(getString(R.string.group_applications));
2019 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2020
2021 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2022 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2023 R.drawable.ic_launcher_application));
2024 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2025
2026 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2027 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
2028 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
2029 pickIntent.putExtras(bundle);
2030
2031 startActivityForResult(pickIntent, requestCode);
2032 }
2033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 private class RenameFolder {
2035 private EditText mInput;
2036
2037 Dialog createDialog() {
2038 mWaitingForResult = true;
2039 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2040 mInput = (EditText) layout.findViewById(R.id.folder_name);
2041
2042 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2043 builder.setIcon(0);
2044 builder.setTitle(getString(R.string.rename_folder_title));
2045 builder.setCancelable(true);
2046 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2047 public void onCancel(DialogInterface dialog) {
2048 cleanup();
2049 }
2050 });
2051 builder.setNegativeButton(getString(R.string.cancel_action),
2052 new Dialog.OnClickListener() {
2053 public void onClick(DialogInterface dialog, int which) {
2054 cleanup();
2055 }
2056 }
2057 );
2058 builder.setPositiveButton(getString(R.string.rename_action),
2059 new Dialog.OnClickListener() {
2060 public void onClick(DialogInterface dialog, int which) {
2061 changeFolderName();
2062 }
2063 }
2064 );
2065 builder.setView(layout);
2066 return builder.create();
2067 }
2068
2069 private void changeFolderName() {
2070 final String name = mInput.getText().toString();
2071 if (!TextUtils.isEmpty(name)) {
2072 // Make sure we have the right folder info
2073 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
2074 mFolderInfo.title = name;
2075 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2076
2077 if (mDesktopLocked) {
2078 mDrawer.lock();
2079 sModel.loadUserItems(false, Launcher.this, false, false);
2080 } else {
2081 final FolderIcon folderIcon = (FolderIcon)
2082 mWorkspace.getViewForTag(mFolderInfo);
2083 if (folderIcon != null) {
2084 folderIcon.setText(name);
2085 getWorkspace().requestLayout();
2086 } else {
2087 mDesktopLocked = true;
2088 mDrawer.lock();
2089 sModel.loadUserItems(false, Launcher.this, false, false);
2090 }
2091 }
2092 }
2093 cleanup();
2094 }
2095
2096 private void cleanup() {
2097 mWorkspace.unlock();
2098 dismissDialog(DIALOG_RENAME_FOLDER);
2099 mWaitingForResult = false;
2100 mFolderInfo = null;
2101 }
2102 }
2103
2104 /**
2105 * Displays the shortcut creation dialog and launches, if necessary, the
2106 * appropriate activity.
2107 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002108 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07002109 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002111
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 Dialog createDialog() {
2113 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2118 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002119 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 builder.setInverseBackgroundForced(true);
2122
2123 AlertDialog dialog = builder.create();
2124 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002125 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127 return dialog;
2128 }
2129
2130 public void onCancel(DialogInterface dialog) {
2131 mWaitingForResult = false;
2132 cleanup();
2133 }
2134
Romain Guycbb89e42009-06-08 15:52:54 -07002135 public void onDismiss(DialogInterface dialog) {
2136 mWorkspace.unlock();
2137 }
2138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 private void cleanup() {
2140 mWorkspace.unlock();
2141 dismissDialog(DIALOG_CREATE_SHORTCUT);
2142 }
2143
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002144 /**
2145 * Handle the action clicked in the "Add to home" dialog.
2146 */
2147 public void onClick(DialogInterface dialog, int which) {
2148 Resources res = getResources();
2149 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002150
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002151 switch (which) {
2152 case AddAdapter.ITEM_SHORTCUT: {
2153 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002154 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002155 break;
2156 }
Romain Guycbb89e42009-06-08 15:52:54 -07002157
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002158 case AddAdapter.ITEM_APPWIDGET: {
2159 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002160
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002161 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2162 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2163 // add the search widget
2164 ArrayList<AppWidgetProviderInfo> customInfo =
2165 new ArrayList<AppWidgetProviderInfo>();
2166 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2167 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2168 info.label = getString(R.string.group_search);
2169 info.icon = R.drawable.ic_search_widget;
2170 customInfo.add(info);
2171 pickIntent.putParcelableArrayListExtra(
2172 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2173 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2174 Bundle b = new Bundle();
2175 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2176 customExtras.add(b);
2177 pickIntent.putParcelableArrayListExtra(
2178 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2179 // start the pick activity
2180 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2181 break;
2182 }
Romain Guycbb89e42009-06-08 15:52:54 -07002183
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002184 case AddAdapter.ITEM_LIVE_FOLDER: {
2185 // Insert extra item to handle inserting folder
2186 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002187
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002188 ArrayList<String> shortcutNames = new ArrayList<String>();
2189 shortcutNames.add(res.getString(R.string.group_folder));
2190 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002191
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002192 ArrayList<ShortcutIconResource> shortcutIcons =
2193 new ArrayList<ShortcutIconResource>();
2194 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2195 R.drawable.ic_launcher_folder));
2196 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2197
2198 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2199 pickIntent.putExtra(Intent.EXTRA_INTENT,
2200 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2201 pickIntent.putExtra(Intent.EXTRA_TITLE,
2202 getText(R.string.title_select_live_folder));
2203 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002204
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002205 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2206 break;
2207 }
2208
2209 case AddAdapter.ITEM_WALLPAPER: {
2210 startWallpaper();
2211 break;
2212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 }
2214 }
2215 }
2216
2217 /**
2218 * Receives notifications when applications are added/removed.
2219 */
2220 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2221 @Override
2222 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002223 final String action = intent.getAction();
2224 final String packageName = intent.getData().getSchemeSpecificPart();
2225 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2226
2227 if (LauncherModel.DEBUG_LOADERS) {
2228 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2229 ", replacing=" + replacing);
2230 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002232
2233 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2234 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2235 if (!replacing) {
2236 removeShortcutsForPackage(packageName);
2237 if (LauncherModel.DEBUG_LOADERS) {
2238 d(LauncherModel.LOG_TAG, " --> remove package");
2239 }
2240 sModel.removePackage(Launcher.this, packageName);
2241 }
2242 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2243 // later, we will update the package at this time
2244 } else {
2245 if (!replacing) {
2246 if (LauncherModel.DEBUG_LOADERS) {
2247 d(LauncherModel.LOG_TAG, " --> add package");
2248 }
2249 sModel.addPackage(Launcher.this, packageName);
2250 } else {
2251 if (LauncherModel.DEBUG_LOADERS) {
2252 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2253 }
2254 sModel.updatePackage(Launcher.this, packageName);
2255 updateShortcutsForPackage(packageName);
2256 }
2257 }
2258 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002260 if (LauncherModel.DEBUG_LOADERS) {
2261 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2262 }
2263 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264 }
2265 }
2266 }
2267
2268 /**
2269 * Receives notifications whenever the user favorites have changed.
2270 */
2271 private class FavoritesChangeObserver extends ContentObserver {
2272 public FavoritesChangeObserver() {
2273 super(new Handler());
2274 }
2275
2276 @Override
2277 public void onChange(boolean selfChange) {
2278 onFavoritesChanged();
2279 }
2280 }
2281
2282 /**
2283 * Receives intents from other applications to change the wallpaper.
2284 */
2285 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2286 private final Application mApplication;
2287 private WeakReference<Launcher> mLauncher;
2288
2289 WallpaperIntentReceiver(Application application, Launcher launcher) {
2290 mApplication = application;
2291 setLauncher(launcher);
2292 }
2293
2294 void setLauncher(Launcher launcher) {
2295 mLauncher = new WeakReference<Launcher>(launcher);
2296 }
2297
2298 @Override
2299 public void onReceive(Context context, Intent intent) {
2300 // Load the wallpaper from the ApplicationContext and store it locally
2301 // until the Launcher Activity is ready to use it
2302 final Drawable drawable = mApplication.getWallpaper();
2303 if (drawable instanceof BitmapDrawable) {
2304 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2305 } else {
2306 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2307 }
2308
2309 // If Launcher is alive, notify we have a new wallpaper
2310 if (mLauncher != null) {
2311 final Launcher launcher = mLauncher.get();
2312 if (launcher != null) {
2313 launcher.loadWallpaper();
2314 }
2315 }
2316 }
2317 }
2318
2319 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2320 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2321 private boolean mOpen;
2322
2323 public void onDrawerOpened() {
2324 if (!mOpen) {
2325 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002328 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002329
2330 mOpen = true;
2331 }
2332 }
2333
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002334 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2335 view.getDrawingRect(bounds);
2336
2337 while (view != mDragLayer) {
2338 bounds.offset(view.getLeft(), view.getTop());
2339 view = (View) view.getParent();
2340 }
2341 }
2342
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002343 public void onDrawerClosed() {
2344 if (mOpen) {
2345 mHandleIcon.reverseTransition(150);
2346 mWorkspace.mDrawerBounds.setEmpty();
2347 mOpen = false;
2348 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 mAllAppsGrid.setSelection(0);
2351 mAllAppsGrid.clearTextFilter();
2352 }
2353
2354 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002355 if (PROFILE_DRAWER) {
2356 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2357 }
2358
2359 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2360 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361 }
2362
2363 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002364 if (PROFILE_DRAWER) {
2365 android.os.Debug.stopMethodTracing();
2366 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 }
2368 }
2369
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002370 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002372 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002373 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002374
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002375 // Number of items to bind in every pass
2376 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377
2378 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002379 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002380 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002382
Karl Rosaen138a0412009-04-23 19:00:21 -07002383 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384
2385 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002386 ArrayList<LauncherAppWidgetInfo> appWidgets,
2387 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388
2389 mLauncher = new WeakReference<Launcher>(launcher);
2390 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002391 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002392
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002393 // Sort widgets so active workspace is bound first
2394 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2395 final int size = appWidgets.size();
2396 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002397
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002398 for (int i = 0; i < size; i++) {
2399 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2400 if (appWidgetInfo.screen == currentScreen) {
2401 mAppWidgets.addFirst(appWidgetInfo);
2402 } else {
2403 mAppWidgets.addLast(appWidgetInfo);
2404 }
2405 }
2406 }
Romain Guycbb89e42009-06-08 15:52:54 -07002407
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 public void startBindingItems() {
2409 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2410 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002411
2412 public void startBindingDrawer() {
2413 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2414 }
Romain Guycbb89e42009-06-08 15:52:54 -07002415
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002416 public void startBindingAppWidgetsWhenIdle() {
2417 // Ask for notification when message queue becomes idle
2418 final MessageQueue messageQueue = Looper.myQueue();
2419 messageQueue.addIdleHandler(this);
2420 }
Romain Guycbb89e42009-06-08 15:52:54 -07002421
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002422 public boolean queueIdle() {
2423 // Queue is idle, so start binding items
2424 startBindingAppWidgets();
2425 return false;
2426 }
2427
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002428 public void startBindingAppWidgets() {
2429 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2430 }
2431
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 @Override
2433 public void handleMessage(Message msg) {
2434 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002435 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 return;
2437 }
Romain Guycbb89e42009-06-08 15:52:54 -07002438
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439 switch (msg.what) {
2440 case MESSAGE_BIND_ITEMS: {
2441 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2442 break;
2443 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002444 case MESSAGE_BIND_DRAWER: {
2445 launcher.bindDrawer(this, mDrawerAdapter);
2446 break;
2447 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002448 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002449 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 break;
2451 }
2452 }
2453 }
2454 }
Romain Guy73b979d2009-06-09 12:57:21 -07002455
2456 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2457 GestureOverlayView.OnGesturePerformedListener {
2458
2459 private final GestureMatcher mMatcher = new GestureMatcher();
2460
2461 GesturesProcessor() {
2462 // TODO: Maybe the load should happen on a background thread?
2463 sLibrary.load();
2464 }
2465
2466 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002467 //noinspection PointlessBooleanExpression,ConstantConditions
2468 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2469 overlay.removeCallbacks(mMatcher);
2470 resetGesturesNextPrompt();
2471 }
Romain Guy73b979d2009-06-09 12:57:21 -07002472
2473 mGesturesAdd.setAlpha(128);
2474 mGesturesAdd.setEnabled(false);
2475 }
2476
2477 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2478 }
2479
2480 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2481 }
2482
2483 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002484 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2485 mMatcher.gesture = overlay.getGesture();
2486 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2487 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002488 if (mGesturesAction.intent != null) {
2489 mGesturesAction.intent = null;
2490 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2491 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002492 } else {
2493 mMatcher.run();
2494 }
Romain Guy73b979d2009-06-09 12:57:21 -07002495 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002496 overlay.removeCallbacks(mMatcher);
2497
2498 mMatcher.gesture = overlay.getGesture();
2499 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2500 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002501 if (mGesturesAction.intent != null) {
2502 mGesturesAction.intent = null;
2503 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2504 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002505 } else {
2506 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2507 }
Romain Guy73b979d2009-06-09 12:57:21 -07002508 }
2509 }
2510
Romain Guy3cf604f2009-06-16 13:12:53 -07002511 void matchGesture(Gesture gesture) {
2512 matchGesture(gesture, true);
2513 }
2514
2515 void matchGesture(Gesture gesture, boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07002516 mGesturesAdd.setAlpha(255);
2517 mGesturesAdd.setEnabled(true);
2518
2519 if (gesture != null) {
2520 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2521
2522 if (DEBUG_GESTURES) {
2523 for (Prediction p : predictions) {
2524 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2525 }
2526 }
2527
2528 boolean match = false;
2529 if (predictions.size() > 0) {
2530 final Prediction prediction = predictions.get(0);
2531 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2532 match = true;
2533
2534 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2535 if (info != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002536 updatePrompt(info, animate);
Romain Guy73b979d2009-06-09 12:57:21 -07002537 }
2538 }
2539 }
2540
2541 if (!match){
Romain Guy1ce1a242009-06-23 17:34:54 -07002542 mGesturesAction.intent = null;
Romain Guy3cf604f2009-06-16 13:12:53 -07002543 if (animate) {
2544 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2545 } else {
2546 setGesturesPrompt(null, getString(R.string.gestures_unknown));
2547 }
Romain Guy73b979d2009-06-09 12:57:21 -07002548 }
2549 }
2550 }
2551
2552 private void updatePrompt(ApplicationInfo info) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002553 updatePrompt(info, true);
2554 }
2555
2556 private void updatePrompt(ApplicationInfo info, boolean animate) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002557 if (mGesturesAction.intent != null &&
Romain Guy1ce1a242009-06-23 17:34:54 -07002558 info.intent.toUri(0).equals(mGesturesAction.intent.toUri(0)) &&
Romain Guy6fefcf12009-06-11 13:07:43 -07002559 info.title.equals(((TextView) mGesturesPrompt.getCurrentView()).getText())) {
2560 return;
2561 }
Romain Guy3cf604f2009-06-16 13:12:53 -07002562
2563 if (animate) {
2564 setGesturesNextPrompt(info.icon, info.title);
2565 } else {
2566 setGesturesPrompt(info.icon, info.title);
2567 }
2568
Romain Guy73b979d2009-06-09 12:57:21 -07002569 mGesturesAction.intent = info.intent;
2570 }
2571
2572 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002573 //noinspection PointlessBooleanExpression,ConstantConditions
2574 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2575 overlay.removeCallbacks(mMatcher);
2576 }
Romain Guy73b979d2009-06-09 12:57:21 -07002577 }
2578
2579 void addGesture(String name, Gesture gesture) {
2580 sLibrary.addGesture(name, gesture);
2581 // TODO: On a background thread?
2582 sLibrary.save();
2583 }
2584
2585 void update(ApplicationInfo info, Gesture gesture) {
2586 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002587 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002588 }
2589
2590 class GestureMatcher implements Runnable {
2591 Gesture gesture;
2592
2593 public void run() {
2594 if (gesture != null) {
2595 matchGesture(gesture);
2596 }
2597 }
2598 }
2599 }
2600
2601 private class GesturesAction implements View.OnClickListener {
2602 Intent intent;
2603
2604 public void onClick(View v) {
2605 if (intent != null) {
2606 startActivitySafely(intent);
2607 }
2608 }
2609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002610}
Karl Rosaen138a0412009-04-23 19:00:21 -07002611