blob: 25ad4e57563a5e8c4af21d0455bcf5666f38fd3b [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;
45import android.graphics.drawable.BitmapDrawable;
46import android.graphics.drawable.Drawable;
47import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.os.Bundle;
49import android.os.Handler;
50import android.os.IBinder;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070051import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070052import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070053import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.os.Parcelable;
55import android.os.RemoteException;
56import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070057import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.text.Selection;
59import android.text.SpannableStringBuilder;
60import android.text.TextUtils;
61import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070062import static android.util.Log.*;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.view.KeyEvent;
65import android.view.LayoutInflater;
66import android.view.Menu;
67import android.view.MenuItem;
68import android.view.View;
69import android.view.ViewGroup;
Romain Guy73b979d2009-06-09 12:57:21 -070070import android.view.MotionEvent;
71import android.view.Gravity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.view.View.OnLongClickListener;
73import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.widget.EditText;
Karl Rosaen138a0412009-04-23 19:00:21 -070075import android.widget.GridView;
Karl Rosaen138a0412009-04-23 19:00:21 -070076import android.widget.SlidingDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.widget.TextView;
78import android.widget.Toast;
Romain Guy73b979d2009-06-09 12:57:21 -070079import android.widget.ImageView;
80import android.widget.PopupWindow;
Romain Guy1dd3a072009-07-16 13:21:01 -070081import android.widget.Button;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070082import android.appwidget.AppWidgetManager;
83import android.appwidget.AppWidgetProviderInfo;
Romain Guy73b979d2009-06-09 12:57:21 -070084import android.gesture.GestureOverlayView;
85import android.gesture.GestureLibraries;
86import android.gesture.GestureLibrary;
87import android.gesture.Gesture;
88import android.gesture.Prediction;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
90import java.lang.ref.WeakReference;
91import java.util.ArrayList;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070092import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070093import java.io.DataOutputStream;
94import java.io.FileNotFoundException;
95import java.io.IOException;
96import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
98/**
99 * Default launcher application.
100 */
101public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener {
102 static final String LOG_TAG = "Launcher";
103 static final boolean LOGD = false;
104
105 private static final boolean PROFILE_STARTUP = false;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700106 private static final boolean PROFILE_DRAWER = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700107 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 private static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700109 private static final boolean DEBUG_GESTURES = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Romain Guy6fefcf12009-06-11 13:07:43 -0700111 private static final boolean CONFIG_GESTURES_IMMEDIATE_MODE = true;
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 private static final int WALLPAPER_SCREENS_SPAN = 2;
114
115 private static final int MENU_GROUP_ADD = 1;
116 private static final int MENU_ADD = Menu.FIRST + 1;
117 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
118 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
119 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy73b979d2009-06-09 12:57:21 -0700120 private static final int MENU_GESTURES = MENU_NOTIFICATIONS + 1;
121 private static final int MENU_SETTINGS = MENU_GESTURES + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
123 private static final int REQUEST_CREATE_SHORTCUT = 1;
124 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700125 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int REQUEST_PICK_APPLICATION = 6;
127 private static final int REQUEST_PICK_SHORTCUT = 7;
128 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700129 private static final int REQUEST_PICK_APPWIDGET = 9;
Romain Guy73b979d2009-06-09 12:57:21 -0700130 private static final int REQUEST_PICK_GESTURE_ACTION = 10;
131 private static final int REQUEST_CREATE_GESTURE_ACTION = 11;
132 private static final int REQUEST_CREATE_GESTURE_APPLICATION_ACTION = 12;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
135
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700136 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
137 static final String SEARCH_WIDGET = "search_widget";
138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 static final int SCREEN_COUNT = 3;
140 static final int DEFAULT_SCREN = 1;
141 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700142 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
144 private static final int DIALOG_CREATE_SHORTCUT = 1;
Romain Guycbb89e42009-06-08 15:52:54 -0700145 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Romain Guy98d01652009-06-30 16:21:04 -0700147 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
149 // Type: int
150 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
151 // Type: boolean
152 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
153 // Type: long
154 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
169 // Type: int[]
170 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
171 // Type: boolean
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
173 // Type: long
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Romain Guy73b979d2009-06-09 12:57:21 -0700175 // Type: Gesture (Parcelable)
176 private static final String RUNTIME_STATE_PENDING_GESTURE = "launcher.gesture";
Romain Guy3cf604f2009-06-16 13:12:53 -0700177 // Type: boolean
178 private static final String RUNTIME_STATE_GESTURES_PANEL = "launcher.gesture_panel_showing";
179 // Type: Gesture (Parcelable)
180 private static final String RUNTIME_STATE_GESTURES_PANEL_GESTURE = "launcher.gesture_panel_gesture";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700182 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
184 private static Bitmap sWallpaper;
185
186 private static final Object sLock = new Object();
187 private static int sScreen = DEFAULT_SCREN;
188
189 private static WallpaperIntentReceiver sWallpaperReceiver;
190
Romain Guy73b979d2009-06-09 12:57:21 -0700191 private static GestureLibrary sLibrary;
192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
194 private final ContentObserver mObserver = new FavoritesChangeObserver();
195
196 private LayoutInflater mInflater;
197
198 private DragLayer mDragLayer;
199 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700200
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700201 private AppWidgetManager mAppWidgetManager;
202 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private CellLayout.CellInfo mAddItemCellInfo;
207 private CellLayout.CellInfo mMenuAddInfo;
208 private final int[] mCellCoordinates = new int[2];
209 private FolderInfo mFolderInfo;
210
211 private SlidingDrawer mDrawer;
212 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 private HandleView mHandleView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 private AllAppsGridView mAllAppsGrid;
215
216 private boolean mDesktopLocked = true;
217 private Bundle mSavedState;
218
219 private SpannableStringBuilder mDefaultKeySsb = null;
220
221 private boolean mDestroyed;
Mike LeBeau736cf282009-07-02 17:46:59 -0700222
223 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224
225 private boolean mRestoring;
226 private boolean mWaitingForResult;
227 private boolean mLocaleChanged;
228
229 private Bundle mSavedInstanceState;
230
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700231 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700232
Romain Guy73b979d2009-06-09 12:57:21 -0700233 private View mGesturesPanel;
234 private GestureOverlayView mGesturesOverlay;
Romain Guy73b979d2009-06-09 12:57:21 -0700235 private ImageView mGesturesAdd;
236 private PopupWindow mGesturesWindow;
237 private Launcher.GesturesProcessor mGesturesProcessor;
238 private Gesture mCurrentGesture;
239 private GesturesAction mGesturesAction;
Romain Guyaad5ef42009-06-10 02:48:37 -0700240 private boolean mHideGesturesPanel;
Romain Guy1dd3a072009-07-16 13:21:01 -0700241 private TextView mGesturesPrompt;
242 private Button mGesturesSend;
Romain Guy73b979d2009-06-09 12:57:21 -0700243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 @Override
245 protected void onCreate(Bundle savedInstanceState) {
246 super.onCreate(savedInstanceState);
247 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700248
Romain Guy6c8bbcb2009-07-17 16:17:40 -0700249 getGestureLibrary(this);
Romain Guy73b979d2009-06-09 12:57:21 -0700250
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700251 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700252
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700253 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
254 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700255
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 if (PROFILE_STARTUP) {
257 android.os.Debug.startMethodTracing("/sdcard/launcher");
258 }
259
260 checkForLocaleChange();
261 setWallpaperDimension();
262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 setContentView(R.layout.launcher);
264 setupViews();
265
266 registerIntentReceivers();
267 registerContentObservers();
268
269 mSavedState = savedInstanceState;
270 restoreState(mSavedState);
271
272 if (PROFILE_STARTUP) {
273 android.os.Debug.stopMethodTracing();
274 }
275
276 if (!mRestoring) {
277 startLoaders();
278 }
279
280 // For handling default keys
281 mDefaultKeySsb = new SpannableStringBuilder();
282 Selection.setSelection(mDefaultKeySsb, 0);
283 }
Romain Guycbb89e42009-06-08 15:52:54 -0700284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700286 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
287 readConfiguration(this, localeConfiguration);
288
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 final Configuration configuration = getResources().getConfiguration();
290
Romain Guy98d01652009-06-30 16:21:04 -0700291 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 final String locale = configuration.locale.toString();
293
Romain Guy98d01652009-06-30 16:21:04 -0700294 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 final int mcc = configuration.mcc;
296
Romain Guy98d01652009-06-30 16:21:04 -0700297 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 final int mnc = configuration.mnc;
299
300 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
301
302 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700303 localeConfiguration.locale = locale;
304 localeConfiguration.mcc = mcc;
305 localeConfiguration.mnc = mnc;
306
307 writeConfiguration(this, localeConfiguration);
308 }
309 }
310
311 private static class LocaleConfiguration {
312 public String locale;
313 public int mcc = -1;
314 public int mnc = -1;
315 }
316
317 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
318 DataInputStream in = null;
319 try {
320 in = new DataInputStream(context.openFileInput(PREFERENCES));
321 configuration.locale = in.readUTF();
322 configuration.mcc = in.readInt();
323 configuration.mnc = in.readInt();
324 } catch (FileNotFoundException e) {
325 // Ignore
326 } catch (IOException e) {
327 // Ignore
328 } finally {
329 if (in != null) {
330 try {
331 in.close();
332 } catch (IOException e) {
333 // Ignore
334 }
335 }
336 }
337 }
338
339 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
340 DataOutputStream out = null;
341 try {
342 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
343 out.writeUTF(configuration.locale);
344 out.writeInt(configuration.mcc);
345 out.writeInt(configuration.mnc);
346 out.flush();
347 } catch (FileNotFoundException e) {
348 // Ignore
349 } catch (IOException e) {
350 //noinspection ResultOfMethodCallIgnored
351 context.getFileStreamPath(PREFERENCES).delete();
352 } finally {
353 if (out != null) {
354 try {
355 out.close();
356 } catch (IOException e) {
357 // Ignore
358 }
359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 }
361 }
362
363 static int getScreen() {
364 synchronized (sLock) {
365 return sScreen;
366 }
367 }
368
369 static void setScreen(int screen) {
370 synchronized (sLock) {
371 sScreen = screen;
372 }
373 }
374
375 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700376 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
377 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 mRestoring = false;
380 }
381
382 private void setWallpaperDimension() {
383 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
384 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
385
386 Display display = getWindowManager().getDefaultDisplay();
387 boolean isPortrait = display.getWidth() < display.getHeight();
388
389 final int width = isPortrait ? display.getWidth() : display.getHeight();
390 final int height = isPortrait ? display.getHeight() : display.getWidth();
391 try {
392 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
393 } catch (RemoteException e) {
394 // System is dead!
395 }
396 }
397
398 @Override
399 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700400 mWaitingForResult = false;
401
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 // The pattern used here is that a user PICKs a specific application,
403 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700404
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
406 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700407
Romain Guy73b979d2009-06-09 12:57:21 -0700408 if (resultCode == RESULT_OK && (mAddItemCellInfo != null ||
409 ((requestCode == REQUEST_PICK_GESTURE_ACTION ||
410 requestCode == REQUEST_CREATE_GESTURE_ACTION ||
411 requestCode == REQUEST_CREATE_GESTURE_APPLICATION_ACTION) && mCurrentGesture != null))) {
412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 switch (requestCode) {
414 case REQUEST_PICK_APPLICATION:
415 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
416 break;
417 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700418 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 break;
420 case REQUEST_CREATE_SHORTCUT:
421 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
422 break;
423 case REQUEST_PICK_LIVE_FOLDER:
424 addLiveFolder(data);
425 break;
426 case REQUEST_CREATE_LIVE_FOLDER:
427 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
428 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700429 case REQUEST_PICK_APPWIDGET:
430 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700432 case REQUEST_CREATE_APPWIDGET:
433 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 break;
Romain Guy73b979d2009-06-09 12:57:21 -0700435 case REQUEST_PICK_GESTURE_ACTION:
436 processShortcut(data, REQUEST_CREATE_GESTURE_APPLICATION_ACTION,
437 REQUEST_CREATE_GESTURE_ACTION);
438 break;
439 case REQUEST_CREATE_GESTURE_ACTION:
440 completeCreateGesture(data, true);
441 break;
442 case REQUEST_CREATE_GESTURE_APPLICATION_ACTION:
443 completeCreateGesture(data, false);
444 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700446 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700448 // Clean up the appWidgetId if we canceled
449 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
450 if (appWidgetId != -1) {
451 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 }
453 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
455
456 @Override
457 protected void onResume() {
458 super.onResume();
459
460 if (mRestoring) {
461 startLoaders();
462 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700463
464 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
465 // onNewIntent), then close the search dialog if needed, because it probably
466 // came from the user pressing 'home' (rather than, for example, pressing 'back').
467 if (mIsNewIntent) {
468 // Post to a handler so that this happens after the search dialog tries to open
469 // itself again.
470 mWorkspace.post(new Runnable() {
471 public void run() {
472 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
473 ServiceManager.getService(Context.SEARCH_SERVICE));
474 try {
475 searchManagerService.stopSearch();
476 } catch (RemoteException e) {
477 e(LOG_TAG, "error stopping search", e);
478 }
479 }
480 });
481 }
482
483 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 }
485
486 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700487 protected void onPause() {
488 super.onPause();
Romain Guy73b979d2009-06-09 12:57:21 -0700489 if (mGesturesWindow != null) {
490 mGesturesWindow.setAnimationStyle(0);
491 mGesturesWindow.update();
492 }
Romain Guycbb89e42009-06-08 15:52:54 -0700493 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700494 }
Romain Guycbb89e42009-06-08 15:52:54 -0700495
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700496 @Override
Romain Guy73b979d2009-06-09 12:57:21 -0700497 protected void onStop() {
498 super.onStop();
Romain Guyaad5ef42009-06-10 02:48:37 -0700499 if (mHideGesturesPanel) {
500 mHideGesturesPanel = false;
501 hideGesturesPanel();
502 }
Romain Guy73b979d2009-06-09 12:57:21 -0700503 }
504
505 @Override
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700506 public Object onRetainNonConfigurationInstance() {
507 // Flag any binder to stop early before switching
508 if (mBinder != null) {
509 mBinder.mTerminate = true;
510 }
Romain Guycbb89e42009-06-08 15:52:54 -0700511
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700512 if (PROFILE_ROTATE) {
513 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
514 }
515 return null;
516 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700517
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 private boolean acceptFilter() {
519 final InputMethodManager inputManager = (InputMethodManager)
520 getSystemService(Context.INPUT_METHOD_SERVICE);
521 return !inputManager.isFullscreenMode();
522 }
523
524 @Override
525 public boolean onKeyDown(int keyCode, KeyEvent event) {
526 boolean handled = super.onKeyDown(keyCode, event);
527 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
528 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
529 keyCode, event);
530 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700531 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700532 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700533 // showSearchDialog()
534 // If there are multiple keystrokes before the search dialog takes focus,
535 // onSearchRequested() will be called for every keystroke,
536 // but it is idempotent, so it's fine.
537 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 }
539 }
540
541 return handled;
542 }
543
Karl Rosaen138a0412009-04-23 19:00:21 -0700544 private String getTypedText() {
545 return mDefaultKeySsb.toString();
546 }
547
548 private void clearTypedText() {
549 mDefaultKeySsb.clear();
550 mDefaultKeySsb.clearSpans();
551 Selection.setSelection(mDefaultKeySsb, 0);
552 }
553
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 /**
555 * Restores the previous state, if it exists.
556 *
557 * @param savedState The previous state.
558 */
559 private void restoreState(Bundle savedState) {
560 if (savedState == null) {
561 return;
562 }
563
564 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
565 if (currentScreen > -1) {
566 mWorkspace.setCurrentScreen(currentScreen);
567 }
568
569 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
570 if (addScreen > -1) {
571 mAddItemCellInfo = new CellLayout.CellInfo();
572 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
573 addItemCellInfo.valid = true;
574 addItemCellInfo.screen = addScreen;
575 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
576 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
577 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
578 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
579 addItemCellInfo.findVacantCellsFromOccupied(
580 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
581 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
582 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
583 mRestoring = true;
584 }
585
586 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
587 if (renameFolder) {
588 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
589 mFolderInfo = sModel.getFolderById(this, id);
590 mRestoring = true;
591 }
Romain Guy73b979d2009-06-09 12:57:21 -0700592
Romain Guy1ce1a242009-06-23 17:34:54 -0700593 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_PENDING_GESTURE);
594
Romain Guy3cf604f2009-06-16 13:12:53 -0700595 boolean gesturesShowing = savedState.getBoolean(RUNTIME_STATE_GESTURES_PANEL, false);
596 if (gesturesShowing) {
Romain Guyf38b3d52009-07-01 16:39:46 -0700597 if (mCurrentGesture == null) {
598 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_GESTURES_PANEL_GESTURE);
599 }
600 final Gesture gesture = mCurrentGesture;
Romain Guy3cf604f2009-06-16 13:12:53 -0700601 mWorkspace.post(new Runnable() {
602 public void run() {
603 showGesturesPanel(false);
Romain Guy6e1fd5b2009-07-17 16:36:31 -0700604 if (gesture != null && mWorkspace.getParent() != null) {
Romain Guye769bc72009-07-17 14:18:50 -0700605 mGesturesProcessor.matchGesture(gesture);
606 mWorkspace.post(new Runnable() {
607 public void run() {
Romain Guy6e1fd5b2009-07-17 16:36:31 -0700608 if (gesture != null && mWorkspace.getParent() != null) {
Romain Guye769bc72009-07-17 14:18:50 -0700609 mGesturesOverlay.setGesture(gesture);
610 }
Romain Guy3cf604f2009-06-16 13:12:53 -0700611 }
Romain Guye769bc72009-07-17 14:18:50 -0700612 });
613 }
Romain Guy3cf604f2009-06-16 13:12:53 -0700614 }
615 });
616 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 }
618
619 /**
620 * Finds all the views we need and configure them properly.
621 */
622 private void setupViews() {
623 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
624 final DragLayer dragLayer = mDragLayer;
625
626 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
627 final Workspace workspace = mWorkspace;
628
629 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
630 final SlidingDrawer drawer = mDrawer;
631
632 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
633 final AllAppsGridView grid = mAllAppsGrid;
634
635 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
636
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700637 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
638 mHandleView.setLauncher(this);
639 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 mHandleIcon.setCrossFadeEnabled(true);
641
642 drawer.lock();
643 final DrawerManager drawerManager = new DrawerManager();
644 drawer.setOnDrawerOpenListener(drawerManager);
645 drawer.setOnDrawerCloseListener(drawerManager);
646 drawer.setOnDrawerScrollListener(drawerManager);
647
Karl Rosaen138a0412009-04-23 19:00:21 -0700648 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 grid.setDragger(dragLayer);
650 grid.setLauncher(this);
651
652 workspace.setOnLongClickListener(this);
653 workspace.setDragger(dragLayer);
654 workspace.setLauncher(this);
655 loadWallpaper();
656
657 deleteZone.setLauncher(this);
658 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700659 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660
661 dragLayer.setIgnoredDropTarget(grid);
662 dragLayer.setDragScoller(workspace);
663 dragLayer.setDragListener(deleteZone);
Romain Guy73b979d2009-06-09 12:57:21 -0700664
665 mGesturesPanel = mInflater.inflate(R.layout.gestures, mDragLayer, false);
666 final View gesturesPanel = mGesturesPanel;
667
Romain Guy73b979d2009-06-09 12:57:21 -0700668 mGesturesAction = new GesturesAction();
669
Romain Guy1dd3a072009-07-16 13:21:01 -0700670 mGesturesPrompt = (TextView) gesturesPanel.findViewById(R.id.gestures_prompt);
671 mGesturesSend = (Button) gesturesPanel.findViewById(R.id.gestures_action);
672 mGesturesSend.setOnClickListener(mGesturesAction);
673
Romain Guy73b979d2009-06-09 12:57:21 -0700674 mGesturesAdd = (ImageView) gesturesPanel.findViewById(R.id.gestures_add);
675 final ImageView gesturesAdd = mGesturesAdd;
Romain Guy73b979d2009-06-09 12:57:21 -0700676 gesturesAdd.setOnClickListener(new View.OnClickListener() {
677 public void onClick(View v) {
678 createGesture();
679 }
680 });
681
Romain Guy1dd3a072009-07-16 13:21:01 -0700682 gesturesPanel.findViewById(R.id.gestures_list).setOnClickListener(new View.OnClickListener() {
683 public void onClick(View v) {
684 startActivity(new Intent(Launcher.this, GesturesActivity.class));
685 }
686 });
687
Romain Guy73b979d2009-06-09 12:57:21 -0700688 mGesturesOverlay = (GestureOverlayView) gesturesPanel.findViewById(R.id.gestures_overlay);
689 mGesturesProcessor = new GesturesProcessor();
690
691 final GestureOverlayView overlay = mGesturesOverlay;
Romain Guy73b979d2009-06-09 12:57:21 -0700692 overlay.addOnGestureListener(mGesturesProcessor);
Romain Guy73b979d2009-06-09 12:57:21 -0700693 }
694
695 private void createGesture() {
Romain Guyf280f202009-06-26 10:31:07 -0700696 if (!mWaitingForResult) {
697 mCurrentGesture = mGesturesOverlay.getGesture();
698 mWaitingForResult = true;
699 pickShortcut(REQUEST_PICK_GESTURE_ACTION, R.string.title_select_shortcut);
700 }
Romain Guy73b979d2009-06-09 12:57:21 -0700701 }
702
703 private void completeCreateGesture(Intent data, boolean isShortcut) {
704 ApplicationInfo info;
705
706 if (isShortcut) {
707 info = infoFromShortcutIntent(this, data);
708 } else {
709 info = infoFromApplicationIntent(this, data);
710 }
711
712 boolean success = false;
713 if (info != null) {
714 info.isGesture = true;
715
716 if (LauncherModel.addGestureToDatabase(this, info, false)) {
717 mGesturesProcessor.addGesture(String.valueOf(info.id), mCurrentGesture);
718 mGesturesProcessor.update(info, mCurrentGesture);
719 Toast.makeText(this, getString(R.string.gestures_created, info.title),
720 Toast.LENGTH_SHORT).show();
721 success = true;
722 }
723 }
724
725 if (!success) {
726 Toast.makeText(this, getString(R.string.gestures_failed), Toast.LENGTH_SHORT).show();
727 }
728
729 mCurrentGesture = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 }
731
732 /**
733 * Creates a view representing a shortcut.
734 *
735 * @param info The data structure describing the shortcut.
736 *
737 * @return A View inflated from R.layout.application.
738 */
739 View createShortcut(ApplicationInfo info) {
740 return createShortcut(R.layout.application,
741 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
742 }
743
744 /**
745 * Creates a view representing a shortcut inflated from the specified resource.
746 *
747 * @param layoutResId The id of the XML layout used to create the shortcut.
748 * @param parent The group the shortcut belongs to.
749 * @param info The data structure describing the shortcut.
750 *
751 * @return A View inflated from layoutResId.
752 */
753 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
754 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
755
756 if (!info.filtered) {
757 info.icon = Utilities.createIconThumbnail(info.icon, this);
758 info.filtered = true;
759 }
760
761 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
762 favorite.setText(info.title);
763 favorite.setTag(info);
764 favorite.setOnClickListener(this);
765
766 return favorite;
767 }
768
769 /**
770 * Add an application shortcut to the workspace.
771 *
772 * @param data The intent describing the application.
773 * @param cellInfo The position on screen where to create the shortcut.
774 */
775 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
776 boolean insertAtFirst) {
777 cellInfo.screen = mWorkspace.getCurrentScreen();
778 if (!findSingleSlot(cellInfo)) return;
779
Romain Guy73b979d2009-06-09 12:57:21 -0700780 final ApplicationInfo info = infoFromApplicationIntent(context, data);
781 if (info != null) {
782 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
783 }
784 }
785
786 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 ComponentName component = data.getComponent();
788 PackageManager packageManager = context.getPackageManager();
789 ActivityInfo activityInfo = null;
790 try {
791 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
792 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700793 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 }
Romain Guycbb89e42009-06-08 15:52:54 -0700795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 if (activityInfo != null) {
797 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700798
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 itemInfo.title = activityInfo.loadLabel(packageManager);
800 if (itemInfo.title == null) {
801 itemInfo.title = activityInfo.name;
802 }
Romain Guycbb89e42009-06-08 15:52:54 -0700803
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
805 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
806 itemInfo.icon = activityInfo.loadIcon(packageManager);
807 itemInfo.container = ItemInfo.NO_ID;
808
Romain Guy73b979d2009-06-09 12:57:21 -0700809 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 }
Romain Guy73b979d2009-06-09 12:57:21 -0700811
812 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 }
Romain Guycbb89e42009-06-08 15:52:54 -0700814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 /**
816 * Add a shortcut to the workspace.
817 *
818 * @param data The intent describing the shortcut.
819 * @param cellInfo The position on screen where to create the shortcut.
820 * @param insertAtFirst
821 */
822 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
823 boolean insertAtFirst) {
824 cellInfo.screen = mWorkspace.getCurrentScreen();
825 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700826
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
828
829 if (!mRestoring) {
830 sModel.addDesktopItem(info);
831
832 final View view = createShortcut(info);
833 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
834 } else if (sModel.isDesktopLoaded()) {
835 sModel.addDesktopItem(info);
836 }
837 }
838
Romain Guycbb89e42009-06-08 15:52:54 -0700839
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700841 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700843 * @param data The intent describing the appWidgetId.
844 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700846 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 boolean insertAtFirst) {
848
849 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700850 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700851
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700852 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700853
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700854 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700855
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700856 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700858 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 // Try finding open space on Launcher screen
861 final int[] xy = mCellCoordinates;
862 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
863
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700864 // Build Launcher-specific widget info and save to database
865 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 launcherInfo.spanX = spans[0];
867 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700868
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 LauncherModel.addItemToDatabase(this, launcherInfo,
870 LauncherSettings.Favorites.CONTAINER_DESKTOP,
871 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
872
873 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700874 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700875
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700877 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700878
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700879 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700881
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
883 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
884 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700885 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 }
887 }
Romain Guycbb89e42009-06-08 15:52:54 -0700888
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700889 public LauncherAppWidgetHost getAppWidgetHost() {
890 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
Romain Guycbb89e42009-06-08 15:52:54 -0700892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 static ApplicationInfo addShortcut(Context context, Intent data,
894 CellLayout.CellInfo cellInfo, boolean notify) {
895
Romain Guy73b979d2009-06-09 12:57:21 -0700896 final ApplicationInfo info = infoFromShortcutIntent(context, data);
897 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
898 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
899
900 return info;
901 }
902
903 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
905 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
906 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
907
908 Drawable icon = null;
909 boolean filtered = false;
910 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700911 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912
913 if (bitmap != null) {
914 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
915 filtered = true;
916 customIcon = true;
917 } else {
918 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700919 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700921 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 final PackageManager packageManager = context.getPackageManager();
923 Resources resources = packageManager.getResourcesForApplication(
924 iconResource.packageName);
925 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
926 icon = resources.getDrawable(id);
927 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700928 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 }
930 }
931 }
932
933 if (icon == null) {
934 icon = context.getPackageManager().getDefaultActivityIcon();
935 }
936
937 final ApplicationInfo info = new ApplicationInfo();
938 info.icon = icon;
939 info.filtered = filtered;
940 info.title = name;
941 info.intent = intent;
942 info.customIcon = customIcon;
943 info.iconResource = iconResource;
944
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 return info;
946 }
947
948 @Override
949 protected void onNewIntent(Intent intent) {
950 super.onNewIntent(intent);
951
952 // Close the menu
953 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
954 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700955
956 // Set this flag so that onResume knows to close the search dialog if it's open,
957 // because this was a new intent (thus a press of 'home' or some such) rather than
958 // for example onResume being called when the user pressed the 'back' button.
959 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960
961 try {
962 dismissDialog(DIALOG_CREATE_SHORTCUT);
963 // Unlock the workspace if the dialog was showing
964 mWorkspace.unlock();
965 } catch (Exception e) {
966 // An exception is thrown if the dialog is not visible, which is fine
967 }
968
969 try {
970 dismissDialog(DIALOG_RENAME_FOLDER);
971 // Unlock the workspace if the dialog was showing
972 mWorkspace.unlock();
973 } catch (Exception e) {
974 // An exception is thrown if the dialog is not visible, which is fine
975 }
976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
978 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700979
Romain Guy1dd3a072009-07-16 13:21:01 -0700980 // TODO: This really should not be done here every time
981 final SharedPreferences preferences =
Romain Guy6b3f51d2009-07-16 17:45:04 -0700982 getSharedPreferences(GesturesConstants.PREFERENCES_NAME, MODE_PRIVATE);
Romain Guy1dd3a072009-07-16 13:21:01 -0700983 final boolean homeKey = preferences.getBoolean(
Romain Guy6b3f51d2009-07-16 17:45:04 -0700984 GesturesConstants.PREFERENCES_HOME_KEY, false);
Romain Guy94406842009-06-17 16:18:58 -0700985
Romain Guy1dd3a072009-07-16 13:21:01 -0700986 if (!homeKey) {
987 if (!mWorkspace.isDefaultScreenShowing()) {
988 mWorkspace.moveToDefaultScreen();
989 }
Romain Guy94406842009-06-17 16:18:58 -0700990
Romain Guy1dd3a072009-07-16 13:21:01 -0700991 if (mGesturesWindow == null || mGesturesWindow.isShowing()) {
992 hideGesturesPanel();
993 }
994 } else {
995 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE
996 && (mDragLayer.hasWindowFocus() || (mGesturesWindow != null
997 && mGesturesWindow.isShowing()))) {
998
999 SearchManager searchManager =
1000 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1001
1002 if (!searchManager.isVisible()) {
1003 onHomeKeyPressed();
1004 }
Romain Guyb8734242009-06-10 11:53:57 -07001005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -07001009
1010 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 if (v != null && v.getWindowToken() != null) {
1012 InputMethodManager imm = (InputMethodManager)getSystemService(
1013 INPUT_METHOD_SERVICE);
1014 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1015 }
1016 } else {
1017 closeDrawer(false);
1018 }
1019 }
1020 }
1021
Romain Guy73b979d2009-06-09 12:57:21 -07001022 private void onHomeKeyPressed() {
1023 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
1024 showGesturesPanel();
1025 } else {
1026 hideGesturesPanel();
1027 }
1028 }
1029
1030 private void showGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -07001031 showGesturesPanel(true);
1032 }
1033
1034 private void showGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07001035 resetGesturesPrompt();
1036
Romain Guy1dd3a072009-07-16 13:21:01 -07001037 mGesturesAdd.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07001038
1039 mGesturesOverlay.clear(false);
1040
1041 PopupWindow window;
1042 if (mGesturesWindow == null) {
1043 mGesturesWindow = new PopupWindow(this);
1044 window = mGesturesWindow;
1045 window.setFocusable(true);
1046 window.setTouchable(true);
Romain Guy1dd3a072009-07-16 13:21:01 -07001047 window.setBackgroundDrawable(getResources().getDrawable(
1048 android.R.drawable.screen_background_dark));
Romain Guy73b979d2009-06-09 12:57:21 -07001049 window.setContentView(mGesturesPanel);
1050 } else {
1051 window = mGesturesWindow;
1052 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001053 window.setAnimationStyle(animate ? com.android.internal.R.style.Animation_Toast : 0);
Romain Guy73b979d2009-06-09 12:57:21 -07001054
1055 final int[] xy = new int[2];
1056 final DragLayer dragLayer = mDragLayer;
1057 dragLayer.getLocationOnScreen(xy);
1058
1059 window.setWidth(dragLayer.getWidth());
1060 window.setHeight(dragLayer.getHeight() - 1);
1061 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
1062 }
1063
1064 private void resetGesturesPrompt() {
1065 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07001066 mGesturesPrompt.setText(R.string.gestures_instructions);
1067 mGesturesPrompt.setVisibility(View.VISIBLE);
1068 mGesturesSend.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07001069 }
1070
Romain Guy1dd3a072009-07-16 13:21:01 -07001071 private void setGestureUnknown() {
Romain Guy73b979d2009-06-09 12:57:21 -07001072 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07001073 mGesturesPrompt.setText(R.string.gestures_unknown);
1074 mGesturesPrompt.setVisibility(View.VISIBLE);
1075 mGesturesSend.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07001076 }
1077
Romain Guy1dd3a072009-07-16 13:21:01 -07001078 private void setGestureAction(Drawable icon, CharSequence title) {
1079 mGesturesPrompt.setVisibility(View.GONE);
1080 mGesturesSend.setVisibility(View.VISIBLE);
1081 mGesturesSend.setText(title);
1082 mGesturesSend.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
Romain Guy3cf604f2009-06-16 13:12:53 -07001083 }
1084
Romain Guy73b979d2009-06-09 12:57:21 -07001085 void hideGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -07001086 hideGesturesPanel(true);
1087 }
1088
1089 void hideGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07001090 if (mGesturesWindow != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001091 final PopupWindow popupWindow = mGesturesWindow;
1092 popupWindow.setAnimationStyle(animate ?
Romain Guy1dd3a072009-07-16 13:21:01 -07001093 com.android.internal.R.style.Animation_Toast : 0);
Romain Guy3cf604f2009-06-16 13:12:53 -07001094 popupWindow.update();
1095 popupWindow.dismiss();
Romain Guy73b979d2009-06-09 12:57:21 -07001096 }
1097 }
1098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 @Override
1100 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1101 // Do not call super here
1102 mSavedInstanceState = savedInstanceState;
1103 }
1104
1105 @Override
1106 protected void onSaveInstanceState(Bundle outState) {
1107 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1108
1109 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1110 if (folders.size() > 0) {
1111 final int count = folders.size();
1112 long[] ids = new long[count];
1113 for (int i = 0; i < count; i++) {
1114 final FolderInfo info = folders.get(i).getInfo();
1115 ids[i] = info.id;
1116 }
1117 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1118 } else {
1119 super.onSaveInstanceState(outState);
1120 }
1121
Romain Guy3cf604f2009-06-16 13:12:53 -07001122 final boolean isConfigurationChange = getChangingConfigurations() != 0;
1123
Romain Guy5a941392009-04-28 15:18:25 -07001124 // When the drawer is opened and we are saving the state because of a
1125 // configuration change
Romain Guy3cf604f2009-06-16 13:12:53 -07001126 if (mDrawer.isOpened() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129
1130 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1131 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1132 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1133
1134 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1135 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1136 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1137 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1138 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1139 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1140 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1141 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1142 layout.getOccupiedCells());
1143 }
1144
1145 if (mFolderInfo != null && mWaitingForResult) {
1146 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1147 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1148 }
Romain Guy73b979d2009-06-09 12:57:21 -07001149
1150 if (mCurrentGesture != null && mWaitingForResult) {
1151 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1152 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001153
Romain Guy1ce1a242009-06-23 17:34:54 -07001154 if (mGesturesWindow != null && mGesturesWindow.isShowing()) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001155 outState.putBoolean(RUNTIME_STATE_GESTURES_PANEL, true);
1156
Romain Guy1ce1a242009-06-23 17:34:54 -07001157 if (mCurrentGesture == null || !mWaitingForResult) {
1158 final Gesture gesture = mGesturesOverlay.getGesture();
1159 if (gesture != null) {
1160 outState.putParcelable(RUNTIME_STATE_GESTURES_PANEL_GESTURE, gesture);
1161 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001162 }
1163 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 }
1165
1166 @Override
1167 public void onDestroy() {
1168 mDestroyed = true;
1169
1170 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001173 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001175 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
1177
1178 TextKeyListener.getInstance().release();
1179
Romain Guy3cf604f2009-06-16 13:12:53 -07001180 hideGesturesPanel(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 mAllAppsGrid.clearTextFilter();
1182 mAllAppsGrid.setAdapter(null);
1183 sModel.unbind();
1184 sModel.abortLoaders();
1185
1186 getContentResolver().unregisterContentObserver(mObserver);
1187 unregisterReceiver(mApplicationsReceiver);
1188 }
1189
1190 @Override
1191 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001192 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 super.startActivityForResult(intent, requestCode);
1194 }
1195
1196 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001197 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001199
1200 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001201
Karl Rosaen138a0412009-04-23 19:00:21 -07001202 // Slide the search widget to the top, if it's on the current screen,
1203 // otherwise show the search dialog immediately.
1204 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1205 if (searchWidget == null) {
1206 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1207 } else {
1208 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1209 // show the currently typed text in the search widget while sliding
1210 searchWidget.setQuery(getTypedText());
1211 }
1212 }
Romain Guycbb89e42009-06-08 15:52:54 -07001213
Karl Rosaen138a0412009-04-23 19:00:21 -07001214 /**
1215 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001216 *
1217 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001218 */
Romain Guycbb89e42009-06-08 15:52:54 -07001219 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001220 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001221
Karl Rosaen138a0412009-04-23 19:00:21 -07001222 if (initialQuery == null) {
1223 // Use any text typed in the launcher as the initial query
1224 initialQuery = getTypedText();
1225 clearTypedText();
1226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 if (appSearchData == null) {
1228 appSearchData = new Bundle();
1229 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1230 }
Romain Guycbb89e42009-06-08 15:52:54 -07001231
Karl Rosaen138a0412009-04-23 19:00:21 -07001232 final SearchManager searchManager =
1233 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1234
1235 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1236 if (searchWidget != null) {
1237 // This gets called when the user leaves the search dialog to go back to
1238 // the Launcher.
1239 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1240 public void onCancel() {
1241 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001242 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001243 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001244 });
1245 }
Romain Guycbb89e42009-06-08 15:52:54 -07001246
Karl Rosaen138a0412009-04-23 19:00:21 -07001247 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001248 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
1250
Karl Rosaen138a0412009-04-23 19:00:21 -07001251 /**
1252 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001253 */
Romain Guy5a941392009-04-28 15:18:25 -07001254 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001255 // Close search dialog
1256 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001257 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001258 // Restore search widget to its normal position
1259 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1260 if (searchWidget != null) {
1261 searchWidget.stopSearch(false);
1262 }
1263 }
Romain Guycbb89e42009-06-08 15:52:54 -07001264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 @Override
1266 public boolean onCreateOptionsMenu(Menu menu) {
1267 if (mDesktopLocked) return false;
1268
1269 super.onCreateOptionsMenu(menu);
1270 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1271 .setIcon(android.R.drawable.ic_menu_add)
1272 .setAlphabeticShortcut('A');
1273 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1274 .setIcon(android.R.drawable.ic_menu_gallery)
1275 .setAlphabeticShortcut('W');
1276 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1277 .setIcon(android.R.drawable.ic_search_category_default)
1278 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1279 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1280 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1281 .setAlphabeticShortcut('N');
1282
Romain Guy1dd3a072009-07-16 13:21:01 -07001283 // TODO: Remove
Romain Guy73b979d2009-06-09 12:57:21 -07001284 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
Romain Guy1dd3a072009-07-16 13:21:01 -07001285 .setIcon(com.android.internal.R.drawable.ic_menu_compose)
1286 .setAlphabeticShortcut('G');
Romain Guy73b979d2009-06-09 12:57:21 -07001287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001289 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1290 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291
1292 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1293 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1294 .setIntent(settings);
1295
1296 return true;
1297 }
1298
1299 @Override
1300 public boolean onPrepareOptionsMenu(Menu menu) {
1301 super.onPrepareOptionsMenu(menu);
1302
1303 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1304 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1305
1306 return true;
1307 }
1308
1309 @Override
1310 public boolean onOptionsItemSelected(MenuItem item) {
1311 switch (item.getItemId()) {
1312 case MENU_ADD:
1313 addItems();
1314 return true;
1315 case MENU_WALLPAPER_SETTINGS:
1316 startWallpaper();
1317 return true;
1318 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001319 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 return true;
1321 case MENU_NOTIFICATIONS:
1322 showNotifications();
1323 return true;
Romain Guy1dd3a072009-07-16 13:21:01 -07001324 case MENU_GESTURES:
1325 showGesturesPanel();
1326 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328
1329 return super.onOptionsItemSelected(item);
1330 }
Romain Guycbb89e42009-06-08 15:52:54 -07001331
Karl Rosaen138a0412009-04-23 19:00:21 -07001332 /**
1333 * Indicates that we want global search for this activity by setting the globalSearch
1334 * argument for {@link #startSearch} to true.
1335 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001337 @Override
1338 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001339 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001340 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001341 }
1342
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 private void addItems() {
1344 showAddDialog(mMenuAddInfo);
1345 }
1346
1347 private void removeShortcutsForPackage(String packageName) {
1348 if (packageName != null && packageName.length() > 0) {
1349 mWorkspace.removeShortcutsForPackage(packageName);
1350 }
1351 }
Romain Guycbb89e42009-06-08 15:52:54 -07001352
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001353 private void updateShortcutsForPackage(String packageName) {
1354 if (packageName != null && packageName.length() > 0) {
1355 mWorkspace.updateShortcutsForPackage(packageName);
1356 }
1357 }
1358
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001359 void addAppWidget(Intent data) {
1360 // TODO: catch bad widget exception when sent
1361 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001363 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1364 if (SEARCH_WIDGET.equals(customWidget)) {
1365 // We don't need this any more, since this isn't a real app widget.
1366 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1367 // add the search widget
1368 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001370 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1371
1372 if (appWidget.configure != null) {
1373 // Launch over to configure widget, if needed
1374 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1375 intent.setComponent(appWidget.configure);
1376 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1377
1378 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1379 } else {
1380 // Otherwise just add it
1381 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1382 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 }
1384 }
Romain Guycbb89e42009-06-08 15:52:54 -07001385
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 void addSearch() {
1387 final Widget info = Widget.makeSearch();
1388 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 final int[] xy = mCellCoordinates;
1391 final int spanX = info.spanX;
1392 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001393
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 sModel.addDesktopItem(info);
1397 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1398 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001399
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 final View view = mInflater.inflate(info.layoutResource, null);
1401 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001402 Search search = (Search) view.findViewById(R.id.widget_search);
1403 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1406 }
1407
Romain Guy73b979d2009-06-09 12:57:21 -07001408 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001409 // Handle case where user selected "Applications"
1410 String applicationName = getResources().getString(R.string.group_applications);
1411 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001412
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001413 if (applicationName != null && applicationName.equals(shortcutName)) {
1414 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1415 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001416
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001417 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1418 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001419 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001420 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001421 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
1424
1425 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001426 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001427 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001428 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001429
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001430 if (folderName != null && folderName.equals(shortcutName)) {
1431 addFolder(!mDesktopLocked);
1432 } else {
1433 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1434 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
1436
1437 void addFolder(boolean insertAtFirst) {
1438 UserFolderInfo folderInfo = new UserFolderInfo();
1439 folderInfo.title = getText(R.string.folder_name);
1440
1441 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1442 cellInfo.screen = mWorkspace.getCurrentScreen();
1443 if (!findSingleSlot(cellInfo)) return;
1444
1445 // Update the model
1446 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1447 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1448 sModel.addDesktopItem(folderInfo);
1449 sModel.addFolder(folderInfo);
1450
1451 // Create the view
1452 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1453 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1454 mWorkspace.addInCurrentScreen(newFolder,
1455 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1456 }
Romain Guycbb89e42009-06-08 15:52:54 -07001457
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1459 boolean insertAtFirst) {
1460 cellInfo.screen = mWorkspace.getCurrentScreen();
1461 if (!findSingleSlot(cellInfo)) return;
1462
1463 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1464
1465 if (!mRestoring) {
1466 sModel.addDesktopItem(info);
1467
1468 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1469 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1470 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1471 } else if (sModel.isDesktopLoaded()) {
1472 sModel.addDesktopItem(info);
1473 }
1474 }
1475
1476 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1477 CellLayout.CellInfo cellInfo, boolean notify) {
1478
1479 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1480 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1481
1482 Drawable icon = null;
1483 boolean filtered = false;
1484 Intent.ShortcutIconResource iconResource = null;
1485
1486 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1487 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1488 try {
1489 iconResource = (Intent.ShortcutIconResource) extra;
1490 final PackageManager packageManager = context.getPackageManager();
1491 Resources resources = packageManager.getResourcesForApplication(
1492 iconResource.packageName);
1493 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1494 icon = resources.getDrawable(id);
1495 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001496 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 }
1498 }
1499
1500 if (icon == null) {
1501 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1502 }
1503
1504 final LiveFolderInfo info = new LiveFolderInfo();
1505 info.icon = icon;
1506 info.filtered = filtered;
1507 info.title = name;
1508 info.iconResource = iconResource;
1509 info.uri = data.getData();
1510 info.baseIntent = baseIntent;
1511 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1512 LiveFolders.DISPLAY_MODE_GRID);
1513
1514 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1515 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1516 sModel.addFolder(info);
1517
1518 return info;
1519 }
1520
1521 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1522 final int[] xy = new int[2];
1523 if (findSlot(cellInfo, xy, 1, 1)) {
1524 cellInfo.cellX = xy[0];
1525 cellInfo.cellY = xy[1];
1526 return true;
1527 }
1528 return false;
1529 }
1530
1531 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1532 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1533 boolean[] occupied = mSavedState != null ?
1534 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1535 cellInfo = mWorkspace.findAllVacantCells(occupied);
1536 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1537 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1538 return false;
1539 }
1540 }
1541 return true;
1542 }
1543
1544 private void showNotifications() {
1545 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1546 if (statusBar != null) {
1547 statusBar.expand();
1548 }
1549 }
1550
1551 private void startWallpaper() {
1552 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1553 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1554 }
1555
1556 /**
1557 * Registers various intent receivers. The current implementation registers
1558 * only a wallpaper intent receiver to let other applications change the
1559 * wallpaper.
1560 */
1561 private void registerIntentReceivers() {
1562 if (sWallpaperReceiver == null) {
1563 final Application application = getApplication();
1564
1565 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1566
1567 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1568 application.registerReceiver(sWallpaperReceiver, filter);
1569 } else {
1570 sWallpaperReceiver.setLauncher(this);
1571 }
1572
1573 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1574 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1575 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1576 filter.addDataScheme("package");
1577 registerReceiver(mApplicationsReceiver, filter);
1578 }
1579
1580 /**
1581 * Registers various content observers. The current implementation registers
1582 * only a favorites observer to keep track of the favorites applications.
1583 */
1584 private void registerContentObservers() {
1585 ContentResolver resolver = getContentResolver();
1586 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1587 }
1588
1589 @Override
1590 public boolean dispatchKeyEvent(KeyEvent event) {
1591 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1592 switch (event.getKeyCode()) {
1593 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001594 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001595 if (mDrawer.isOpened()) {
1596 closeDrawer();
1597 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001598 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 return true;
1601 case KeyEvent.KEYCODE_HOME:
1602 return true;
1603 }
1604 }
1605
1606 return super.dispatchKeyEvent(event);
1607 }
1608
1609 private void closeDrawer() {
1610 closeDrawer(true);
1611 }
1612
1613 private void closeDrawer(boolean animated) {
1614 if (mDrawer.isOpened()) {
1615 if (animated) {
1616 mDrawer.animateClose();
1617 } else {
1618 mDrawer.close();
1619 }
1620 if (mDrawer.hasFocus()) {
1621 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1622 }
1623 }
1624 }
1625
1626 private void closeFolder() {
1627 Folder folder = mWorkspace.getOpenFolder();
1628 if (folder != null) {
1629 closeFolder(folder);
1630 }
1631 }
1632
1633 void closeFolder(Folder folder) {
1634 folder.getInfo().opened = false;
1635 ViewGroup parent = (ViewGroup) folder.getParent();
1636 if (parent != null) {
1637 parent.removeView(folder);
1638 }
1639 folder.onClose();
1640 }
1641
1642 /**
1643 * When the notification that favorites have changed is received, requests
1644 * a favorites list refresh.
1645 */
1646 private void onFavoritesChanged() {
1647 mDesktopLocked = true;
1648 mDrawer.lock();
1649 sModel.loadUserItems(false, this, false, false);
1650 }
1651
1652 void onDesktopItemsLoaded() {
1653 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 bindDesktopItems();
1655 }
Romain Guycbb89e42009-06-08 15:52:54 -07001656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 /**
1658 * Refreshes the shortcuts shown on the workspace.
1659 */
1660 private void bindDesktopItems() {
1661 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001662 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001663 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1664 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 return;
1666 }
1667
1668 final Workspace workspace = mWorkspace;
1669 int count = workspace.getChildCount();
1670 for (int i = 0; i < count; i++) {
1671 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1672 }
Romain Guycbb89e42009-06-08 15:52:54 -07001673
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 if (DEBUG_USER_INTERFACE) {
1675 android.widget.Button finishButton = new android.widget.Button(this);
1676 finishButton.setText("Finish");
1677 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1678
1679 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1680 public void onClick(View v) {
1681 finish();
1682 }
1683 });
1684 }
Romain Guycbb89e42009-06-08 15:52:54 -07001685
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001686 // Flag any old binder to terminate early
1687 if (mBinder != null) {
1688 mBinder.mTerminate = true;
1689 }
Romain Guycbb89e42009-06-08 15:52:54 -07001690
Karl Rosaen138a0412009-04-23 19:00:21 -07001691 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001692 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 }
1694
1695 private void bindItems(Launcher.DesktopBinder binder,
1696 ArrayList<ItemInfo> shortcuts, int start, int count) {
1697
1698 final Workspace workspace = mWorkspace;
1699 final boolean desktopLocked = mDesktopLocked;
1700
1701 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1702 int i = start;
1703
1704 for ( ; i < end; i++) {
1705 final ItemInfo item = shortcuts.get(i);
1706 switch (item.itemType) {
1707 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1708 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1709 final View shortcut = createShortcut((ApplicationInfo) item);
1710 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1711 !desktopLocked);
1712 break;
1713 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1714 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1715 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1716 (UserFolderInfo) item);
1717 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1718 !desktopLocked);
1719 break;
1720 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1721 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1722 R.layout.live_folder_icon, this,
1723 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1724 (LiveFolderInfo) item);
1725 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1726 !desktopLocked);
1727 break;
1728 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1729 final int screen = workspace.getCurrentScreen();
1730 final View view = mInflater.inflate(R.layout.widget_search,
1731 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001732
Karl Rosaen138a0412009-04-23 19:00:21 -07001733 Search search = (Search) view.findViewById(R.id.widget_search);
1734 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001735
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 final Widget widget = (Widget) item;
1737 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001738
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 workspace.addWidget(view, widget, !desktopLocked);
1740 break;
1741 }
1742 }
1743
1744 workspace.requestLayout();
1745
1746 if (end >= count) {
1747 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001748 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 } else {
1750 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1751 }
1752 }
1753
1754 private void finishBindDesktopItems() {
1755 if (mSavedState != null) {
1756 if (!mWorkspace.hasFocus()) {
1757 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1758 }
1759
1760 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1761 if (userFolders != null) {
1762 for (long folderId : userFolders) {
1763 final FolderInfo info = sModel.findFolderById(folderId);
1764 if (info != null) {
1765 openFolder(info);
1766 }
1767 }
1768 final Folder openFolder = mWorkspace.getOpenFolder();
1769 if (openFolder != null) {
1770 openFolder.requestFocus();
1771 }
1772 }
1773
1774 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1775 if (allApps) {
1776 mDrawer.open();
1777 }
1778
1779 mSavedState = null;
1780 }
1781
1782 if (mSavedInstanceState != null) {
1783 super.onRestoreInstanceState(mSavedInstanceState);
1784 mSavedInstanceState = null;
1785 }
1786
1787 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1788 mDrawer.requestFocus();
1789 }
1790
1791 mDesktopLocked = false;
1792 mDrawer.unlock();
1793 }
Romain Guycbb89e42009-06-08 15:52:54 -07001794
Karl Rosaen138a0412009-04-23 19:00:21 -07001795 private void bindDrawer(Launcher.DesktopBinder binder,
1796 ApplicationsAdapter drawerAdapter) {
1797 mAllAppsGrid.setAdapter(drawerAdapter);
1798 binder.startBindingAppWidgetsWhenIdle();
1799 }
Romain Guycbb89e42009-06-08 15:52:54 -07001800
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001801 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001802 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001803
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 final Workspace workspace = mWorkspace;
1805 final boolean desktopLocked = mDesktopLocked;
1806
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001807 if (!appWidgets.isEmpty()) {
1808 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001809
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001810 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001811 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001812 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001813
Karl Rosaen138a0412009-04-23 19:00:21 -07001814 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001815
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001816 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1817 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001818
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001819 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1820 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001821
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001822 workspace.requestLayout();
1823 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001825 if (appWidgets.isEmpty()) {
1826 if (PROFILE_ROTATE) {
1827 android.os.Debug.stopMethodTracing();
1828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001830 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 }
1832 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001833
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 DragController getDragController() {
1835 return mDragLayer;
1836 }
1837
1838 /**
1839 * Launches the intent referred by the clicked shortcut.
1840 *
1841 * @param v The view representing the clicked shortcut.
1842 */
1843 public void onClick(View v) {
1844 Object tag = v.getTag();
1845 if (tag instanceof ApplicationInfo) {
1846 // Open shortcut
1847 final Intent intent = ((ApplicationInfo) tag).intent;
1848 startActivitySafely(intent);
1849 } else if (tag instanceof FolderInfo) {
1850 handleFolderClick((FolderInfo) tag);
1851 }
1852 }
1853
1854 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001855 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1857 try {
1858 startActivity(intent);
1859 } catch (ActivityNotFoundException e) {
1860 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1861 } catch (SecurityException e) {
1862 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001863 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1865 "or use the exported attribute for this activity.", e);
1866 }
1867 }
1868
1869 private void handleFolderClick(FolderInfo folderInfo) {
1870 if (!folderInfo.opened) {
1871 // Close any open folder
1872 closeFolder();
1873 // Open the requested folder
1874 openFolder(folderInfo);
1875 } else {
1876 // Find the open folder...
1877 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1878 int folderScreen;
1879 if (openFolder != null) {
1880 folderScreen = mWorkspace.getScreenForView(openFolder);
1881 // .. and close it
1882 closeFolder(openFolder);
1883 if (folderScreen != mWorkspace.getCurrentScreen()) {
1884 // Close any folder open on the current screen
1885 closeFolder();
1886 // Pull the folder onto this screen
1887 openFolder(folderInfo);
1888 }
1889 }
1890 }
1891 }
1892
1893 private void loadWallpaper() {
1894 // The first time the application is started, we load the wallpaper from
1895 // the ApplicationContext
1896 if (sWallpaper == null) {
1897 final Drawable drawable = getWallpaper();
1898 if (drawable instanceof BitmapDrawable) {
1899 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1900 } else {
1901 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1902 }
1903 }
1904 mWorkspace.loadWallpaper(sWallpaper);
1905 }
1906
1907 /**
1908 * Opens the user fodler described by the specified tag. The opening of the folder
1909 * is animated relative to the specified View. If the View is null, no animation
1910 * is played.
1911 *
1912 * @param folderInfo The FolderInfo describing the folder to open.
1913 */
1914 private void openFolder(FolderInfo folderInfo) {
1915 Folder openFolder;
1916
1917 if (folderInfo instanceof UserFolderInfo) {
1918 openFolder = UserFolder.fromXml(this);
1919 } else if (folderInfo instanceof LiveFolderInfo) {
1920 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1921 } else {
1922 return;
1923 }
1924
1925 openFolder.setDragger(mDragLayer);
1926 openFolder.setLauncher(this);
1927
1928 openFolder.bind(folderInfo);
1929 folderInfo.opened = true;
1930
1931 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1932 openFolder.onOpen();
1933 }
1934
1935 /**
1936 * Returns true if the workspace is being loaded. When the workspace is loading,
1937 * no user interaction should be allowed to avoid any conflict.
1938 *
1939 * @return True if the workspace is locked, false otherwise.
1940 */
1941 boolean isWorkspaceLocked() {
1942 return mDesktopLocked;
1943 }
1944
1945 public boolean onLongClick(View v) {
1946 if (mDesktopLocked) {
1947 return false;
1948 }
1949
1950 if (!(v instanceof CellLayout)) {
1951 v = (View) v.getParent();
1952 }
1953
1954 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1955
1956 // This happens when long clicking an item with the dpad/trackball
1957 if (cellInfo == null) {
1958 return true;
1959 }
1960
1961 if (mWorkspace.allowLongPress()) {
1962 if (cellInfo.cell == null) {
1963 if (cellInfo.valid) {
1964 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001965 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 showAddDialog(cellInfo);
1967 }
1968 } else {
1969 if (!(cellInfo.cell instanceof Folder)) {
1970 // User long pressed on an item
1971 mWorkspace.startDrag(cellInfo);
1972 }
1973 }
1974 }
1975 return true;
1976 }
1977
1978 static LauncherModel getModel() {
1979 return sModel;
1980 }
1981
Romain Guy6c8bbcb2009-07-17 16:17:40 -07001982 static GestureLibrary getGestureLibrary(Context context) {
1983 if (sLibrary == null) {
1984 // The context is not kept by the library so it's safe to do this
1985 sLibrary = GestureLibraries.fromPrivateFile(context, GesturesConstants.STORE_NAME);
1986 }
Romain Guy73b979d2009-06-09 12:57:21 -07001987 return sLibrary;
1988 }
1989
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 void closeAllApplications() {
1991 mDrawer.close();
1992 }
1993
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001994 View getDrawerHandle() {
1995 return mHandleView;
1996 }
1997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 boolean isDrawerDown() {
1999 return !mDrawer.isMoving() && !mDrawer.isOpened();
2000 }
2001
2002 boolean isDrawerUp() {
2003 return mDrawer.isOpened() && !mDrawer.isMoving();
2004 }
2005
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002006 boolean isDrawerMoving() {
2007 return mDrawer.isMoving();
2008 }
2009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 Workspace getWorkspace() {
2011 return mWorkspace;
2012 }
2013
2014 GridView getApplicationsGrid() {
2015 return mAllAppsGrid;
2016 }
2017
2018 @Override
2019 protected Dialog onCreateDialog(int id) {
2020 switch (id) {
2021 case DIALOG_CREATE_SHORTCUT:
2022 return new CreateShortcut().createDialog();
2023 case DIALOG_RENAME_FOLDER:
2024 return new RenameFolder().createDialog();
2025 }
2026
2027 return super.onCreateDialog(id);
2028 }
2029
2030 @Override
2031 protected void onPrepareDialog(int id, Dialog dialog) {
2032 switch (id) {
2033 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 break;
2035 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002036 if (mFolderInfo != null) {
2037 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2038 final CharSequence text = mFolderInfo.title;
2039 input.setText(text);
2040 input.setSelection(0, text.length());
2041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 break;
2043 }
2044 }
2045
2046 void showRenameDialog(FolderInfo info) {
2047 mFolderInfo = info;
2048 mWaitingForResult = true;
2049 showDialog(DIALOG_RENAME_FOLDER);
2050 }
2051
2052 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2053 mAddItemCellInfo = cellInfo;
2054 mWaitingForResult = true;
2055 showDialog(DIALOG_CREATE_SHORTCUT);
2056 }
2057
Romain Guy73b979d2009-06-09 12:57:21 -07002058 private void pickShortcut(int requestCode, int title) {
2059 Bundle bundle = new Bundle();
2060
2061 ArrayList<String> shortcutNames = new ArrayList<String>();
2062 shortcutNames.add(getString(R.string.group_applications));
2063 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2064
2065 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2066 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2067 R.drawable.ic_launcher_application));
2068 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2069
2070 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2071 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
2072 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
2073 pickIntent.putExtras(bundle);
2074
2075 startActivityForResult(pickIntent, requestCode);
2076 }
2077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 private class RenameFolder {
2079 private EditText mInput;
2080
2081 Dialog createDialog() {
2082 mWaitingForResult = true;
2083 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2084 mInput = (EditText) layout.findViewById(R.id.folder_name);
2085
2086 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2087 builder.setIcon(0);
2088 builder.setTitle(getString(R.string.rename_folder_title));
2089 builder.setCancelable(true);
2090 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2091 public void onCancel(DialogInterface dialog) {
2092 cleanup();
2093 }
2094 });
2095 builder.setNegativeButton(getString(R.string.cancel_action),
2096 new Dialog.OnClickListener() {
2097 public void onClick(DialogInterface dialog, int which) {
2098 cleanup();
2099 }
2100 }
2101 );
2102 builder.setPositiveButton(getString(R.string.rename_action),
2103 new Dialog.OnClickListener() {
2104 public void onClick(DialogInterface dialog, int which) {
2105 changeFolderName();
2106 }
2107 }
2108 );
2109 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002110
2111 final AlertDialog dialog = builder.create();
2112 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2113 public void onShow(DialogInterface dialog) {
2114 mWorkspace.lock();
2115 }
2116 });
2117
2118 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 }
2120
2121 private void changeFolderName() {
2122 final String name = mInput.getText().toString();
2123 if (!TextUtils.isEmpty(name)) {
2124 // Make sure we have the right folder info
2125 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
2126 mFolderInfo.title = name;
2127 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2128
2129 if (mDesktopLocked) {
2130 mDrawer.lock();
2131 sModel.loadUserItems(false, Launcher.this, false, false);
2132 } else {
2133 final FolderIcon folderIcon = (FolderIcon)
2134 mWorkspace.getViewForTag(mFolderInfo);
2135 if (folderIcon != null) {
2136 folderIcon.setText(name);
2137 getWorkspace().requestLayout();
2138 } else {
2139 mDesktopLocked = true;
2140 mDrawer.lock();
2141 sModel.loadUserItems(false, Launcher.this, false, false);
2142 }
2143 }
2144 }
2145 cleanup();
2146 }
2147
2148 private void cleanup() {
2149 mWorkspace.unlock();
2150 dismissDialog(DIALOG_RENAME_FOLDER);
2151 mWaitingForResult = false;
2152 mFolderInfo = null;
2153 }
2154 }
2155
2156 /**
2157 * Displays the shortcut creation dialog and launches, if necessary, the
2158 * appropriate activity.
2159 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002160 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002161 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2162 DialogInterface.OnShowListener {
2163
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002165
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 Dialog createDialog() {
2167 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002168
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2172 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002173 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002174
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 builder.setInverseBackgroundForced(true);
2176
2177 AlertDialog dialog = builder.create();
2178 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002179 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002180 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 return dialog;
2183 }
2184
2185 public void onCancel(DialogInterface dialog) {
2186 mWaitingForResult = false;
2187 cleanup();
2188 }
2189
Romain Guycbb89e42009-06-08 15:52:54 -07002190 public void onDismiss(DialogInterface dialog) {
2191 mWorkspace.unlock();
2192 }
2193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 private void cleanup() {
2195 mWorkspace.unlock();
2196 dismissDialog(DIALOG_CREATE_SHORTCUT);
2197 }
2198
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002199 /**
2200 * Handle the action clicked in the "Add to home" dialog.
2201 */
2202 public void onClick(DialogInterface dialog, int which) {
2203 Resources res = getResources();
2204 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002205
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002206 switch (which) {
2207 case AddAdapter.ITEM_SHORTCUT: {
2208 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002209 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002210 break;
2211 }
Romain Guycbb89e42009-06-08 15:52:54 -07002212
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002213 case AddAdapter.ITEM_APPWIDGET: {
2214 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002215
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002216 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2217 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2218 // add the search widget
2219 ArrayList<AppWidgetProviderInfo> customInfo =
2220 new ArrayList<AppWidgetProviderInfo>();
2221 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2222 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2223 info.label = getString(R.string.group_search);
2224 info.icon = R.drawable.ic_search_widget;
2225 customInfo.add(info);
2226 pickIntent.putParcelableArrayListExtra(
2227 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2228 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2229 Bundle b = new Bundle();
2230 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2231 customExtras.add(b);
2232 pickIntent.putParcelableArrayListExtra(
2233 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2234 // start the pick activity
2235 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2236 break;
2237 }
Romain Guycbb89e42009-06-08 15:52:54 -07002238
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002239 case AddAdapter.ITEM_LIVE_FOLDER: {
2240 // Insert extra item to handle inserting folder
2241 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002242
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002243 ArrayList<String> shortcutNames = new ArrayList<String>();
2244 shortcutNames.add(res.getString(R.string.group_folder));
2245 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002246
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002247 ArrayList<ShortcutIconResource> shortcutIcons =
2248 new ArrayList<ShortcutIconResource>();
2249 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2250 R.drawable.ic_launcher_folder));
2251 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2252
2253 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2254 pickIntent.putExtra(Intent.EXTRA_INTENT,
2255 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2256 pickIntent.putExtra(Intent.EXTRA_TITLE,
2257 getText(R.string.title_select_live_folder));
2258 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002259
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002260 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2261 break;
2262 }
2263
2264 case AddAdapter.ITEM_WALLPAPER: {
2265 startWallpaper();
2266 break;
2267 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268 }
2269 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002270
2271 public void onShow(DialogInterface dialog) {
2272 mWorkspace.lock();
2273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 }
2275
2276 /**
2277 * Receives notifications when applications are added/removed.
2278 */
2279 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2280 @Override
2281 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002282 final String action = intent.getAction();
2283 final String packageName = intent.getData().getSchemeSpecificPart();
2284 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2285
2286 if (LauncherModel.DEBUG_LOADERS) {
2287 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2288 ", replacing=" + replacing);
2289 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002291
2292 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2293 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2294 if (!replacing) {
2295 removeShortcutsForPackage(packageName);
2296 if (LauncherModel.DEBUG_LOADERS) {
2297 d(LauncherModel.LOG_TAG, " --> remove package");
2298 }
2299 sModel.removePackage(Launcher.this, packageName);
2300 }
2301 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2302 // later, we will update the package at this time
2303 } else {
2304 if (!replacing) {
2305 if (LauncherModel.DEBUG_LOADERS) {
2306 d(LauncherModel.LOG_TAG, " --> add package");
2307 }
2308 sModel.addPackage(Launcher.this, packageName);
2309 } else {
2310 if (LauncherModel.DEBUG_LOADERS) {
2311 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2312 }
2313 sModel.updatePackage(Launcher.this, packageName);
2314 updateShortcutsForPackage(packageName);
2315 }
2316 }
2317 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002318 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002319 if (LauncherModel.DEBUG_LOADERS) {
2320 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2321 }
2322 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 }
2324 }
2325 }
2326
2327 /**
2328 * Receives notifications whenever the user favorites have changed.
2329 */
2330 private class FavoritesChangeObserver extends ContentObserver {
2331 public FavoritesChangeObserver() {
2332 super(new Handler());
2333 }
2334
2335 @Override
2336 public void onChange(boolean selfChange) {
2337 onFavoritesChanged();
2338 }
2339 }
2340
2341 /**
2342 * Receives intents from other applications to change the wallpaper.
2343 */
2344 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2345 private final Application mApplication;
2346 private WeakReference<Launcher> mLauncher;
2347
2348 WallpaperIntentReceiver(Application application, Launcher launcher) {
2349 mApplication = application;
2350 setLauncher(launcher);
2351 }
2352
2353 void setLauncher(Launcher launcher) {
2354 mLauncher = new WeakReference<Launcher>(launcher);
2355 }
2356
2357 @Override
2358 public void onReceive(Context context, Intent intent) {
2359 // Load the wallpaper from the ApplicationContext and store it locally
2360 // until the Launcher Activity is ready to use it
2361 final Drawable drawable = mApplication.getWallpaper();
2362 if (drawable instanceof BitmapDrawable) {
2363 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2364 } else {
2365 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2366 }
2367
2368 // If Launcher is alive, notify we have a new wallpaper
2369 if (mLauncher != null) {
2370 final Launcher launcher = mLauncher.get();
2371 if (launcher != null) {
2372 launcher.loadWallpaper();
2373 }
2374 }
2375 }
2376 }
2377
2378 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2379 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2380 private boolean mOpen;
2381
2382 public void onDrawerOpened() {
2383 if (!mOpen) {
2384 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002385
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002387 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388
2389 mOpen = true;
2390 }
2391 }
2392
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002393 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2394 view.getDrawingRect(bounds);
2395
2396 while (view != mDragLayer) {
2397 bounds.offset(view.getLeft(), view.getTop());
2398 view = (View) view.getParent();
2399 }
2400 }
2401
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 public void onDrawerClosed() {
2403 if (mOpen) {
2404 mHandleIcon.reverseTransition(150);
2405 mWorkspace.mDrawerBounds.setEmpty();
2406 mOpen = false;
2407 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 mAllAppsGrid.setSelection(0);
2410 mAllAppsGrid.clearTextFilter();
2411 }
2412
2413 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002414 if (PROFILE_DRAWER) {
2415 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2416 }
2417
2418 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2419 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 }
2421
2422 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002423 if (PROFILE_DRAWER) {
2424 android.os.Debug.stopMethodTracing();
2425 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 }
2427 }
2428
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002429 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002430 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002431 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002432 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 // Number of items to bind in every pass
2435 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436
2437 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002438 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002439 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002441
Karl Rosaen138a0412009-04-23 19:00:21 -07002442 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443
2444 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002445 ArrayList<LauncherAppWidgetInfo> appWidgets,
2446 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447
2448 mLauncher = new WeakReference<Launcher>(launcher);
2449 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002450 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002451
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002452 // Sort widgets so active workspace is bound first
2453 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2454 final int size = appWidgets.size();
2455 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002456
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002457 for (int i = 0; i < size; i++) {
2458 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2459 if (appWidgetInfo.screen == currentScreen) {
2460 mAppWidgets.addFirst(appWidgetInfo);
2461 } else {
2462 mAppWidgets.addLast(appWidgetInfo);
2463 }
2464 }
2465 }
Romain Guycbb89e42009-06-08 15:52:54 -07002466
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 public void startBindingItems() {
2468 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2469 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002470
2471 public void startBindingDrawer() {
2472 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2473 }
Romain Guycbb89e42009-06-08 15:52:54 -07002474
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002475 public void startBindingAppWidgetsWhenIdle() {
2476 // Ask for notification when message queue becomes idle
2477 final MessageQueue messageQueue = Looper.myQueue();
2478 messageQueue.addIdleHandler(this);
2479 }
Romain Guycbb89e42009-06-08 15:52:54 -07002480
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002481 public boolean queueIdle() {
2482 // Queue is idle, so start binding items
2483 startBindingAppWidgets();
2484 return false;
2485 }
2486
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002487 public void startBindingAppWidgets() {
2488 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2489 }
2490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 @Override
2492 public void handleMessage(Message msg) {
2493 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002494 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 return;
2496 }
Romain Guycbb89e42009-06-08 15:52:54 -07002497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 switch (msg.what) {
2499 case MESSAGE_BIND_ITEMS: {
2500 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2501 break;
2502 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002503 case MESSAGE_BIND_DRAWER: {
2504 launcher.bindDrawer(this, mDrawerAdapter);
2505 break;
2506 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002507 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002508 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002509 break;
2510 }
2511 }
2512 }
2513 }
Romain Guy73b979d2009-06-09 12:57:21 -07002514
Romain Guy27f98952009-07-17 15:28:42 -07002515 private class GesturesProcessor implements GestureOverlayView.OnGestureListener {
Romain Guy73b979d2009-06-09 12:57:21 -07002516
2517 private final GestureMatcher mMatcher = new GestureMatcher();
2518
2519 GesturesProcessor() {
2520 // TODO: Maybe the load should happen on a background thread?
2521 sLibrary.load();
2522 }
2523
2524 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002525 //noinspection PointlessBooleanExpression,ConstantConditions
2526 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2527 overlay.removeCallbacks(mMatcher);
Romain Guy1dd3a072009-07-16 13:21:01 -07002528 resetGesturesPrompt();
Romain Guy6fefcf12009-06-11 13:07:43 -07002529 }
Romain Guy73b979d2009-06-09 12:57:21 -07002530
Romain Guy1dd3a072009-07-16 13:21:01 -07002531 mGesturesAdd.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07002532 }
2533
2534 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2535 }
2536
Romain Guy73b979d2009-06-09 12:57:21 -07002537 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002538 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2539 mMatcher.gesture = overlay.getGesture();
2540 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2541 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002542 if (mGesturesAction.intent != null) {
2543 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07002544 setGestureAction(null, getString(R.string.gestures_unknown));
Romain Guy1ce1a242009-06-23 17:34:54 -07002545 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002546 } else {
2547 mMatcher.run();
2548 }
Romain Guy73b979d2009-06-09 12:57:21 -07002549 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002550 overlay.removeCallbacks(mMatcher);
2551
2552 mMatcher.gesture = overlay.getGesture();
2553 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2554 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002555 if (mGesturesAction.intent != null) {
2556 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07002557 setGestureAction(null, getString(R.string.gestures_unknown));
Romain Guy1ce1a242009-06-23 17:34:54 -07002558 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002559 } else {
2560 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2561 }
Romain Guy73b979d2009-06-09 12:57:21 -07002562 }
2563 }
2564
Romain Guy3cf604f2009-06-16 13:12:53 -07002565 void matchGesture(Gesture gesture) {
Romain Guy1dd3a072009-07-16 13:21:01 -07002566 mGesturesAdd.setVisibility(View.VISIBLE);
Romain Guy73b979d2009-06-09 12:57:21 -07002567
2568 if (gesture != null) {
2569 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2570
2571 if (DEBUG_GESTURES) {
2572 for (Prediction p : predictions) {
2573 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2574 }
2575 }
2576
2577 boolean match = false;
2578 if (predictions.size() > 0) {
2579 final Prediction prediction = predictions.get(0);
2580 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2581 match = true;
2582
2583 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2584 if (info != null) {
Romain Guy1dd3a072009-07-16 13:21:01 -07002585 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002586 }
2587 }
2588 }
2589
2590 if (!match){
Romain Guy1ce1a242009-06-23 17:34:54 -07002591 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07002592 setGestureUnknown();
Romain Guy73b979d2009-06-09 12:57:21 -07002593 }
2594 }
2595 }
2596
2597 private void updatePrompt(ApplicationInfo info) {
Romain Guy1dd3a072009-07-16 13:21:01 -07002598 // TODO: BRING BACK
Romain Guy6fefcf12009-06-11 13:07:43 -07002599 if (mGesturesAction.intent != null &&
Romain Guy1ce1a242009-06-23 17:34:54 -07002600 info.intent.toUri(0).equals(mGesturesAction.intent.toUri(0)) &&
Romain Guy1dd3a072009-07-16 13:21:01 -07002601 info.title.equals(mGesturesSend.getText())) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002602 return;
2603 }
Romain Guy3cf604f2009-06-16 13:12:53 -07002604
Romain Guy1dd3a072009-07-16 13:21:01 -07002605 setGestureAction(info.icon, info.title);
Romain Guy3cf604f2009-06-16 13:12:53 -07002606
Romain Guy73b979d2009-06-09 12:57:21 -07002607 mGesturesAction.intent = info.intent;
2608 }
2609
2610 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002611 //noinspection PointlessBooleanExpression,ConstantConditions
2612 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2613 overlay.removeCallbacks(mMatcher);
2614 }
Romain Guy73b979d2009-06-09 12:57:21 -07002615 }
2616
2617 void addGesture(String name, Gesture gesture) {
2618 sLibrary.addGesture(name, gesture);
2619 // TODO: On a background thread?
2620 sLibrary.save();
2621 }
2622
2623 void update(ApplicationInfo info, Gesture gesture) {
2624 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002625 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002626 }
2627
2628 class GestureMatcher implements Runnable {
2629 Gesture gesture;
2630
2631 public void run() {
2632 if (gesture != null) {
2633 matchGesture(gesture);
2634 }
2635 }
2636 }
2637 }
2638
2639 private class GesturesAction implements View.OnClickListener {
2640 Intent intent;
2641
2642 public void onClick(View v) {
2643 if (intent != null) {
2644 startActivitySafely(intent);
2645 }
2646 }
2647 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002648}
Karl Rosaen138a0412009-04-23 19:00:21 -07002649