blob: d47b45d22cb38e811b261fd9c575dd9d973bead3 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Application;
22import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070023import android.app.ISearchManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070024import android.app.IWallpaperService;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.app.SearchManager;
26import android.app.StatusBarManager;
27import android.content.ActivityNotFoundException;
28import android.content.BroadcastReceiver;
29import android.content.ComponentName;
30import android.content.ContentResolver;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
Romain Guy1dd3a072009-07-16 13:21:01 -070035import android.content.SharedPreferences;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070036import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.pm.ActivityInfo;
38import android.content.pm.PackageManager;
39import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070041import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.graphics.Bitmap;
44import android.graphics.Rect;
Romain Guy73b979d2009-06-09 12:57:21 -070045import android.graphics.PorterDuffXfermode;
46import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.graphics.drawable.BitmapDrawable;
48import android.graphics.drawable.Drawable;
49import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Bundle;
51import android.os.Handler;
52import android.os.IBinder;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070053import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070054import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070055import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.os.Parcelable;
57import android.os.RemoteException;
58import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070059import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.text.Selection;
61import android.text.SpannableStringBuilder;
62import android.text.TextUtils;
63import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070064import static android.util.Log.*;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.view.KeyEvent;
67import android.view.LayoutInflater;
68import android.view.Menu;
69import android.view.MenuItem;
70import android.view.View;
71import android.view.ViewGroup;
Romain Guy73b979d2009-06-09 12:57:21 -070072import android.view.MotionEvent;
73import android.view.Gravity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.View.OnLongClickListener;
75import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.widget.EditText;
Karl Rosaen138a0412009-04-23 19:00:21 -070077import android.widget.GridView;
Karl Rosaen138a0412009-04-23 19:00:21 -070078import android.widget.SlidingDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.widget.TextView;
80import android.widget.Toast;
Romain Guy73b979d2009-06-09 12:57:21 -070081import android.widget.ImageView;
82import android.widget.PopupWindow;
Romain Guy1dd3a072009-07-16 13:21:01 -070083import android.widget.Button;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070084import android.appwidget.AppWidgetManager;
85import android.appwidget.AppWidgetProviderInfo;
Romain Guy73b979d2009-06-09 12:57:21 -070086import android.gesture.GestureOverlayView;
87import android.gesture.GestureLibraries;
88import android.gesture.GestureLibrary;
89import android.gesture.Gesture;
90import android.gesture.Prediction;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
92import java.lang.ref.WeakReference;
93import java.util.ArrayList;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070094import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070095import java.io.DataOutputStream;
96import java.io.FileNotFoundException;
97import java.io.IOException;
98import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099
100/**
101 * Default launcher application.
102 */
103public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener {
104 static final String LOG_TAG = "Launcher";
105 static final boolean LOGD = false;
106
107 private static final boolean PROFILE_STARTUP = false;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700108 private static final boolean PROFILE_DRAWER = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700109 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 private static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700111 private static final boolean DEBUG_GESTURES = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Romain Guy6fefcf12009-06-11 13:07:43 -0700113 private static final boolean CONFIG_GESTURES_IMMEDIATE_MODE = true;
114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 private static final int WALLPAPER_SCREENS_SPAN = 2;
116
117 private static final int MENU_GROUP_ADD = 1;
118 private static final int MENU_ADD = Menu.FIRST + 1;
119 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
120 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
121 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy73b979d2009-06-09 12:57:21 -0700122 private static final int MENU_GESTURES = MENU_NOTIFICATIONS + 1;
123 private static final int MENU_SETTINGS = MENU_GESTURES + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
125 private static final int REQUEST_CREATE_SHORTCUT = 1;
126 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700127 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 private static final int REQUEST_PICK_APPLICATION = 6;
129 private static final int REQUEST_PICK_SHORTCUT = 7;
130 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_PICK_APPWIDGET = 9;
Romain Guy73b979d2009-06-09 12:57:21 -0700132 private static final int REQUEST_PICK_GESTURE_ACTION = 10;
133 private static final int REQUEST_CREATE_GESTURE_ACTION = 11;
134 private static final int REQUEST_CREATE_GESTURE_APPLICATION_ACTION = 12;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
137
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700138 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
139 static final String SEARCH_WIDGET = "search_widget";
140
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 static final int SCREEN_COUNT = 3;
142 static final int DEFAULT_SCREN = 1;
143 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700144 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
146 private static final int DIALOG_CREATE_SHORTCUT = 1;
Romain Guycbb89e42009-06-08 15:52:54 -0700147 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Romain Guy98d01652009-06-30 16:21:04 -0700149 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
151 // Type: int
152 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
153 // Type: boolean
154 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
155 // Type: long
156 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
171 // Type: int[]
172 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
173 // Type: boolean
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
175 // Type: long
176 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Romain Guy73b979d2009-06-09 12:57:21 -0700177 // Type: Gesture (Parcelable)
178 private static final String RUNTIME_STATE_PENDING_GESTURE = "launcher.gesture";
Romain Guy3cf604f2009-06-16 13:12:53 -0700179 // Type: boolean
180 private static final String RUNTIME_STATE_GESTURES_PANEL = "launcher.gesture_panel_showing";
181 // Type: Gesture (Parcelable)
182 private static final String RUNTIME_STATE_GESTURES_PANEL_GESTURE = "launcher.gesture_panel_gesture";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700184 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
186 private static Bitmap sWallpaper;
187
188 private static final Object sLock = new Object();
189 private static int sScreen = DEFAULT_SCREN;
190
191 private static WallpaperIntentReceiver sWallpaperReceiver;
192
Romain Guy73b979d2009-06-09 12:57:21 -0700193 private static GestureLibrary sLibrary;
194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
196 private final ContentObserver mObserver = new FavoritesChangeObserver();
197
198 private LayoutInflater mInflater;
199
200 private DragLayer mDragLayer;
201 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private AppWidgetManager mAppWidgetManager;
204 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700206 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private CellLayout.CellInfo mAddItemCellInfo;
209 private CellLayout.CellInfo mMenuAddInfo;
210 private final int[] mCellCoordinates = new int[2];
211 private FolderInfo mFolderInfo;
212
213 private SlidingDrawer mDrawer;
214 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private HandleView mHandleView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private AllAppsGridView mAllAppsGrid;
217
218 private boolean mDesktopLocked = true;
219 private Bundle mSavedState;
220
221 private SpannableStringBuilder mDefaultKeySsb = null;
222
223 private boolean mDestroyed;
Mike LeBeau736cf282009-07-02 17:46:59 -0700224
225 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private boolean mRestoring;
228 private boolean mWaitingForResult;
229 private boolean mLocaleChanged;
230
231 private Bundle mSavedInstanceState;
232
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700233 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Romain Guy73b979d2009-06-09 12:57:21 -0700235 private View mGesturesPanel;
236 private GestureOverlayView mGesturesOverlay;
Romain Guy73b979d2009-06-09 12:57:21 -0700237 private ImageView mGesturesAdd;
238 private PopupWindow mGesturesWindow;
239 private Launcher.GesturesProcessor mGesturesProcessor;
240 private Gesture mCurrentGesture;
241 private GesturesAction mGesturesAction;
Romain Guyaad5ef42009-06-10 02:48:37 -0700242 private boolean mHideGesturesPanel;
Romain Guy1dd3a072009-07-16 13:21:01 -0700243 private TextView mGesturesPrompt;
244 private Button mGesturesSend;
Romain Guy73b979d2009-06-09 12:57:21 -0700245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 @Override
247 protected void onCreate(Bundle savedInstanceState) {
248 super.onCreate(savedInstanceState);
249 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700250
Romain Guy73b979d2009-06-09 12:57:21 -0700251 if (sLibrary == null) {
252 // The context is not kept by the library so it's safe to do this
253 sLibrary = GestureLibraries.fromPrivateFile(Launcher.this,
254 GesturesConstants.STORE_NAME);
255 }
256
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700257 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700258
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700259 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
260 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 if (PROFILE_STARTUP) {
263 android.os.Debug.startMethodTracing("/sdcard/launcher");
264 }
265
266 checkForLocaleChange();
267 setWallpaperDimension();
268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 setContentView(R.layout.launcher);
270 setupViews();
271
272 registerIntentReceivers();
273 registerContentObservers();
274
275 mSavedState = savedInstanceState;
276 restoreState(mSavedState);
277
278 if (PROFILE_STARTUP) {
279 android.os.Debug.stopMethodTracing();
280 }
281
282 if (!mRestoring) {
283 startLoaders();
284 }
285
286 // For handling default keys
287 mDefaultKeySsb = new SpannableStringBuilder();
288 Selection.setSelection(mDefaultKeySsb, 0);
289 }
Romain Guycbb89e42009-06-08 15:52:54 -0700290
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700292 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
293 readConfiguration(this, localeConfiguration);
294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 final Configuration configuration = getResources().getConfiguration();
296
Romain Guy98d01652009-06-30 16:21:04 -0700297 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 final String locale = configuration.locale.toString();
299
Romain Guy98d01652009-06-30 16:21:04 -0700300 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 final int mcc = configuration.mcc;
302
Romain Guy98d01652009-06-30 16:21:04 -0700303 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 final int mnc = configuration.mnc;
305
306 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
307
308 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700309 localeConfiguration.locale = locale;
310 localeConfiguration.mcc = mcc;
311 localeConfiguration.mnc = mnc;
312
313 writeConfiguration(this, localeConfiguration);
314 }
315 }
316
317 private static class LocaleConfiguration {
318 public String locale;
319 public int mcc = -1;
320 public int mnc = -1;
321 }
322
323 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
324 DataInputStream in = null;
325 try {
326 in = new DataInputStream(context.openFileInput(PREFERENCES));
327 configuration.locale = in.readUTF();
328 configuration.mcc = in.readInt();
329 configuration.mnc = in.readInt();
330 } catch (FileNotFoundException e) {
331 // Ignore
332 } catch (IOException e) {
333 // Ignore
334 } finally {
335 if (in != null) {
336 try {
337 in.close();
338 } catch (IOException e) {
339 // Ignore
340 }
341 }
342 }
343 }
344
345 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
346 DataOutputStream out = null;
347 try {
348 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
349 out.writeUTF(configuration.locale);
350 out.writeInt(configuration.mcc);
351 out.writeInt(configuration.mnc);
352 out.flush();
353 } catch (FileNotFoundException e) {
354 // Ignore
355 } catch (IOException e) {
356 //noinspection ResultOfMethodCallIgnored
357 context.getFileStreamPath(PREFERENCES).delete();
358 } finally {
359 if (out != null) {
360 try {
361 out.close();
362 } catch (IOException e) {
363 // Ignore
364 }
365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 }
367 }
368
369 static int getScreen() {
370 synchronized (sLock) {
371 return sScreen;
372 }
373 }
374
375 static void setScreen(int screen) {
376 synchronized (sLock) {
377 sScreen = screen;
378 }
379 }
380
381 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700382 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
383 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
384
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 mRestoring = false;
386 }
387
388 private void setWallpaperDimension() {
389 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
390 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
391
392 Display display = getWindowManager().getDefaultDisplay();
393 boolean isPortrait = display.getWidth() < display.getHeight();
394
395 final int width = isPortrait ? display.getWidth() : display.getHeight();
396 final int height = isPortrait ? display.getHeight() : display.getWidth();
397 try {
398 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
399 } catch (RemoteException e) {
400 // System is dead!
401 }
402 }
403
404 @Override
405 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700406 mWaitingForResult = false;
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 // The pattern used here is that a user PICKs a specific application,
409 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700410
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
412 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700413
Romain Guy73b979d2009-06-09 12:57:21 -0700414 if (resultCode == RESULT_OK && (mAddItemCellInfo != null ||
415 ((requestCode == REQUEST_PICK_GESTURE_ACTION ||
416 requestCode == REQUEST_CREATE_GESTURE_ACTION ||
417 requestCode == REQUEST_CREATE_GESTURE_APPLICATION_ACTION) && mCurrentGesture != null))) {
418
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 switch (requestCode) {
420 case REQUEST_PICK_APPLICATION:
421 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
422 break;
423 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700424 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 break;
426 case REQUEST_CREATE_SHORTCUT:
427 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
428 break;
429 case REQUEST_PICK_LIVE_FOLDER:
430 addLiveFolder(data);
431 break;
432 case REQUEST_CREATE_LIVE_FOLDER:
433 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
434 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435 case REQUEST_PICK_APPWIDGET:
436 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700438 case REQUEST_CREATE_APPWIDGET:
439 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 break;
Romain Guy73b979d2009-06-09 12:57:21 -0700441 case REQUEST_PICK_GESTURE_ACTION:
442 processShortcut(data, REQUEST_CREATE_GESTURE_APPLICATION_ACTION,
443 REQUEST_CREATE_GESTURE_ACTION);
444 break;
445 case REQUEST_CREATE_GESTURE_ACTION:
446 completeCreateGesture(data, true);
447 break;
448 case REQUEST_CREATE_GESTURE_APPLICATION_ACTION:
449 completeCreateGesture(data, false);
450 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700452 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700454 // Clean up the appWidgetId if we canceled
455 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
456 if (appWidgetId != -1) {
457 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461
462 @Override
463 protected void onResume() {
464 super.onResume();
465
466 if (mRestoring) {
467 startLoaders();
468 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700469
470 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
471 // onNewIntent), then close the search dialog if needed, because it probably
472 // came from the user pressing 'home' (rather than, for example, pressing 'back').
473 if (mIsNewIntent) {
474 // Post to a handler so that this happens after the search dialog tries to open
475 // itself again.
476 mWorkspace.post(new Runnable() {
477 public void run() {
478 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
479 ServiceManager.getService(Context.SEARCH_SERVICE));
480 try {
481 searchManagerService.stopSearch();
482 } catch (RemoteException e) {
483 e(LOG_TAG, "error stopping search", e);
484 }
485 }
486 });
487 }
488
489 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 }
491
492 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700493 protected void onPause() {
494 super.onPause();
Romain Guy73b979d2009-06-09 12:57:21 -0700495 if (mGesturesWindow != null) {
496 mGesturesWindow.setAnimationStyle(0);
497 mGesturesWindow.update();
498 }
Romain Guycbb89e42009-06-08 15:52:54 -0700499 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700500 }
Romain Guycbb89e42009-06-08 15:52:54 -0700501
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700502 @Override
Romain Guy73b979d2009-06-09 12:57:21 -0700503 protected void onStop() {
504 super.onStop();
Romain Guyaad5ef42009-06-10 02:48:37 -0700505 if (mHideGesturesPanel) {
506 mHideGesturesPanel = false;
507 hideGesturesPanel();
508 }
Romain Guy73b979d2009-06-09 12:57:21 -0700509 }
510
511 @Override
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700512 public Object onRetainNonConfigurationInstance() {
513 // Flag any binder to stop early before switching
514 if (mBinder != null) {
515 mBinder.mTerminate = true;
516 }
Romain Guycbb89e42009-06-08 15:52:54 -0700517
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700518 if (PROFILE_ROTATE) {
519 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
520 }
521 return null;
522 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700523
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 private boolean acceptFilter() {
525 final InputMethodManager inputManager = (InputMethodManager)
526 getSystemService(Context.INPUT_METHOD_SERVICE);
527 return !inputManager.isFullscreenMode();
528 }
529
530 @Override
531 public boolean onKeyDown(int keyCode, KeyEvent event) {
532 boolean handled = super.onKeyDown(keyCode, event);
533 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
534 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
535 keyCode, event);
536 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700537 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700538 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700539 // showSearchDialog()
540 // If there are multiple keystrokes before the search dialog takes focus,
541 // onSearchRequested() will be called for every keystroke,
542 // but it is idempotent, so it's fine.
543 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 }
545 }
546
547 return handled;
548 }
549
Karl Rosaen138a0412009-04-23 19:00:21 -0700550 private String getTypedText() {
551 return mDefaultKeySsb.toString();
552 }
553
554 private void clearTypedText() {
555 mDefaultKeySsb.clear();
556 mDefaultKeySsb.clearSpans();
557 Selection.setSelection(mDefaultKeySsb, 0);
558 }
559
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 /**
561 * Restores the previous state, if it exists.
562 *
563 * @param savedState The previous state.
564 */
565 private void restoreState(Bundle savedState) {
566 if (savedState == null) {
567 return;
568 }
569
570 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
571 if (currentScreen > -1) {
572 mWorkspace.setCurrentScreen(currentScreen);
573 }
574
575 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
576 if (addScreen > -1) {
577 mAddItemCellInfo = new CellLayout.CellInfo();
578 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
579 addItemCellInfo.valid = true;
580 addItemCellInfo.screen = addScreen;
581 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
582 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
583 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
584 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
585 addItemCellInfo.findVacantCellsFromOccupied(
586 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
587 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
588 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
589 mRestoring = true;
590 }
591
592 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
593 if (renameFolder) {
594 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
595 mFolderInfo = sModel.getFolderById(this, id);
596 mRestoring = true;
597 }
Romain Guy73b979d2009-06-09 12:57:21 -0700598
Romain Guy1ce1a242009-06-23 17:34:54 -0700599 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_PENDING_GESTURE);
600
Romain Guy3cf604f2009-06-16 13:12:53 -0700601 boolean gesturesShowing = savedState.getBoolean(RUNTIME_STATE_GESTURES_PANEL, false);
602 if (gesturesShowing) {
Romain Guyf38b3d52009-07-01 16:39:46 -0700603 if (mCurrentGesture == null) {
604 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_GESTURES_PANEL_GESTURE);
605 }
606 final Gesture gesture = mCurrentGesture;
Romain Guy3cf604f2009-06-16 13:12:53 -0700607 mWorkspace.post(new Runnable() {
608 public void run() {
609 showGesturesPanel(false);
Romain Guye769bc72009-07-17 14:18:50 -0700610 if (gesture != null) {
611 mGesturesProcessor.matchGesture(gesture);
612 mWorkspace.post(new Runnable() {
613 public void run() {
614 if (gesture != null) {
615 mGesturesOverlay.setGesture(gesture);
616 }
Romain Guy3cf604f2009-06-16 13:12:53 -0700617 }
Romain Guye769bc72009-07-17 14:18:50 -0700618 });
619 }
Romain Guy3cf604f2009-06-16 13:12:53 -0700620 }
621 });
622 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 }
624
625 /**
626 * Finds all the views we need and configure them properly.
627 */
628 private void setupViews() {
629 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
630 final DragLayer dragLayer = mDragLayer;
631
632 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
633 final Workspace workspace = mWorkspace;
634
635 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
636 final SlidingDrawer drawer = mDrawer;
637
638 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
639 final AllAppsGridView grid = mAllAppsGrid;
640
641 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
642
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700643 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
644 mHandleView.setLauncher(this);
645 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 mHandleIcon.setCrossFadeEnabled(true);
647
648 drawer.lock();
649 final DrawerManager drawerManager = new DrawerManager();
650 drawer.setOnDrawerOpenListener(drawerManager);
651 drawer.setOnDrawerCloseListener(drawerManager);
652 drawer.setOnDrawerScrollListener(drawerManager);
653
Karl Rosaen138a0412009-04-23 19:00:21 -0700654 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 grid.setDragger(dragLayer);
656 grid.setLauncher(this);
657
658 workspace.setOnLongClickListener(this);
659 workspace.setDragger(dragLayer);
660 workspace.setLauncher(this);
661 loadWallpaper();
662
663 deleteZone.setLauncher(this);
664 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700665 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666
667 dragLayer.setIgnoredDropTarget(grid);
668 dragLayer.setDragScoller(workspace);
669 dragLayer.setDragListener(deleteZone);
Romain Guy73b979d2009-06-09 12:57:21 -0700670
671 mGesturesPanel = mInflater.inflate(R.layout.gestures, mDragLayer, false);
672 final View gesturesPanel = mGesturesPanel;
673
Romain Guy73b979d2009-06-09 12:57:21 -0700674 mGesturesAction = new GesturesAction();
675
Romain Guy1dd3a072009-07-16 13:21:01 -0700676 mGesturesPrompt = (TextView) gesturesPanel.findViewById(R.id.gestures_prompt);
677 mGesturesSend = (Button) gesturesPanel.findViewById(R.id.gestures_action);
678 mGesturesSend.setOnClickListener(mGesturesAction);
679
Romain Guy73b979d2009-06-09 12:57:21 -0700680 mGesturesAdd = (ImageView) gesturesPanel.findViewById(R.id.gestures_add);
681 final ImageView gesturesAdd = mGesturesAdd;
Romain Guy73b979d2009-06-09 12:57:21 -0700682 gesturesAdd.setOnClickListener(new View.OnClickListener() {
683 public void onClick(View v) {
684 createGesture();
685 }
686 });
687
Romain Guy1dd3a072009-07-16 13:21:01 -0700688 gesturesPanel.findViewById(R.id.gestures_list).setOnClickListener(new View.OnClickListener() {
689 public void onClick(View v) {
690 startActivity(new Intent(Launcher.this, GesturesActivity.class));
691 }
692 });
693
Romain Guy73b979d2009-06-09 12:57:21 -0700694 mGesturesOverlay = (GestureOverlayView) gesturesPanel.findViewById(R.id.gestures_overlay);
695 mGesturesProcessor = new GesturesProcessor();
696
697 final GestureOverlayView overlay = mGesturesOverlay;
Romain Guy73b979d2009-06-09 12:57:21 -0700698 overlay.addOnGestureListener(mGesturesProcessor);
Romain Guyb8734242009-06-10 11:53:57 -0700699 overlay.getGesturePaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
Romain Guy73b979d2009-06-09 12:57:21 -0700700 }
701
702 private void createGesture() {
Romain Guyf280f202009-06-26 10:31:07 -0700703 if (!mWaitingForResult) {
704 mCurrentGesture = mGesturesOverlay.getGesture();
705 mWaitingForResult = true;
706 pickShortcut(REQUEST_PICK_GESTURE_ACTION, R.string.title_select_shortcut);
707 }
Romain Guy73b979d2009-06-09 12:57:21 -0700708 }
709
710 private void completeCreateGesture(Intent data, boolean isShortcut) {
711 ApplicationInfo info;
712
713 if (isShortcut) {
714 info = infoFromShortcutIntent(this, data);
715 } else {
716 info = infoFromApplicationIntent(this, data);
717 }
718
719 boolean success = false;
720 if (info != null) {
721 info.isGesture = true;
722
723 if (LauncherModel.addGestureToDatabase(this, info, false)) {
724 mGesturesProcessor.addGesture(String.valueOf(info.id), mCurrentGesture);
725 mGesturesProcessor.update(info, mCurrentGesture);
726 Toast.makeText(this, getString(R.string.gestures_created, info.title),
727 Toast.LENGTH_SHORT).show();
728 success = true;
729 }
730 }
731
732 if (!success) {
733 Toast.makeText(this, getString(R.string.gestures_failed), Toast.LENGTH_SHORT).show();
734 }
735
736 mCurrentGesture = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 }
738
739 /**
740 * Creates a view representing a shortcut.
741 *
742 * @param info The data structure describing the shortcut.
743 *
744 * @return A View inflated from R.layout.application.
745 */
746 View createShortcut(ApplicationInfo info) {
747 return createShortcut(R.layout.application,
748 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
749 }
750
751 /**
752 * Creates a view representing a shortcut inflated from the specified resource.
753 *
754 * @param layoutResId The id of the XML layout used to create the shortcut.
755 * @param parent The group the shortcut belongs to.
756 * @param info The data structure describing the shortcut.
757 *
758 * @return A View inflated from layoutResId.
759 */
760 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
761 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
762
763 if (!info.filtered) {
764 info.icon = Utilities.createIconThumbnail(info.icon, this);
765 info.filtered = true;
766 }
767
768 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
769 favorite.setText(info.title);
770 favorite.setTag(info);
771 favorite.setOnClickListener(this);
772
773 return favorite;
774 }
775
776 /**
777 * Add an application shortcut to the workspace.
778 *
779 * @param data The intent describing the application.
780 * @param cellInfo The position on screen where to create the shortcut.
781 */
782 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
783 boolean insertAtFirst) {
784 cellInfo.screen = mWorkspace.getCurrentScreen();
785 if (!findSingleSlot(cellInfo)) return;
786
Romain Guy73b979d2009-06-09 12:57:21 -0700787 final ApplicationInfo info = infoFromApplicationIntent(context, data);
788 if (info != null) {
789 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
790 }
791 }
792
793 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 ComponentName component = data.getComponent();
795 PackageManager packageManager = context.getPackageManager();
796 ActivityInfo activityInfo = null;
797 try {
798 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
799 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700800 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 }
Romain Guycbb89e42009-06-08 15:52:54 -0700802
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 if (activityInfo != null) {
804 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700805
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 itemInfo.title = activityInfo.loadLabel(packageManager);
807 if (itemInfo.title == null) {
808 itemInfo.title = activityInfo.name;
809 }
Romain Guycbb89e42009-06-08 15:52:54 -0700810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
812 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
813 itemInfo.icon = activityInfo.loadIcon(packageManager);
814 itemInfo.container = ItemInfo.NO_ID;
815
Romain Guy73b979d2009-06-09 12:57:21 -0700816 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
Romain Guy73b979d2009-06-09 12:57:21 -0700818
819 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 }
Romain Guycbb89e42009-06-08 15:52:54 -0700821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 /**
823 * Add a shortcut to the workspace.
824 *
825 * @param data The intent describing the shortcut.
826 * @param cellInfo The position on screen where to create the shortcut.
827 * @param insertAtFirst
828 */
829 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
830 boolean insertAtFirst) {
831 cellInfo.screen = mWorkspace.getCurrentScreen();
832 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700833
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
835
836 if (!mRestoring) {
837 sModel.addDesktopItem(info);
838
839 final View view = createShortcut(info);
840 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
841 } else if (sModel.isDesktopLoaded()) {
842 sModel.addDesktopItem(info);
843 }
844 }
845
Romain Guycbb89e42009-06-08 15:52:54 -0700846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700848 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700850 * @param data The intent describing the appWidgetId.
851 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700853 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 boolean insertAtFirst) {
855
856 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700857 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700858
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700859 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700860
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700861 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700862
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700863 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700865 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700866
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 // Try finding open space on Launcher screen
868 final int[] xy = mCellCoordinates;
869 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
870
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700871 // Build Launcher-specific widget info and save to database
872 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 launcherInfo.spanX = spans[0];
874 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700875
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 LauncherModel.addItemToDatabase(this, launcherInfo,
877 LauncherSettings.Favorites.CONTAINER_DESKTOP,
878 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
879
880 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700881 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700882
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700884 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700885
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700886 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700888
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
890 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
891 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700892 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 }
894 }
Romain Guycbb89e42009-06-08 15:52:54 -0700895
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700896 public LauncherAppWidgetHost getAppWidgetHost() {
897 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
Romain Guycbb89e42009-06-08 15:52:54 -0700899
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 static ApplicationInfo addShortcut(Context context, Intent data,
901 CellLayout.CellInfo cellInfo, boolean notify) {
902
Romain Guy73b979d2009-06-09 12:57:21 -0700903 final ApplicationInfo info = infoFromShortcutIntent(context, data);
904 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
905 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
906
907 return info;
908 }
909
910 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
912 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
913 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
914
915 Drawable icon = null;
916 boolean filtered = false;
917 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700918 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919
920 if (bitmap != null) {
921 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
922 filtered = true;
923 customIcon = true;
924 } else {
925 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700926 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700928 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 final PackageManager packageManager = context.getPackageManager();
930 Resources resources = packageManager.getResourcesForApplication(
931 iconResource.packageName);
932 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
933 icon = resources.getDrawable(id);
934 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700935 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
937 }
938 }
939
940 if (icon == null) {
941 icon = context.getPackageManager().getDefaultActivityIcon();
942 }
943
944 final ApplicationInfo info = new ApplicationInfo();
945 info.icon = icon;
946 info.filtered = filtered;
947 info.title = name;
948 info.intent = intent;
949 info.customIcon = customIcon;
950 info.iconResource = iconResource;
951
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 return info;
953 }
954
955 @Override
956 protected void onNewIntent(Intent intent) {
957 super.onNewIntent(intent);
958
959 // Close the menu
960 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
961 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700962
963 // Set this flag so that onResume knows to close the search dialog if it's open,
964 // because this was a new intent (thus a press of 'home' or some such) rather than
965 // for example onResume being called when the user pressed the 'back' button.
966 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967
968 try {
969 dismissDialog(DIALOG_CREATE_SHORTCUT);
970 // Unlock the workspace if the dialog was showing
971 mWorkspace.unlock();
972 } catch (Exception e) {
973 // An exception is thrown if the dialog is not visible, which is fine
974 }
975
976 try {
977 dismissDialog(DIALOG_RENAME_FOLDER);
978 // Unlock the workspace if the dialog was showing
979 mWorkspace.unlock();
980 } catch (Exception e) {
981 // An exception is thrown if the dialog is not visible, which is fine
982 }
983
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
985 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700986
Romain Guy1dd3a072009-07-16 13:21:01 -0700987 // TODO: This really should not be done here every time
988 final SharedPreferences preferences =
Romain Guy6b3f51d2009-07-16 17:45:04 -0700989 getSharedPreferences(GesturesConstants.PREFERENCES_NAME, MODE_PRIVATE);
Romain Guy1dd3a072009-07-16 13:21:01 -0700990 final boolean homeKey = preferences.getBoolean(
Romain Guy6b3f51d2009-07-16 17:45:04 -0700991 GesturesConstants.PREFERENCES_HOME_KEY, false);
Romain Guy94406842009-06-17 16:18:58 -0700992
Romain Guy1dd3a072009-07-16 13:21:01 -0700993 if (!homeKey) {
994 if (!mWorkspace.isDefaultScreenShowing()) {
995 mWorkspace.moveToDefaultScreen();
996 }
Romain Guy94406842009-06-17 16:18:58 -0700997
Romain Guy1dd3a072009-07-16 13:21:01 -0700998 if (mGesturesWindow == null || mGesturesWindow.isShowing()) {
999 hideGesturesPanel();
1000 }
1001 } else {
1002 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE
1003 && (mDragLayer.hasWindowFocus() || (mGesturesWindow != null
1004 && mGesturesWindow.isShowing()))) {
1005
1006 SearchManager searchManager =
1007 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1008
1009 if (!searchManager.isVisible()) {
1010 onHomeKeyPressed();
1011 }
Romain Guyb8734242009-06-10 11:53:57 -07001012 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -07001016
1017 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 if (v != null && v.getWindowToken() != null) {
1019 InputMethodManager imm = (InputMethodManager)getSystemService(
1020 INPUT_METHOD_SERVICE);
1021 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1022 }
1023 } else {
1024 closeDrawer(false);
1025 }
1026 }
1027 }
1028
Romain Guy73b979d2009-06-09 12:57:21 -07001029 private void onHomeKeyPressed() {
1030 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
1031 showGesturesPanel();
1032 } else {
1033 hideGesturesPanel();
1034 }
1035 }
1036
1037 private void showGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -07001038 showGesturesPanel(true);
1039 }
1040
1041 private void showGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07001042 resetGesturesPrompt();
1043
Romain Guy1dd3a072009-07-16 13:21:01 -07001044 mGesturesAdd.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07001045
1046 mGesturesOverlay.clear(false);
1047
1048 PopupWindow window;
1049 if (mGesturesWindow == null) {
1050 mGesturesWindow = new PopupWindow(this);
1051 window = mGesturesWindow;
1052 window.setFocusable(true);
1053 window.setTouchable(true);
Romain Guy1dd3a072009-07-16 13:21:01 -07001054 window.setBackgroundDrawable(getResources().getDrawable(
1055 android.R.drawable.screen_background_dark));
Romain Guy73b979d2009-06-09 12:57:21 -07001056 window.setContentView(mGesturesPanel);
1057 } else {
1058 window = mGesturesWindow;
1059 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001060 window.setAnimationStyle(animate ? com.android.internal.R.style.Animation_Toast : 0);
Romain Guy73b979d2009-06-09 12:57:21 -07001061
1062 final int[] xy = new int[2];
1063 final DragLayer dragLayer = mDragLayer;
1064 dragLayer.getLocationOnScreen(xy);
1065
1066 window.setWidth(dragLayer.getWidth());
1067 window.setHeight(dragLayer.getHeight() - 1);
1068 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
1069 }
1070
1071 private void resetGesturesPrompt() {
1072 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07001073 mGesturesPrompt.setText(R.string.gestures_instructions);
1074 mGesturesPrompt.setVisibility(View.VISIBLE);
1075 mGesturesSend.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07001076 }
1077
Romain Guy1dd3a072009-07-16 13:21:01 -07001078 private void setGestureUnknown() {
Romain Guy73b979d2009-06-09 12:57:21 -07001079 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07001080 mGesturesPrompt.setText(R.string.gestures_unknown);
1081 mGesturesPrompt.setVisibility(View.VISIBLE);
1082 mGesturesSend.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07001083 }
1084
Romain Guy1dd3a072009-07-16 13:21:01 -07001085 private void setGestureAction(Drawable icon, CharSequence title) {
1086 mGesturesPrompt.setVisibility(View.GONE);
1087 mGesturesSend.setVisibility(View.VISIBLE);
1088 mGesturesSend.setText(title);
1089 mGesturesSend.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
Romain Guy3cf604f2009-06-16 13:12:53 -07001090 }
1091
Romain Guy73b979d2009-06-09 12:57:21 -07001092 void hideGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -07001093 hideGesturesPanel(true);
1094 }
1095
1096 void hideGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07001097 if (mGesturesWindow != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001098 final PopupWindow popupWindow = mGesturesWindow;
1099 popupWindow.setAnimationStyle(animate ?
Romain Guy1dd3a072009-07-16 13:21:01 -07001100 com.android.internal.R.style.Animation_Toast : 0);
Romain Guy3cf604f2009-06-16 13:12:53 -07001101 popupWindow.update();
1102 popupWindow.dismiss();
Romain Guy73b979d2009-06-09 12:57:21 -07001103 }
1104 }
1105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 @Override
1107 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1108 // Do not call super here
1109 mSavedInstanceState = savedInstanceState;
1110 }
1111
1112 @Override
1113 protected void onSaveInstanceState(Bundle outState) {
1114 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1115
1116 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1117 if (folders.size() > 0) {
1118 final int count = folders.size();
1119 long[] ids = new long[count];
1120 for (int i = 0; i < count; i++) {
1121 final FolderInfo info = folders.get(i).getInfo();
1122 ids[i] = info.id;
1123 }
1124 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1125 } else {
1126 super.onSaveInstanceState(outState);
1127 }
1128
Romain Guy3cf604f2009-06-16 13:12:53 -07001129 final boolean isConfigurationChange = getChangingConfigurations() != 0;
1130
Romain Guy5a941392009-04-28 15:18:25 -07001131 // When the drawer is opened and we are saving the state because of a
1132 // configuration change
Romain Guy3cf604f2009-06-16 13:12:53 -07001133 if (mDrawer.isOpened() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136
1137 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1138 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1139 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1140
1141 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1142 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1143 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1144 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1145 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1146 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1147 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1148 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1149 layout.getOccupiedCells());
1150 }
1151
1152 if (mFolderInfo != null && mWaitingForResult) {
1153 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1154 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1155 }
Romain Guy73b979d2009-06-09 12:57:21 -07001156
1157 if (mCurrentGesture != null && mWaitingForResult) {
1158 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1159 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001160
Romain Guy1ce1a242009-06-23 17:34:54 -07001161 if (mGesturesWindow != null && mGesturesWindow.isShowing()) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001162 outState.putBoolean(RUNTIME_STATE_GESTURES_PANEL, true);
1163
Romain Guy1ce1a242009-06-23 17:34:54 -07001164 if (mCurrentGesture == null || !mWaitingForResult) {
1165 final Gesture gesture = mGesturesOverlay.getGesture();
1166 if (gesture != null) {
1167 outState.putParcelable(RUNTIME_STATE_GESTURES_PANEL_GESTURE, gesture);
1168 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001169 }
1170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 }
1172
1173 @Override
1174 public void onDestroy() {
1175 mDestroyed = true;
1176
1177 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001180 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001182 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 }
1184
1185 TextKeyListener.getInstance().release();
1186
Romain Guy3cf604f2009-06-16 13:12:53 -07001187 hideGesturesPanel(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 mAllAppsGrid.clearTextFilter();
1189 mAllAppsGrid.setAdapter(null);
1190 sModel.unbind();
1191 sModel.abortLoaders();
1192
1193 getContentResolver().unregisterContentObserver(mObserver);
1194 unregisterReceiver(mApplicationsReceiver);
1195 }
1196
1197 @Override
1198 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001199 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 super.startActivityForResult(intent, requestCode);
1201 }
1202
1203 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001204 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001206
1207 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001208
Karl Rosaen138a0412009-04-23 19:00:21 -07001209 // Slide the search widget to the top, if it's on the current screen,
1210 // otherwise show the search dialog immediately.
1211 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1212 if (searchWidget == null) {
1213 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1214 } else {
1215 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1216 // show the currently typed text in the search widget while sliding
1217 searchWidget.setQuery(getTypedText());
1218 }
1219 }
Romain Guycbb89e42009-06-08 15:52:54 -07001220
Karl Rosaen138a0412009-04-23 19:00:21 -07001221 /**
1222 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001223 *
1224 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001225 */
Romain Guycbb89e42009-06-08 15:52:54 -07001226 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001227 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001228
Karl Rosaen138a0412009-04-23 19:00:21 -07001229 if (initialQuery == null) {
1230 // Use any text typed in the launcher as the initial query
1231 initialQuery = getTypedText();
1232 clearTypedText();
1233 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 if (appSearchData == null) {
1235 appSearchData = new Bundle();
1236 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1237 }
Romain Guycbb89e42009-06-08 15:52:54 -07001238
Karl Rosaen138a0412009-04-23 19:00:21 -07001239 final SearchManager searchManager =
1240 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1241
1242 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1243 if (searchWidget != null) {
1244 // This gets called when the user leaves the search dialog to go back to
1245 // the Launcher.
1246 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1247 public void onCancel() {
1248 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001249 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001250 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001251 });
1252 }
Romain Guycbb89e42009-06-08 15:52:54 -07001253
Karl Rosaen138a0412009-04-23 19:00:21 -07001254 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001255 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
1257
Karl Rosaen138a0412009-04-23 19:00:21 -07001258 /**
1259 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001260 */
Romain Guy5a941392009-04-28 15:18:25 -07001261 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001262 // Close search dialog
1263 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001264 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001265 // Restore search widget to its normal position
1266 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1267 if (searchWidget != null) {
1268 searchWidget.stopSearch(false);
1269 }
1270 }
Romain Guycbb89e42009-06-08 15:52:54 -07001271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 @Override
1273 public boolean onCreateOptionsMenu(Menu menu) {
1274 if (mDesktopLocked) return false;
1275
1276 super.onCreateOptionsMenu(menu);
1277 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1278 .setIcon(android.R.drawable.ic_menu_add)
1279 .setAlphabeticShortcut('A');
1280 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1281 .setIcon(android.R.drawable.ic_menu_gallery)
1282 .setAlphabeticShortcut('W');
1283 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1284 .setIcon(android.R.drawable.ic_search_category_default)
1285 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1286 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1287 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1288 .setAlphabeticShortcut('N');
1289
Romain Guy1dd3a072009-07-16 13:21:01 -07001290 // TODO: Remove
Romain Guy73b979d2009-06-09 12:57:21 -07001291 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
Romain Guy1dd3a072009-07-16 13:21:01 -07001292 .setIcon(com.android.internal.R.drawable.ic_menu_compose)
1293 .setAlphabeticShortcut('G');
Romain Guy73b979d2009-06-09 12:57:21 -07001294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001296 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1297 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298
1299 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1300 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1301 .setIntent(settings);
1302
1303 return true;
1304 }
1305
1306 @Override
1307 public boolean onPrepareOptionsMenu(Menu menu) {
1308 super.onPrepareOptionsMenu(menu);
1309
1310 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1311 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1312
1313 return true;
1314 }
1315
1316 @Override
1317 public boolean onOptionsItemSelected(MenuItem item) {
1318 switch (item.getItemId()) {
1319 case MENU_ADD:
1320 addItems();
1321 return true;
1322 case MENU_WALLPAPER_SETTINGS:
1323 startWallpaper();
1324 return true;
1325 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001326 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 return true;
1328 case MENU_NOTIFICATIONS:
1329 showNotifications();
1330 return true;
Romain Guy1dd3a072009-07-16 13:21:01 -07001331 case MENU_GESTURES:
1332 showGesturesPanel();
1333 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335
1336 return super.onOptionsItemSelected(item);
1337 }
Romain Guycbb89e42009-06-08 15:52:54 -07001338
Karl Rosaen138a0412009-04-23 19:00:21 -07001339 /**
1340 * Indicates that we want global search for this activity by setting the globalSearch
1341 * argument for {@link #startSearch} to true.
1342 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001344 @Override
1345 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001346 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001347 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001348 }
1349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 private void addItems() {
1351 showAddDialog(mMenuAddInfo);
1352 }
1353
1354 private void removeShortcutsForPackage(String packageName) {
1355 if (packageName != null && packageName.length() > 0) {
1356 mWorkspace.removeShortcutsForPackage(packageName);
1357 }
1358 }
Romain Guycbb89e42009-06-08 15:52:54 -07001359
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001360 private void updateShortcutsForPackage(String packageName) {
1361 if (packageName != null && packageName.length() > 0) {
1362 mWorkspace.updateShortcutsForPackage(packageName);
1363 }
1364 }
1365
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001366 void addAppWidget(Intent data) {
1367 // TODO: catch bad widget exception when sent
1368 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001370 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1371 if (SEARCH_WIDGET.equals(customWidget)) {
1372 // We don't need this any more, since this isn't a real app widget.
1373 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1374 // add the search widget
1375 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001377 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1378
1379 if (appWidget.configure != null) {
1380 // Launch over to configure widget, if needed
1381 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1382 intent.setComponent(appWidget.configure);
1383 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1384
1385 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1386 } else {
1387 // Otherwise just add it
1388 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 }
1391 }
Romain Guycbb89e42009-06-08 15:52:54 -07001392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 void addSearch() {
1394 final Widget info = Widget.makeSearch();
1395 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 final int[] xy = mCellCoordinates;
1398 final int spanX = info.spanX;
1399 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 sModel.addDesktopItem(info);
1404 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1405 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 final View view = mInflater.inflate(info.layoutResource, null);
1408 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001409 Search search = (Search) view.findViewById(R.id.widget_search);
1410 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1413 }
1414
Romain Guy73b979d2009-06-09 12:57:21 -07001415 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001416 // Handle case where user selected "Applications"
1417 String applicationName = getResources().getString(R.string.group_applications);
1418 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001419
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001420 if (applicationName != null && applicationName.equals(shortcutName)) {
1421 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1422 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001423
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001424 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1425 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001426 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001427 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001428 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 }
1431
1432 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001433 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001434 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001435 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001436
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001437 if (folderName != null && folderName.equals(shortcutName)) {
1438 addFolder(!mDesktopLocked);
1439 } else {
1440 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1441 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 }
1443
1444 void addFolder(boolean insertAtFirst) {
1445 UserFolderInfo folderInfo = new UserFolderInfo();
1446 folderInfo.title = getText(R.string.folder_name);
1447
1448 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1449 cellInfo.screen = mWorkspace.getCurrentScreen();
1450 if (!findSingleSlot(cellInfo)) return;
1451
1452 // Update the model
1453 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1454 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1455 sModel.addDesktopItem(folderInfo);
1456 sModel.addFolder(folderInfo);
1457
1458 // Create the view
1459 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1460 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1461 mWorkspace.addInCurrentScreen(newFolder,
1462 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1463 }
Romain Guycbb89e42009-06-08 15:52:54 -07001464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1466 boolean insertAtFirst) {
1467 cellInfo.screen = mWorkspace.getCurrentScreen();
1468 if (!findSingleSlot(cellInfo)) return;
1469
1470 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1471
1472 if (!mRestoring) {
1473 sModel.addDesktopItem(info);
1474
1475 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1476 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1477 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1478 } else if (sModel.isDesktopLoaded()) {
1479 sModel.addDesktopItem(info);
1480 }
1481 }
1482
1483 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1484 CellLayout.CellInfo cellInfo, boolean notify) {
1485
1486 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1487 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1488
1489 Drawable icon = null;
1490 boolean filtered = false;
1491 Intent.ShortcutIconResource iconResource = null;
1492
1493 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1494 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1495 try {
1496 iconResource = (Intent.ShortcutIconResource) extra;
1497 final PackageManager packageManager = context.getPackageManager();
1498 Resources resources = packageManager.getResourcesForApplication(
1499 iconResource.packageName);
1500 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1501 icon = resources.getDrawable(id);
1502 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001503 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 }
1505 }
1506
1507 if (icon == null) {
1508 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1509 }
1510
1511 final LiveFolderInfo info = new LiveFolderInfo();
1512 info.icon = icon;
1513 info.filtered = filtered;
1514 info.title = name;
1515 info.iconResource = iconResource;
1516 info.uri = data.getData();
1517 info.baseIntent = baseIntent;
1518 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1519 LiveFolders.DISPLAY_MODE_GRID);
1520
1521 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1522 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1523 sModel.addFolder(info);
1524
1525 return info;
1526 }
1527
1528 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1529 final int[] xy = new int[2];
1530 if (findSlot(cellInfo, xy, 1, 1)) {
1531 cellInfo.cellX = xy[0];
1532 cellInfo.cellY = xy[1];
1533 return true;
1534 }
1535 return false;
1536 }
1537
1538 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1539 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1540 boolean[] occupied = mSavedState != null ?
1541 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1542 cellInfo = mWorkspace.findAllVacantCells(occupied);
1543 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1544 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1545 return false;
1546 }
1547 }
1548 return true;
1549 }
1550
1551 private void showNotifications() {
1552 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1553 if (statusBar != null) {
1554 statusBar.expand();
1555 }
1556 }
1557
1558 private void startWallpaper() {
1559 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1560 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1561 }
1562
1563 /**
1564 * Registers various intent receivers. The current implementation registers
1565 * only a wallpaper intent receiver to let other applications change the
1566 * wallpaper.
1567 */
1568 private void registerIntentReceivers() {
1569 if (sWallpaperReceiver == null) {
1570 final Application application = getApplication();
1571
1572 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1573
1574 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1575 application.registerReceiver(sWallpaperReceiver, filter);
1576 } else {
1577 sWallpaperReceiver.setLauncher(this);
1578 }
1579
1580 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1581 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1582 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1583 filter.addDataScheme("package");
1584 registerReceiver(mApplicationsReceiver, filter);
1585 }
1586
1587 /**
1588 * Registers various content observers. The current implementation registers
1589 * only a favorites observer to keep track of the favorites applications.
1590 */
1591 private void registerContentObservers() {
1592 ContentResolver resolver = getContentResolver();
1593 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1594 }
1595
1596 @Override
1597 public boolean dispatchKeyEvent(KeyEvent event) {
1598 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1599 switch (event.getKeyCode()) {
1600 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001601 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001602 if (mDrawer.isOpened()) {
1603 closeDrawer();
1604 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001605 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001606 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 return true;
1608 case KeyEvent.KEYCODE_HOME:
1609 return true;
1610 }
1611 }
1612
1613 return super.dispatchKeyEvent(event);
1614 }
1615
1616 private void closeDrawer() {
1617 closeDrawer(true);
1618 }
1619
1620 private void closeDrawer(boolean animated) {
1621 if (mDrawer.isOpened()) {
1622 if (animated) {
1623 mDrawer.animateClose();
1624 } else {
1625 mDrawer.close();
1626 }
1627 if (mDrawer.hasFocus()) {
1628 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1629 }
1630 }
1631 }
1632
1633 private void closeFolder() {
1634 Folder folder = mWorkspace.getOpenFolder();
1635 if (folder != null) {
1636 closeFolder(folder);
1637 }
1638 }
1639
1640 void closeFolder(Folder folder) {
1641 folder.getInfo().opened = false;
1642 ViewGroup parent = (ViewGroup) folder.getParent();
1643 if (parent != null) {
1644 parent.removeView(folder);
1645 }
1646 folder.onClose();
1647 }
1648
1649 /**
1650 * When the notification that favorites have changed is received, requests
1651 * a favorites list refresh.
1652 */
1653 private void onFavoritesChanged() {
1654 mDesktopLocked = true;
1655 mDrawer.lock();
1656 sModel.loadUserItems(false, this, false, false);
1657 }
1658
1659 void onDesktopItemsLoaded() {
1660 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 bindDesktopItems();
1662 }
Romain Guycbb89e42009-06-08 15:52:54 -07001663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 /**
1665 * Refreshes the shortcuts shown on the workspace.
1666 */
1667 private void bindDesktopItems() {
1668 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001669 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001670 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1671 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 return;
1673 }
1674
1675 final Workspace workspace = mWorkspace;
1676 int count = workspace.getChildCount();
1677 for (int i = 0; i < count; i++) {
1678 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1679 }
Romain Guycbb89e42009-06-08 15:52:54 -07001680
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 if (DEBUG_USER_INTERFACE) {
1682 android.widget.Button finishButton = new android.widget.Button(this);
1683 finishButton.setText("Finish");
1684 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1685
1686 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1687 public void onClick(View v) {
1688 finish();
1689 }
1690 });
1691 }
Romain Guycbb89e42009-06-08 15:52:54 -07001692
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001693 // Flag any old binder to terminate early
1694 if (mBinder != null) {
1695 mBinder.mTerminate = true;
1696 }
Romain Guycbb89e42009-06-08 15:52:54 -07001697
Karl Rosaen138a0412009-04-23 19:00:21 -07001698 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001699 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 }
1701
1702 private void bindItems(Launcher.DesktopBinder binder,
1703 ArrayList<ItemInfo> shortcuts, int start, int count) {
1704
1705 final Workspace workspace = mWorkspace;
1706 final boolean desktopLocked = mDesktopLocked;
1707
1708 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1709 int i = start;
1710
1711 for ( ; i < end; i++) {
1712 final ItemInfo item = shortcuts.get(i);
1713 switch (item.itemType) {
1714 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1715 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1716 final View shortcut = createShortcut((ApplicationInfo) item);
1717 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1718 !desktopLocked);
1719 break;
1720 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1721 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1722 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1723 (UserFolderInfo) item);
1724 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1725 !desktopLocked);
1726 break;
1727 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1728 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1729 R.layout.live_folder_icon, this,
1730 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1731 (LiveFolderInfo) item);
1732 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1733 !desktopLocked);
1734 break;
1735 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1736 final int screen = workspace.getCurrentScreen();
1737 final View view = mInflater.inflate(R.layout.widget_search,
1738 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001739
Karl Rosaen138a0412009-04-23 19:00:21 -07001740 Search search = (Search) view.findViewById(R.id.widget_search);
1741 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001742
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 final Widget widget = (Widget) item;
1744 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001745
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 workspace.addWidget(view, widget, !desktopLocked);
1747 break;
1748 }
1749 }
1750
1751 workspace.requestLayout();
1752
1753 if (end >= count) {
1754 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001755 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 } else {
1757 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1758 }
1759 }
1760
1761 private void finishBindDesktopItems() {
1762 if (mSavedState != null) {
1763 if (!mWorkspace.hasFocus()) {
1764 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1765 }
1766
1767 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1768 if (userFolders != null) {
1769 for (long folderId : userFolders) {
1770 final FolderInfo info = sModel.findFolderById(folderId);
1771 if (info != null) {
1772 openFolder(info);
1773 }
1774 }
1775 final Folder openFolder = mWorkspace.getOpenFolder();
1776 if (openFolder != null) {
1777 openFolder.requestFocus();
1778 }
1779 }
1780
1781 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1782 if (allApps) {
1783 mDrawer.open();
1784 }
1785
1786 mSavedState = null;
1787 }
1788
1789 if (mSavedInstanceState != null) {
1790 super.onRestoreInstanceState(mSavedInstanceState);
1791 mSavedInstanceState = null;
1792 }
1793
1794 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1795 mDrawer.requestFocus();
1796 }
1797
1798 mDesktopLocked = false;
1799 mDrawer.unlock();
1800 }
Romain Guycbb89e42009-06-08 15:52:54 -07001801
Karl Rosaen138a0412009-04-23 19:00:21 -07001802 private void bindDrawer(Launcher.DesktopBinder binder,
1803 ApplicationsAdapter drawerAdapter) {
1804 mAllAppsGrid.setAdapter(drawerAdapter);
1805 binder.startBindingAppWidgetsWhenIdle();
1806 }
Romain Guycbb89e42009-06-08 15:52:54 -07001807
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001808 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001809 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001810
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 final Workspace workspace = mWorkspace;
1812 final boolean desktopLocked = mDesktopLocked;
1813
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001814 if (!appWidgets.isEmpty()) {
1815 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001816
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001817 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001818 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001819 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001820
Karl Rosaen138a0412009-04-23 19:00:21 -07001821 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001822
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001823 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1824 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001825
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001826 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1827 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001828
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001829 workspace.requestLayout();
1830 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001832 if (appWidgets.isEmpty()) {
1833 if (PROFILE_ROTATE) {
1834 android.os.Debug.stopMethodTracing();
1835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001837 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 }
1839 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001840
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 DragController getDragController() {
1842 return mDragLayer;
1843 }
1844
1845 /**
1846 * Launches the intent referred by the clicked shortcut.
1847 *
1848 * @param v The view representing the clicked shortcut.
1849 */
1850 public void onClick(View v) {
1851 Object tag = v.getTag();
1852 if (tag instanceof ApplicationInfo) {
1853 // Open shortcut
1854 final Intent intent = ((ApplicationInfo) tag).intent;
1855 startActivitySafely(intent);
1856 } else if (tag instanceof FolderInfo) {
1857 handleFolderClick((FolderInfo) tag);
1858 }
1859 }
1860
1861 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001862 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1864 try {
1865 startActivity(intent);
1866 } catch (ActivityNotFoundException e) {
1867 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1868 } catch (SecurityException e) {
1869 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001870 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1872 "or use the exported attribute for this activity.", e);
1873 }
1874 }
1875
1876 private void handleFolderClick(FolderInfo folderInfo) {
1877 if (!folderInfo.opened) {
1878 // Close any open folder
1879 closeFolder();
1880 // Open the requested folder
1881 openFolder(folderInfo);
1882 } else {
1883 // Find the open folder...
1884 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1885 int folderScreen;
1886 if (openFolder != null) {
1887 folderScreen = mWorkspace.getScreenForView(openFolder);
1888 // .. and close it
1889 closeFolder(openFolder);
1890 if (folderScreen != mWorkspace.getCurrentScreen()) {
1891 // Close any folder open on the current screen
1892 closeFolder();
1893 // Pull the folder onto this screen
1894 openFolder(folderInfo);
1895 }
1896 }
1897 }
1898 }
1899
1900 private void loadWallpaper() {
1901 // The first time the application is started, we load the wallpaper from
1902 // the ApplicationContext
1903 if (sWallpaper == null) {
1904 final Drawable drawable = getWallpaper();
1905 if (drawable instanceof BitmapDrawable) {
1906 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1907 } else {
1908 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1909 }
1910 }
1911 mWorkspace.loadWallpaper(sWallpaper);
1912 }
1913
1914 /**
1915 * Opens the user fodler described by the specified tag. The opening of the folder
1916 * is animated relative to the specified View. If the View is null, no animation
1917 * is played.
1918 *
1919 * @param folderInfo The FolderInfo describing the folder to open.
1920 */
1921 private void openFolder(FolderInfo folderInfo) {
1922 Folder openFolder;
1923
1924 if (folderInfo instanceof UserFolderInfo) {
1925 openFolder = UserFolder.fromXml(this);
1926 } else if (folderInfo instanceof LiveFolderInfo) {
1927 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1928 } else {
1929 return;
1930 }
1931
1932 openFolder.setDragger(mDragLayer);
1933 openFolder.setLauncher(this);
1934
1935 openFolder.bind(folderInfo);
1936 folderInfo.opened = true;
1937
1938 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1939 openFolder.onOpen();
1940 }
1941
1942 /**
1943 * Returns true if the workspace is being loaded. When the workspace is loading,
1944 * no user interaction should be allowed to avoid any conflict.
1945 *
1946 * @return True if the workspace is locked, false otherwise.
1947 */
1948 boolean isWorkspaceLocked() {
1949 return mDesktopLocked;
1950 }
1951
1952 public boolean onLongClick(View v) {
1953 if (mDesktopLocked) {
1954 return false;
1955 }
1956
1957 if (!(v instanceof CellLayout)) {
1958 v = (View) v.getParent();
1959 }
1960
1961 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1962
1963 // This happens when long clicking an item with the dpad/trackball
1964 if (cellInfo == null) {
1965 return true;
1966 }
1967
1968 if (mWorkspace.allowLongPress()) {
1969 if (cellInfo.cell == null) {
1970 if (cellInfo.valid) {
1971 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001972 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 showAddDialog(cellInfo);
1974 }
1975 } else {
1976 if (!(cellInfo.cell instanceof Folder)) {
1977 // User long pressed on an item
1978 mWorkspace.startDrag(cellInfo);
1979 }
1980 }
1981 }
1982 return true;
1983 }
1984
1985 static LauncherModel getModel() {
1986 return sModel;
1987 }
1988
Romain Guy73b979d2009-06-09 12:57:21 -07001989 static GestureLibrary getGestureLibrary() {
1990 return sLibrary;
1991 }
1992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 void closeAllApplications() {
1994 mDrawer.close();
1995 }
1996
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001997 View getDrawerHandle() {
1998 return mHandleView;
1999 }
2000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 boolean isDrawerDown() {
2002 return !mDrawer.isMoving() && !mDrawer.isOpened();
2003 }
2004
2005 boolean isDrawerUp() {
2006 return mDrawer.isOpened() && !mDrawer.isMoving();
2007 }
2008
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002009 boolean isDrawerMoving() {
2010 return mDrawer.isMoving();
2011 }
2012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 Workspace getWorkspace() {
2014 return mWorkspace;
2015 }
2016
2017 GridView getApplicationsGrid() {
2018 return mAllAppsGrid;
2019 }
2020
2021 @Override
2022 protected Dialog onCreateDialog(int id) {
2023 switch (id) {
2024 case DIALOG_CREATE_SHORTCUT:
2025 return new CreateShortcut().createDialog();
2026 case DIALOG_RENAME_FOLDER:
2027 return new RenameFolder().createDialog();
2028 }
2029
2030 return super.onCreateDialog(id);
2031 }
2032
2033 @Override
2034 protected void onPrepareDialog(int id, Dialog dialog) {
2035 switch (id) {
2036 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 break;
2038 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002039 if (mFolderInfo != null) {
2040 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2041 final CharSequence text = mFolderInfo.title;
2042 input.setText(text);
2043 input.setSelection(0, text.length());
2044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 break;
2046 }
2047 }
2048
2049 void showRenameDialog(FolderInfo info) {
2050 mFolderInfo = info;
2051 mWaitingForResult = true;
2052 showDialog(DIALOG_RENAME_FOLDER);
2053 }
2054
2055 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2056 mAddItemCellInfo = cellInfo;
2057 mWaitingForResult = true;
2058 showDialog(DIALOG_CREATE_SHORTCUT);
2059 }
2060
Romain Guy73b979d2009-06-09 12:57:21 -07002061 private void pickShortcut(int requestCode, int title) {
2062 Bundle bundle = new Bundle();
2063
2064 ArrayList<String> shortcutNames = new ArrayList<String>();
2065 shortcutNames.add(getString(R.string.group_applications));
2066 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2067
2068 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2069 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2070 R.drawable.ic_launcher_application));
2071 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2072
2073 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2074 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
2075 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
2076 pickIntent.putExtras(bundle);
2077
2078 startActivityForResult(pickIntent, requestCode);
2079 }
2080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 private class RenameFolder {
2082 private EditText mInput;
2083
2084 Dialog createDialog() {
2085 mWaitingForResult = true;
2086 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2087 mInput = (EditText) layout.findViewById(R.id.folder_name);
2088
2089 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2090 builder.setIcon(0);
2091 builder.setTitle(getString(R.string.rename_folder_title));
2092 builder.setCancelable(true);
2093 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2094 public void onCancel(DialogInterface dialog) {
2095 cleanup();
2096 }
2097 });
2098 builder.setNegativeButton(getString(R.string.cancel_action),
2099 new Dialog.OnClickListener() {
2100 public void onClick(DialogInterface dialog, int which) {
2101 cleanup();
2102 }
2103 }
2104 );
2105 builder.setPositiveButton(getString(R.string.rename_action),
2106 new Dialog.OnClickListener() {
2107 public void onClick(DialogInterface dialog, int which) {
2108 changeFolderName();
2109 }
2110 }
2111 );
2112 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002113
2114 final AlertDialog dialog = builder.create();
2115 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2116 public void onShow(DialogInterface dialog) {
2117 mWorkspace.lock();
2118 }
2119 });
2120
2121 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 }
2123
2124 private void changeFolderName() {
2125 final String name = mInput.getText().toString();
2126 if (!TextUtils.isEmpty(name)) {
2127 // Make sure we have the right folder info
2128 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
2129 mFolderInfo.title = name;
2130 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2131
2132 if (mDesktopLocked) {
2133 mDrawer.lock();
2134 sModel.loadUserItems(false, Launcher.this, false, false);
2135 } else {
2136 final FolderIcon folderIcon = (FolderIcon)
2137 mWorkspace.getViewForTag(mFolderInfo);
2138 if (folderIcon != null) {
2139 folderIcon.setText(name);
2140 getWorkspace().requestLayout();
2141 } else {
2142 mDesktopLocked = true;
2143 mDrawer.lock();
2144 sModel.loadUserItems(false, Launcher.this, false, false);
2145 }
2146 }
2147 }
2148 cleanup();
2149 }
2150
2151 private void cleanup() {
2152 mWorkspace.unlock();
2153 dismissDialog(DIALOG_RENAME_FOLDER);
2154 mWaitingForResult = false;
2155 mFolderInfo = null;
2156 }
2157 }
2158
2159 /**
2160 * Displays the shortcut creation dialog and launches, if necessary, the
2161 * appropriate activity.
2162 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002163 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002164 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2165 DialogInterface.OnShowListener {
2166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002168
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 Dialog createDialog() {
2170 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2175 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002176 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 builder.setInverseBackgroundForced(true);
2179
2180 AlertDialog dialog = builder.create();
2181 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002182 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002183 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 return dialog;
2186 }
2187
2188 public void onCancel(DialogInterface dialog) {
2189 mWaitingForResult = false;
2190 cleanup();
2191 }
2192
Romain Guycbb89e42009-06-08 15:52:54 -07002193 public void onDismiss(DialogInterface dialog) {
2194 mWorkspace.unlock();
2195 }
2196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 private void cleanup() {
2198 mWorkspace.unlock();
2199 dismissDialog(DIALOG_CREATE_SHORTCUT);
2200 }
2201
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002202 /**
2203 * Handle the action clicked in the "Add to home" dialog.
2204 */
2205 public void onClick(DialogInterface dialog, int which) {
2206 Resources res = getResources();
2207 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002208
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002209 switch (which) {
2210 case AddAdapter.ITEM_SHORTCUT: {
2211 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002212 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002213 break;
2214 }
Romain Guycbb89e42009-06-08 15:52:54 -07002215
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002216 case AddAdapter.ITEM_APPWIDGET: {
2217 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002218
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002219 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2220 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2221 // add the search widget
2222 ArrayList<AppWidgetProviderInfo> customInfo =
2223 new ArrayList<AppWidgetProviderInfo>();
2224 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2225 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2226 info.label = getString(R.string.group_search);
2227 info.icon = R.drawable.ic_search_widget;
2228 customInfo.add(info);
2229 pickIntent.putParcelableArrayListExtra(
2230 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2231 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2232 Bundle b = new Bundle();
2233 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2234 customExtras.add(b);
2235 pickIntent.putParcelableArrayListExtra(
2236 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2237 // start the pick activity
2238 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2239 break;
2240 }
Romain Guycbb89e42009-06-08 15:52:54 -07002241
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002242 case AddAdapter.ITEM_LIVE_FOLDER: {
2243 // Insert extra item to handle inserting folder
2244 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002245
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002246 ArrayList<String> shortcutNames = new ArrayList<String>();
2247 shortcutNames.add(res.getString(R.string.group_folder));
2248 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002249
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002250 ArrayList<ShortcutIconResource> shortcutIcons =
2251 new ArrayList<ShortcutIconResource>();
2252 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2253 R.drawable.ic_launcher_folder));
2254 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2255
2256 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2257 pickIntent.putExtra(Intent.EXTRA_INTENT,
2258 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2259 pickIntent.putExtra(Intent.EXTRA_TITLE,
2260 getText(R.string.title_select_live_folder));
2261 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002262
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002263 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2264 break;
2265 }
2266
2267 case AddAdapter.ITEM_WALLPAPER: {
2268 startWallpaper();
2269 break;
2270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 }
2272 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002273
2274 public void onShow(DialogInterface dialog) {
2275 mWorkspace.lock();
2276 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 }
2278
2279 /**
2280 * Receives notifications when applications are added/removed.
2281 */
2282 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2283 @Override
2284 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002285 final String action = intent.getAction();
2286 final String packageName = intent.getData().getSchemeSpecificPart();
2287 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2288
2289 if (LauncherModel.DEBUG_LOADERS) {
2290 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2291 ", replacing=" + replacing);
2292 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002294
2295 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2296 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2297 if (!replacing) {
2298 removeShortcutsForPackage(packageName);
2299 if (LauncherModel.DEBUG_LOADERS) {
2300 d(LauncherModel.LOG_TAG, " --> remove package");
2301 }
2302 sModel.removePackage(Launcher.this, packageName);
2303 }
2304 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2305 // later, we will update the package at this time
2306 } else {
2307 if (!replacing) {
2308 if (LauncherModel.DEBUG_LOADERS) {
2309 d(LauncherModel.LOG_TAG, " --> add package");
2310 }
2311 sModel.addPackage(Launcher.this, packageName);
2312 } else {
2313 if (LauncherModel.DEBUG_LOADERS) {
2314 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2315 }
2316 sModel.updatePackage(Launcher.this, packageName);
2317 updateShortcutsForPackage(packageName);
2318 }
2319 }
2320 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002322 if (LauncherModel.DEBUG_LOADERS) {
2323 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2324 }
2325 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 }
2327 }
2328 }
2329
2330 /**
2331 * Receives notifications whenever the user favorites have changed.
2332 */
2333 private class FavoritesChangeObserver extends ContentObserver {
2334 public FavoritesChangeObserver() {
2335 super(new Handler());
2336 }
2337
2338 @Override
2339 public void onChange(boolean selfChange) {
2340 onFavoritesChanged();
2341 }
2342 }
2343
2344 /**
2345 * Receives intents from other applications to change the wallpaper.
2346 */
2347 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2348 private final Application mApplication;
2349 private WeakReference<Launcher> mLauncher;
2350
2351 WallpaperIntentReceiver(Application application, Launcher launcher) {
2352 mApplication = application;
2353 setLauncher(launcher);
2354 }
2355
2356 void setLauncher(Launcher launcher) {
2357 mLauncher = new WeakReference<Launcher>(launcher);
2358 }
2359
2360 @Override
2361 public void onReceive(Context context, Intent intent) {
2362 // Load the wallpaper from the ApplicationContext and store it locally
2363 // until the Launcher Activity is ready to use it
2364 final Drawable drawable = mApplication.getWallpaper();
2365 if (drawable instanceof BitmapDrawable) {
2366 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2367 } else {
2368 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2369 }
2370
2371 // If Launcher is alive, notify we have a new wallpaper
2372 if (mLauncher != null) {
2373 final Launcher launcher = mLauncher.get();
2374 if (launcher != null) {
2375 launcher.loadWallpaper();
2376 }
2377 }
2378 }
2379 }
2380
2381 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2382 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2383 private boolean mOpen;
2384
2385 public void onDrawerOpened() {
2386 if (!mOpen) {
2387 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002390 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391
2392 mOpen = true;
2393 }
2394 }
2395
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002396 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2397 view.getDrawingRect(bounds);
2398
2399 while (view != mDragLayer) {
2400 bounds.offset(view.getLeft(), view.getTop());
2401 view = (View) view.getParent();
2402 }
2403 }
2404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 public void onDrawerClosed() {
2406 if (mOpen) {
2407 mHandleIcon.reverseTransition(150);
2408 mWorkspace.mDrawerBounds.setEmpty();
2409 mOpen = false;
2410 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 mAllAppsGrid.setSelection(0);
2413 mAllAppsGrid.clearTextFilter();
2414 }
2415
2416 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002417 if (PROFILE_DRAWER) {
2418 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2419 }
2420
2421 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2422 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 }
2424
2425 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002426 if (PROFILE_DRAWER) {
2427 android.os.Debug.stopMethodTracing();
2428 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 }
2430 }
2431
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002432 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002434 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002435 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002436
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 // Number of items to bind in every pass
2438 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439
2440 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002441 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002442 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002444
Karl Rosaen138a0412009-04-23 19:00:21 -07002445 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002446
2447 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002448 ArrayList<LauncherAppWidgetInfo> appWidgets,
2449 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450
2451 mLauncher = new WeakReference<Launcher>(launcher);
2452 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002453 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002454
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002455 // Sort widgets so active workspace is bound first
2456 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2457 final int size = appWidgets.size();
2458 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002459
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002460 for (int i = 0; i < size; i++) {
2461 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2462 if (appWidgetInfo.screen == currentScreen) {
2463 mAppWidgets.addFirst(appWidgetInfo);
2464 } else {
2465 mAppWidgets.addLast(appWidgetInfo);
2466 }
2467 }
2468 }
Romain Guycbb89e42009-06-08 15:52:54 -07002469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 public void startBindingItems() {
2471 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2472 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002473
2474 public void startBindingDrawer() {
2475 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2476 }
Romain Guycbb89e42009-06-08 15:52:54 -07002477
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002478 public void startBindingAppWidgetsWhenIdle() {
2479 // Ask for notification when message queue becomes idle
2480 final MessageQueue messageQueue = Looper.myQueue();
2481 messageQueue.addIdleHandler(this);
2482 }
Romain Guycbb89e42009-06-08 15:52:54 -07002483
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002484 public boolean queueIdle() {
2485 // Queue is idle, so start binding items
2486 startBindingAppWidgets();
2487 return false;
2488 }
2489
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002490 public void startBindingAppWidgets() {
2491 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2492 }
2493
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 @Override
2495 public void handleMessage(Message msg) {
2496 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002497 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 return;
2499 }
Romain Guycbb89e42009-06-08 15:52:54 -07002500
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002501 switch (msg.what) {
2502 case MESSAGE_BIND_ITEMS: {
2503 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2504 break;
2505 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002506 case MESSAGE_BIND_DRAWER: {
2507 launcher.bindDrawer(this, mDrawerAdapter);
2508 break;
2509 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002510 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002511 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002512 break;
2513 }
2514 }
2515 }
2516 }
Romain Guy73b979d2009-06-09 12:57:21 -07002517
2518 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2519 GestureOverlayView.OnGesturePerformedListener {
2520
2521 private final GestureMatcher mMatcher = new GestureMatcher();
2522
2523 GesturesProcessor() {
2524 // TODO: Maybe the load should happen on a background thread?
2525 sLibrary.load();
2526 }
2527
2528 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002529 //noinspection PointlessBooleanExpression,ConstantConditions
2530 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2531 overlay.removeCallbacks(mMatcher);
Romain Guy1dd3a072009-07-16 13:21:01 -07002532 resetGesturesPrompt();
Romain Guy6fefcf12009-06-11 13:07:43 -07002533 }
Romain Guy73b979d2009-06-09 12:57:21 -07002534
Romain Guy1dd3a072009-07-16 13:21:01 -07002535 mGesturesAdd.setVisibility(View.GONE);
Romain Guy73b979d2009-06-09 12:57:21 -07002536 }
2537
2538 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2539 }
2540
2541 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2542 }
2543
2544 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002545 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2546 mMatcher.gesture = overlay.getGesture();
2547 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2548 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002549 if (mGesturesAction.intent != null) {
2550 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07002551 setGestureAction(null, getString(R.string.gestures_unknown));
Romain Guy1ce1a242009-06-23 17:34:54 -07002552 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002553 } else {
2554 mMatcher.run();
2555 }
Romain Guy73b979d2009-06-09 12:57:21 -07002556 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002557 overlay.removeCallbacks(mMatcher);
2558
2559 mMatcher.gesture = overlay.getGesture();
2560 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2561 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002562 if (mGesturesAction.intent != null) {
2563 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07002564 setGestureAction(null, getString(R.string.gestures_unknown));
Romain Guy1ce1a242009-06-23 17:34:54 -07002565 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002566 } else {
2567 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2568 }
Romain Guy73b979d2009-06-09 12:57:21 -07002569 }
2570 }
2571
Romain Guy3cf604f2009-06-16 13:12:53 -07002572 void matchGesture(Gesture gesture) {
Romain Guy1dd3a072009-07-16 13:21:01 -07002573 mGesturesAdd.setVisibility(View.VISIBLE);
Romain Guy73b979d2009-06-09 12:57:21 -07002574
2575 if (gesture != null) {
2576 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2577
2578 if (DEBUG_GESTURES) {
2579 for (Prediction p : predictions) {
2580 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2581 }
2582 }
2583
2584 boolean match = false;
2585 if (predictions.size() > 0) {
2586 final Prediction prediction = predictions.get(0);
2587 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2588 match = true;
2589
2590 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2591 if (info != null) {
Romain Guy1dd3a072009-07-16 13:21:01 -07002592 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002593 }
2594 }
2595 }
2596
2597 if (!match){
Romain Guy1ce1a242009-06-23 17:34:54 -07002598 mGesturesAction.intent = null;
Romain Guy1dd3a072009-07-16 13:21:01 -07002599 setGestureUnknown();
Romain Guy73b979d2009-06-09 12:57:21 -07002600 }
2601 }
2602 }
2603
2604 private void updatePrompt(ApplicationInfo info) {
Romain Guy1dd3a072009-07-16 13:21:01 -07002605 // TODO: BRING BACK
Romain Guy6fefcf12009-06-11 13:07:43 -07002606 if (mGesturesAction.intent != null &&
Romain Guy1ce1a242009-06-23 17:34:54 -07002607 info.intent.toUri(0).equals(mGesturesAction.intent.toUri(0)) &&
Romain Guy1dd3a072009-07-16 13:21:01 -07002608 info.title.equals(mGesturesSend.getText())) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002609 return;
2610 }
Romain Guy3cf604f2009-06-16 13:12:53 -07002611
Romain Guy1dd3a072009-07-16 13:21:01 -07002612 setGestureAction(info.icon, info.title);
Romain Guy3cf604f2009-06-16 13:12:53 -07002613
Romain Guy73b979d2009-06-09 12:57:21 -07002614 mGesturesAction.intent = info.intent;
2615 }
2616
2617 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002618 //noinspection PointlessBooleanExpression,ConstantConditions
2619 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2620 overlay.removeCallbacks(mMatcher);
2621 }
Romain Guy73b979d2009-06-09 12:57:21 -07002622 }
2623
2624 void addGesture(String name, Gesture gesture) {
2625 sLibrary.addGesture(name, gesture);
2626 // TODO: On a background thread?
2627 sLibrary.save();
2628 }
2629
2630 void update(ApplicationInfo info, Gesture gesture) {
2631 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002632 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002633 }
2634
2635 class GestureMatcher implements Runnable {
2636 Gesture gesture;
2637
2638 public void run() {
2639 if (gesture != null) {
2640 matchGesture(gesture);
2641 }
2642 }
2643 }
2644 }
2645
2646 private class GesturesAction implements View.OnClickListener {
2647 Intent intent;
2648
2649 public void onClick(View v) {
2650 if (intent != null) {
2651 startActivitySafely(intent);
2652 }
2653 }
2654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002655}
Karl Rosaen138a0412009-04-23 19:00:21 -07002656