blob: 8ac47d038fabd8007b7e06e2021943bcbe51f5a4 [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 Guy73b979d2009-06-09 12:57:21 -0700249 if (sLibrary == null) {
250 // The context is not kept by the library so it's safe to do this
251 sLibrary = GestureLibraries.fromPrivateFile(Launcher.this,
252 GesturesConstants.STORE_NAME);
253 }
254
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700255 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700256
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700257 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
258 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 if (PROFILE_STARTUP) {
261 android.os.Debug.startMethodTracing("/sdcard/launcher");
262 }
263
264 checkForLocaleChange();
265 setWallpaperDimension();
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 setContentView(R.layout.launcher);
268 setupViews();
269
270 registerIntentReceivers();
271 registerContentObservers();
272
273 mSavedState = savedInstanceState;
274 restoreState(mSavedState);
275
276 if (PROFILE_STARTUP) {
277 android.os.Debug.stopMethodTracing();
278 }
279
280 if (!mRestoring) {
281 startLoaders();
282 }
283
284 // For handling default keys
285 mDefaultKeySsb = new SpannableStringBuilder();
286 Selection.setSelection(mDefaultKeySsb, 0);
287 }
Romain Guycbb89e42009-06-08 15:52:54 -0700288
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700290 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
291 readConfiguration(this, localeConfiguration);
292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 final Configuration configuration = getResources().getConfiguration();
294
Romain Guy98d01652009-06-30 16:21:04 -0700295 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 final String locale = configuration.locale.toString();
297
Romain Guy98d01652009-06-30 16:21:04 -0700298 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 final int mcc = configuration.mcc;
300
Romain Guy98d01652009-06-30 16:21:04 -0700301 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 final int mnc = configuration.mnc;
303
304 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
305
306 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700307 localeConfiguration.locale = locale;
308 localeConfiguration.mcc = mcc;
309 localeConfiguration.mnc = mnc;
310
311 writeConfiguration(this, localeConfiguration);
312 }
313 }
314
315 private static class LocaleConfiguration {
316 public String locale;
317 public int mcc = -1;
318 public int mnc = -1;
319 }
320
321 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
322 DataInputStream in = null;
323 try {
324 in = new DataInputStream(context.openFileInput(PREFERENCES));
325 configuration.locale = in.readUTF();
326 configuration.mcc = in.readInt();
327 configuration.mnc = in.readInt();
328 } catch (FileNotFoundException e) {
329 // Ignore
330 } catch (IOException e) {
331 // Ignore
332 } finally {
333 if (in != null) {
334 try {
335 in.close();
336 } catch (IOException e) {
337 // Ignore
338 }
339 }
340 }
341 }
342
343 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
344 DataOutputStream out = null;
345 try {
346 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
347 out.writeUTF(configuration.locale);
348 out.writeInt(configuration.mcc);
349 out.writeInt(configuration.mnc);
350 out.flush();
351 } catch (FileNotFoundException e) {
352 // Ignore
353 } catch (IOException e) {
354 //noinspection ResultOfMethodCallIgnored
355 context.getFileStreamPath(PREFERENCES).delete();
356 } finally {
357 if (out != null) {
358 try {
359 out.close();
360 } catch (IOException e) {
361 // Ignore
362 }
363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 }
365 }
366
367 static int getScreen() {
368 synchronized (sLock) {
369 return sScreen;
370 }
371 }
372
373 static void setScreen(int screen) {
374 synchronized (sLock) {
375 sScreen = screen;
376 }
377 }
378
379 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700380 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
381 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 mRestoring = false;
384 }
385
386 private void setWallpaperDimension() {
387 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
388 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
389
390 Display display = getWindowManager().getDefaultDisplay();
391 boolean isPortrait = display.getWidth() < display.getHeight();
392
393 final int width = isPortrait ? display.getWidth() : display.getHeight();
394 final int height = isPortrait ? display.getHeight() : display.getWidth();
395 try {
396 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
397 } catch (RemoteException e) {
398 // System is dead!
399 }
400 }
401
402 @Override
403 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700404 mWaitingForResult = false;
405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 // The pattern used here is that a user PICKs a specific application,
407 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
410 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700411
Romain Guy73b979d2009-06-09 12:57:21 -0700412 if (resultCode == RESULT_OK && (mAddItemCellInfo != null ||
413 ((requestCode == REQUEST_PICK_GESTURE_ACTION ||
414 requestCode == REQUEST_CREATE_GESTURE_ACTION ||
415 requestCode == REQUEST_CREATE_GESTURE_APPLICATION_ACTION) && mCurrentGesture != null))) {
416
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 switch (requestCode) {
418 case REQUEST_PICK_APPLICATION:
419 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
420 break;
421 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700422 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 break;
424 case REQUEST_CREATE_SHORTCUT:
425 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
426 break;
427 case REQUEST_PICK_LIVE_FOLDER:
428 addLiveFolder(data);
429 break;
430 case REQUEST_CREATE_LIVE_FOLDER:
431 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
432 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 case REQUEST_PICK_APPWIDGET:
434 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700436 case REQUEST_CREATE_APPWIDGET:
437 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 break;
Romain Guy73b979d2009-06-09 12:57:21 -0700439 case REQUEST_PICK_GESTURE_ACTION:
440 processShortcut(data, REQUEST_CREATE_GESTURE_APPLICATION_ACTION,
441 REQUEST_CREATE_GESTURE_ACTION);
442 break;
443 case REQUEST_CREATE_GESTURE_ACTION:
444 completeCreateGesture(data, true);
445 break;
446 case REQUEST_CREATE_GESTURE_APPLICATION_ACTION:
447 completeCreateGesture(data, false);
448 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700450 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700452 // Clean up the appWidgetId if we canceled
453 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
454 if (appWidgetId != -1) {
455 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 }
457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
459
460 @Override
461 protected void onResume() {
462 super.onResume();
463
464 if (mRestoring) {
465 startLoaders();
466 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700467
468 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
469 // onNewIntent), then close the search dialog if needed, because it probably
470 // came from the user pressing 'home' (rather than, for example, pressing 'back').
471 if (mIsNewIntent) {
472 // Post to a handler so that this happens after the search dialog tries to open
473 // itself again.
474 mWorkspace.post(new Runnable() {
475 public void run() {
476 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
477 ServiceManager.getService(Context.SEARCH_SERVICE));
478 try {
479 searchManagerService.stopSearch();
480 } catch (RemoteException e) {
481 e(LOG_TAG, "error stopping search", e);
482 }
483 }
484 });
485 }
486
487 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 }
489
490 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700491 protected void onPause() {
492 super.onPause();
Romain Guy73b979d2009-06-09 12:57:21 -0700493 if (mGesturesWindow != null) {
494 mGesturesWindow.setAnimationStyle(0);
495 mGesturesWindow.update();
496 }
Romain Guycbb89e42009-06-08 15:52:54 -0700497 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700498 }
Romain Guycbb89e42009-06-08 15:52:54 -0700499
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700500 @Override
Romain Guy73b979d2009-06-09 12:57:21 -0700501 protected void onStop() {
502 super.onStop();
Romain Guyaad5ef42009-06-10 02:48:37 -0700503 if (mHideGesturesPanel) {
504 mHideGesturesPanel = false;
505 hideGesturesPanel();
506 }
Romain Guy73b979d2009-06-09 12:57:21 -0700507 }
508
509 @Override
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700510 public Object onRetainNonConfigurationInstance() {
511 // Flag any binder to stop early before switching
512 if (mBinder != null) {
513 mBinder.mTerminate = true;
514 }
Romain Guycbb89e42009-06-08 15:52:54 -0700515
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700516 if (PROFILE_ROTATE) {
517 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
518 }
519 return null;
520 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700521
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522 private boolean acceptFilter() {
523 final InputMethodManager inputManager = (InputMethodManager)
524 getSystemService(Context.INPUT_METHOD_SERVICE);
525 return !inputManager.isFullscreenMode();
526 }
527
528 @Override
529 public boolean onKeyDown(int keyCode, KeyEvent event) {
530 boolean handled = super.onKeyDown(keyCode, event);
531 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
532 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
533 keyCode, event);
534 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700535 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700536 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700537 // showSearchDialog()
538 // If there are multiple keystrokes before the search dialog takes focus,
539 // onSearchRequested() will be called for every keystroke,
540 // but it is idempotent, so it's fine.
541 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 }
543 }
544
545 return handled;
546 }
547
Karl Rosaen138a0412009-04-23 19:00:21 -0700548 private String getTypedText() {
549 return mDefaultKeySsb.toString();
550 }
551
552 private void clearTypedText() {
553 mDefaultKeySsb.clear();
554 mDefaultKeySsb.clearSpans();
555 Selection.setSelection(mDefaultKeySsb, 0);
556 }
557
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 /**
559 * Restores the previous state, if it exists.
560 *
561 * @param savedState The previous state.
562 */
563 private void restoreState(Bundle savedState) {
564 if (savedState == null) {
565 return;
566 }
567
568 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
569 if (currentScreen > -1) {
570 mWorkspace.setCurrentScreen(currentScreen);
571 }
572
573 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
574 if (addScreen > -1) {
575 mAddItemCellInfo = new CellLayout.CellInfo();
576 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
577 addItemCellInfo.valid = true;
578 addItemCellInfo.screen = addScreen;
579 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
580 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
581 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
582 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
583 addItemCellInfo.findVacantCellsFromOccupied(
584 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
585 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
586 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
587 mRestoring = true;
588 }
589
590 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
591 if (renameFolder) {
592 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
593 mFolderInfo = sModel.getFolderById(this, id);
594 mRestoring = true;
595 }
Romain Guy73b979d2009-06-09 12:57:21 -0700596
Romain Guy1ce1a242009-06-23 17:34:54 -0700597 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_PENDING_GESTURE);
598
Romain Guy3cf604f2009-06-16 13:12:53 -0700599 boolean gesturesShowing = savedState.getBoolean(RUNTIME_STATE_GESTURES_PANEL, false);
600 if (gesturesShowing) {
Romain Guyf38b3d52009-07-01 16:39:46 -0700601 if (mCurrentGesture == null) {
602 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_GESTURES_PANEL_GESTURE);
603 }
604 final Gesture gesture = mCurrentGesture;
Romain Guy3cf604f2009-06-16 13:12:53 -0700605 mWorkspace.post(new Runnable() {
606 public void run() {
607 showGesturesPanel(false);
Romain Guye769bc72009-07-17 14:18:50 -0700608 if (gesture != null) {
609 mGesturesProcessor.matchGesture(gesture);
610 mWorkspace.post(new Runnable() {
611 public void run() {
612 if (gesture != null) {
613 mGesturesOverlay.setGesture(gesture);
614 }
Romain Guy3cf604f2009-06-16 13:12:53 -0700615 }
Romain Guye769bc72009-07-17 14:18:50 -0700616 });
617 }
Romain Guy3cf604f2009-06-16 13:12:53 -0700618 }
619 });
620 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 }
622
623 /**
624 * Finds all the views we need and configure them properly.
625 */
626 private void setupViews() {
627 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
628 final DragLayer dragLayer = mDragLayer;
629
630 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
631 final Workspace workspace = mWorkspace;
632
633 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
634 final SlidingDrawer drawer = mDrawer;
635
636 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
637 final AllAppsGridView grid = mAllAppsGrid;
638
639 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
640
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700641 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
642 mHandleView.setLauncher(this);
643 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 mHandleIcon.setCrossFadeEnabled(true);
645
646 drawer.lock();
647 final DrawerManager drawerManager = new DrawerManager();
648 drawer.setOnDrawerOpenListener(drawerManager);
649 drawer.setOnDrawerCloseListener(drawerManager);
650 drawer.setOnDrawerScrollListener(drawerManager);
651
Karl Rosaen138a0412009-04-23 19:00:21 -0700652 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 grid.setDragger(dragLayer);
654 grid.setLauncher(this);
655
656 workspace.setOnLongClickListener(this);
657 workspace.setDragger(dragLayer);
658 workspace.setLauncher(this);
659 loadWallpaper();
660
661 deleteZone.setLauncher(this);
662 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700663 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664
665 dragLayer.setIgnoredDropTarget(grid);
666 dragLayer.setDragScoller(workspace);
667 dragLayer.setDragListener(deleteZone);
Romain Guy73b979d2009-06-09 12:57:21 -0700668
669 mGesturesPanel = mInflater.inflate(R.layout.gestures, mDragLayer, false);
670 final View gesturesPanel = mGesturesPanel;
671
Romain Guy73b979d2009-06-09 12:57:21 -0700672 mGesturesAction = new GesturesAction();
673
Romain Guy1dd3a072009-07-16 13:21:01 -0700674 mGesturesPrompt = (TextView) gesturesPanel.findViewById(R.id.gestures_prompt);
675 mGesturesSend = (Button) gesturesPanel.findViewById(R.id.gestures_action);
676 mGesturesSend.setOnClickListener(mGesturesAction);
677
Romain Guy73b979d2009-06-09 12:57:21 -0700678 mGesturesAdd = (ImageView) gesturesPanel.findViewById(R.id.gestures_add);
679 final ImageView gesturesAdd = mGesturesAdd;
Romain Guy73b979d2009-06-09 12:57:21 -0700680 gesturesAdd.setOnClickListener(new View.OnClickListener() {
681 public void onClick(View v) {
682 createGesture();
683 }
684 });
685
Romain Guy1dd3a072009-07-16 13:21:01 -0700686 gesturesPanel.findViewById(R.id.gestures_list).setOnClickListener(new View.OnClickListener() {
687 public void onClick(View v) {
688 startActivity(new Intent(Launcher.this, GesturesActivity.class));
689 }
690 });
691
Romain Guy73b979d2009-06-09 12:57:21 -0700692 mGesturesOverlay = (GestureOverlayView) gesturesPanel.findViewById(R.id.gestures_overlay);
693 mGesturesProcessor = new GesturesProcessor();
694
695 final GestureOverlayView overlay = mGesturesOverlay;
Romain Guy73b979d2009-06-09 12:57:21 -0700696 overlay.addOnGestureListener(mGesturesProcessor);
Romain Guy73b979d2009-06-09 12:57:21 -0700697 }
698
699 private void createGesture() {
Romain Guyf280f202009-06-26 10:31:07 -0700700 if (!mWaitingForResult) {
701 mCurrentGesture = mGesturesOverlay.getGesture();
702 mWaitingForResult = true;
703 pickShortcut(REQUEST_PICK_GESTURE_ACTION, R.string.title_select_shortcut);
704 }
Romain Guy73b979d2009-06-09 12:57:21 -0700705 }
706
707 private void completeCreateGesture(Intent data, boolean isShortcut) {
708 ApplicationInfo info;
709
710 if (isShortcut) {
711 info = infoFromShortcutIntent(this, data);
712 } else {
713 info = infoFromApplicationIntent(this, data);
714 }
715
716 boolean success = false;
717 if (info != null) {
718 info.isGesture = true;
719
720 if (LauncherModel.addGestureToDatabase(this, info, false)) {
721 mGesturesProcessor.addGesture(String.valueOf(info.id), mCurrentGesture);
722 mGesturesProcessor.update(info, mCurrentGesture);
723 Toast.makeText(this, getString(R.string.gestures_created, info.title),
724 Toast.LENGTH_SHORT).show();
725 success = true;
726 }
727 }
728
729 if (!success) {
730 Toast.makeText(this, getString(R.string.gestures_failed), Toast.LENGTH_SHORT).show();
731 }
732
733 mCurrentGesture = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 }
735
736 /**
737 * Creates a view representing a shortcut.
738 *
739 * @param info The data structure describing the shortcut.
740 *
741 * @return A View inflated from R.layout.application.
742 */
743 View createShortcut(ApplicationInfo info) {
744 return createShortcut(R.layout.application,
745 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
746 }
747
748 /**
749 * Creates a view representing a shortcut inflated from the specified resource.
750 *
751 * @param layoutResId The id of the XML layout used to create the shortcut.
752 * @param parent The group the shortcut belongs to.
753 * @param info The data structure describing the shortcut.
754 *
755 * @return A View inflated from layoutResId.
756 */
757 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
758 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
759
760 if (!info.filtered) {
761 info.icon = Utilities.createIconThumbnail(info.icon, this);
762 info.filtered = true;
763 }
764
765 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
766 favorite.setText(info.title);
767 favorite.setTag(info);
768 favorite.setOnClickListener(this);
769
770 return favorite;
771 }
772
773 /**
774 * Add an application shortcut to the workspace.
775 *
776 * @param data The intent describing the application.
777 * @param cellInfo The position on screen where to create the shortcut.
778 */
779 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
780 boolean insertAtFirst) {
781 cellInfo.screen = mWorkspace.getCurrentScreen();
782 if (!findSingleSlot(cellInfo)) return;
783
Romain Guy73b979d2009-06-09 12:57:21 -0700784 final ApplicationInfo info = infoFromApplicationIntent(context, data);
785 if (info != null) {
786 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
787 }
788 }
789
790 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 ComponentName component = data.getComponent();
792 PackageManager packageManager = context.getPackageManager();
793 ActivityInfo activityInfo = null;
794 try {
795 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
796 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700797 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 }
Romain Guycbb89e42009-06-08 15:52:54 -0700799
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 if (activityInfo != null) {
801 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700802
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 itemInfo.title = activityInfo.loadLabel(packageManager);
804 if (itemInfo.title == null) {
805 itemInfo.title = activityInfo.name;
806 }
Romain Guycbb89e42009-06-08 15:52:54 -0700807
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
809 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
810 itemInfo.icon = activityInfo.loadIcon(packageManager);
811 itemInfo.container = ItemInfo.NO_ID;
812
Romain Guy73b979d2009-06-09 12:57:21 -0700813 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 }
Romain Guy73b979d2009-06-09 12:57:21 -0700815
816 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
Romain Guycbb89e42009-06-08 15:52:54 -0700818
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 /**
820 * Add a shortcut to the workspace.
821 *
822 * @param data The intent describing the shortcut.
823 * @param cellInfo The position on screen where to create the shortcut.
824 * @param insertAtFirst
825 */
826 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
827 boolean insertAtFirst) {
828 cellInfo.screen = mWorkspace.getCurrentScreen();
829 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700830
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
832
833 if (!mRestoring) {
834 sModel.addDesktopItem(info);
835
836 final View view = createShortcut(info);
837 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
838 } else if (sModel.isDesktopLoaded()) {
839 sModel.addDesktopItem(info);
840 }
841 }
842
Romain Guycbb89e42009-06-08 15:52:54 -0700843
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700845 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700847 * @param data The intent describing the appWidgetId.
848 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700850 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 boolean insertAtFirst) {
852
853 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700854 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700855
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700856 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700857
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700858 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700859
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700860 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700862 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700863
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 // Try finding open space on Launcher screen
865 final int[] xy = mCellCoordinates;
866 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
867
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700868 // Build Launcher-specific widget info and save to database
869 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 launcherInfo.spanX = spans[0];
871 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700872
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 LauncherModel.addItemToDatabase(this, launcherInfo,
874 LauncherSettings.Favorites.CONTAINER_DESKTOP,
875 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
876
877 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700878 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700879
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700881 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700882
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700883 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700885
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
887 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
888 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700889 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
891 }
Romain Guycbb89e42009-06-08 15:52:54 -0700892
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700893 public LauncherAppWidgetHost getAppWidgetHost() {
894 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
Romain Guycbb89e42009-06-08 15:52:54 -0700896
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 static ApplicationInfo addShortcut(Context context, Intent data,
898 CellLayout.CellInfo cellInfo, boolean notify) {
899
Romain Guy73b979d2009-06-09 12:57:21 -0700900 final ApplicationInfo info = infoFromShortcutIntent(context, data);
901 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
902 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
903
904 return info;
905 }
906
907 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
909 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
910 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
911
912 Drawable icon = null;
913 boolean filtered = false;
914 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700915 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916
917 if (bitmap != null) {
918 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
919 filtered = true;
920 customIcon = true;
921 } else {
922 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700923 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700925 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 final PackageManager packageManager = context.getPackageManager();
927 Resources resources = packageManager.getResourcesForApplication(
928 iconResource.packageName);
929 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
930 icon = resources.getDrawable(id);
931 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700932 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 }
934 }
935 }
936
937 if (icon == null) {
938 icon = context.getPackageManager().getDefaultActivityIcon();
939 }
940
941 final ApplicationInfo info = new ApplicationInfo();
942 info.icon = icon;
943 info.filtered = filtered;
944 info.title = name;
945 info.intent = intent;
946 info.customIcon = customIcon;
947 info.iconResource = iconResource;
948
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 return info;
950 }
951
952 @Override
953 protected void onNewIntent(Intent intent) {
954 super.onNewIntent(intent);
955
956 // Close the menu
957 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
958 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700959
960 // Set this flag so that onResume knows to close the search dialog if it's open,
961 // because this was a new intent (thus a press of 'home' or some such) rather than
962 // for example onResume being called when the user pressed the 'back' button.
963 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964
965 try {
966 dismissDialog(DIALOG_CREATE_SHORTCUT);
967 // Unlock the workspace if the dialog was showing
968 mWorkspace.unlock();
969 } catch (Exception e) {
970 // An exception is thrown if the dialog is not visible, which is fine
971 }
972
973 try {
974 dismissDialog(DIALOG_RENAME_FOLDER);
975 // Unlock the workspace if the dialog was showing
976 mWorkspace.unlock();
977 } catch (Exception e) {
978 // An exception is thrown if the dialog is not visible, which is fine
979 }
980
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
982 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700983
Romain Guy1dd3a072009-07-16 13:21:01 -0700984 // TODO: This really should not be done here every time
985 final SharedPreferences preferences =
Romain Guy6b3f51d2009-07-16 17:45:04 -0700986 getSharedPreferences(GesturesConstants.PREFERENCES_NAME, MODE_PRIVATE);
Romain Guy1dd3a072009-07-16 13:21:01 -0700987 final boolean homeKey = preferences.getBoolean(
Romain Guy6b3f51d2009-07-16 17:45:04 -0700988 GesturesConstants.PREFERENCES_HOME_KEY, false);
Romain Guy94406842009-06-17 16:18:58 -0700989
Romain Guy1dd3a072009-07-16 13:21:01 -0700990 if (!homeKey) {
991 if (!mWorkspace.isDefaultScreenShowing()) {
992 mWorkspace.moveToDefaultScreen();
993 }
Romain Guy94406842009-06-17 16:18:58 -0700994
Romain Guy1dd3a072009-07-16 13:21:01 -0700995 if (mGesturesWindow == null || mGesturesWindow.isShowing()) {
996 hideGesturesPanel();
997 }
998 } else {
999 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE
1000 && (mDragLayer.hasWindowFocus() || (mGesturesWindow != null
1001 && mGesturesWindow.isShowing()))) {
1002
1003 SearchManager searchManager =
1004 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1005
1006 if (!searchManager.isVisible()) {
1007 onHomeKeyPressed();
1008 }
Romain Guyb8734242009-06-10 11:53:57 -07001009 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -07001013
1014 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 if (v != null && v.getWindowToken() != null) {
1016 InputMethodManager imm = (InputMethodManager)getSystemService(
1017 INPUT_METHOD_SERVICE);
1018 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1019 }
1020 } else {
1021 closeDrawer(false);
1022 }
1023 }
1024 }
1025
Romain Guy73b979d2009-06-09 12:57:21 -07001026 private void onHomeKeyPressed() {
1027 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
1028 showGesturesPanel();
1029 } else {
1030 hideGesturesPanel();
1031 }
1032 }
1033
1034 private void showGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -07001035 showGesturesPanel(true);
1036 }
1037
1038 private void showGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07001039 resetGesturesPrompt();
1040
Romain Guy1dd3a072009-07-16 13:21:01 -07001041 mGesturesAdd.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07001042
1043 mGesturesOverlay.clear(false);
1044
1045 PopupWindow window;
1046 if (mGesturesWindow == null) {
1047 mGesturesWindow = new PopupWindow(this);
1048 window = mGesturesWindow;
1049 window.setFocusable(true);
1050 window.setTouchable(true);
Romain Guy1dd3a072009-07-16 13:21:01 -07001051 window.setBackgroundDrawable(getResources().getDrawable(
1052 android.R.drawable.screen_background_dark));
Romain Guy73b979d2009-06-09 12:57:21 -07001053 window.setContentView(mGesturesPanel);
1054 } else {
1055 window = mGesturesWindow;
1056 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001057 window.setAnimationStyle(animate ? com.android.internal.R.style.Animation_Toast : 0);
Romain Guy73b979d2009-06-09 12:57:21 -07001058
1059 final int[] xy = new int[2];
1060 final DragLayer dragLayer = mDragLayer;
1061 dragLayer.getLocationOnScreen(xy);
1062
1063 window.setWidth(dragLayer.getWidth());
1064 window.setHeight(dragLayer.getHeight() - 1);
1065 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
1066 }
1067
1068 private void resetGesturesPrompt() {
1069 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07001070 mGesturesPrompt.setText(R.string.gestures_instructions);
1071 mGesturesPrompt.setVisibility(View.VISIBLE);
1072 mGesturesSend.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07001073 }
1074
Romain Guy1dd3a072009-07-16 13:21:01 -07001075 private void setGestureUnknown() {
Romain Guy73b979d2009-06-09 12:57:21 -07001076 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07001077 mGesturesPrompt.setText(R.string.gestures_unknown);
1078 mGesturesPrompt.setVisibility(View.VISIBLE);
1079 mGesturesSend.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07001080 }
1081
Romain Guy1dd3a072009-07-16 13:21:01 -07001082 private void setGestureAction(Drawable icon, CharSequence title) {
1083 mGesturesPrompt.setVisibility(View.GONE);
1084 mGesturesSend.setVisibility(View.VISIBLE);
1085 mGesturesSend.setText(title);
1086 mGesturesSend.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
Romain Guy3cf604f2009-06-16 13:12:53 -07001087 }
1088
Romain Guy73b979d2009-06-09 12:57:21 -07001089 void hideGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -07001090 hideGesturesPanel(true);
1091 }
1092
1093 void hideGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07001094 if (mGesturesWindow != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001095 final PopupWindow popupWindow = mGesturesWindow;
1096 popupWindow.setAnimationStyle(animate ?
Romain Guy1dd3a072009-07-16 13:21:01 -07001097 com.android.internal.R.style.Animation_Toast : 0);
Romain Guy3cf604f2009-06-16 13:12:53 -07001098 popupWindow.update();
1099 popupWindow.dismiss();
Romain Guy73b979d2009-06-09 12:57:21 -07001100 }
1101 }
1102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 @Override
1104 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1105 // Do not call super here
1106 mSavedInstanceState = savedInstanceState;
1107 }
1108
1109 @Override
1110 protected void onSaveInstanceState(Bundle outState) {
1111 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1112
1113 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1114 if (folders.size() > 0) {
1115 final int count = folders.size();
1116 long[] ids = new long[count];
1117 for (int i = 0; i < count; i++) {
1118 final FolderInfo info = folders.get(i).getInfo();
1119 ids[i] = info.id;
1120 }
1121 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1122 } else {
1123 super.onSaveInstanceState(outState);
1124 }
1125
Romain Guy3cf604f2009-06-16 13:12:53 -07001126 final boolean isConfigurationChange = getChangingConfigurations() != 0;
1127
Romain Guy5a941392009-04-28 15:18:25 -07001128 // When the drawer is opened and we are saving the state because of a
1129 // configuration change
Romain Guy3cf604f2009-06-16 13:12:53 -07001130 if (mDrawer.isOpened() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133
1134 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1135 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1136 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1137
1138 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1139 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1140 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1141 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1142 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1143 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1144 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1145 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1146 layout.getOccupiedCells());
1147 }
1148
1149 if (mFolderInfo != null && mWaitingForResult) {
1150 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1151 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1152 }
Romain Guy73b979d2009-06-09 12:57:21 -07001153
1154 if (mCurrentGesture != null && mWaitingForResult) {
1155 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1156 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001157
Romain Guy1ce1a242009-06-23 17:34:54 -07001158 if (mGesturesWindow != null && mGesturesWindow.isShowing()) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001159 outState.putBoolean(RUNTIME_STATE_GESTURES_PANEL, true);
1160
Romain Guy1ce1a242009-06-23 17:34:54 -07001161 if (mCurrentGesture == null || !mWaitingForResult) {
1162 final Gesture gesture = mGesturesOverlay.getGesture();
1163 if (gesture != null) {
1164 outState.putParcelable(RUNTIME_STATE_GESTURES_PANEL_GESTURE, gesture);
1165 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001166 }
1167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 }
1169
1170 @Override
1171 public void onDestroy() {
1172 mDestroyed = true;
1173
1174 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001177 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001179 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 }
1181
1182 TextKeyListener.getInstance().release();
1183
Romain Guy3cf604f2009-06-16 13:12:53 -07001184 hideGesturesPanel(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 mAllAppsGrid.clearTextFilter();
1186 mAllAppsGrid.setAdapter(null);
1187 sModel.unbind();
1188 sModel.abortLoaders();
1189
1190 getContentResolver().unregisterContentObserver(mObserver);
1191 unregisterReceiver(mApplicationsReceiver);
1192 }
1193
1194 @Override
1195 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001196 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 super.startActivityForResult(intent, requestCode);
1198 }
1199
1200 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001201 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001203
1204 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001205
Karl Rosaen138a0412009-04-23 19:00:21 -07001206 // Slide the search widget to the top, if it's on the current screen,
1207 // otherwise show the search dialog immediately.
1208 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1209 if (searchWidget == null) {
1210 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1211 } else {
1212 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1213 // show the currently typed text in the search widget while sliding
1214 searchWidget.setQuery(getTypedText());
1215 }
1216 }
Romain Guycbb89e42009-06-08 15:52:54 -07001217
Karl Rosaen138a0412009-04-23 19:00:21 -07001218 /**
1219 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001220 *
1221 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001222 */
Romain Guycbb89e42009-06-08 15:52:54 -07001223 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001224 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001225
Karl Rosaen138a0412009-04-23 19:00:21 -07001226 if (initialQuery == null) {
1227 // Use any text typed in the launcher as the initial query
1228 initialQuery = getTypedText();
1229 clearTypedText();
1230 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 if (appSearchData == null) {
1232 appSearchData = new Bundle();
1233 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1234 }
Romain Guycbb89e42009-06-08 15:52:54 -07001235
Karl Rosaen138a0412009-04-23 19:00:21 -07001236 final SearchManager searchManager =
1237 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1238
1239 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1240 if (searchWidget != null) {
1241 // This gets called when the user leaves the search dialog to go back to
1242 // the Launcher.
1243 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1244 public void onCancel() {
1245 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001246 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001247 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001248 });
1249 }
Romain Guycbb89e42009-06-08 15:52:54 -07001250
Karl Rosaen138a0412009-04-23 19:00:21 -07001251 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001252 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
1254
Karl Rosaen138a0412009-04-23 19:00:21 -07001255 /**
1256 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001257 */
Romain Guy5a941392009-04-28 15:18:25 -07001258 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001259 // Close search dialog
1260 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001261 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001262 // Restore search widget to its normal position
1263 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1264 if (searchWidget != null) {
1265 searchWidget.stopSearch(false);
1266 }
1267 }
Romain Guycbb89e42009-06-08 15:52:54 -07001268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 @Override
1270 public boolean onCreateOptionsMenu(Menu menu) {
1271 if (mDesktopLocked) return false;
1272
1273 super.onCreateOptionsMenu(menu);
1274 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1275 .setIcon(android.R.drawable.ic_menu_add)
1276 .setAlphabeticShortcut('A');
1277 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1278 .setIcon(android.R.drawable.ic_menu_gallery)
1279 .setAlphabeticShortcut('W');
1280 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1281 .setIcon(android.R.drawable.ic_search_category_default)
1282 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1283 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1284 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1285 .setAlphabeticShortcut('N');
1286
Romain Guy1dd3a072009-07-16 13:21:01 -07001287 // TODO: Remove
Romain Guy73b979d2009-06-09 12:57:21 -07001288 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
Romain Guy1dd3a072009-07-16 13:21:01 -07001289 .setIcon(com.android.internal.R.drawable.ic_menu_compose)
1290 .setAlphabeticShortcut('G');
Romain Guy73b979d2009-06-09 12:57:21 -07001291
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001293 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1294 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295
1296 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1297 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1298 .setIntent(settings);
1299
1300 return true;
1301 }
1302
1303 @Override
1304 public boolean onPrepareOptionsMenu(Menu menu) {
1305 super.onPrepareOptionsMenu(menu);
1306
1307 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1308 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1309
1310 return true;
1311 }
1312
1313 @Override
1314 public boolean onOptionsItemSelected(MenuItem item) {
1315 switch (item.getItemId()) {
1316 case MENU_ADD:
1317 addItems();
1318 return true;
1319 case MENU_WALLPAPER_SETTINGS:
1320 startWallpaper();
1321 return true;
1322 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001323 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 return true;
1325 case MENU_NOTIFICATIONS:
1326 showNotifications();
1327 return true;
Romain Guy1dd3a072009-07-16 13:21:01 -07001328 case MENU_GESTURES:
1329 showGesturesPanel();
1330 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
1333 return super.onOptionsItemSelected(item);
1334 }
Romain Guycbb89e42009-06-08 15:52:54 -07001335
Karl Rosaen138a0412009-04-23 19:00:21 -07001336 /**
1337 * Indicates that we want global search for this activity by setting the globalSearch
1338 * argument for {@link #startSearch} to true.
1339 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001341 @Override
1342 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001343 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001344 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001345 }
1346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 private void addItems() {
1348 showAddDialog(mMenuAddInfo);
1349 }
1350
1351 private void removeShortcutsForPackage(String packageName) {
1352 if (packageName != null && packageName.length() > 0) {
1353 mWorkspace.removeShortcutsForPackage(packageName);
1354 }
1355 }
Romain Guycbb89e42009-06-08 15:52:54 -07001356
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001357 private void updateShortcutsForPackage(String packageName) {
1358 if (packageName != null && packageName.length() > 0) {
1359 mWorkspace.updateShortcutsForPackage(packageName);
1360 }
1361 }
1362
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001363 void addAppWidget(Intent data) {
1364 // TODO: catch bad widget exception when sent
1365 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001367 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1368 if (SEARCH_WIDGET.equals(customWidget)) {
1369 // We don't need this any more, since this isn't a real app widget.
1370 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1371 // add the search widget
1372 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001374 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1375
1376 if (appWidget.configure != null) {
1377 // Launch over to configure widget, if needed
1378 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1379 intent.setComponent(appWidget.configure);
1380 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1381
1382 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1383 } else {
1384 // Otherwise just add it
1385 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
1388 }
Romain Guycbb89e42009-06-08 15:52:54 -07001389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 void addSearch() {
1391 final Widget info = Widget.makeSearch();
1392 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001393
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 final int[] xy = mCellCoordinates;
1395 final int spanX = info.spanX;
1396 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001399
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 sModel.addDesktopItem(info);
1401 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1402 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 final View view = mInflater.inflate(info.layoutResource, null);
1405 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001406 Search search = (Search) view.findViewById(R.id.widget_search);
1407 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1410 }
1411
Romain Guy73b979d2009-06-09 12:57:21 -07001412 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001413 // Handle case where user selected "Applications"
1414 String applicationName = getResources().getString(R.string.group_applications);
1415 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001416
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001417 if (applicationName != null && applicationName.equals(shortcutName)) {
1418 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1419 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001420
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001421 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1422 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001423 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001424 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001425 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001426 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 }
1428
1429 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001430 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001431 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001432 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001433
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001434 if (folderName != null && folderName.equals(shortcutName)) {
1435 addFolder(!mDesktopLocked);
1436 } else {
1437 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1438 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 }
1440
1441 void addFolder(boolean insertAtFirst) {
1442 UserFolderInfo folderInfo = new UserFolderInfo();
1443 folderInfo.title = getText(R.string.folder_name);
1444
1445 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1446 cellInfo.screen = mWorkspace.getCurrentScreen();
1447 if (!findSingleSlot(cellInfo)) return;
1448
1449 // Update the model
1450 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1451 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1452 sModel.addDesktopItem(folderInfo);
1453 sModel.addFolder(folderInfo);
1454
1455 // Create the view
1456 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1457 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1458 mWorkspace.addInCurrentScreen(newFolder,
1459 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1460 }
Romain Guycbb89e42009-06-08 15:52:54 -07001461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1463 boolean insertAtFirst) {
1464 cellInfo.screen = mWorkspace.getCurrentScreen();
1465 if (!findSingleSlot(cellInfo)) return;
1466
1467 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1468
1469 if (!mRestoring) {
1470 sModel.addDesktopItem(info);
1471
1472 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1473 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1474 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1475 } else if (sModel.isDesktopLoaded()) {
1476 sModel.addDesktopItem(info);
1477 }
1478 }
1479
1480 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1481 CellLayout.CellInfo cellInfo, boolean notify) {
1482
1483 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1484 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1485
1486 Drawable icon = null;
1487 boolean filtered = false;
1488 Intent.ShortcutIconResource iconResource = null;
1489
1490 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1491 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1492 try {
1493 iconResource = (Intent.ShortcutIconResource) extra;
1494 final PackageManager packageManager = context.getPackageManager();
1495 Resources resources = packageManager.getResourcesForApplication(
1496 iconResource.packageName);
1497 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1498 icon = resources.getDrawable(id);
1499 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001500 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 }
1502 }
1503
1504 if (icon == null) {
1505 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1506 }
1507
1508 final LiveFolderInfo info = new LiveFolderInfo();
1509 info.icon = icon;
1510 info.filtered = filtered;
1511 info.title = name;
1512 info.iconResource = iconResource;
1513 info.uri = data.getData();
1514 info.baseIntent = baseIntent;
1515 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1516 LiveFolders.DISPLAY_MODE_GRID);
1517
1518 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1519 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1520 sModel.addFolder(info);
1521
1522 return info;
1523 }
1524
1525 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1526 final int[] xy = new int[2];
1527 if (findSlot(cellInfo, xy, 1, 1)) {
1528 cellInfo.cellX = xy[0];
1529 cellInfo.cellY = xy[1];
1530 return true;
1531 }
1532 return false;
1533 }
1534
1535 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1536 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1537 boolean[] occupied = mSavedState != null ?
1538 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1539 cellInfo = mWorkspace.findAllVacantCells(occupied);
1540 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1541 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1542 return false;
1543 }
1544 }
1545 return true;
1546 }
1547
1548 private void showNotifications() {
1549 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1550 if (statusBar != null) {
1551 statusBar.expand();
1552 }
1553 }
1554
1555 private void startWallpaper() {
1556 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1557 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1558 }
1559
1560 /**
1561 * Registers various intent receivers. The current implementation registers
1562 * only a wallpaper intent receiver to let other applications change the
1563 * wallpaper.
1564 */
1565 private void registerIntentReceivers() {
1566 if (sWallpaperReceiver == null) {
1567 final Application application = getApplication();
1568
1569 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1570
1571 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1572 application.registerReceiver(sWallpaperReceiver, filter);
1573 } else {
1574 sWallpaperReceiver.setLauncher(this);
1575 }
1576
1577 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1578 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1579 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1580 filter.addDataScheme("package");
1581 registerReceiver(mApplicationsReceiver, filter);
1582 }
1583
1584 /**
1585 * Registers various content observers. The current implementation registers
1586 * only a favorites observer to keep track of the favorites applications.
1587 */
1588 private void registerContentObservers() {
1589 ContentResolver resolver = getContentResolver();
1590 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1591 }
1592
1593 @Override
1594 public boolean dispatchKeyEvent(KeyEvent event) {
1595 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1596 switch (event.getKeyCode()) {
1597 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001598 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001599 if (mDrawer.isOpened()) {
1600 closeDrawer();
1601 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001602 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 return true;
1605 case KeyEvent.KEYCODE_HOME:
1606 return true;
1607 }
1608 }
1609
1610 return super.dispatchKeyEvent(event);
1611 }
1612
1613 private void closeDrawer() {
1614 closeDrawer(true);
1615 }
1616
1617 private void closeDrawer(boolean animated) {
1618 if (mDrawer.isOpened()) {
1619 if (animated) {
1620 mDrawer.animateClose();
1621 } else {
1622 mDrawer.close();
1623 }
1624 if (mDrawer.hasFocus()) {
1625 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1626 }
1627 }
1628 }
1629
1630 private void closeFolder() {
1631 Folder folder = mWorkspace.getOpenFolder();
1632 if (folder != null) {
1633 closeFolder(folder);
1634 }
1635 }
1636
1637 void closeFolder(Folder folder) {
1638 folder.getInfo().opened = false;
1639 ViewGroup parent = (ViewGroup) folder.getParent();
1640 if (parent != null) {
1641 parent.removeView(folder);
1642 }
1643 folder.onClose();
1644 }
1645
1646 /**
1647 * When the notification that favorites have changed is received, requests
1648 * a favorites list refresh.
1649 */
1650 private void onFavoritesChanged() {
1651 mDesktopLocked = true;
1652 mDrawer.lock();
1653 sModel.loadUserItems(false, this, false, false);
1654 }
1655
1656 void onDesktopItemsLoaded() {
1657 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 bindDesktopItems();
1659 }
Romain Guycbb89e42009-06-08 15:52:54 -07001660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 /**
1662 * Refreshes the shortcuts shown on the workspace.
1663 */
1664 private void bindDesktopItems() {
1665 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001666 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001667 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1668 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 return;
1670 }
1671
1672 final Workspace workspace = mWorkspace;
1673 int count = workspace.getChildCount();
1674 for (int i = 0; i < count; i++) {
1675 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1676 }
Romain Guycbb89e42009-06-08 15:52:54 -07001677
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 if (DEBUG_USER_INTERFACE) {
1679 android.widget.Button finishButton = new android.widget.Button(this);
1680 finishButton.setText("Finish");
1681 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1682
1683 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1684 public void onClick(View v) {
1685 finish();
1686 }
1687 });
1688 }
Romain Guycbb89e42009-06-08 15:52:54 -07001689
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001690 // Flag any old binder to terminate early
1691 if (mBinder != null) {
1692 mBinder.mTerminate = true;
1693 }
Romain Guycbb89e42009-06-08 15:52:54 -07001694
Karl Rosaen138a0412009-04-23 19:00:21 -07001695 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001696 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 }
1698
1699 private void bindItems(Launcher.DesktopBinder binder,
1700 ArrayList<ItemInfo> shortcuts, int start, int count) {
1701
1702 final Workspace workspace = mWorkspace;
1703 final boolean desktopLocked = mDesktopLocked;
1704
1705 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1706 int i = start;
1707
1708 for ( ; i < end; i++) {
1709 final ItemInfo item = shortcuts.get(i);
1710 switch (item.itemType) {
1711 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1712 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1713 final View shortcut = createShortcut((ApplicationInfo) item);
1714 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1715 !desktopLocked);
1716 break;
1717 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1718 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1719 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1720 (UserFolderInfo) item);
1721 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1722 !desktopLocked);
1723 break;
1724 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1725 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1726 R.layout.live_folder_icon, this,
1727 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1728 (LiveFolderInfo) item);
1729 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1730 !desktopLocked);
1731 break;
1732 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1733 final int screen = workspace.getCurrentScreen();
1734 final View view = mInflater.inflate(R.layout.widget_search,
1735 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001736
Karl Rosaen138a0412009-04-23 19:00:21 -07001737 Search search = (Search) view.findViewById(R.id.widget_search);
1738 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001739
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 final Widget widget = (Widget) item;
1741 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001742
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 workspace.addWidget(view, widget, !desktopLocked);
1744 break;
1745 }
1746 }
1747
1748 workspace.requestLayout();
1749
1750 if (end >= count) {
1751 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001752 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 } else {
1754 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1755 }
1756 }
1757
1758 private void finishBindDesktopItems() {
1759 if (mSavedState != null) {
1760 if (!mWorkspace.hasFocus()) {
1761 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1762 }
1763
1764 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1765 if (userFolders != null) {
1766 for (long folderId : userFolders) {
1767 final FolderInfo info = sModel.findFolderById(folderId);
1768 if (info != null) {
1769 openFolder(info);
1770 }
1771 }
1772 final Folder openFolder = mWorkspace.getOpenFolder();
1773 if (openFolder != null) {
1774 openFolder.requestFocus();
1775 }
1776 }
1777
1778 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1779 if (allApps) {
1780 mDrawer.open();
1781 }
1782
1783 mSavedState = null;
1784 }
1785
1786 if (mSavedInstanceState != null) {
1787 super.onRestoreInstanceState(mSavedInstanceState);
1788 mSavedInstanceState = null;
1789 }
1790
1791 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1792 mDrawer.requestFocus();
1793 }
1794
1795 mDesktopLocked = false;
1796 mDrawer.unlock();
1797 }
Romain Guycbb89e42009-06-08 15:52:54 -07001798
Karl Rosaen138a0412009-04-23 19:00:21 -07001799 private void bindDrawer(Launcher.DesktopBinder binder,
1800 ApplicationsAdapter drawerAdapter) {
1801 mAllAppsGrid.setAdapter(drawerAdapter);
1802 binder.startBindingAppWidgetsWhenIdle();
1803 }
Romain Guycbb89e42009-06-08 15:52:54 -07001804
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001805 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001806 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001807
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 final Workspace workspace = mWorkspace;
1809 final boolean desktopLocked = mDesktopLocked;
1810
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001811 if (!appWidgets.isEmpty()) {
1812 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001813
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001814 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001815 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001816 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001817
Karl Rosaen138a0412009-04-23 19:00:21 -07001818 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001819
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001820 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1821 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001822
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001823 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1824 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001825
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001826 workspace.requestLayout();
1827 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001829 if (appWidgets.isEmpty()) {
1830 if (PROFILE_ROTATE) {
1831 android.os.Debug.stopMethodTracing();
1832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001834 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 }
1836 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001837
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 DragController getDragController() {
1839 return mDragLayer;
1840 }
1841
1842 /**
1843 * Launches the intent referred by the clicked shortcut.
1844 *
1845 * @param v The view representing the clicked shortcut.
1846 */
1847 public void onClick(View v) {
1848 Object tag = v.getTag();
1849 if (tag instanceof ApplicationInfo) {
1850 // Open shortcut
1851 final Intent intent = ((ApplicationInfo) tag).intent;
1852 startActivitySafely(intent);
1853 } else if (tag instanceof FolderInfo) {
1854 handleFolderClick((FolderInfo) tag);
1855 }
1856 }
1857
1858 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001859 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1861 try {
1862 startActivity(intent);
1863 } catch (ActivityNotFoundException e) {
1864 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1865 } catch (SecurityException e) {
1866 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001867 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1869 "or use the exported attribute for this activity.", e);
1870 }
1871 }
1872
1873 private void handleFolderClick(FolderInfo folderInfo) {
1874 if (!folderInfo.opened) {
1875 // Close any open folder
1876 closeFolder();
1877 // Open the requested folder
1878 openFolder(folderInfo);
1879 } else {
1880 // Find the open folder...
1881 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1882 int folderScreen;
1883 if (openFolder != null) {
1884 folderScreen = mWorkspace.getScreenForView(openFolder);
1885 // .. and close it
1886 closeFolder(openFolder);
1887 if (folderScreen != mWorkspace.getCurrentScreen()) {
1888 // Close any folder open on the current screen
1889 closeFolder();
1890 // Pull the folder onto this screen
1891 openFolder(folderInfo);
1892 }
1893 }
1894 }
1895 }
1896
1897 private void loadWallpaper() {
1898 // The first time the application is started, we load the wallpaper from
1899 // the ApplicationContext
1900 if (sWallpaper == null) {
1901 final Drawable drawable = getWallpaper();
1902 if (drawable instanceof BitmapDrawable) {
1903 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1904 } else {
1905 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1906 }
1907 }
1908 mWorkspace.loadWallpaper(sWallpaper);
1909 }
1910
1911 /**
1912 * Opens the user fodler described by the specified tag. The opening of the folder
1913 * is animated relative to the specified View. If the View is null, no animation
1914 * is played.
1915 *
1916 * @param folderInfo The FolderInfo describing the folder to open.
1917 */
1918 private void openFolder(FolderInfo folderInfo) {
1919 Folder openFolder;
1920
1921 if (folderInfo instanceof UserFolderInfo) {
1922 openFolder = UserFolder.fromXml(this);
1923 } else if (folderInfo instanceof LiveFolderInfo) {
1924 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1925 } else {
1926 return;
1927 }
1928
1929 openFolder.setDragger(mDragLayer);
1930 openFolder.setLauncher(this);
1931
1932 openFolder.bind(folderInfo);
1933 folderInfo.opened = true;
1934
1935 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1936 openFolder.onOpen();
1937 }
1938
1939 /**
1940 * Returns true if the workspace is being loaded. When the workspace is loading,
1941 * no user interaction should be allowed to avoid any conflict.
1942 *
1943 * @return True if the workspace is locked, false otherwise.
1944 */
1945 boolean isWorkspaceLocked() {
1946 return mDesktopLocked;
1947 }
1948
1949 public boolean onLongClick(View v) {
1950 if (mDesktopLocked) {
1951 return false;
1952 }
1953
1954 if (!(v instanceof CellLayout)) {
1955 v = (View) v.getParent();
1956 }
1957
1958 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1959
1960 // This happens when long clicking an item with the dpad/trackball
1961 if (cellInfo == null) {
1962 return true;
1963 }
1964
1965 if (mWorkspace.allowLongPress()) {
1966 if (cellInfo.cell == null) {
1967 if (cellInfo.valid) {
1968 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001969 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 showAddDialog(cellInfo);
1971 }
1972 } else {
1973 if (!(cellInfo.cell instanceof Folder)) {
1974 // User long pressed on an item
1975 mWorkspace.startDrag(cellInfo);
1976 }
1977 }
1978 }
1979 return true;
1980 }
1981
1982 static LauncherModel getModel() {
1983 return sModel;
1984 }
1985
Romain Guy73b979d2009-06-09 12:57:21 -07001986 static GestureLibrary getGestureLibrary() {
1987 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