blob: 9a112b8aa762466c88d3e734ea262a9f63d840ce [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) {
Romain Guyf38b3d52009-07-01 16:39:46 -0700580 if (mCurrentGesture == null) {
581 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_GESTURES_PANEL_GESTURE);
582 }
583 final Gesture gesture = mCurrentGesture;
Romain Guy3cf604f2009-06-16 13:12:53 -0700584 mWorkspace.post(new Runnable() {
585 public void run() {
586 showGesturesPanel(false);
587 mGesturesProcessor.matchGesture(gesture, false);
588 mWorkspace.post(new Runnable() {
589 public void run() {
590 if (gesture != null) {
591 mGesturesOverlay.setGesture(gesture);
592 }
593 }
594 });
595 }
596 });
597 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599
600 /**
601 * Finds all the views we need and configure them properly.
602 */
603 private void setupViews() {
604 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
605 final DragLayer dragLayer = mDragLayer;
606
607 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
608 final Workspace workspace = mWorkspace;
609
610 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
611 final SlidingDrawer drawer = mDrawer;
612
613 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
614 final AllAppsGridView grid = mAllAppsGrid;
615
616 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
617
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700618 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
619 mHandleView.setLauncher(this);
620 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 mHandleIcon.setCrossFadeEnabled(true);
622
623 drawer.lock();
624 final DrawerManager drawerManager = new DrawerManager();
625 drawer.setOnDrawerOpenListener(drawerManager);
626 drawer.setOnDrawerCloseListener(drawerManager);
627 drawer.setOnDrawerScrollListener(drawerManager);
628
Karl Rosaen138a0412009-04-23 19:00:21 -0700629 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 grid.setDragger(dragLayer);
631 grid.setLauncher(this);
632
633 workspace.setOnLongClickListener(this);
634 workspace.setDragger(dragLayer);
635 workspace.setLauncher(this);
636 loadWallpaper();
637
638 deleteZone.setLauncher(this);
639 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700640 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641
642 dragLayer.setIgnoredDropTarget(grid);
643 dragLayer.setDragScoller(workspace);
644 dragLayer.setDragListener(deleteZone);
Romain Guy73b979d2009-06-09 12:57:21 -0700645
646 mGesturesPanel = mInflater.inflate(R.layout.gestures, mDragLayer, false);
647 final View gesturesPanel = mGesturesPanel;
648
649 mGesturesPrompt = (ViewSwitcher) gesturesPanel.findViewById(R.id.gestures_actions);
650 mGesturesAction = new GesturesAction();
651
652 mGesturesPrompt.getChildAt(0).setOnClickListener(mGesturesAction);
653 mGesturesPrompt.getChildAt(1).setOnClickListener(mGesturesAction);
654
655 mGesturesAdd = (ImageView) gesturesPanel.findViewById(R.id.gestures_add);
656 final ImageView gesturesAdd = mGesturesAdd;
657 gesturesAdd.setAlpha(128);
658 gesturesAdd.setEnabled(false);
659 gesturesAdd.setOnClickListener(new View.OnClickListener() {
660 public void onClick(View v) {
661 createGesture();
662 }
663 });
664
665 mGesturesOverlay = (GestureOverlayView) gesturesPanel.findViewById(R.id.gestures_overlay);
666 mGesturesProcessor = new GesturesProcessor();
667
668 final GestureOverlayView overlay = mGesturesOverlay;
Romain Guy73b979d2009-06-09 12:57:21 -0700669 overlay.addOnGestureListener(mGesturesProcessor);
Romain Guyb8734242009-06-10 11:53:57 -0700670 overlay.getGesturePaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
Romain Guy73b979d2009-06-09 12:57:21 -0700671 }
672
673 private void createGesture() {
Romain Guyf280f202009-06-26 10:31:07 -0700674 if (!mWaitingForResult) {
675 mCurrentGesture = mGesturesOverlay.getGesture();
676 mWaitingForResult = true;
677 pickShortcut(REQUEST_PICK_GESTURE_ACTION, R.string.title_select_shortcut);
678 }
Romain Guy73b979d2009-06-09 12:57:21 -0700679 }
680
681 private void completeCreateGesture(Intent data, boolean isShortcut) {
682 ApplicationInfo info;
683
684 if (isShortcut) {
685 info = infoFromShortcutIntent(this, data);
686 } else {
687 info = infoFromApplicationIntent(this, data);
688 }
689
690 boolean success = false;
691 if (info != null) {
692 info.isGesture = true;
693
694 if (LauncherModel.addGestureToDatabase(this, info, false)) {
695 mGesturesProcessor.addGesture(String.valueOf(info.id), mCurrentGesture);
696 mGesturesProcessor.update(info, mCurrentGesture);
697 Toast.makeText(this, getString(R.string.gestures_created, info.title),
698 Toast.LENGTH_SHORT).show();
699 success = true;
700 }
701 }
702
703 if (!success) {
704 Toast.makeText(this, getString(R.string.gestures_failed), Toast.LENGTH_SHORT).show();
705 }
706
707 mCurrentGesture = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 }
709
710 /**
711 * Creates a view representing a shortcut.
712 *
713 * @param info The data structure describing the shortcut.
714 *
715 * @return A View inflated from R.layout.application.
716 */
717 View createShortcut(ApplicationInfo info) {
718 return createShortcut(R.layout.application,
719 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
720 }
721
722 /**
723 * Creates a view representing a shortcut inflated from the specified resource.
724 *
725 * @param layoutResId The id of the XML layout used to create the shortcut.
726 * @param parent The group the shortcut belongs to.
727 * @param info The data structure describing the shortcut.
728 *
729 * @return A View inflated from layoutResId.
730 */
731 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
732 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
733
734 if (!info.filtered) {
735 info.icon = Utilities.createIconThumbnail(info.icon, this);
736 info.filtered = true;
737 }
738
739 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
740 favorite.setText(info.title);
741 favorite.setTag(info);
742 favorite.setOnClickListener(this);
743
744 return favorite;
745 }
746
747 /**
748 * Add an application shortcut to the workspace.
749 *
750 * @param data The intent describing the application.
751 * @param cellInfo The position on screen where to create the shortcut.
752 */
753 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
754 boolean insertAtFirst) {
755 cellInfo.screen = mWorkspace.getCurrentScreen();
756 if (!findSingleSlot(cellInfo)) return;
757
Romain Guy73b979d2009-06-09 12:57:21 -0700758 final ApplicationInfo info = infoFromApplicationIntent(context, data);
759 if (info != null) {
760 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
761 }
762 }
763
764 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 ComponentName component = data.getComponent();
766 PackageManager packageManager = context.getPackageManager();
767 ActivityInfo activityInfo = null;
768 try {
769 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
770 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700771 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 }
Romain Guycbb89e42009-06-08 15:52:54 -0700773
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 if (activityInfo != null) {
775 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700776
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 itemInfo.title = activityInfo.loadLabel(packageManager);
778 if (itemInfo.title == null) {
779 itemInfo.title = activityInfo.name;
780 }
Romain Guycbb89e42009-06-08 15:52:54 -0700781
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
783 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
784 itemInfo.icon = activityInfo.loadIcon(packageManager);
785 itemInfo.container = ItemInfo.NO_ID;
786
Romain Guy73b979d2009-06-09 12:57:21 -0700787 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 }
Romain Guy73b979d2009-06-09 12:57:21 -0700789
790 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 }
Romain Guycbb89e42009-06-08 15:52:54 -0700792
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 /**
794 * Add a shortcut to the workspace.
795 *
796 * @param data The intent describing the shortcut.
797 * @param cellInfo The position on screen where to create the shortcut.
798 * @param insertAtFirst
799 */
800 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
801 boolean insertAtFirst) {
802 cellInfo.screen = mWorkspace.getCurrentScreen();
803 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700804
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
806
807 if (!mRestoring) {
808 sModel.addDesktopItem(info);
809
810 final View view = createShortcut(info);
811 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
812 } else if (sModel.isDesktopLoaded()) {
813 sModel.addDesktopItem(info);
814 }
815 }
816
Romain Guycbb89e42009-06-08 15:52:54 -0700817
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700819 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700821 * @param data The intent describing the appWidgetId.
822 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700824 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 boolean insertAtFirst) {
826
827 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700828 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700829
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700830 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700831
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700832 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700833
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700834 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700836 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700837
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 // Try finding open space on Launcher screen
839 final int[] xy = mCellCoordinates;
840 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
841
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700842 // Build Launcher-specific widget info and save to database
843 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 launcherInfo.spanX = spans[0];
845 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 LauncherModel.addItemToDatabase(this, launcherInfo,
848 LauncherSettings.Favorites.CONTAINER_DESKTOP,
849 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
850
851 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700852 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700853
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700855 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700856
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700857 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
861 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
862 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700863 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
865 }
Romain Guycbb89e42009-06-08 15:52:54 -0700866
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700867 public LauncherAppWidgetHost getAppWidgetHost() {
868 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 }
Romain Guycbb89e42009-06-08 15:52:54 -0700870
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 static ApplicationInfo addShortcut(Context context, Intent data,
872 CellLayout.CellInfo cellInfo, boolean notify) {
873
Romain Guy73b979d2009-06-09 12:57:21 -0700874 final ApplicationInfo info = infoFromShortcutIntent(context, data);
875 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
876 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
877
878 return info;
879 }
880
881 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
883 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
884 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
885
886 Drawable icon = null;
887 boolean filtered = false;
888 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700889 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890
891 if (bitmap != null) {
892 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
893 filtered = true;
894 customIcon = true;
895 } else {
896 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700897 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700899 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 final PackageManager packageManager = context.getPackageManager();
901 Resources resources = packageManager.getResourcesForApplication(
902 iconResource.packageName);
903 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
904 icon = resources.getDrawable(id);
905 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700906 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 }
908 }
909 }
910
911 if (icon == null) {
912 icon = context.getPackageManager().getDefaultActivityIcon();
913 }
914
915 final ApplicationInfo info = new ApplicationInfo();
916 info.icon = icon;
917 info.filtered = filtered;
918 info.title = name;
919 info.intent = intent;
920 info.customIcon = customIcon;
921 info.iconResource = iconResource;
922
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 return info;
924 }
925
926 @Override
927 protected void onNewIntent(Intent intent) {
928 super.onNewIntent(intent);
929
930 // Close the menu
931 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
932 getWindow().closeAllPanels();
933
934 try {
935 dismissDialog(DIALOG_CREATE_SHORTCUT);
936 // Unlock the workspace if the dialog was showing
937 mWorkspace.unlock();
938 } catch (Exception e) {
939 // An exception is thrown if the dialog is not visible, which is fine
940 }
941
942 try {
943 dismissDialog(DIALOG_RENAME_FOLDER);
944 // Unlock the workspace if the dialog was showing
945 mWorkspace.unlock();
946 } catch (Exception e) {
947 // An exception is thrown if the dialog is not visible, which is fine
948 }
949
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
951 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700952
Romain Guye3895ae2009-06-16 13:25:29 -0700953 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE &&
Romain Guy94406842009-06-17 16:18:58 -0700954 (mDragLayer.hasWindowFocus() ||
955 (mGesturesWindow != null && mGesturesWindow.isShowing()))) {
956
Romain Guyb8734242009-06-10 11:53:57 -0700957 SearchManager searchManager =
958 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Romain Guy94406842009-06-17 16:18:58 -0700959
Romain Guyb8734242009-06-10 11:53:57 -0700960 if (!searchManager.isVisible()) {
961 onHomeKeyPressed();
962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 }
964 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700965
966 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 if (v != null && v.getWindowToken() != null) {
968 InputMethodManager imm = (InputMethodManager)getSystemService(
969 INPUT_METHOD_SERVICE);
970 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
971 }
972 } else {
973 closeDrawer(false);
974 }
975 }
976 }
977
Romain Guy73b979d2009-06-09 12:57:21 -0700978 private void onHomeKeyPressed() {
979 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
980 showGesturesPanel();
981 } else {
982 hideGesturesPanel();
983 }
984 }
985
986 private void showGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -0700987 showGesturesPanel(true);
988 }
989
990 private void showGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -0700991 resetGesturesPrompt();
992
993 mGesturesAdd.setEnabled(false);
994 mGesturesAdd.setAlpha(128);
995
996 mGesturesOverlay.clear(false);
997
998 PopupWindow window;
999 if (mGesturesWindow == null) {
1000 mGesturesWindow = new PopupWindow(this);
1001 window = mGesturesWindow;
1002 window.setFocusable(true);
1003 window.setTouchable(true);
1004 window.setBackgroundDrawable(null);
1005 window.setContentView(mGesturesPanel);
1006 } else {
1007 window = mGesturesWindow;
1008 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001009 window.setAnimationStyle(animate ? com.android.internal.R.style.Animation_SlidingCard : 0);
Romain Guy73b979d2009-06-09 12:57:21 -07001010
1011 final int[] xy = new int[2];
1012 final DragLayer dragLayer = mDragLayer;
1013 dragLayer.getLocationOnScreen(xy);
1014
1015 window.setWidth(dragLayer.getWidth());
1016 window.setHeight(dragLayer.getHeight() - 1);
1017 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
1018 }
1019
1020 private void resetGesturesPrompt() {
1021 mGesturesAction.intent = null;
1022 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
1023 prompt.setText(R.string.gestures_instructions);
1024 prompt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
1025 prompt.setClickable(false);
1026 }
1027
1028 private void resetGesturesNextPrompt() {
1029 mGesturesAction.intent = null;
1030 setGesturesNextPrompt(null, getString(R.string.gestures_instructions));
1031 mGesturesPrompt.getNextView().setClickable(false);
1032 }
1033
1034 private void setGesturesNextPrompt(Drawable icon, CharSequence title) {
1035 final TextView prompt = (TextView) mGesturesPrompt.getNextView();
1036 prompt.setText(title);
1037 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
1038 prompt.setClickable(true);
1039 mGesturesPrompt.showNext();
1040 }
1041
Romain Guy3cf604f2009-06-16 13:12:53 -07001042 private void setGesturesPrompt(Drawable icon, CharSequence title) {
1043 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
1044 prompt.setText(title);
1045 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
1046 prompt.setClickable(true);
1047 }
1048
Romain Guy73b979d2009-06-09 12:57:21 -07001049 void hideGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -07001050 hideGesturesPanel(true);
1051 }
1052
1053 void hideGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07001054 if (mGesturesWindow != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001055 final PopupWindow popupWindow = mGesturesWindow;
1056 popupWindow.setAnimationStyle(animate ?
1057 com.android.internal.R.style.Animation_SlidingCard : 0);
1058 popupWindow.update();
1059 popupWindow.dismiss();
Romain Guy73b979d2009-06-09 12:57:21 -07001060 }
1061 }
1062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 @Override
1064 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1065 // Do not call super here
1066 mSavedInstanceState = savedInstanceState;
1067 }
1068
1069 @Override
1070 protected void onSaveInstanceState(Bundle outState) {
1071 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1072
1073 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1074 if (folders.size() > 0) {
1075 final int count = folders.size();
1076 long[] ids = new long[count];
1077 for (int i = 0; i < count; i++) {
1078 final FolderInfo info = folders.get(i).getInfo();
1079 ids[i] = info.id;
1080 }
1081 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1082 } else {
1083 super.onSaveInstanceState(outState);
1084 }
1085
Romain Guy3cf604f2009-06-16 13:12:53 -07001086 final boolean isConfigurationChange = getChangingConfigurations() != 0;
1087
Romain Guy5a941392009-04-28 15:18:25 -07001088 // When the drawer is opened and we are saving the state because of a
1089 // configuration change
Romain Guy3cf604f2009-06-16 13:12:53 -07001090 if (mDrawer.isOpened() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001092 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093
1094 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1095 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1096 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1097
1098 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1099 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1100 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1101 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1102 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1103 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1104 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1105 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1106 layout.getOccupiedCells());
1107 }
1108
1109 if (mFolderInfo != null && mWaitingForResult) {
1110 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1111 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1112 }
Romain Guy73b979d2009-06-09 12:57:21 -07001113
1114 if (mCurrentGesture != null && mWaitingForResult) {
1115 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1116 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001117
Romain Guy1ce1a242009-06-23 17:34:54 -07001118 if (mGesturesWindow != null && mGesturesWindow.isShowing()) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001119 outState.putBoolean(RUNTIME_STATE_GESTURES_PANEL, true);
1120
Romain Guy1ce1a242009-06-23 17:34:54 -07001121 if (mCurrentGesture == null || !mWaitingForResult) {
1122 final Gesture gesture = mGesturesOverlay.getGesture();
1123 if (gesture != null) {
1124 outState.putParcelable(RUNTIME_STATE_GESTURES_PANEL_GESTURE, gesture);
1125 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001126 }
1127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 }
1129
1130 @Override
1131 public void onDestroy() {
1132 mDestroyed = true;
1133
1134 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001137 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001139 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
1141
1142 TextKeyListener.getInstance().release();
1143
Romain Guy3cf604f2009-06-16 13:12:53 -07001144 hideGesturesPanel(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 mAllAppsGrid.clearTextFilter();
1146 mAllAppsGrid.setAdapter(null);
1147 sModel.unbind();
1148 sModel.abortLoaders();
1149
1150 getContentResolver().unregisterContentObserver(mObserver);
1151 unregisterReceiver(mApplicationsReceiver);
1152 }
1153
1154 @Override
1155 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001156 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 super.startActivityForResult(intent, requestCode);
1158 }
1159
1160 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001161 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001163
1164 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001165
Karl Rosaen138a0412009-04-23 19:00:21 -07001166 // Slide the search widget to the top, if it's on the current screen,
1167 // otherwise show the search dialog immediately.
1168 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1169 if (searchWidget == null) {
1170 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1171 } else {
1172 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1173 // show the currently typed text in the search widget while sliding
1174 searchWidget.setQuery(getTypedText());
1175 }
1176 }
Romain Guycbb89e42009-06-08 15:52:54 -07001177
Karl Rosaen138a0412009-04-23 19:00:21 -07001178 /**
1179 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001180 *
1181 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001182 */
Romain Guycbb89e42009-06-08 15:52:54 -07001183 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001184 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001185
Karl Rosaen138a0412009-04-23 19:00:21 -07001186 if (initialQuery == null) {
1187 // Use any text typed in the launcher as the initial query
1188 initialQuery = getTypedText();
1189 clearTypedText();
1190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 if (appSearchData == null) {
1192 appSearchData = new Bundle();
1193 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1194 }
Romain Guycbb89e42009-06-08 15:52:54 -07001195
Karl Rosaen138a0412009-04-23 19:00:21 -07001196 final SearchManager searchManager =
1197 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1198
1199 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1200 if (searchWidget != null) {
1201 // This gets called when the user leaves the search dialog to go back to
1202 // the Launcher.
1203 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1204 public void onCancel() {
1205 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001206 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001207 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001208 });
1209 }
Romain Guycbb89e42009-06-08 15:52:54 -07001210
Karl Rosaen138a0412009-04-23 19:00:21 -07001211 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001212 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 }
1214
Karl Rosaen138a0412009-04-23 19:00:21 -07001215 /**
1216 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001217 */
Romain Guy5a941392009-04-28 15:18:25 -07001218 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001219 // Close search dialog
1220 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1221 if (searchManager.isVisible()) {
1222 searchManager.stopSearch();
1223 }
1224 // Restore search widget to its normal position
1225 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1226 if (searchWidget != null) {
1227 searchWidget.stopSearch(false);
1228 }
1229 }
Romain Guycbb89e42009-06-08 15:52:54 -07001230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 @Override
1232 public boolean onCreateOptionsMenu(Menu menu) {
1233 if (mDesktopLocked) return false;
1234
1235 super.onCreateOptionsMenu(menu);
1236 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1237 .setIcon(android.R.drawable.ic_menu_add)
1238 .setAlphabeticShortcut('A');
1239 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1240 .setIcon(android.R.drawable.ic_menu_gallery)
1241 .setAlphabeticShortcut('W');
1242 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1243 .setIcon(android.R.drawable.ic_search_category_default)
1244 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1245 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1246 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1247 .setAlphabeticShortcut('N');
1248
Romain Guy73b979d2009-06-09 12:57:21 -07001249 final Intent gestures = new Intent(this, GesturesActivity.class);
1250 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1251 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1252 .setIntent(gestures);
1253
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001255 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1256 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257
1258 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1259 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1260 .setIntent(settings);
1261
1262 return true;
1263 }
1264
1265 @Override
1266 public boolean onPrepareOptionsMenu(Menu menu) {
1267 super.onPrepareOptionsMenu(menu);
1268
1269 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1270 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1271
1272 return true;
1273 }
1274
1275 @Override
1276 public boolean onOptionsItemSelected(MenuItem item) {
1277 switch (item.getItemId()) {
1278 case MENU_ADD:
1279 addItems();
1280 return true;
1281 case MENU_WALLPAPER_SETTINGS:
1282 startWallpaper();
1283 return true;
1284 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001285 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 return true;
1287 case MENU_NOTIFICATIONS:
1288 showNotifications();
1289 return true;
1290 }
1291
1292 return super.onOptionsItemSelected(item);
1293 }
Romain Guycbb89e42009-06-08 15:52:54 -07001294
Karl Rosaen138a0412009-04-23 19:00:21 -07001295 /**
1296 * Indicates that we want global search for this activity by setting the globalSearch
1297 * argument for {@link #startSearch} to true.
1298 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001300 @Override
1301 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001302 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001303 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001304 }
1305
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 private void addItems() {
1307 showAddDialog(mMenuAddInfo);
1308 }
1309
1310 private void removeShortcutsForPackage(String packageName) {
1311 if (packageName != null && packageName.length() > 0) {
1312 mWorkspace.removeShortcutsForPackage(packageName);
1313 }
1314 }
Romain Guycbb89e42009-06-08 15:52:54 -07001315
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001316 private void updateShortcutsForPackage(String packageName) {
1317 if (packageName != null && packageName.length() > 0) {
1318 mWorkspace.updateShortcutsForPackage(packageName);
1319 }
1320 }
1321
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001322 void addAppWidget(Intent data) {
1323 // TODO: catch bad widget exception when sent
1324 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001326 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1327 if (SEARCH_WIDGET.equals(customWidget)) {
1328 // We don't need this any more, since this isn't a real app widget.
1329 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1330 // add the search widget
1331 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001333 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1334
1335 if (appWidget.configure != null) {
1336 // Launch over to configure widget, if needed
1337 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1338 intent.setComponent(appWidget.configure);
1339 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1340
1341 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1342 } else {
1343 // Otherwise just add it
1344 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347 }
Romain Guycbb89e42009-06-08 15:52:54 -07001348
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 void addSearch() {
1350 final Widget info = Widget.makeSearch();
1351 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 final int[] xy = mCellCoordinates;
1354 final int spanX = info.spanX;
1355 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001356
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001358
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 sModel.addDesktopItem(info);
1360 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1361 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 final View view = mInflater.inflate(info.layoutResource, null);
1364 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001365 Search search = (Search) view.findViewById(R.id.widget_search);
1366 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1369 }
1370
Romain Guy73b979d2009-06-09 12:57:21 -07001371 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001372 // Handle case where user selected "Applications"
1373 String applicationName = getResources().getString(R.string.group_applications);
1374 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001375
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001376 if (applicationName != null && applicationName.equals(shortcutName)) {
1377 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1378 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001379
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001380 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1381 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001382 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001383 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001384 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001385 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
1387
1388 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001389 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001390 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001391 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001392
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001393 if (folderName != null && folderName.equals(shortcutName)) {
1394 addFolder(!mDesktopLocked);
1395 } else {
1396 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
1399
1400 void addFolder(boolean insertAtFirst) {
1401 UserFolderInfo folderInfo = new UserFolderInfo();
1402 folderInfo.title = getText(R.string.folder_name);
1403
1404 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1405 cellInfo.screen = mWorkspace.getCurrentScreen();
1406 if (!findSingleSlot(cellInfo)) return;
1407
1408 // Update the model
1409 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1410 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1411 sModel.addDesktopItem(folderInfo);
1412 sModel.addFolder(folderInfo);
1413
1414 // Create the view
1415 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1416 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1417 mWorkspace.addInCurrentScreen(newFolder,
1418 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1419 }
Romain Guycbb89e42009-06-08 15:52:54 -07001420
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1422 boolean insertAtFirst) {
1423 cellInfo.screen = mWorkspace.getCurrentScreen();
1424 if (!findSingleSlot(cellInfo)) return;
1425
1426 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1427
1428 if (!mRestoring) {
1429 sModel.addDesktopItem(info);
1430
1431 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1432 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1433 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1434 } else if (sModel.isDesktopLoaded()) {
1435 sModel.addDesktopItem(info);
1436 }
1437 }
1438
1439 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1440 CellLayout.CellInfo cellInfo, boolean notify) {
1441
1442 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1443 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1444
1445 Drawable icon = null;
1446 boolean filtered = false;
1447 Intent.ShortcutIconResource iconResource = null;
1448
1449 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1450 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1451 try {
1452 iconResource = (Intent.ShortcutIconResource) extra;
1453 final PackageManager packageManager = context.getPackageManager();
1454 Resources resources = packageManager.getResourcesForApplication(
1455 iconResource.packageName);
1456 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1457 icon = resources.getDrawable(id);
1458 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001459 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 }
1461 }
1462
1463 if (icon == null) {
1464 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1465 }
1466
1467 final LiveFolderInfo info = new LiveFolderInfo();
1468 info.icon = icon;
1469 info.filtered = filtered;
1470 info.title = name;
1471 info.iconResource = iconResource;
1472 info.uri = data.getData();
1473 info.baseIntent = baseIntent;
1474 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1475 LiveFolders.DISPLAY_MODE_GRID);
1476
1477 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1478 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1479 sModel.addFolder(info);
1480
1481 return info;
1482 }
1483
1484 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1485 final int[] xy = new int[2];
1486 if (findSlot(cellInfo, xy, 1, 1)) {
1487 cellInfo.cellX = xy[0];
1488 cellInfo.cellY = xy[1];
1489 return true;
1490 }
1491 return false;
1492 }
1493
1494 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1495 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1496 boolean[] occupied = mSavedState != null ?
1497 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1498 cellInfo = mWorkspace.findAllVacantCells(occupied);
1499 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1500 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1501 return false;
1502 }
1503 }
1504 return true;
1505 }
1506
1507 private void showNotifications() {
1508 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1509 if (statusBar != null) {
1510 statusBar.expand();
1511 }
1512 }
1513
1514 private void startWallpaper() {
1515 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1516 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1517 }
1518
1519 /**
1520 * Registers various intent receivers. The current implementation registers
1521 * only a wallpaper intent receiver to let other applications change the
1522 * wallpaper.
1523 */
1524 private void registerIntentReceivers() {
1525 if (sWallpaperReceiver == null) {
1526 final Application application = getApplication();
1527
1528 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1529
1530 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1531 application.registerReceiver(sWallpaperReceiver, filter);
1532 } else {
1533 sWallpaperReceiver.setLauncher(this);
1534 }
1535
1536 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1537 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1538 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1539 filter.addDataScheme("package");
1540 registerReceiver(mApplicationsReceiver, filter);
1541 }
1542
1543 /**
1544 * Registers various content observers. The current implementation registers
1545 * only a favorites observer to keep track of the favorites applications.
1546 */
1547 private void registerContentObservers() {
1548 ContentResolver resolver = getContentResolver();
1549 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1550 }
1551
1552 @Override
1553 public boolean dispatchKeyEvent(KeyEvent event) {
1554 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1555 switch (event.getKeyCode()) {
1556 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001557 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001558 if (mDrawer.isOpened()) {
1559 closeDrawer();
1560 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001561 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 return true;
1564 case KeyEvent.KEYCODE_HOME:
1565 return true;
1566 }
1567 }
1568
1569 return super.dispatchKeyEvent(event);
1570 }
1571
1572 private void closeDrawer() {
1573 closeDrawer(true);
1574 }
1575
1576 private void closeDrawer(boolean animated) {
1577 if (mDrawer.isOpened()) {
1578 if (animated) {
1579 mDrawer.animateClose();
1580 } else {
1581 mDrawer.close();
1582 }
1583 if (mDrawer.hasFocus()) {
1584 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1585 }
1586 }
1587 }
1588
1589 private void closeFolder() {
1590 Folder folder = mWorkspace.getOpenFolder();
1591 if (folder != null) {
1592 closeFolder(folder);
1593 }
1594 }
1595
1596 void closeFolder(Folder folder) {
1597 folder.getInfo().opened = false;
1598 ViewGroup parent = (ViewGroup) folder.getParent();
1599 if (parent != null) {
1600 parent.removeView(folder);
1601 }
1602 folder.onClose();
1603 }
1604
1605 /**
1606 * When the notification that favorites have changed is received, requests
1607 * a favorites list refresh.
1608 */
1609 private void onFavoritesChanged() {
1610 mDesktopLocked = true;
1611 mDrawer.lock();
1612 sModel.loadUserItems(false, this, false, false);
1613 }
1614
1615 void onDesktopItemsLoaded() {
1616 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 bindDesktopItems();
1618 }
Romain Guycbb89e42009-06-08 15:52:54 -07001619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 /**
1621 * Refreshes the shortcuts shown on the workspace.
1622 */
1623 private void bindDesktopItems() {
1624 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001625 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001626 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1627 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 return;
1629 }
1630
1631 final Workspace workspace = mWorkspace;
1632 int count = workspace.getChildCount();
1633 for (int i = 0; i < count; i++) {
1634 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1635 }
Romain Guycbb89e42009-06-08 15:52:54 -07001636
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 if (DEBUG_USER_INTERFACE) {
1638 android.widget.Button finishButton = new android.widget.Button(this);
1639 finishButton.setText("Finish");
1640 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1641
1642 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1643 public void onClick(View v) {
1644 finish();
1645 }
1646 });
1647 }
Romain Guycbb89e42009-06-08 15:52:54 -07001648
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001649 // Flag any old binder to terminate early
1650 if (mBinder != null) {
1651 mBinder.mTerminate = true;
1652 }
Romain Guycbb89e42009-06-08 15:52:54 -07001653
Karl Rosaen138a0412009-04-23 19:00:21 -07001654 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001655 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 }
1657
1658 private void bindItems(Launcher.DesktopBinder binder,
1659 ArrayList<ItemInfo> shortcuts, int start, int count) {
1660
1661 final Workspace workspace = mWorkspace;
1662 final boolean desktopLocked = mDesktopLocked;
1663
1664 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1665 int i = start;
1666
1667 for ( ; i < end; i++) {
1668 final ItemInfo item = shortcuts.get(i);
1669 switch (item.itemType) {
1670 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1671 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1672 final View shortcut = createShortcut((ApplicationInfo) item);
1673 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1674 !desktopLocked);
1675 break;
1676 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1677 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1678 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1679 (UserFolderInfo) item);
1680 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1681 !desktopLocked);
1682 break;
1683 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1684 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1685 R.layout.live_folder_icon, this,
1686 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1687 (LiveFolderInfo) item);
1688 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1689 !desktopLocked);
1690 break;
1691 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1692 final int screen = workspace.getCurrentScreen();
1693 final View view = mInflater.inflate(R.layout.widget_search,
1694 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001695
Karl Rosaen138a0412009-04-23 19:00:21 -07001696 Search search = (Search) view.findViewById(R.id.widget_search);
1697 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001698
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 final Widget widget = (Widget) item;
1700 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001701
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 workspace.addWidget(view, widget, !desktopLocked);
1703 break;
1704 }
1705 }
1706
1707 workspace.requestLayout();
1708
1709 if (end >= count) {
1710 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001711 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 } else {
1713 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1714 }
1715 }
1716
1717 private void finishBindDesktopItems() {
1718 if (mSavedState != null) {
1719 if (!mWorkspace.hasFocus()) {
1720 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1721 }
1722
1723 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1724 if (userFolders != null) {
1725 for (long folderId : userFolders) {
1726 final FolderInfo info = sModel.findFolderById(folderId);
1727 if (info != null) {
1728 openFolder(info);
1729 }
1730 }
1731 final Folder openFolder = mWorkspace.getOpenFolder();
1732 if (openFolder != null) {
1733 openFolder.requestFocus();
1734 }
1735 }
1736
1737 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1738 if (allApps) {
1739 mDrawer.open();
1740 }
1741
1742 mSavedState = null;
1743 }
1744
1745 if (mSavedInstanceState != null) {
1746 super.onRestoreInstanceState(mSavedInstanceState);
1747 mSavedInstanceState = null;
1748 }
1749
1750 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1751 mDrawer.requestFocus();
1752 }
1753
1754 mDesktopLocked = false;
1755 mDrawer.unlock();
1756 }
Romain Guycbb89e42009-06-08 15:52:54 -07001757
Karl Rosaen138a0412009-04-23 19:00:21 -07001758 private void bindDrawer(Launcher.DesktopBinder binder,
1759 ApplicationsAdapter drawerAdapter) {
1760 mAllAppsGrid.setAdapter(drawerAdapter);
1761 binder.startBindingAppWidgetsWhenIdle();
1762 }
Romain Guycbb89e42009-06-08 15:52:54 -07001763
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001764 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001765 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001766
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 final Workspace workspace = mWorkspace;
1768 final boolean desktopLocked = mDesktopLocked;
1769
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001770 if (!appWidgets.isEmpty()) {
1771 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001772
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001773 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001774 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001775 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001776
Karl Rosaen138a0412009-04-23 19:00:21 -07001777 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001778
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001779 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1780 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001781
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001782 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1783 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001784
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001785 workspace.requestLayout();
1786 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001788 if (appWidgets.isEmpty()) {
1789 if (PROFILE_ROTATE) {
1790 android.os.Debug.stopMethodTracing();
1791 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001793 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
1795 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001796
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 DragController getDragController() {
1798 return mDragLayer;
1799 }
1800
1801 /**
1802 * Launches the intent referred by the clicked shortcut.
1803 *
1804 * @param v The view representing the clicked shortcut.
1805 */
1806 public void onClick(View v) {
1807 Object tag = v.getTag();
1808 if (tag instanceof ApplicationInfo) {
1809 // Open shortcut
1810 final Intent intent = ((ApplicationInfo) tag).intent;
1811 startActivitySafely(intent);
1812 } else if (tag instanceof FolderInfo) {
1813 handleFolderClick((FolderInfo) tag);
1814 }
1815 }
1816
1817 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001818 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1820 try {
1821 startActivity(intent);
1822 } catch (ActivityNotFoundException e) {
1823 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1824 } catch (SecurityException e) {
1825 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001826 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1828 "or use the exported attribute for this activity.", e);
1829 }
1830 }
1831
1832 private void handleFolderClick(FolderInfo folderInfo) {
1833 if (!folderInfo.opened) {
1834 // Close any open folder
1835 closeFolder();
1836 // Open the requested folder
1837 openFolder(folderInfo);
1838 } else {
1839 // Find the open folder...
1840 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1841 int folderScreen;
1842 if (openFolder != null) {
1843 folderScreen = mWorkspace.getScreenForView(openFolder);
1844 // .. and close it
1845 closeFolder(openFolder);
1846 if (folderScreen != mWorkspace.getCurrentScreen()) {
1847 // Close any folder open on the current screen
1848 closeFolder();
1849 // Pull the folder onto this screen
1850 openFolder(folderInfo);
1851 }
1852 }
1853 }
1854 }
1855
1856 private void loadWallpaper() {
1857 // The first time the application is started, we load the wallpaper from
1858 // the ApplicationContext
1859 if (sWallpaper == null) {
1860 final Drawable drawable = getWallpaper();
1861 if (drawable instanceof BitmapDrawable) {
1862 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1863 } else {
1864 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1865 }
1866 }
1867 mWorkspace.loadWallpaper(sWallpaper);
1868 }
1869
1870 /**
1871 * Opens the user fodler described by the specified tag. The opening of the folder
1872 * is animated relative to the specified View. If the View is null, no animation
1873 * is played.
1874 *
1875 * @param folderInfo The FolderInfo describing the folder to open.
1876 */
1877 private void openFolder(FolderInfo folderInfo) {
1878 Folder openFolder;
1879
1880 if (folderInfo instanceof UserFolderInfo) {
1881 openFolder = UserFolder.fromXml(this);
1882 } else if (folderInfo instanceof LiveFolderInfo) {
1883 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1884 } else {
1885 return;
1886 }
1887
1888 openFolder.setDragger(mDragLayer);
1889 openFolder.setLauncher(this);
1890
1891 openFolder.bind(folderInfo);
1892 folderInfo.opened = true;
1893
1894 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1895 openFolder.onOpen();
1896 }
1897
1898 /**
1899 * Returns true if the workspace is being loaded. When the workspace is loading,
1900 * no user interaction should be allowed to avoid any conflict.
1901 *
1902 * @return True if the workspace is locked, false otherwise.
1903 */
1904 boolean isWorkspaceLocked() {
1905 return mDesktopLocked;
1906 }
1907
1908 public boolean onLongClick(View v) {
1909 if (mDesktopLocked) {
1910 return false;
1911 }
1912
1913 if (!(v instanceof CellLayout)) {
1914 v = (View) v.getParent();
1915 }
1916
1917 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1918
1919 // This happens when long clicking an item with the dpad/trackball
1920 if (cellInfo == null) {
1921 return true;
1922 }
1923
1924 if (mWorkspace.allowLongPress()) {
1925 if (cellInfo.cell == null) {
1926 if (cellInfo.valid) {
1927 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001928 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 showAddDialog(cellInfo);
1930 }
1931 } else {
1932 if (!(cellInfo.cell instanceof Folder)) {
1933 // User long pressed on an item
1934 mWorkspace.startDrag(cellInfo);
1935 }
1936 }
1937 }
1938 return true;
1939 }
1940
1941 static LauncherModel getModel() {
1942 return sModel;
1943 }
1944
Romain Guy73b979d2009-06-09 12:57:21 -07001945 static GestureLibrary getGestureLibrary() {
1946 return sLibrary;
1947 }
1948
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 void closeAllApplications() {
1950 mDrawer.close();
1951 }
1952
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001953 View getDrawerHandle() {
1954 return mHandleView;
1955 }
1956
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 boolean isDrawerDown() {
1958 return !mDrawer.isMoving() && !mDrawer.isOpened();
1959 }
1960
1961 boolean isDrawerUp() {
1962 return mDrawer.isOpened() && !mDrawer.isMoving();
1963 }
1964
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001965 boolean isDrawerMoving() {
1966 return mDrawer.isMoving();
1967 }
1968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 Workspace getWorkspace() {
1970 return mWorkspace;
1971 }
1972
1973 GridView getApplicationsGrid() {
1974 return mAllAppsGrid;
1975 }
1976
1977 @Override
1978 protected Dialog onCreateDialog(int id) {
1979 switch (id) {
1980 case DIALOG_CREATE_SHORTCUT:
1981 return new CreateShortcut().createDialog();
1982 case DIALOG_RENAME_FOLDER:
1983 return new RenameFolder().createDialog();
1984 }
1985
1986 return super.onCreateDialog(id);
1987 }
1988
1989 @Override
1990 protected void onPrepareDialog(int id, Dialog dialog) {
1991 switch (id) {
1992 case DIALOG_CREATE_SHORTCUT:
1993 mWorkspace.lock();
1994 break;
1995 case DIALOG_RENAME_FOLDER:
1996 mWorkspace.lock();
1997 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1998 final CharSequence text = mFolderInfo.title;
1999 input.setText(text);
Romain Guycbb89e42009-06-08 15:52:54 -07002000 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 break;
2002 }
2003 }
2004
2005 void showRenameDialog(FolderInfo info) {
2006 mFolderInfo = info;
2007 mWaitingForResult = true;
2008 showDialog(DIALOG_RENAME_FOLDER);
2009 }
2010
2011 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2012 mAddItemCellInfo = cellInfo;
2013 mWaitingForResult = true;
2014 showDialog(DIALOG_CREATE_SHORTCUT);
2015 }
2016
Romain Guy73b979d2009-06-09 12:57:21 -07002017 private void pickShortcut(int requestCode, int title) {
2018 Bundle bundle = new Bundle();
2019
2020 ArrayList<String> shortcutNames = new ArrayList<String>();
2021 shortcutNames.add(getString(R.string.group_applications));
2022 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2023
2024 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2025 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2026 R.drawable.ic_launcher_application));
2027 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2028
2029 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2030 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
2031 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
2032 pickIntent.putExtras(bundle);
2033
2034 startActivityForResult(pickIntent, requestCode);
2035 }
2036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 private class RenameFolder {
2038 private EditText mInput;
2039
2040 Dialog createDialog() {
2041 mWaitingForResult = true;
2042 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2043 mInput = (EditText) layout.findViewById(R.id.folder_name);
2044
2045 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2046 builder.setIcon(0);
2047 builder.setTitle(getString(R.string.rename_folder_title));
2048 builder.setCancelable(true);
2049 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2050 public void onCancel(DialogInterface dialog) {
2051 cleanup();
2052 }
2053 });
2054 builder.setNegativeButton(getString(R.string.cancel_action),
2055 new Dialog.OnClickListener() {
2056 public void onClick(DialogInterface dialog, int which) {
2057 cleanup();
2058 }
2059 }
2060 );
2061 builder.setPositiveButton(getString(R.string.rename_action),
2062 new Dialog.OnClickListener() {
2063 public void onClick(DialogInterface dialog, int which) {
2064 changeFolderName();
2065 }
2066 }
2067 );
2068 builder.setView(layout);
2069 return builder.create();
2070 }
2071
2072 private void changeFolderName() {
2073 final String name = mInput.getText().toString();
2074 if (!TextUtils.isEmpty(name)) {
2075 // Make sure we have the right folder info
2076 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
2077 mFolderInfo.title = name;
2078 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2079
2080 if (mDesktopLocked) {
2081 mDrawer.lock();
2082 sModel.loadUserItems(false, Launcher.this, false, false);
2083 } else {
2084 final FolderIcon folderIcon = (FolderIcon)
2085 mWorkspace.getViewForTag(mFolderInfo);
2086 if (folderIcon != null) {
2087 folderIcon.setText(name);
2088 getWorkspace().requestLayout();
2089 } else {
2090 mDesktopLocked = true;
2091 mDrawer.lock();
2092 sModel.loadUserItems(false, Launcher.this, false, false);
2093 }
2094 }
2095 }
2096 cleanup();
2097 }
2098
2099 private void cleanup() {
2100 mWorkspace.unlock();
2101 dismissDialog(DIALOG_RENAME_FOLDER);
2102 mWaitingForResult = false;
2103 mFolderInfo = null;
2104 }
2105 }
2106
2107 /**
2108 * Displays the shortcut creation dialog and launches, if necessary, the
2109 * appropriate activity.
2110 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002111 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07002112 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 Dialog createDialog() {
2116 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002117
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2121 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002122 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 builder.setInverseBackgroundForced(true);
2125
2126 AlertDialog dialog = builder.create();
2127 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002128 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 return dialog;
2131 }
2132
2133 public void onCancel(DialogInterface dialog) {
2134 mWaitingForResult = false;
2135 cleanup();
2136 }
2137
Romain Guycbb89e42009-06-08 15:52:54 -07002138 public void onDismiss(DialogInterface dialog) {
2139 mWorkspace.unlock();
2140 }
2141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 private void cleanup() {
2143 mWorkspace.unlock();
2144 dismissDialog(DIALOG_CREATE_SHORTCUT);
2145 }
2146
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002147 /**
2148 * Handle the action clicked in the "Add to home" dialog.
2149 */
2150 public void onClick(DialogInterface dialog, int which) {
2151 Resources res = getResources();
2152 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002153
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002154 switch (which) {
2155 case AddAdapter.ITEM_SHORTCUT: {
2156 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002157 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002158 break;
2159 }
Romain Guycbb89e42009-06-08 15:52:54 -07002160
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002161 case AddAdapter.ITEM_APPWIDGET: {
2162 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002163
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002164 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2165 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2166 // add the search widget
2167 ArrayList<AppWidgetProviderInfo> customInfo =
2168 new ArrayList<AppWidgetProviderInfo>();
2169 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2170 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2171 info.label = getString(R.string.group_search);
2172 info.icon = R.drawable.ic_search_widget;
2173 customInfo.add(info);
2174 pickIntent.putParcelableArrayListExtra(
2175 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2176 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2177 Bundle b = new Bundle();
2178 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2179 customExtras.add(b);
2180 pickIntent.putParcelableArrayListExtra(
2181 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2182 // start the pick activity
2183 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2184 break;
2185 }
Romain Guycbb89e42009-06-08 15:52:54 -07002186
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002187 case AddAdapter.ITEM_LIVE_FOLDER: {
2188 // Insert extra item to handle inserting folder
2189 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002190
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002191 ArrayList<String> shortcutNames = new ArrayList<String>();
2192 shortcutNames.add(res.getString(R.string.group_folder));
2193 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002194
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002195 ArrayList<ShortcutIconResource> shortcutIcons =
2196 new ArrayList<ShortcutIconResource>();
2197 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2198 R.drawable.ic_launcher_folder));
2199 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2200
2201 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2202 pickIntent.putExtra(Intent.EXTRA_INTENT,
2203 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2204 pickIntent.putExtra(Intent.EXTRA_TITLE,
2205 getText(R.string.title_select_live_folder));
2206 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002207
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002208 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2209 break;
2210 }
2211
2212 case AddAdapter.ITEM_WALLPAPER: {
2213 startWallpaper();
2214 break;
2215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 }
2217 }
2218 }
2219
2220 /**
2221 * Receives notifications when applications are added/removed.
2222 */
2223 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2224 @Override
2225 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002226 final String action = intent.getAction();
2227 final String packageName = intent.getData().getSchemeSpecificPart();
2228 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2229
2230 if (LauncherModel.DEBUG_LOADERS) {
2231 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2232 ", replacing=" + replacing);
2233 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002235
2236 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2237 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2238 if (!replacing) {
2239 removeShortcutsForPackage(packageName);
2240 if (LauncherModel.DEBUG_LOADERS) {
2241 d(LauncherModel.LOG_TAG, " --> remove package");
2242 }
2243 sModel.removePackage(Launcher.this, packageName);
2244 }
2245 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2246 // later, we will update the package at this time
2247 } else {
2248 if (!replacing) {
2249 if (LauncherModel.DEBUG_LOADERS) {
2250 d(LauncherModel.LOG_TAG, " --> add package");
2251 }
2252 sModel.addPackage(Launcher.this, packageName);
2253 } else {
2254 if (LauncherModel.DEBUG_LOADERS) {
2255 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2256 }
2257 sModel.updatePackage(Launcher.this, packageName);
2258 updateShortcutsForPackage(packageName);
2259 }
2260 }
2261 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002263 if (LauncherModel.DEBUG_LOADERS) {
2264 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2265 }
2266 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 }
2268 }
2269 }
2270
2271 /**
2272 * Receives notifications whenever the user favorites have changed.
2273 */
2274 private class FavoritesChangeObserver extends ContentObserver {
2275 public FavoritesChangeObserver() {
2276 super(new Handler());
2277 }
2278
2279 @Override
2280 public void onChange(boolean selfChange) {
2281 onFavoritesChanged();
2282 }
2283 }
2284
2285 /**
2286 * Receives intents from other applications to change the wallpaper.
2287 */
2288 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2289 private final Application mApplication;
2290 private WeakReference<Launcher> mLauncher;
2291
2292 WallpaperIntentReceiver(Application application, Launcher launcher) {
2293 mApplication = application;
2294 setLauncher(launcher);
2295 }
2296
2297 void setLauncher(Launcher launcher) {
2298 mLauncher = new WeakReference<Launcher>(launcher);
2299 }
2300
2301 @Override
2302 public void onReceive(Context context, Intent intent) {
2303 // Load the wallpaper from the ApplicationContext and store it locally
2304 // until the Launcher Activity is ready to use it
2305 final Drawable drawable = mApplication.getWallpaper();
2306 if (drawable instanceof BitmapDrawable) {
2307 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2308 } else {
2309 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2310 }
2311
2312 // If Launcher is alive, notify we have a new wallpaper
2313 if (mLauncher != null) {
2314 final Launcher launcher = mLauncher.get();
2315 if (launcher != null) {
2316 launcher.loadWallpaper();
2317 }
2318 }
2319 }
2320 }
2321
2322 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2323 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2324 private boolean mOpen;
2325
2326 public void onDrawerOpened() {
2327 if (!mOpen) {
2328 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002331 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332
2333 mOpen = true;
2334 }
2335 }
2336
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002337 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2338 view.getDrawingRect(bounds);
2339
2340 while (view != mDragLayer) {
2341 bounds.offset(view.getLeft(), view.getTop());
2342 view = (View) view.getParent();
2343 }
2344 }
2345
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 public void onDrawerClosed() {
2347 if (mOpen) {
2348 mHandleIcon.reverseTransition(150);
2349 mWorkspace.mDrawerBounds.setEmpty();
2350 mOpen = false;
2351 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 mAllAppsGrid.setSelection(0);
2354 mAllAppsGrid.clearTextFilter();
2355 }
2356
2357 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002358 if (PROFILE_DRAWER) {
2359 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2360 }
2361
2362 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2363 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 }
2365
2366 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002367 if (PROFILE_DRAWER) {
2368 android.os.Debug.stopMethodTracing();
2369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 }
2371 }
2372
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002373 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002375 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002376 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002377
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 // Number of items to bind in every pass
2379 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380
2381 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002382 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002383 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002385
Karl Rosaen138a0412009-04-23 19:00:21 -07002386 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002387
2388 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002389 ArrayList<LauncherAppWidgetInfo> appWidgets,
2390 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391
2392 mLauncher = new WeakReference<Launcher>(launcher);
2393 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002394 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002395
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002396 // Sort widgets so active workspace is bound first
2397 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2398 final int size = appWidgets.size();
2399 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002400
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002401 for (int i = 0; i < size; i++) {
2402 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2403 if (appWidgetInfo.screen == currentScreen) {
2404 mAppWidgets.addFirst(appWidgetInfo);
2405 } else {
2406 mAppWidgets.addLast(appWidgetInfo);
2407 }
2408 }
2409 }
Romain Guycbb89e42009-06-08 15:52:54 -07002410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 public void startBindingItems() {
2412 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2413 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002414
2415 public void startBindingDrawer() {
2416 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2417 }
Romain Guycbb89e42009-06-08 15:52:54 -07002418
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002419 public void startBindingAppWidgetsWhenIdle() {
2420 // Ask for notification when message queue becomes idle
2421 final MessageQueue messageQueue = Looper.myQueue();
2422 messageQueue.addIdleHandler(this);
2423 }
Romain Guycbb89e42009-06-08 15:52:54 -07002424
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002425 public boolean queueIdle() {
2426 // Queue is idle, so start binding items
2427 startBindingAppWidgets();
2428 return false;
2429 }
2430
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002431 public void startBindingAppWidgets() {
2432 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2433 }
2434
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 @Override
2436 public void handleMessage(Message msg) {
2437 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002438 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439 return;
2440 }
Romain Guycbb89e42009-06-08 15:52:54 -07002441
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 switch (msg.what) {
2443 case MESSAGE_BIND_ITEMS: {
2444 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2445 break;
2446 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002447 case MESSAGE_BIND_DRAWER: {
2448 launcher.bindDrawer(this, mDrawerAdapter);
2449 break;
2450 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002451 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002452 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 break;
2454 }
2455 }
2456 }
2457 }
Romain Guy73b979d2009-06-09 12:57:21 -07002458
2459 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2460 GestureOverlayView.OnGesturePerformedListener {
2461
2462 private final GestureMatcher mMatcher = new GestureMatcher();
2463
2464 GesturesProcessor() {
2465 // TODO: Maybe the load should happen on a background thread?
2466 sLibrary.load();
2467 }
2468
2469 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002470 //noinspection PointlessBooleanExpression,ConstantConditions
2471 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2472 overlay.removeCallbacks(mMatcher);
2473 resetGesturesNextPrompt();
2474 }
Romain Guy73b979d2009-06-09 12:57:21 -07002475
2476 mGesturesAdd.setAlpha(128);
2477 mGesturesAdd.setEnabled(false);
2478 }
2479
2480 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2481 }
2482
2483 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2484 }
2485
2486 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002487 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2488 mMatcher.gesture = overlay.getGesture();
2489 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2490 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002491 if (mGesturesAction.intent != null) {
2492 mGesturesAction.intent = null;
2493 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2494 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002495 } else {
2496 mMatcher.run();
2497 }
Romain Guy73b979d2009-06-09 12:57:21 -07002498 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002499 overlay.removeCallbacks(mMatcher);
2500
2501 mMatcher.gesture = overlay.getGesture();
2502 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2503 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002504 if (mGesturesAction.intent != null) {
2505 mGesturesAction.intent = null;
2506 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2507 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002508 } else {
2509 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2510 }
Romain Guy73b979d2009-06-09 12:57:21 -07002511 }
2512 }
2513
Romain Guy3cf604f2009-06-16 13:12:53 -07002514 void matchGesture(Gesture gesture) {
2515 matchGesture(gesture, true);
2516 }
2517
2518 void matchGesture(Gesture gesture, boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07002519 mGesturesAdd.setAlpha(255);
2520 mGesturesAdd.setEnabled(true);
2521
2522 if (gesture != null) {
2523 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2524
2525 if (DEBUG_GESTURES) {
2526 for (Prediction p : predictions) {
2527 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2528 }
2529 }
2530
2531 boolean match = false;
2532 if (predictions.size() > 0) {
2533 final Prediction prediction = predictions.get(0);
2534 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2535 match = true;
2536
2537 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2538 if (info != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002539 updatePrompt(info, animate);
Romain Guy73b979d2009-06-09 12:57:21 -07002540 }
2541 }
2542 }
2543
2544 if (!match){
Romain Guy1ce1a242009-06-23 17:34:54 -07002545 mGesturesAction.intent = null;
Romain Guy3cf604f2009-06-16 13:12:53 -07002546 if (animate) {
2547 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2548 } else {
2549 setGesturesPrompt(null, getString(R.string.gestures_unknown));
2550 }
Romain Guy73b979d2009-06-09 12:57:21 -07002551 }
2552 }
2553 }
2554
2555 private void updatePrompt(ApplicationInfo info) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002556 updatePrompt(info, true);
2557 }
2558
2559 private void updatePrompt(ApplicationInfo info, boolean animate) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002560 if (mGesturesAction.intent != null &&
Romain Guy1ce1a242009-06-23 17:34:54 -07002561 info.intent.toUri(0).equals(mGesturesAction.intent.toUri(0)) &&
Romain Guy6fefcf12009-06-11 13:07:43 -07002562 info.title.equals(((TextView) mGesturesPrompt.getCurrentView()).getText())) {
2563 return;
2564 }
Romain Guy3cf604f2009-06-16 13:12:53 -07002565
2566 if (animate) {
2567 setGesturesNextPrompt(info.icon, info.title);
2568 } else {
2569 setGesturesPrompt(info.icon, info.title);
2570 }
2571
Romain Guy73b979d2009-06-09 12:57:21 -07002572 mGesturesAction.intent = info.intent;
2573 }
2574
2575 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002576 //noinspection PointlessBooleanExpression,ConstantConditions
2577 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2578 overlay.removeCallbacks(mMatcher);
2579 }
Romain Guy73b979d2009-06-09 12:57:21 -07002580 }
2581
2582 void addGesture(String name, Gesture gesture) {
2583 sLibrary.addGesture(name, gesture);
2584 // TODO: On a background thread?
2585 sLibrary.save();
2586 }
2587
2588 void update(ApplicationInfo info, Gesture gesture) {
2589 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002590 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002591 }
2592
2593 class GestureMatcher implements Runnable {
2594 Gesture gesture;
2595
2596 public void run() {
2597 if (gesture != null) {
2598 matchGesture(gesture);
2599 }
2600 }
2601 }
2602 }
2603
2604 private class GesturesAction implements View.OnClickListener {
2605 Intent intent;
2606
2607 public void onClick(View v) {
2608 if (intent != null) {
2609 startActivitySafely(intent);
2610 }
2611 }
2612 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002613}
Karl Rosaen138a0412009-04-23 19:00:21 -07002614