blob: 21b69491a2514e06819b72415c23f1f86681a9ba [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;
Mike LeBeau736cf282009-07-02 17:46:59 -070023import android.app.ISearchManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070024import android.app.IWallpaperService;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.app.SearchManager;
26import android.app.StatusBarManager;
27import android.content.ActivityNotFoundException;
28import android.content.BroadcastReceiver;
29import android.content.ComponentName;
30import android.content.ContentResolver;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
Romain Guy1dd3a072009-07-16 13:21:01 -070035import android.content.SharedPreferences;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070036import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.pm.ActivityInfo;
38import android.content.pm.PackageManager;
39import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070041import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.graphics.Bitmap;
44import android.graphics.Rect;
Romain Guy73b979d2009-06-09 12:57:21 -070045import android.graphics.PorterDuffXfermode;
46import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.graphics.drawable.BitmapDrawable;
48import android.graphics.drawable.Drawable;
49import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Bundle;
51import android.os.Handler;
52import android.os.IBinder;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070053import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070054import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070055import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.os.Parcelable;
57import android.os.RemoteException;
58import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070059import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.text.Selection;
61import android.text.SpannableStringBuilder;
62import android.text.TextUtils;
63import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070064import static android.util.Log.*;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.view.KeyEvent;
67import android.view.LayoutInflater;
68import android.view.Menu;
69import android.view.MenuItem;
70import android.view.View;
71import android.view.ViewGroup;
Romain Guy73b979d2009-06-09 12:57:21 -070072import android.view.MotionEvent;
73import android.view.Gravity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.View.OnLongClickListener;
75import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.widget.EditText;
Karl Rosaen138a0412009-04-23 19:00:21 -070077import android.widget.GridView;
Karl Rosaen138a0412009-04-23 19:00:21 -070078import android.widget.SlidingDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.widget.TextView;
80import android.widget.Toast;
Romain Guy73b979d2009-06-09 12:57:21 -070081import android.widget.ImageView;
82import android.widget.PopupWindow;
Romain Guy1dd3a072009-07-16 13:21:01 -070083import android.widget.Button;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070084import android.appwidget.AppWidgetManager;
85import android.appwidget.AppWidgetProviderInfo;
Romain Guy73b979d2009-06-09 12:57:21 -070086import android.gesture.GestureOverlayView;
87import android.gesture.GestureLibraries;
88import android.gesture.GestureLibrary;
89import android.gesture.Gesture;
90import android.gesture.Prediction;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
92import java.lang.ref.WeakReference;
93import java.util.ArrayList;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070094import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070095import java.io.DataOutputStream;
96import java.io.FileNotFoundException;
97import java.io.IOException;
98import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099
100/**
101 * Default launcher application.
102 */
103public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener {
104 static final String LOG_TAG = "Launcher";
105 static final boolean LOGD = false;
106
107 private static final boolean PROFILE_STARTUP = false;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700108 private static final boolean PROFILE_DRAWER = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700109 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 private static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700111 private static final boolean DEBUG_GESTURES = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Romain Guy6fefcf12009-06-11 13:07:43 -0700113 private static final boolean CONFIG_GESTURES_IMMEDIATE_MODE = true;
114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 private static final int WALLPAPER_SCREENS_SPAN = 2;
116
117 private static final int MENU_GROUP_ADD = 1;
118 private static final int MENU_ADD = Menu.FIRST + 1;
119 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
120 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
121 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy73b979d2009-06-09 12:57:21 -0700122 private static final int MENU_GESTURES = MENU_NOTIFICATIONS + 1;
123 private static final int MENU_SETTINGS = MENU_GESTURES + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
125 private static final int REQUEST_CREATE_SHORTCUT = 1;
126 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700127 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 private static final int REQUEST_PICK_APPLICATION = 6;
129 private static final int REQUEST_PICK_SHORTCUT = 7;
130 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_PICK_APPWIDGET = 9;
Romain Guy73b979d2009-06-09 12:57:21 -0700132 private static final int REQUEST_PICK_GESTURE_ACTION = 10;
133 private static final int REQUEST_CREATE_GESTURE_ACTION = 11;
134 private static final int REQUEST_CREATE_GESTURE_APPLICATION_ACTION = 12;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
137
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700138 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
139 static final String SEARCH_WIDGET = "search_widget";
140
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 static final int SCREEN_COUNT = 3;
142 static final int DEFAULT_SCREN = 1;
143 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700144 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
146 private static final int DIALOG_CREATE_SHORTCUT = 1;
Romain Guycbb89e42009-06-08 15:52:54 -0700147 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Romain Guy98d01652009-06-30 16:21:04 -0700149 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
151 // Type: int
152 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
153 // Type: boolean
154 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
155 // Type: long
156 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
171 // Type: int[]
172 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
173 // Type: boolean
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
175 // Type: long
176 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Romain Guy73b979d2009-06-09 12:57:21 -0700177 // Type: Gesture (Parcelable)
178 private static final String RUNTIME_STATE_PENDING_GESTURE = "launcher.gesture";
Romain Guy3cf604f2009-06-16 13:12:53 -0700179 // Type: boolean
180 private static final String RUNTIME_STATE_GESTURES_PANEL = "launcher.gesture_panel_showing";
181 // Type: Gesture (Parcelable)
182 private static final String RUNTIME_STATE_GESTURES_PANEL_GESTURE = "launcher.gesture_panel_gesture";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700184 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
186 private static Bitmap sWallpaper;
187
188 private static final Object sLock = new Object();
189 private static int sScreen = DEFAULT_SCREN;
190
191 private static WallpaperIntentReceiver sWallpaperReceiver;
192
Romain Guy73b979d2009-06-09 12:57:21 -0700193 private static GestureLibrary sLibrary;
194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
196 private final ContentObserver mObserver = new FavoritesChangeObserver();
197
198 private LayoutInflater mInflater;
199
200 private DragLayer mDragLayer;
201 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private AppWidgetManager mAppWidgetManager;
204 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700206 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private CellLayout.CellInfo mAddItemCellInfo;
209 private CellLayout.CellInfo mMenuAddInfo;
210 private final int[] mCellCoordinates = new int[2];
211 private FolderInfo mFolderInfo;
212
213 private SlidingDrawer mDrawer;
214 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private HandleView mHandleView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private AllAppsGridView mAllAppsGrid;
217
218 private boolean mDesktopLocked = true;
219 private Bundle mSavedState;
220
221 private SpannableStringBuilder mDefaultKeySsb = null;
222
223 private boolean mDestroyed;
Mike LeBeau736cf282009-07-02 17:46:59 -0700224
225 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private boolean mRestoring;
228 private boolean mWaitingForResult;
229 private boolean mLocaleChanged;
230
231 private Bundle mSavedInstanceState;
232
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700233 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Romain Guy73b979d2009-06-09 12:57:21 -0700235 private View mGesturesPanel;
236 private GestureOverlayView mGesturesOverlay;
Romain Guy73b979d2009-06-09 12:57:21 -0700237 private ImageView mGesturesAdd;
238 private PopupWindow mGesturesWindow;
239 private Launcher.GesturesProcessor mGesturesProcessor;
240 private Gesture mCurrentGesture;
241 private GesturesAction mGesturesAction;
Romain Guyaad5ef42009-06-10 02:48:37 -0700242 private boolean mHideGesturesPanel;
Romain Guy1dd3a072009-07-16 13:21:01 -0700243 private TextView mGesturesPrompt;
244 private Button mGesturesSend;
Romain Guy73b979d2009-06-09 12:57:21 -0700245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 @Override
247 protected void onCreate(Bundle savedInstanceState) {
248 super.onCreate(savedInstanceState);
249 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700250
Romain Guy73b979d2009-06-09 12:57:21 -0700251 if (sLibrary == null) {
252 // The context is not kept by the library so it's safe to do this
253 sLibrary = GestureLibraries.fromPrivateFile(Launcher.this,
254 GesturesConstants.STORE_NAME);
255 }
256
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700257 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700258
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700259 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
260 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 if (PROFILE_STARTUP) {
263 android.os.Debug.startMethodTracing("/sdcard/launcher");
264 }
265
266 checkForLocaleChange();
267 setWallpaperDimension();
268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 setContentView(R.layout.launcher);
270 setupViews();
271
272 registerIntentReceivers();
273 registerContentObservers();
274
275 mSavedState = savedInstanceState;
276 restoreState(mSavedState);
277
278 if (PROFILE_STARTUP) {
279 android.os.Debug.stopMethodTracing();
280 }
281
282 if (!mRestoring) {
283 startLoaders();
284 }
285
286 // For handling default keys
287 mDefaultKeySsb = new SpannableStringBuilder();
288 Selection.setSelection(mDefaultKeySsb, 0);
289 }
Romain Guycbb89e42009-06-08 15:52:54 -0700290
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700292 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
293 readConfiguration(this, localeConfiguration);
294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 final Configuration configuration = getResources().getConfiguration();
296
Romain Guy98d01652009-06-30 16:21:04 -0700297 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 final String locale = configuration.locale.toString();
299
Romain Guy98d01652009-06-30 16:21:04 -0700300 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 final int mcc = configuration.mcc;
302
Romain Guy98d01652009-06-30 16:21:04 -0700303 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 final int mnc = configuration.mnc;
305
306 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
307
308 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700309 localeConfiguration.locale = locale;
310 localeConfiguration.mcc = mcc;
311 localeConfiguration.mnc = mnc;
312
313 writeConfiguration(this, localeConfiguration);
314 }
315 }
316
317 private static class LocaleConfiguration {
318 public String locale;
319 public int mcc = -1;
320 public int mnc = -1;
321 }
322
323 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
324 DataInputStream in = null;
325 try {
326 in = new DataInputStream(context.openFileInput(PREFERENCES));
327 configuration.locale = in.readUTF();
328 configuration.mcc = in.readInt();
329 configuration.mnc = in.readInt();
330 } catch (FileNotFoundException e) {
331 // Ignore
332 } catch (IOException e) {
333 // Ignore
334 } finally {
335 if (in != null) {
336 try {
337 in.close();
338 } catch (IOException e) {
339 // Ignore
340 }
341 }
342 }
343 }
344
345 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
346 DataOutputStream out = null;
347 try {
348 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
349 out.writeUTF(configuration.locale);
350 out.writeInt(configuration.mcc);
351 out.writeInt(configuration.mnc);
352 out.flush();
353 } catch (FileNotFoundException e) {
354 // Ignore
355 } catch (IOException e) {
356 //noinspection ResultOfMethodCallIgnored
357 context.getFileStreamPath(PREFERENCES).delete();
358 } finally {
359 if (out != null) {
360 try {
361 out.close();
362 } catch (IOException e) {
363 // Ignore
364 }
365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 }
367 }
368
369 static int getScreen() {
370 synchronized (sLock) {
371 return sScreen;
372 }
373 }
374
375 static void setScreen(int screen) {
376 synchronized (sLock) {
377 sScreen = screen;
378 }
379 }
380
381 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700382 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
383 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
384
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 mRestoring = false;
386 }
387
388 private void setWallpaperDimension() {
389 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
390 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
391
392 Display display = getWindowManager().getDefaultDisplay();
393 boolean isPortrait = display.getWidth() < display.getHeight();
394
395 final int width = isPortrait ? display.getWidth() : display.getHeight();
396 final int height = isPortrait ? display.getHeight() : display.getWidth();
397 try {
398 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
399 } catch (RemoteException e) {
400 // System is dead!
401 }
402 }
403
404 @Override
405 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700406 mWaitingForResult = false;
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 // The pattern used here is that a user PICKs a specific application,
409 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700410
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
412 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700413
Romain Guy73b979d2009-06-09 12:57:21 -0700414 if (resultCode == RESULT_OK && (mAddItemCellInfo != null ||
415 ((requestCode == REQUEST_PICK_GESTURE_ACTION ||
416 requestCode == REQUEST_CREATE_GESTURE_ACTION ||
417 requestCode == REQUEST_CREATE_GESTURE_APPLICATION_ACTION) && mCurrentGesture != null))) {
418
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 switch (requestCode) {
420 case REQUEST_PICK_APPLICATION:
421 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
422 break;
423 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700424 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 break;
426 case REQUEST_CREATE_SHORTCUT:
427 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
428 break;
429 case REQUEST_PICK_LIVE_FOLDER:
430 addLiveFolder(data);
431 break;
432 case REQUEST_CREATE_LIVE_FOLDER:
433 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
434 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435 case REQUEST_PICK_APPWIDGET:
436 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700438 case REQUEST_CREATE_APPWIDGET:
439 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 break;
Romain Guy73b979d2009-06-09 12:57:21 -0700441 case REQUEST_PICK_GESTURE_ACTION:
442 processShortcut(data, REQUEST_CREATE_GESTURE_APPLICATION_ACTION,
443 REQUEST_CREATE_GESTURE_ACTION);
444 break;
445 case REQUEST_CREATE_GESTURE_ACTION:
446 completeCreateGesture(data, true);
447 break;
448 case REQUEST_CREATE_GESTURE_APPLICATION_ACTION:
449 completeCreateGesture(data, false);
450 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700452 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700454 // Clean up the appWidgetId if we canceled
455 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
456 if (appWidgetId != -1) {
457 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461
462 @Override
463 protected void onResume() {
464 super.onResume();
465
466 if (mRestoring) {
467 startLoaders();
468 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700469
470 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
471 // onNewIntent), then close the search dialog if needed, because it probably
472 // came from the user pressing 'home' (rather than, for example, pressing 'back').
473 if (mIsNewIntent) {
474 // Post to a handler so that this happens after the search dialog tries to open
475 // itself again.
476 mWorkspace.post(new Runnable() {
477 public void run() {
478 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
479 ServiceManager.getService(Context.SEARCH_SERVICE));
480 try {
481 searchManagerService.stopSearch();
482 } catch (RemoteException e) {
483 e(LOG_TAG, "error stopping search", e);
484 }
485 }
486 });
487 }
488
489 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 }
491
492 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700493 protected void onPause() {
494 super.onPause();
Romain Guy73b979d2009-06-09 12:57:21 -0700495 if (mGesturesWindow != null) {
496 mGesturesWindow.setAnimationStyle(0);
497 mGesturesWindow.update();
498 }
Romain Guycbb89e42009-06-08 15:52:54 -0700499 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700500 }
Romain Guycbb89e42009-06-08 15:52:54 -0700501
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700502 @Override
Romain Guy73b979d2009-06-09 12:57:21 -0700503 protected void onStop() {
504 super.onStop();
Romain Guyaad5ef42009-06-10 02:48:37 -0700505 if (mHideGesturesPanel) {
506 mHideGesturesPanel = false;
507 hideGesturesPanel();
508 }
Romain Guy73b979d2009-06-09 12:57:21 -0700509 }
510
511 @Override
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700512 public Object onRetainNonConfigurationInstance() {
513 // Flag any binder to stop early before switching
514 if (mBinder != null) {
515 mBinder.mTerminate = true;
516 }
Romain Guycbb89e42009-06-08 15:52:54 -0700517
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700518 if (PROFILE_ROTATE) {
519 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
520 }
521 return null;
522 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700523
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 private boolean acceptFilter() {
525 final InputMethodManager inputManager = (InputMethodManager)
526 getSystemService(Context.INPUT_METHOD_SERVICE);
527 return !inputManager.isFullscreenMode();
528 }
529
530 @Override
531 public boolean onKeyDown(int keyCode, KeyEvent event) {
532 boolean handled = super.onKeyDown(keyCode, event);
533 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
534 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
535 keyCode, event);
536 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700537 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700538 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700539 // showSearchDialog()
540 // If there are multiple keystrokes before the search dialog takes focus,
541 // onSearchRequested() will be called for every keystroke,
542 // but it is idempotent, so it's fine.
543 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 }
545 }
546
547 return handled;
548 }
549
Karl Rosaen138a0412009-04-23 19:00:21 -0700550 private String getTypedText() {
551 return mDefaultKeySsb.toString();
552 }
553
554 private void clearTypedText() {
555 mDefaultKeySsb.clear();
556 mDefaultKeySsb.clearSpans();
557 Selection.setSelection(mDefaultKeySsb, 0);
558 }
559
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 /**
561 * Restores the previous state, if it exists.
562 *
563 * @param savedState The previous state.
564 */
565 private void restoreState(Bundle savedState) {
566 if (savedState == null) {
567 return;
568 }
569
570 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
571 if (currentScreen > -1) {
572 mWorkspace.setCurrentScreen(currentScreen);
573 }
574
575 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
576 if (addScreen > -1) {
577 mAddItemCellInfo = new CellLayout.CellInfo();
578 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
579 addItemCellInfo.valid = true;
580 addItemCellInfo.screen = addScreen;
581 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
582 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
583 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
584 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
585 addItemCellInfo.findVacantCellsFromOccupied(
586 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
587 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
588 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
589 mRestoring = true;
590 }
591
592 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
593 if (renameFolder) {
594 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
595 mFolderInfo = sModel.getFolderById(this, id);
596 mRestoring = true;
597 }
Romain Guy73b979d2009-06-09 12:57:21 -0700598
Romain Guy1ce1a242009-06-23 17:34:54 -0700599 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_PENDING_GESTURE);
600
Romain Guy3cf604f2009-06-16 13:12:53 -0700601 boolean gesturesShowing = savedState.getBoolean(RUNTIME_STATE_GESTURES_PANEL, false);
602 if (gesturesShowing) {
Romain Guyf38b3d52009-07-01 16:39:46 -0700603 if (mCurrentGesture == null) {
604 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_GESTURES_PANEL_GESTURE);
605 }
606 final Gesture gesture = mCurrentGesture;
Romain Guy3cf604f2009-06-16 13:12:53 -0700607 mWorkspace.post(new Runnable() {
608 public void run() {
609 showGesturesPanel(false);
Romain Guy1dd3a072009-07-16 13:21:01 -0700610 mGesturesProcessor.matchGesture(gesture);
Romain Guy3cf604f2009-06-16 13:12:53 -0700611 mWorkspace.post(new Runnable() {
612 public void run() {
613 if (gesture != null) {
614 mGesturesOverlay.setGesture(gesture);
615 }
616 }
617 });
618 }
619 });
620 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 }
622
623 /**
624 * Finds all the views we need and configure them properly.
625 */
626 private void setupViews() {
627 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
628 final DragLayer dragLayer = mDragLayer;
629
630 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
631 final Workspace workspace = mWorkspace;
632
633 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
634 final SlidingDrawer drawer = mDrawer;
635
636 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
637 final AllAppsGridView grid = mAllAppsGrid;
638
639 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
640
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700641 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
642 mHandleView.setLauncher(this);
643 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 mHandleIcon.setCrossFadeEnabled(true);
645
646 drawer.lock();
647 final DrawerManager drawerManager = new DrawerManager();
648 drawer.setOnDrawerOpenListener(drawerManager);
649 drawer.setOnDrawerCloseListener(drawerManager);
650 drawer.setOnDrawerScrollListener(drawerManager);
651
Karl Rosaen138a0412009-04-23 19:00:21 -0700652 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 grid.setDragger(dragLayer);
654 grid.setLauncher(this);
655
656 workspace.setOnLongClickListener(this);
657 workspace.setDragger(dragLayer);
658 workspace.setLauncher(this);
659 loadWallpaper();
660
661 deleteZone.setLauncher(this);
662 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700663 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664
665 dragLayer.setIgnoredDropTarget(grid);
666 dragLayer.setDragScoller(workspace);
667 dragLayer.setDragListener(deleteZone);
Romain Guy73b979d2009-06-09 12:57:21 -0700668
669 mGesturesPanel = mInflater.inflate(R.layout.gestures, mDragLayer, false);
670 final View gesturesPanel = mGesturesPanel;
671
Romain Guy73b979d2009-06-09 12:57:21 -0700672 mGesturesAction = new GesturesAction();
673
Romain Guy1dd3a072009-07-16 13:21:01 -0700674 mGesturesPrompt = (TextView) gesturesPanel.findViewById(R.id.gestures_prompt);
675 mGesturesSend = (Button) gesturesPanel.findViewById(R.id.gestures_action);
676 mGesturesSend.setOnClickListener(mGesturesAction);
677
Romain Guy73b979d2009-06-09 12:57:21 -0700678 mGesturesAdd = (ImageView) gesturesPanel.findViewById(R.id.gestures_add);
679 final ImageView gesturesAdd = mGesturesAdd;
Romain Guy73b979d2009-06-09 12:57:21 -0700680 gesturesAdd.setOnClickListener(new View.OnClickListener() {
681 public void onClick(View v) {
682 createGesture();
683 }
684 });
685
Romain Guy1dd3a072009-07-16 13:21:01 -0700686 gesturesPanel.findViewById(R.id.gestures_list).setOnClickListener(new View.OnClickListener() {
687 public void onClick(View v) {
688 startActivity(new Intent(Launcher.this, GesturesActivity.class));
689 }
690 });
691
Romain Guy73b979d2009-06-09 12:57:21 -0700692 mGesturesOverlay = (GestureOverlayView) gesturesPanel.findViewById(R.id.gestures_overlay);
693 mGesturesProcessor = new GesturesProcessor();
694
695 final GestureOverlayView overlay = mGesturesOverlay;
Romain Guy73b979d2009-06-09 12:57:21 -0700696 overlay.addOnGestureListener(mGesturesProcessor);
Romain Guyb8734242009-06-10 11:53:57 -0700697 overlay.getGesturePaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
Romain Guy73b979d2009-06-09 12:57:21 -0700698 }
699
700 private void createGesture() {
Romain Guyf280f202009-06-26 10:31:07 -0700701 if (!mWaitingForResult) {
702 mCurrentGesture = mGesturesOverlay.getGesture();
703 mWaitingForResult = true;
704 pickShortcut(REQUEST_PICK_GESTURE_ACTION, R.string.title_select_shortcut);
705 }
Romain Guy73b979d2009-06-09 12:57:21 -0700706 }
707
708 private void completeCreateGesture(Intent data, boolean isShortcut) {
709 ApplicationInfo info;
710
711 if (isShortcut) {
712 info = infoFromShortcutIntent(this, data);
713 } else {
714 info = infoFromApplicationIntent(this, data);
715 }
716
717 boolean success = false;
718 if (info != null) {
719 info.isGesture = true;
720
721 if (LauncherModel.addGestureToDatabase(this, info, false)) {
722 mGesturesProcessor.addGesture(String.valueOf(info.id), mCurrentGesture);
723 mGesturesProcessor.update(info, mCurrentGesture);
724 Toast.makeText(this, getString(R.string.gestures_created, info.title),
725 Toast.LENGTH_SHORT).show();
726 success = true;
727 }
728 }
729
730 if (!success) {
731 Toast.makeText(this, getString(R.string.gestures_failed), Toast.LENGTH_SHORT).show();
732 }
733
734 mCurrentGesture = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 }
736
737 /**
738 * Creates a view representing a shortcut.
739 *
740 * @param info The data structure describing the shortcut.
741 *
742 * @return A View inflated from R.layout.application.
743 */
744 View createShortcut(ApplicationInfo info) {
745 return createShortcut(R.layout.application,
746 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
747 }
748
749 /**
750 * Creates a view representing a shortcut inflated from the specified resource.
751 *
752 * @param layoutResId The id of the XML layout used to create the shortcut.
753 * @param parent The group the shortcut belongs to.
754 * @param info The data structure describing the shortcut.
755 *
756 * @return A View inflated from layoutResId.
757 */
758 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
759 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
760
761 if (!info.filtered) {
762 info.icon = Utilities.createIconThumbnail(info.icon, this);
763 info.filtered = true;
764 }
765
766 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
767 favorite.setText(info.title);
768 favorite.setTag(info);
769 favorite.setOnClickListener(this);
770
771 return favorite;
772 }
773
774 /**
775 * Add an application shortcut to the workspace.
776 *
777 * @param data The intent describing the application.
778 * @param cellInfo The position on screen where to create the shortcut.
779 */
780 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
781 boolean insertAtFirst) {
782 cellInfo.screen = mWorkspace.getCurrentScreen();
783 if (!findSingleSlot(cellInfo)) return;
784
Romain Guy73b979d2009-06-09 12:57:21 -0700785 final ApplicationInfo info = infoFromApplicationIntent(context, data);
786 if (info != null) {
787 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
788 }
789 }
790
791 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 ComponentName component = data.getComponent();
793 PackageManager packageManager = context.getPackageManager();
794 ActivityInfo activityInfo = null;
795 try {
796 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
797 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700798 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 }
Romain Guycbb89e42009-06-08 15:52:54 -0700800
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 if (activityInfo != null) {
802 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700803
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 itemInfo.title = activityInfo.loadLabel(packageManager);
805 if (itemInfo.title == null) {
806 itemInfo.title = activityInfo.name;
807 }
Romain Guycbb89e42009-06-08 15:52:54 -0700808
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
810 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
811 itemInfo.icon = activityInfo.loadIcon(packageManager);
812 itemInfo.container = ItemInfo.NO_ID;
813
Romain Guy73b979d2009-06-09 12:57:21 -0700814 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 }
Romain Guy73b979d2009-06-09 12:57:21 -0700816
817 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 }
Romain Guycbb89e42009-06-08 15:52:54 -0700819
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 /**
821 * Add a shortcut to the workspace.
822 *
823 * @param data The intent describing the shortcut.
824 * @param cellInfo The position on screen where to create the shortcut.
825 * @param insertAtFirst
826 */
827 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
828 boolean insertAtFirst) {
829 cellInfo.screen = mWorkspace.getCurrentScreen();
830 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700831
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
833
834 if (!mRestoring) {
835 sModel.addDesktopItem(info);
836
837 final View view = createShortcut(info);
838 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
839 } else if (sModel.isDesktopLoaded()) {
840 sModel.addDesktopItem(info);
841 }
842 }
843
Romain Guycbb89e42009-06-08 15:52:54 -0700844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700846 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700848 * @param data The intent describing the appWidgetId.
849 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700851 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 boolean insertAtFirst) {
853
854 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700855 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700856
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700857 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700858
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700859 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700860
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700861 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700863 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700864
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 // Try finding open space on Launcher screen
866 final int[] xy = mCellCoordinates;
867 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
868
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700869 // Build Launcher-specific widget info and save to database
870 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 launcherInfo.spanX = spans[0];
872 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700873
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 LauncherModel.addItemToDatabase(this, launcherInfo,
875 LauncherSettings.Favorites.CONTAINER_DESKTOP,
876 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
877
878 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700879 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700880
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700882 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700883
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700884 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
888 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
889 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700890 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
892 }
Romain Guycbb89e42009-06-08 15:52:54 -0700893
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700894 public LauncherAppWidgetHost getAppWidgetHost() {
895 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 }
Romain Guycbb89e42009-06-08 15:52:54 -0700897
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 static ApplicationInfo addShortcut(Context context, Intent data,
899 CellLayout.CellInfo cellInfo, boolean notify) {
900
Romain Guy73b979d2009-06-09 12:57:21 -0700901 final ApplicationInfo info = infoFromShortcutIntent(context, data);
902 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
903 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
904
905 return info;
906 }
907
908 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
910 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
911 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
912
913 Drawable icon = null;
914 boolean filtered = false;
915 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700916 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917
918 if (bitmap != null) {
919 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
920 filtered = true;
921 customIcon = true;
922 } else {
923 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700924 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700926 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 final PackageManager packageManager = context.getPackageManager();
928 Resources resources = packageManager.getResourcesForApplication(
929 iconResource.packageName);
930 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
931 icon = resources.getDrawable(id);
932 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700933 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 }
935 }
936 }
937
938 if (icon == null) {
939 icon = context.getPackageManager().getDefaultActivityIcon();
940 }
941
942 final ApplicationInfo info = new ApplicationInfo();
943 info.icon = icon;
944 info.filtered = filtered;
945 info.title = name;
946 info.intent = intent;
947 info.customIcon = customIcon;
948 info.iconResource = iconResource;
949
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 return info;
951 }
952
953 @Override
954 protected void onNewIntent(Intent intent) {
955 super.onNewIntent(intent);
956
957 // Close the menu
958 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
959 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700960
961 // Set this flag so that onResume knows to close the search dialog if it's open,
962 // because this was a new intent (thus a press of 'home' or some such) rather than
963 // for example onResume being called when the user pressed the 'back' button.
964 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965
966 try {
967 dismissDialog(DIALOG_CREATE_SHORTCUT);
968 // Unlock the workspace if the dialog was showing
969 mWorkspace.unlock();
970 } catch (Exception e) {
971 // An exception is thrown if the dialog is not visible, which is fine
972 }
973
974 try {
975 dismissDialog(DIALOG_RENAME_FOLDER);
976 // Unlock the workspace if the dialog was showing
977 mWorkspace.unlock();
978 } catch (Exception e) {
979 // An exception is thrown if the dialog is not visible, which is fine
980 }
981
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
983 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700984
Romain Guy1dd3a072009-07-16 13:21:01 -0700985 // TODO: This really should not be done here every time
986 final SharedPreferences preferences =
987 getSharedPreferences(GesturesActivity.PREFERENCES_NAME, MODE_PRIVATE);
988 final boolean homeKey = preferences.getBoolean(
989 GesturesActivity.PREFERENCES_HOME_KEY, false);
Romain Guy94406842009-06-17 16:18:58 -0700990
Romain Guy1dd3a072009-07-16 13:21:01 -0700991 if (!homeKey) {
992 if (!mWorkspace.isDefaultScreenShowing()) {
993 mWorkspace.moveToDefaultScreen();
994 }
Romain Guy94406842009-06-17 16:18:58 -0700995
Romain Guy1dd3a072009-07-16 13:21:01 -0700996 if (mGesturesWindow == null || mGesturesWindow.isShowing()) {
997 hideGesturesPanel();
998 }
999 } else {
1000 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE
1001 && (mDragLayer.hasWindowFocus() || (mGesturesWindow != null
1002 && mGesturesWindow.isShowing()))) {
1003
1004 SearchManager searchManager =
1005 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1006
1007 if (!searchManager.isVisible()) {
1008 onHomeKeyPressed();
1009 }
Romain Guyb8734242009-06-10 11:53:57 -07001010 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -07001014
1015 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 if (v != null && v.getWindowToken() != null) {
1017 InputMethodManager imm = (InputMethodManager)getSystemService(
1018 INPUT_METHOD_SERVICE);
1019 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1020 }
1021 } else {
1022 closeDrawer(false);
1023 }
1024 }
1025 }
1026
Romain Guy73b979d2009-06-09 12:57:21 -07001027 private void onHomeKeyPressed() {
1028 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
1029 showGesturesPanel();
1030 } else {
1031 hideGesturesPanel();
1032 }
1033 }
1034
1035 private void showGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -07001036 showGesturesPanel(true);
1037 }
1038
1039 private void showGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07001040 resetGesturesPrompt();
1041
Romain Guy1dd3a072009-07-16 13:21:01 -07001042 mGesturesAdd.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07001043
1044 mGesturesOverlay.clear(false);
1045
1046 PopupWindow window;
1047 if (mGesturesWindow == null) {
1048 mGesturesWindow = new PopupWindow(this);
1049 window = mGesturesWindow;
1050 window.setFocusable(true);
1051 window.setTouchable(true);
Romain Guy1dd3a072009-07-16 13:21:01 -07001052 window.setBackgroundDrawable(getResources().getDrawable(
1053 android.R.drawable.screen_background_dark));
Romain Guy73b979d2009-06-09 12:57:21 -07001054 window.setContentView(mGesturesPanel);
1055 } else {
1056 window = mGesturesWindow;
1057 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001058 window.setAnimationStyle(animate ? com.android.internal.R.style.Animation_Toast : 0);
Romain Guy73b979d2009-06-09 12:57:21 -07001059
1060 final int[] xy = new int[2];
1061 final DragLayer dragLayer = mDragLayer;
1062 dragLayer.getLocationOnScreen(xy);
1063
1064 window.setWidth(dragLayer.getWidth());
1065 window.setHeight(dragLayer.getHeight() - 1);
1066 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
1067 }
1068
1069 private void resetGesturesPrompt() {
1070 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07001071 mGesturesPrompt.setText(R.string.gestures_instructions);
1072 mGesturesPrompt.setVisibility(View.VISIBLE);
1073 mGesturesSend.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07001074 }
1075
Romain Guy1dd3a072009-07-16 13:21:01 -07001076 private void setGestureUnknown() {
Romain Guy73b979d2009-06-09 12:57:21 -07001077 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07001078 mGesturesPrompt.setText(R.string.gestures_unknown);
1079 mGesturesPrompt.setVisibility(View.VISIBLE);
1080 mGesturesSend.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07001081 }
1082
Romain Guy1dd3a072009-07-16 13:21:01 -07001083 private void setGestureAction(Drawable icon, CharSequence title) {
1084 mGesturesPrompt.setVisibility(View.GONE);
1085 mGesturesSend.setVisibility(View.VISIBLE);
1086 mGesturesSend.setText(title);
1087 mGesturesSend.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
Romain Guy3cf604f2009-06-16 13:12:53 -07001088 }
1089
Romain Guy73b979d2009-06-09 12:57:21 -07001090 void hideGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -07001091 hideGesturesPanel(true);
1092 }
1093
1094 void hideGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07001095 if (mGesturesWindow != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001096 final PopupWindow popupWindow = mGesturesWindow;
1097 popupWindow.setAnimationStyle(animate ?
Romain Guy1dd3a072009-07-16 13:21:01 -07001098 com.android.internal.R.style.Animation_Toast : 0);
Romain Guy3cf604f2009-06-16 13:12:53 -07001099 popupWindow.update();
1100 popupWindow.dismiss();
Romain Guy73b979d2009-06-09 12:57:21 -07001101 }
1102 }
1103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 @Override
1105 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1106 // Do not call super here
1107 mSavedInstanceState = savedInstanceState;
1108 }
1109
1110 @Override
1111 protected void onSaveInstanceState(Bundle outState) {
1112 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1113
1114 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1115 if (folders.size() > 0) {
1116 final int count = folders.size();
1117 long[] ids = new long[count];
1118 for (int i = 0; i < count; i++) {
1119 final FolderInfo info = folders.get(i).getInfo();
1120 ids[i] = info.id;
1121 }
1122 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1123 } else {
1124 super.onSaveInstanceState(outState);
1125 }
1126
Romain Guy3cf604f2009-06-16 13:12:53 -07001127 final boolean isConfigurationChange = getChangingConfigurations() != 0;
1128
Romain Guy5a941392009-04-28 15:18:25 -07001129 // When the drawer is opened and we are saving the state because of a
1130 // configuration change
Romain Guy3cf604f2009-06-16 13:12:53 -07001131 if (mDrawer.isOpened() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134
1135 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1136 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1137 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1138
1139 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1140 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1141 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1142 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1143 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1144 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1145 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1146 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1147 layout.getOccupiedCells());
1148 }
1149
1150 if (mFolderInfo != null && mWaitingForResult) {
1151 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1152 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1153 }
Romain Guy73b979d2009-06-09 12:57:21 -07001154
1155 if (mCurrentGesture != null && mWaitingForResult) {
1156 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1157 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001158
Romain Guy1ce1a242009-06-23 17:34:54 -07001159 if (mGesturesWindow != null && mGesturesWindow.isShowing()) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001160 outState.putBoolean(RUNTIME_STATE_GESTURES_PANEL, true);
1161
Romain Guy1ce1a242009-06-23 17:34:54 -07001162 if (mCurrentGesture == null || !mWaitingForResult) {
1163 final Gesture gesture = mGesturesOverlay.getGesture();
1164 if (gesture != null) {
1165 outState.putParcelable(RUNTIME_STATE_GESTURES_PANEL_GESTURE, gesture);
1166 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001167 }
1168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170
1171 @Override
1172 public void onDestroy() {
1173 mDestroyed = true;
1174
1175 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001178 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001180 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 }
1182
1183 TextKeyListener.getInstance().release();
1184
Romain Guy3cf604f2009-06-16 13:12:53 -07001185 hideGesturesPanel(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 mAllAppsGrid.clearTextFilter();
1187 mAllAppsGrid.setAdapter(null);
1188 sModel.unbind();
1189 sModel.abortLoaders();
1190
1191 getContentResolver().unregisterContentObserver(mObserver);
1192 unregisterReceiver(mApplicationsReceiver);
1193 }
1194
1195 @Override
1196 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001197 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 super.startActivityForResult(intent, requestCode);
1199 }
1200
1201 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001202 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001204
1205 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001206
Karl Rosaen138a0412009-04-23 19:00:21 -07001207 // Slide the search widget to the top, if it's on the current screen,
1208 // otherwise show the search dialog immediately.
1209 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1210 if (searchWidget == null) {
1211 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1212 } else {
1213 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1214 // show the currently typed text in the search widget while sliding
1215 searchWidget.setQuery(getTypedText());
1216 }
1217 }
Romain Guycbb89e42009-06-08 15:52:54 -07001218
Karl Rosaen138a0412009-04-23 19:00:21 -07001219 /**
1220 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001221 *
1222 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001223 */
Romain Guycbb89e42009-06-08 15:52:54 -07001224 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001225 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001226
Karl Rosaen138a0412009-04-23 19:00:21 -07001227 if (initialQuery == null) {
1228 // Use any text typed in the launcher as the initial query
1229 initialQuery = getTypedText();
1230 clearTypedText();
1231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 if (appSearchData == null) {
1233 appSearchData = new Bundle();
1234 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1235 }
Romain Guycbb89e42009-06-08 15:52:54 -07001236
Karl Rosaen138a0412009-04-23 19:00:21 -07001237 final SearchManager searchManager =
1238 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1239
1240 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1241 if (searchWidget != null) {
1242 // This gets called when the user leaves the search dialog to go back to
1243 // the Launcher.
1244 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1245 public void onCancel() {
1246 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001247 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001248 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001249 });
1250 }
Romain Guycbb89e42009-06-08 15:52:54 -07001251
Karl Rosaen138a0412009-04-23 19:00:21 -07001252 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001253 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
1255
Karl Rosaen138a0412009-04-23 19:00:21 -07001256 /**
1257 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001258 */
Romain Guy5a941392009-04-28 15:18:25 -07001259 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001260 // Close search dialog
1261 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001262 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001263 // Restore search widget to its normal position
1264 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1265 if (searchWidget != null) {
1266 searchWidget.stopSearch(false);
1267 }
1268 }
Romain Guycbb89e42009-06-08 15:52:54 -07001269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 @Override
1271 public boolean onCreateOptionsMenu(Menu menu) {
1272 if (mDesktopLocked) return false;
1273
1274 super.onCreateOptionsMenu(menu);
1275 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1276 .setIcon(android.R.drawable.ic_menu_add)
1277 .setAlphabeticShortcut('A');
1278 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1279 .setIcon(android.R.drawable.ic_menu_gallery)
1280 .setAlphabeticShortcut('W');
1281 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1282 .setIcon(android.R.drawable.ic_search_category_default)
1283 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1284 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1285 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1286 .setAlphabeticShortcut('N');
1287
Romain Guy1dd3a072009-07-16 13:21:01 -07001288 // TODO: Remove
Romain Guy73b979d2009-06-09 12:57:21 -07001289 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
Romain Guy1dd3a072009-07-16 13:21:01 -07001290 .setIcon(com.android.internal.R.drawable.ic_menu_compose)
1291 .setAlphabeticShortcut('G');
Romain Guy73b979d2009-06-09 12:57:21 -07001292
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001294 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1295 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296
1297 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1298 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1299 .setIntent(settings);
1300
1301 return true;
1302 }
1303
1304 @Override
1305 public boolean onPrepareOptionsMenu(Menu menu) {
1306 super.onPrepareOptionsMenu(menu);
1307
1308 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1309 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1310
1311 return true;
1312 }
1313
1314 @Override
1315 public boolean onOptionsItemSelected(MenuItem item) {
1316 switch (item.getItemId()) {
1317 case MENU_ADD:
1318 addItems();
1319 return true;
1320 case MENU_WALLPAPER_SETTINGS:
1321 startWallpaper();
1322 return true;
1323 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001324 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 return true;
1326 case MENU_NOTIFICATIONS:
1327 showNotifications();
1328 return true;
Romain Guy1dd3a072009-07-16 13:21:01 -07001329 case MENU_GESTURES:
1330 showGesturesPanel();
1331 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 }
1333
1334 return super.onOptionsItemSelected(item);
1335 }
Romain Guycbb89e42009-06-08 15:52:54 -07001336
Karl Rosaen138a0412009-04-23 19:00:21 -07001337 /**
1338 * Indicates that we want global search for this activity by setting the globalSearch
1339 * argument for {@link #startSearch} to true.
1340 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001342 @Override
1343 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001344 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001345 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001346 }
1347
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 private void addItems() {
1349 showAddDialog(mMenuAddInfo);
1350 }
1351
1352 private void removeShortcutsForPackage(String packageName) {
1353 if (packageName != null && packageName.length() > 0) {
1354 mWorkspace.removeShortcutsForPackage(packageName);
1355 }
1356 }
Romain Guycbb89e42009-06-08 15:52:54 -07001357
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001358 private void updateShortcutsForPackage(String packageName) {
1359 if (packageName != null && packageName.length() > 0) {
1360 mWorkspace.updateShortcutsForPackage(packageName);
1361 }
1362 }
1363
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001364 void addAppWidget(Intent data) {
1365 // TODO: catch bad widget exception when sent
1366 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001368 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1369 if (SEARCH_WIDGET.equals(customWidget)) {
1370 // We don't need this any more, since this isn't a real app widget.
1371 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1372 // add the search widget
1373 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001375 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1376
1377 if (appWidget.configure != null) {
1378 // Launch over to configure widget, if needed
1379 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1380 intent.setComponent(appWidget.configure);
1381 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1382
1383 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1384 } else {
1385 // Otherwise just add it
1386 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 }
1389 }
Romain Guycbb89e42009-06-08 15:52:54 -07001390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 void addSearch() {
1392 final Widget info = Widget.makeSearch();
1393 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 final int[] xy = mCellCoordinates;
1396 final int spanX = info.spanX;
1397 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001398
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 sModel.addDesktopItem(info);
1402 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1403 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 final View view = mInflater.inflate(info.layoutResource, null);
1406 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001407 Search search = (Search) view.findViewById(R.id.widget_search);
1408 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001409
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1411 }
1412
Romain Guy73b979d2009-06-09 12:57:21 -07001413 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001414 // Handle case where user selected "Applications"
1415 String applicationName = getResources().getString(R.string.group_applications);
1416 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001417
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001418 if (applicationName != null && applicationName.equals(shortcutName)) {
1419 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1420 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001421
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001422 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1423 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001424 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001425 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001426 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
1429
1430 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001431 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001432 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001433 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001434
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001435 if (folderName != null && folderName.equals(shortcutName)) {
1436 addFolder(!mDesktopLocked);
1437 } else {
1438 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1439 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 }
1441
1442 void addFolder(boolean insertAtFirst) {
1443 UserFolderInfo folderInfo = new UserFolderInfo();
1444 folderInfo.title = getText(R.string.folder_name);
1445
1446 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1447 cellInfo.screen = mWorkspace.getCurrentScreen();
1448 if (!findSingleSlot(cellInfo)) return;
1449
1450 // Update the model
1451 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1452 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1453 sModel.addDesktopItem(folderInfo);
1454 sModel.addFolder(folderInfo);
1455
1456 // Create the view
1457 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1458 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1459 mWorkspace.addInCurrentScreen(newFolder,
1460 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1461 }
Romain Guycbb89e42009-06-08 15:52:54 -07001462
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1464 boolean insertAtFirst) {
1465 cellInfo.screen = mWorkspace.getCurrentScreen();
1466 if (!findSingleSlot(cellInfo)) return;
1467
1468 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1469
1470 if (!mRestoring) {
1471 sModel.addDesktopItem(info);
1472
1473 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1474 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1475 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1476 } else if (sModel.isDesktopLoaded()) {
1477 sModel.addDesktopItem(info);
1478 }
1479 }
1480
1481 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1482 CellLayout.CellInfo cellInfo, boolean notify) {
1483
1484 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1485 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1486
1487 Drawable icon = null;
1488 boolean filtered = false;
1489 Intent.ShortcutIconResource iconResource = null;
1490
1491 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1492 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1493 try {
1494 iconResource = (Intent.ShortcutIconResource) extra;
1495 final PackageManager packageManager = context.getPackageManager();
1496 Resources resources = packageManager.getResourcesForApplication(
1497 iconResource.packageName);
1498 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1499 icon = resources.getDrawable(id);
1500 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001501 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 }
1503 }
1504
1505 if (icon == null) {
1506 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1507 }
1508
1509 final LiveFolderInfo info = new LiveFolderInfo();
1510 info.icon = icon;
1511 info.filtered = filtered;
1512 info.title = name;
1513 info.iconResource = iconResource;
1514 info.uri = data.getData();
1515 info.baseIntent = baseIntent;
1516 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1517 LiveFolders.DISPLAY_MODE_GRID);
1518
1519 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1520 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1521 sModel.addFolder(info);
1522
1523 return info;
1524 }
1525
1526 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1527 final int[] xy = new int[2];
1528 if (findSlot(cellInfo, xy, 1, 1)) {
1529 cellInfo.cellX = xy[0];
1530 cellInfo.cellY = xy[1];
1531 return true;
1532 }
1533 return false;
1534 }
1535
1536 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1537 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1538 boolean[] occupied = mSavedState != null ?
1539 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1540 cellInfo = mWorkspace.findAllVacantCells(occupied);
1541 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1542 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1543 return false;
1544 }
1545 }
1546 return true;
1547 }
1548
1549 private void showNotifications() {
1550 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1551 if (statusBar != null) {
1552 statusBar.expand();
1553 }
1554 }
1555
1556 private void startWallpaper() {
1557 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1558 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1559 }
1560
1561 /**
1562 * Registers various intent receivers. The current implementation registers
1563 * only a wallpaper intent receiver to let other applications change the
1564 * wallpaper.
1565 */
1566 private void registerIntentReceivers() {
1567 if (sWallpaperReceiver == null) {
1568 final Application application = getApplication();
1569
1570 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1571
1572 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1573 application.registerReceiver(sWallpaperReceiver, filter);
1574 } else {
1575 sWallpaperReceiver.setLauncher(this);
1576 }
1577
1578 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1579 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1580 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1581 filter.addDataScheme("package");
1582 registerReceiver(mApplicationsReceiver, filter);
1583 }
1584
1585 /**
1586 * Registers various content observers. The current implementation registers
1587 * only a favorites observer to keep track of the favorites applications.
1588 */
1589 private void registerContentObservers() {
1590 ContentResolver resolver = getContentResolver();
1591 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1592 }
1593
1594 @Override
1595 public boolean dispatchKeyEvent(KeyEvent event) {
1596 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1597 switch (event.getKeyCode()) {
1598 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001599 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001600 if (mDrawer.isOpened()) {
1601 closeDrawer();
1602 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001603 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001604 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 return true;
1606 case KeyEvent.KEYCODE_HOME:
1607 return true;
1608 }
1609 }
1610
1611 return super.dispatchKeyEvent(event);
1612 }
1613
1614 private void closeDrawer() {
1615 closeDrawer(true);
1616 }
1617
1618 private void closeDrawer(boolean animated) {
1619 if (mDrawer.isOpened()) {
1620 if (animated) {
1621 mDrawer.animateClose();
1622 } else {
1623 mDrawer.close();
1624 }
1625 if (mDrawer.hasFocus()) {
1626 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1627 }
1628 }
1629 }
1630
1631 private void closeFolder() {
1632 Folder folder = mWorkspace.getOpenFolder();
1633 if (folder != null) {
1634 closeFolder(folder);
1635 }
1636 }
1637
1638 void closeFolder(Folder folder) {
1639 folder.getInfo().opened = false;
1640 ViewGroup parent = (ViewGroup) folder.getParent();
1641 if (parent != null) {
1642 parent.removeView(folder);
1643 }
1644 folder.onClose();
1645 }
1646
1647 /**
1648 * When the notification that favorites have changed is received, requests
1649 * a favorites list refresh.
1650 */
1651 private void onFavoritesChanged() {
1652 mDesktopLocked = true;
1653 mDrawer.lock();
1654 sModel.loadUserItems(false, this, false, false);
1655 }
1656
1657 void onDesktopItemsLoaded() {
1658 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 bindDesktopItems();
1660 }
Romain Guycbb89e42009-06-08 15:52:54 -07001661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 /**
1663 * Refreshes the shortcuts shown on the workspace.
1664 */
1665 private void bindDesktopItems() {
1666 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001667 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001668 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1669 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 return;
1671 }
1672
1673 final Workspace workspace = mWorkspace;
1674 int count = workspace.getChildCount();
1675 for (int i = 0; i < count; i++) {
1676 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1677 }
Romain Guycbb89e42009-06-08 15:52:54 -07001678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 if (DEBUG_USER_INTERFACE) {
1680 android.widget.Button finishButton = new android.widget.Button(this);
1681 finishButton.setText("Finish");
1682 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1683
1684 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1685 public void onClick(View v) {
1686 finish();
1687 }
1688 });
1689 }
Romain Guycbb89e42009-06-08 15:52:54 -07001690
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001691 // Flag any old binder to terminate early
1692 if (mBinder != null) {
1693 mBinder.mTerminate = true;
1694 }
Romain Guycbb89e42009-06-08 15:52:54 -07001695
Karl Rosaen138a0412009-04-23 19:00:21 -07001696 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001697 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 }
1699
1700 private void bindItems(Launcher.DesktopBinder binder,
1701 ArrayList<ItemInfo> shortcuts, int start, int count) {
1702
1703 final Workspace workspace = mWorkspace;
1704 final boolean desktopLocked = mDesktopLocked;
1705
1706 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1707 int i = start;
1708
1709 for ( ; i < end; i++) {
1710 final ItemInfo item = shortcuts.get(i);
1711 switch (item.itemType) {
1712 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1713 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1714 final View shortcut = createShortcut((ApplicationInfo) item);
1715 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1716 !desktopLocked);
1717 break;
1718 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1719 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1720 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1721 (UserFolderInfo) item);
1722 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1723 !desktopLocked);
1724 break;
1725 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1726 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1727 R.layout.live_folder_icon, this,
1728 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1729 (LiveFolderInfo) item);
1730 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1731 !desktopLocked);
1732 break;
1733 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1734 final int screen = workspace.getCurrentScreen();
1735 final View view = mInflater.inflate(R.layout.widget_search,
1736 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001737
Karl Rosaen138a0412009-04-23 19:00:21 -07001738 Search search = (Search) view.findViewById(R.id.widget_search);
1739 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001740
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 final Widget widget = (Widget) item;
1742 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001743
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744 workspace.addWidget(view, widget, !desktopLocked);
1745 break;
1746 }
1747 }
1748
1749 workspace.requestLayout();
1750
1751 if (end >= count) {
1752 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001753 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 } else {
1755 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1756 }
1757 }
1758
1759 private void finishBindDesktopItems() {
1760 if (mSavedState != null) {
1761 if (!mWorkspace.hasFocus()) {
1762 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1763 }
1764
1765 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1766 if (userFolders != null) {
1767 for (long folderId : userFolders) {
1768 final FolderInfo info = sModel.findFolderById(folderId);
1769 if (info != null) {
1770 openFolder(info);
1771 }
1772 }
1773 final Folder openFolder = mWorkspace.getOpenFolder();
1774 if (openFolder != null) {
1775 openFolder.requestFocus();
1776 }
1777 }
1778
1779 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1780 if (allApps) {
1781 mDrawer.open();
1782 }
1783
1784 mSavedState = null;
1785 }
1786
1787 if (mSavedInstanceState != null) {
1788 super.onRestoreInstanceState(mSavedInstanceState);
1789 mSavedInstanceState = null;
1790 }
1791
1792 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1793 mDrawer.requestFocus();
1794 }
1795
1796 mDesktopLocked = false;
1797 mDrawer.unlock();
1798 }
Romain Guycbb89e42009-06-08 15:52:54 -07001799
Karl Rosaen138a0412009-04-23 19:00:21 -07001800 private void bindDrawer(Launcher.DesktopBinder binder,
1801 ApplicationsAdapter drawerAdapter) {
1802 mAllAppsGrid.setAdapter(drawerAdapter);
1803 binder.startBindingAppWidgetsWhenIdle();
1804 }
Romain Guycbb89e42009-06-08 15:52:54 -07001805
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001806 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001807 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001808
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 final Workspace workspace = mWorkspace;
1810 final boolean desktopLocked = mDesktopLocked;
1811
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001812 if (!appWidgets.isEmpty()) {
1813 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001814
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001815 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001816 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001817 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001818
Karl Rosaen138a0412009-04-23 19:00:21 -07001819 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001820
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001821 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1822 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001823
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001824 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1825 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001826
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001827 workspace.requestLayout();
1828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001830 if (appWidgets.isEmpty()) {
1831 if (PROFILE_ROTATE) {
1832 android.os.Debug.stopMethodTracing();
1833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001835 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
1837 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001838
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 DragController getDragController() {
1840 return mDragLayer;
1841 }
1842
1843 /**
1844 * Launches the intent referred by the clicked shortcut.
1845 *
1846 * @param v The view representing the clicked shortcut.
1847 */
1848 public void onClick(View v) {
1849 Object tag = v.getTag();
1850 if (tag instanceof ApplicationInfo) {
1851 // Open shortcut
1852 final Intent intent = ((ApplicationInfo) tag).intent;
1853 startActivitySafely(intent);
1854 } else if (tag instanceof FolderInfo) {
1855 handleFolderClick((FolderInfo) tag);
1856 }
1857 }
1858
1859 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001860 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1862 try {
1863 startActivity(intent);
1864 } catch (ActivityNotFoundException e) {
1865 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1866 } catch (SecurityException e) {
1867 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001868 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1870 "or use the exported attribute for this activity.", e);
1871 }
1872 }
1873
1874 private void handleFolderClick(FolderInfo folderInfo) {
1875 if (!folderInfo.opened) {
1876 // Close any open folder
1877 closeFolder();
1878 // Open the requested folder
1879 openFolder(folderInfo);
1880 } else {
1881 // Find the open folder...
1882 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1883 int folderScreen;
1884 if (openFolder != null) {
1885 folderScreen = mWorkspace.getScreenForView(openFolder);
1886 // .. and close it
1887 closeFolder(openFolder);
1888 if (folderScreen != mWorkspace.getCurrentScreen()) {
1889 // Close any folder open on the current screen
1890 closeFolder();
1891 // Pull the folder onto this screen
1892 openFolder(folderInfo);
1893 }
1894 }
1895 }
1896 }
1897
1898 private void loadWallpaper() {
1899 // The first time the application is started, we load the wallpaper from
1900 // the ApplicationContext
1901 if (sWallpaper == null) {
1902 final Drawable drawable = getWallpaper();
1903 if (drawable instanceof BitmapDrawable) {
1904 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1905 } else {
1906 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1907 }
1908 }
1909 mWorkspace.loadWallpaper(sWallpaper);
1910 }
1911
1912 /**
1913 * Opens the user fodler described by the specified tag. The opening of the folder
1914 * is animated relative to the specified View. If the View is null, no animation
1915 * is played.
1916 *
1917 * @param folderInfo The FolderInfo describing the folder to open.
1918 */
1919 private void openFolder(FolderInfo folderInfo) {
1920 Folder openFolder;
1921
1922 if (folderInfo instanceof UserFolderInfo) {
1923 openFolder = UserFolder.fromXml(this);
1924 } else if (folderInfo instanceof LiveFolderInfo) {
1925 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1926 } else {
1927 return;
1928 }
1929
1930 openFolder.setDragger(mDragLayer);
1931 openFolder.setLauncher(this);
1932
1933 openFolder.bind(folderInfo);
1934 folderInfo.opened = true;
1935
1936 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1937 openFolder.onOpen();
1938 }
1939
1940 /**
1941 * Returns true if the workspace is being loaded. When the workspace is loading,
1942 * no user interaction should be allowed to avoid any conflict.
1943 *
1944 * @return True if the workspace is locked, false otherwise.
1945 */
1946 boolean isWorkspaceLocked() {
1947 return mDesktopLocked;
1948 }
1949
1950 public boolean onLongClick(View v) {
1951 if (mDesktopLocked) {
1952 return false;
1953 }
1954
1955 if (!(v instanceof CellLayout)) {
1956 v = (View) v.getParent();
1957 }
1958
1959 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1960
1961 // This happens when long clicking an item with the dpad/trackball
1962 if (cellInfo == null) {
1963 return true;
1964 }
1965
1966 if (mWorkspace.allowLongPress()) {
1967 if (cellInfo.cell == null) {
1968 if (cellInfo.valid) {
1969 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001970 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 showAddDialog(cellInfo);
1972 }
1973 } else {
1974 if (!(cellInfo.cell instanceof Folder)) {
1975 // User long pressed on an item
1976 mWorkspace.startDrag(cellInfo);
1977 }
1978 }
1979 }
1980 return true;
1981 }
1982
1983 static LauncherModel getModel() {
1984 return sModel;
1985 }
1986
Romain Guy73b979d2009-06-09 12:57:21 -07001987 static GestureLibrary getGestureLibrary() {
1988 return sLibrary;
1989 }
1990
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 void closeAllApplications() {
1992 mDrawer.close();
1993 }
1994
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001995 View getDrawerHandle() {
1996 return mHandleView;
1997 }
1998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 boolean isDrawerDown() {
2000 return !mDrawer.isMoving() && !mDrawer.isOpened();
2001 }
2002
2003 boolean isDrawerUp() {
2004 return mDrawer.isOpened() && !mDrawer.isMoving();
2005 }
2006
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002007 boolean isDrawerMoving() {
2008 return mDrawer.isMoving();
2009 }
2010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 Workspace getWorkspace() {
2012 return mWorkspace;
2013 }
2014
2015 GridView getApplicationsGrid() {
2016 return mAllAppsGrid;
2017 }
2018
2019 @Override
2020 protected Dialog onCreateDialog(int id) {
2021 switch (id) {
2022 case DIALOG_CREATE_SHORTCUT:
2023 return new CreateShortcut().createDialog();
2024 case DIALOG_RENAME_FOLDER:
2025 return new RenameFolder().createDialog();
2026 }
2027
2028 return super.onCreateDialog(id);
2029 }
2030
2031 @Override
2032 protected void onPrepareDialog(int id, Dialog dialog) {
2033 switch (id) {
2034 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 break;
2036 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002037 if (mFolderInfo != null) {
2038 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2039 final CharSequence text = mFolderInfo.title;
2040 input.setText(text);
2041 input.setSelection(0, text.length());
2042 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 break;
2044 }
2045 }
2046
2047 void showRenameDialog(FolderInfo info) {
2048 mFolderInfo = info;
2049 mWaitingForResult = true;
2050 showDialog(DIALOG_RENAME_FOLDER);
2051 }
2052
2053 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2054 mAddItemCellInfo = cellInfo;
2055 mWaitingForResult = true;
2056 showDialog(DIALOG_CREATE_SHORTCUT);
2057 }
2058
Romain Guy73b979d2009-06-09 12:57:21 -07002059 private void pickShortcut(int requestCode, int title) {
2060 Bundle bundle = new Bundle();
2061
2062 ArrayList<String> shortcutNames = new ArrayList<String>();
2063 shortcutNames.add(getString(R.string.group_applications));
2064 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2065
2066 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2067 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2068 R.drawable.ic_launcher_application));
2069 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2070
2071 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2072 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
2073 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
2074 pickIntent.putExtras(bundle);
2075
2076 startActivityForResult(pickIntent, requestCode);
2077 }
2078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 private class RenameFolder {
2080 private EditText mInput;
2081
2082 Dialog createDialog() {
2083 mWaitingForResult = true;
2084 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2085 mInput = (EditText) layout.findViewById(R.id.folder_name);
2086
2087 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2088 builder.setIcon(0);
2089 builder.setTitle(getString(R.string.rename_folder_title));
2090 builder.setCancelable(true);
2091 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2092 public void onCancel(DialogInterface dialog) {
2093 cleanup();
2094 }
2095 });
2096 builder.setNegativeButton(getString(R.string.cancel_action),
2097 new Dialog.OnClickListener() {
2098 public void onClick(DialogInterface dialog, int which) {
2099 cleanup();
2100 }
2101 }
2102 );
2103 builder.setPositiveButton(getString(R.string.rename_action),
2104 new Dialog.OnClickListener() {
2105 public void onClick(DialogInterface dialog, int which) {
2106 changeFolderName();
2107 }
2108 }
2109 );
2110 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002111
2112 final AlertDialog dialog = builder.create();
2113 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2114 public void onShow(DialogInterface dialog) {
2115 mWorkspace.lock();
2116 }
2117 });
2118
2119 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 }
2121
2122 private void changeFolderName() {
2123 final String name = mInput.getText().toString();
2124 if (!TextUtils.isEmpty(name)) {
2125 // Make sure we have the right folder info
2126 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
2127 mFolderInfo.title = name;
2128 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2129
2130 if (mDesktopLocked) {
2131 mDrawer.lock();
2132 sModel.loadUserItems(false, Launcher.this, false, false);
2133 } else {
2134 final FolderIcon folderIcon = (FolderIcon)
2135 mWorkspace.getViewForTag(mFolderInfo);
2136 if (folderIcon != null) {
2137 folderIcon.setText(name);
2138 getWorkspace().requestLayout();
2139 } else {
2140 mDesktopLocked = true;
2141 mDrawer.lock();
2142 sModel.loadUserItems(false, Launcher.this, false, false);
2143 }
2144 }
2145 }
2146 cleanup();
2147 }
2148
2149 private void cleanup() {
2150 mWorkspace.unlock();
2151 dismissDialog(DIALOG_RENAME_FOLDER);
2152 mWaitingForResult = false;
2153 mFolderInfo = null;
2154 }
2155 }
2156
2157 /**
2158 * Displays the shortcut creation dialog and launches, if necessary, the
2159 * appropriate activity.
2160 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002161 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002162 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2163 DialogInterface.OnShowListener {
2164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 Dialog createDialog() {
2168 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2173 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002174 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 builder.setInverseBackgroundForced(true);
2177
2178 AlertDialog dialog = builder.create();
2179 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002180 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002181 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 return dialog;
2184 }
2185
2186 public void onCancel(DialogInterface dialog) {
2187 mWaitingForResult = false;
2188 cleanup();
2189 }
2190
Romain Guycbb89e42009-06-08 15:52:54 -07002191 public void onDismiss(DialogInterface dialog) {
2192 mWorkspace.unlock();
2193 }
2194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 private void cleanup() {
2196 mWorkspace.unlock();
2197 dismissDialog(DIALOG_CREATE_SHORTCUT);
2198 }
2199
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002200 /**
2201 * Handle the action clicked in the "Add to home" dialog.
2202 */
2203 public void onClick(DialogInterface dialog, int which) {
2204 Resources res = getResources();
2205 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002206
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002207 switch (which) {
2208 case AddAdapter.ITEM_SHORTCUT: {
2209 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002210 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002211 break;
2212 }
Romain Guycbb89e42009-06-08 15:52:54 -07002213
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002214 case AddAdapter.ITEM_APPWIDGET: {
2215 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002216
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002217 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2218 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2219 // add the search widget
2220 ArrayList<AppWidgetProviderInfo> customInfo =
2221 new ArrayList<AppWidgetProviderInfo>();
2222 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2223 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2224 info.label = getString(R.string.group_search);
2225 info.icon = R.drawable.ic_search_widget;
2226 customInfo.add(info);
2227 pickIntent.putParcelableArrayListExtra(
2228 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2229 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2230 Bundle b = new Bundle();
2231 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2232 customExtras.add(b);
2233 pickIntent.putParcelableArrayListExtra(
2234 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2235 // start the pick activity
2236 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2237 break;
2238 }
Romain Guycbb89e42009-06-08 15:52:54 -07002239
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002240 case AddAdapter.ITEM_LIVE_FOLDER: {
2241 // Insert extra item to handle inserting folder
2242 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002243
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002244 ArrayList<String> shortcutNames = new ArrayList<String>();
2245 shortcutNames.add(res.getString(R.string.group_folder));
2246 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002247
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002248 ArrayList<ShortcutIconResource> shortcutIcons =
2249 new ArrayList<ShortcutIconResource>();
2250 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2251 R.drawable.ic_launcher_folder));
2252 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2253
2254 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2255 pickIntent.putExtra(Intent.EXTRA_INTENT,
2256 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2257 pickIntent.putExtra(Intent.EXTRA_TITLE,
2258 getText(R.string.title_select_live_folder));
2259 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002260
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002261 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2262 break;
2263 }
2264
2265 case AddAdapter.ITEM_WALLPAPER: {
2266 startWallpaper();
2267 break;
2268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 }
2270 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002271
2272 public void onShow(DialogInterface dialog) {
2273 mWorkspace.lock();
2274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275 }
2276
2277 /**
2278 * Receives notifications when applications are added/removed.
2279 */
2280 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2281 @Override
2282 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002283 final String action = intent.getAction();
2284 final String packageName = intent.getData().getSchemeSpecificPart();
2285 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2286
2287 if (LauncherModel.DEBUG_LOADERS) {
2288 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2289 ", replacing=" + replacing);
2290 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002292
2293 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2294 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2295 if (!replacing) {
2296 removeShortcutsForPackage(packageName);
2297 if (LauncherModel.DEBUG_LOADERS) {
2298 d(LauncherModel.LOG_TAG, " --> remove package");
2299 }
2300 sModel.removePackage(Launcher.this, packageName);
2301 }
2302 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2303 // later, we will update the package at this time
2304 } else {
2305 if (!replacing) {
2306 if (LauncherModel.DEBUG_LOADERS) {
2307 d(LauncherModel.LOG_TAG, " --> add package");
2308 }
2309 sModel.addPackage(Launcher.this, packageName);
2310 } else {
2311 if (LauncherModel.DEBUG_LOADERS) {
2312 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2313 }
2314 sModel.updatePackage(Launcher.this, packageName);
2315 updateShortcutsForPackage(packageName);
2316 }
2317 }
2318 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002320 if (LauncherModel.DEBUG_LOADERS) {
2321 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2322 }
2323 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324 }
2325 }
2326 }
2327
2328 /**
2329 * Receives notifications whenever the user favorites have changed.
2330 */
2331 private class FavoritesChangeObserver extends ContentObserver {
2332 public FavoritesChangeObserver() {
2333 super(new Handler());
2334 }
2335
2336 @Override
2337 public void onChange(boolean selfChange) {
2338 onFavoritesChanged();
2339 }
2340 }
2341
2342 /**
2343 * Receives intents from other applications to change the wallpaper.
2344 */
2345 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2346 private final Application mApplication;
2347 private WeakReference<Launcher> mLauncher;
2348
2349 WallpaperIntentReceiver(Application application, Launcher launcher) {
2350 mApplication = application;
2351 setLauncher(launcher);
2352 }
2353
2354 void setLauncher(Launcher launcher) {
2355 mLauncher = new WeakReference<Launcher>(launcher);
2356 }
2357
2358 @Override
2359 public void onReceive(Context context, Intent intent) {
2360 // Load the wallpaper from the ApplicationContext and store it locally
2361 // until the Launcher Activity is ready to use it
2362 final Drawable drawable = mApplication.getWallpaper();
2363 if (drawable instanceof BitmapDrawable) {
2364 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2365 } else {
2366 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2367 }
2368
2369 // If Launcher is alive, notify we have a new wallpaper
2370 if (mLauncher != null) {
2371 final Launcher launcher = mLauncher.get();
2372 if (launcher != null) {
2373 launcher.loadWallpaper();
2374 }
2375 }
2376 }
2377 }
2378
2379 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2380 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2381 private boolean mOpen;
2382
2383 public void onDrawerOpened() {
2384 if (!mOpen) {
2385 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002387 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002388 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389
2390 mOpen = true;
2391 }
2392 }
2393
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002394 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2395 view.getDrawingRect(bounds);
2396
2397 while (view != mDragLayer) {
2398 bounds.offset(view.getLeft(), view.getTop());
2399 view = (View) view.getParent();
2400 }
2401 }
2402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 public void onDrawerClosed() {
2404 if (mOpen) {
2405 mHandleIcon.reverseTransition(150);
2406 mWorkspace.mDrawerBounds.setEmpty();
2407 mOpen = false;
2408 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002409
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 mAllAppsGrid.setSelection(0);
2411 mAllAppsGrid.clearTextFilter();
2412 }
2413
2414 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002415 if (PROFILE_DRAWER) {
2416 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2417 }
2418
2419 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2420 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 }
2422
2423 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002424 if (PROFILE_DRAWER) {
2425 android.os.Debug.stopMethodTracing();
2426 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 }
2428 }
2429
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002430 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002432 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002433 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002434
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 // Number of items to bind in every pass
2436 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437
2438 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002439 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002440 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002442
Karl Rosaen138a0412009-04-23 19:00:21 -07002443 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444
2445 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002446 ArrayList<LauncherAppWidgetInfo> appWidgets,
2447 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448
2449 mLauncher = new WeakReference<Launcher>(launcher);
2450 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002451 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002452
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002453 // Sort widgets so active workspace is bound first
2454 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2455 final int size = appWidgets.size();
2456 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002457
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002458 for (int i = 0; i < size; i++) {
2459 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2460 if (appWidgetInfo.screen == currentScreen) {
2461 mAppWidgets.addFirst(appWidgetInfo);
2462 } else {
2463 mAppWidgets.addLast(appWidgetInfo);
2464 }
2465 }
2466 }
Romain Guycbb89e42009-06-08 15:52:54 -07002467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 public void startBindingItems() {
2469 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2470 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002471
2472 public void startBindingDrawer() {
2473 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2474 }
Romain Guycbb89e42009-06-08 15:52:54 -07002475
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002476 public void startBindingAppWidgetsWhenIdle() {
2477 // Ask for notification when message queue becomes idle
2478 final MessageQueue messageQueue = Looper.myQueue();
2479 messageQueue.addIdleHandler(this);
2480 }
Romain Guycbb89e42009-06-08 15:52:54 -07002481
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002482 public boolean queueIdle() {
2483 // Queue is idle, so start binding items
2484 startBindingAppWidgets();
2485 return false;
2486 }
2487
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002488 public void startBindingAppWidgets() {
2489 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2490 }
2491
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 @Override
2493 public void handleMessage(Message msg) {
2494 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002495 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 return;
2497 }
Romain Guycbb89e42009-06-08 15:52:54 -07002498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 switch (msg.what) {
2500 case MESSAGE_BIND_ITEMS: {
2501 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2502 break;
2503 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002504 case MESSAGE_BIND_DRAWER: {
2505 launcher.bindDrawer(this, mDrawerAdapter);
2506 break;
2507 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002508 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002509 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 break;
2511 }
2512 }
2513 }
2514 }
Romain Guy73b979d2009-06-09 12:57:21 -07002515
2516 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2517 GestureOverlayView.OnGesturePerformedListener {
2518
2519 private final GestureMatcher mMatcher = new GestureMatcher();
2520
2521 GesturesProcessor() {
2522 // TODO: Maybe the load should happen on a background thread?
2523 sLibrary.load();
2524 }
2525
2526 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002527 //noinspection PointlessBooleanExpression,ConstantConditions
2528 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2529 overlay.removeCallbacks(mMatcher);
Romain Guy1dd3a072009-07-16 13:21:01 -07002530 resetGesturesPrompt();
Romain Guy6fefcf12009-06-11 13:07:43 -07002531 }
Romain Guy73b979d2009-06-09 12:57:21 -07002532
Romain Guy1dd3a072009-07-16 13:21:01 -07002533 mGesturesAdd.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07002534 }
2535
2536 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2537 }
2538
2539 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2540 }
2541
2542 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002543 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2544 mMatcher.gesture = overlay.getGesture();
2545 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2546 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002547 if (mGesturesAction.intent != null) {
2548 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07002549 setGestureAction(null, getString(R.string.gestures_unknown));
Romain Guy1ce1a242009-06-23 17:34:54 -07002550 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002551 } else {
2552 mMatcher.run();
2553 }
Romain Guy73b979d2009-06-09 12:57:21 -07002554 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002555 overlay.removeCallbacks(mMatcher);
2556
2557 mMatcher.gesture = overlay.getGesture();
2558 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2559 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002560 if (mGesturesAction.intent != null) {
2561 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07002562 setGestureAction(null, getString(R.string.gestures_unknown));
Romain Guy1ce1a242009-06-23 17:34:54 -07002563 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002564 } else {
2565 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2566 }
Romain Guy73b979d2009-06-09 12:57:21 -07002567 }
2568 }
2569
Romain Guy3cf604f2009-06-16 13:12:53 -07002570 void matchGesture(Gesture gesture) {
Romain Guy1dd3a072009-07-16 13:21:01 -07002571 mGesturesAdd.setVisibility(View.VISIBLE);
Romain Guy73b979d2009-06-09 12:57:21 -07002572
2573 if (gesture != null) {
2574 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2575
2576 if (DEBUG_GESTURES) {
2577 for (Prediction p : predictions) {
2578 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2579 }
2580 }
2581
2582 boolean match = false;
2583 if (predictions.size() > 0) {
2584 final Prediction prediction = predictions.get(0);
2585 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2586 match = true;
2587
2588 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2589 if (info != null) {
Romain Guy1dd3a072009-07-16 13:21:01 -07002590 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002591 }
2592 }
2593 }
2594
2595 if (!match){
Romain Guy1ce1a242009-06-23 17:34:54 -07002596 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07002597 setGestureUnknown();
Romain Guy73b979d2009-06-09 12:57:21 -07002598 }
2599 }
2600 }
2601
2602 private void updatePrompt(ApplicationInfo info) {
Romain Guy1dd3a072009-07-16 13:21:01 -07002603 // TODO: BRING BACK
Romain Guy6fefcf12009-06-11 13:07:43 -07002604 if (mGesturesAction.intent != null &&
Romain Guy1ce1a242009-06-23 17:34:54 -07002605 info.intent.toUri(0).equals(mGesturesAction.intent.toUri(0)) &&
Romain Guy1dd3a072009-07-16 13:21:01 -07002606 info.title.equals(mGesturesSend.getText())) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002607 return;
2608 }
Romain Guy3cf604f2009-06-16 13:12:53 -07002609
Romain Guy1dd3a072009-07-16 13:21:01 -07002610 setGestureAction(info.icon, info.title);
Romain Guy3cf604f2009-06-16 13:12:53 -07002611
Romain Guy73b979d2009-06-09 12:57:21 -07002612 mGesturesAction.intent = info.intent;
2613 }
2614
2615 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002616 //noinspection PointlessBooleanExpression,ConstantConditions
2617 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2618 overlay.removeCallbacks(mMatcher);
2619 }
Romain Guy73b979d2009-06-09 12:57:21 -07002620 }
2621
2622 void addGesture(String name, Gesture gesture) {
2623 sLibrary.addGesture(name, gesture);
2624 // TODO: On a background thread?
2625 sLibrary.save();
2626 }
2627
2628 void update(ApplicationInfo info, Gesture gesture) {
2629 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002630 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002631 }
2632
2633 class GestureMatcher implements Runnable {
2634 Gesture gesture;
2635
2636 public void run() {
2637 if (gesture != null) {
2638 matchGesture(gesture);
2639 }
2640 }
2641 }
2642 }
2643
2644 private class GesturesAction implements View.OnClickListener {
2645 Intent intent;
2646
2647 public void onClick(View v) {
2648 if (intent != null) {
2649 startActivitySafely(intent);
2650 }
2651 }
2652 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002653}
Karl Rosaen138a0412009-04-23 19:00:21 -07002654