blob: 000ce148508f13b37e4209ce08be82ae811ba725 [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;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070035import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.pm.ActivityInfo;
37import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070040import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.Bitmap;
43import android.graphics.Rect;
Romain Guy73b979d2009-06-09 12:57:21 -070044import android.graphics.PorterDuffXfermode;
45import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.graphics.drawable.BitmapDrawable;
47import android.graphics.drawable.Drawable;
48import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.os.Bundle;
50import android.os.Handler;
51import android.os.IBinder;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070052import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070053import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070054import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Parcelable;
56import android.os.RemoteException;
57import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070058import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.text.Selection;
60import android.text.SpannableStringBuilder;
61import android.text.TextUtils;
62import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070063import static android.util.Log.*;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.KeyEvent;
66import android.view.LayoutInflater;
67import android.view.Menu;
68import android.view.MenuItem;
69import android.view.View;
70import android.view.ViewGroup;
Romain Guy73b979d2009-06-09 12:57:21 -070071import android.view.MotionEvent;
72import android.view.Gravity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.View.OnLongClickListener;
74import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.widget.EditText;
Karl Rosaen138a0412009-04-23 19:00:21 -070076import android.widget.GridView;
Karl Rosaen138a0412009-04-23 19:00:21 -070077import android.widget.SlidingDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.widget.TextView;
79import android.widget.Toast;
Romain Guy73b979d2009-06-09 12:57:21 -070080import android.widget.ImageView;
81import android.widget.PopupWindow;
82import android.widget.ViewSwitcher;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070083import android.appwidget.AppWidgetManager;
84import android.appwidget.AppWidgetProviderInfo;
Romain Guy73b979d2009-06-09 12:57:21 -070085import android.gesture.GestureOverlayView;
86import android.gesture.GestureLibraries;
87import android.gesture.GestureLibrary;
88import android.gesture.Gesture;
89import android.gesture.Prediction;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
91import java.lang.ref.WeakReference;
92import java.util.ArrayList;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070093import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070094import java.io.DataOutputStream;
95import java.io.FileNotFoundException;
96import java.io.IOException;
97import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098
99/**
100 * Default launcher application.
101 */
102public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener {
103 static final String LOG_TAG = "Launcher";
104 static final boolean LOGD = false;
105
106 private static final boolean PROFILE_STARTUP = false;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700107 private static final boolean PROFILE_DRAWER = false;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700108 private static final boolean PROFILE_ROTATE = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 private static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700110 private static final boolean DEBUG_GESTURES = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Romain Guy6fefcf12009-06-11 13:07:43 -0700112 private static final boolean CONFIG_GESTURES_IMMEDIATE_MODE = true;
113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 private static final int WALLPAPER_SCREENS_SPAN = 2;
115
116 private static final int MENU_GROUP_ADD = 1;
117 private static final int MENU_ADD = Menu.FIRST + 1;
118 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
119 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
120 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy73b979d2009-06-09 12:57:21 -0700121 private static final int MENU_GESTURES = MENU_NOTIFICATIONS + 1;
122 private static final int MENU_SETTINGS = MENU_GESTURES + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
124 private static final int REQUEST_CREATE_SHORTCUT = 1;
125 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700126 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int REQUEST_PICK_APPLICATION = 6;
128 private static final int REQUEST_PICK_SHORTCUT = 7;
129 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700130 private static final int REQUEST_PICK_APPWIDGET = 9;
Romain Guy73b979d2009-06-09 12:57:21 -0700131 private static final int REQUEST_PICK_GESTURE_ACTION = 10;
132 private static final int REQUEST_CREATE_GESTURE_ACTION = 11;
133 private static final int REQUEST_CREATE_GESTURE_APPLICATION_ACTION = 12;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
136
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700137 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
138 static final String SEARCH_WIDGET = "search_widget";
139
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 static final int SCREEN_COUNT = 3;
141 static final int DEFAULT_SCREN = 1;
142 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700143 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
145 private static final int DIALOG_CREATE_SHORTCUT = 1;
Romain Guycbb89e42009-06-08 15:52:54 -0700146 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Romain Guy98d01652009-06-30 16:21:04 -0700148 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
150 // Type: int
151 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
152 // Type: boolean
153 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
154 // Type: long
155 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
156 // Type: int
157 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
162 // Type: int
163 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
164 // Type: int
165 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
166 // Type: int
167 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
168 // Type: int
169 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
170 // Type: int[]
171 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
172 // Type: boolean
173 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
174 // Type: long
175 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Romain Guy73b979d2009-06-09 12:57:21 -0700176 // Type: Gesture (Parcelable)
177 private static final String RUNTIME_STATE_PENDING_GESTURE = "launcher.gesture";
Romain Guy3cf604f2009-06-16 13:12:53 -0700178 // Type: boolean
179 private static final String RUNTIME_STATE_GESTURES_PANEL = "launcher.gesture_panel_showing";
180 // Type: Gesture (Parcelable)
181 private static final String RUNTIME_STATE_GESTURES_PANEL_GESTURE = "launcher.gesture_panel_gesture";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700183 private static final LauncherModel sModel = new LauncherModel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
185 private static Bitmap sWallpaper;
186
187 private static final Object sLock = new Object();
188 private static int sScreen = DEFAULT_SCREN;
189
190 private static WallpaperIntentReceiver sWallpaperReceiver;
191
Romain Guy73b979d2009-06-09 12:57:21 -0700192 private static GestureLibrary sLibrary;
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
195 private final ContentObserver mObserver = new FavoritesChangeObserver();
196
197 private LayoutInflater mInflater;
198
199 private DragLayer mDragLayer;
200 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700201
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700202 private AppWidgetManager mAppWidgetManager;
203 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700204
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700205 static final int APPWIDGET_HOST_ID = 1024;
Romain Guycbb89e42009-06-08 15:52:54 -0700206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private CellLayout.CellInfo mAddItemCellInfo;
208 private CellLayout.CellInfo mMenuAddInfo;
209 private final int[] mCellCoordinates = new int[2];
210 private FolderInfo mFolderInfo;
211
212 private SlidingDrawer mDrawer;
213 private TransitionDrawable mHandleIcon;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 private HandleView mHandleView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 private AllAppsGridView mAllAppsGrid;
216
217 private boolean mDesktopLocked = true;
218 private Bundle mSavedState;
219
220 private SpannableStringBuilder mDefaultKeySsb = null;
221
222 private boolean mDestroyed;
Mike LeBeau736cf282009-07-02 17:46:59 -0700223
224 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225
226 private boolean mRestoring;
227 private boolean mWaitingForResult;
228 private boolean mLocaleChanged;
229
230 private Bundle mSavedInstanceState;
231
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700232 private DesktopBinder mBinder;
Romain Guycbb89e42009-06-08 15:52:54 -0700233
Romain Guy73b979d2009-06-09 12:57:21 -0700234 private View mGesturesPanel;
235 private GestureOverlayView mGesturesOverlay;
236 private ViewSwitcher mGesturesPrompt;
237 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 Guy73b979d2009-06-09 12:57:21 -0700243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 @Override
245 protected void onCreate(Bundle savedInstanceState) {
246 super.onCreate(savedInstanceState);
247 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700248
Romain Guy73b979d2009-06-09 12:57:21 -0700249 if (sLibrary == null) {
250 // The context is not kept by the library so it's safe to do this
251 sLibrary = GestureLibraries.fromPrivateFile(Launcher.this,
252 GesturesConstants.STORE_NAME);
253 }
254
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700255 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700256
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700257 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
258 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 if (PROFILE_STARTUP) {
261 android.os.Debug.startMethodTracing("/sdcard/launcher");
262 }
263
264 checkForLocaleChange();
265 setWallpaperDimension();
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 setContentView(R.layout.launcher);
268 setupViews();
269
270 registerIntentReceivers();
271 registerContentObservers();
272
273 mSavedState = savedInstanceState;
274 restoreState(mSavedState);
275
276 if (PROFILE_STARTUP) {
277 android.os.Debug.stopMethodTracing();
278 }
279
280 if (!mRestoring) {
281 startLoaders();
282 }
283
284 // For handling default keys
285 mDefaultKeySsb = new SpannableStringBuilder();
286 Selection.setSelection(mDefaultKeySsb, 0);
287 }
Romain Guycbb89e42009-06-08 15:52:54 -0700288
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700290 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
291 readConfiguration(this, localeConfiguration);
292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 final Configuration configuration = getResources().getConfiguration();
294
Romain Guy98d01652009-06-30 16:21:04 -0700295 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 final String locale = configuration.locale.toString();
297
Romain Guy98d01652009-06-30 16:21:04 -0700298 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 final int mcc = configuration.mcc;
300
Romain Guy98d01652009-06-30 16:21:04 -0700301 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 final int mnc = configuration.mnc;
303
304 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
305
306 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700307 localeConfiguration.locale = locale;
308 localeConfiguration.mcc = mcc;
309 localeConfiguration.mnc = mnc;
310
311 writeConfiguration(this, localeConfiguration);
312 }
313 }
314
315 private static class LocaleConfiguration {
316 public String locale;
317 public int mcc = -1;
318 public int mnc = -1;
319 }
320
321 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
322 DataInputStream in = null;
323 try {
324 in = new DataInputStream(context.openFileInput(PREFERENCES));
325 configuration.locale = in.readUTF();
326 configuration.mcc = in.readInt();
327 configuration.mnc = in.readInt();
328 } catch (FileNotFoundException e) {
329 // Ignore
330 } catch (IOException e) {
331 // Ignore
332 } finally {
333 if (in != null) {
334 try {
335 in.close();
336 } catch (IOException e) {
337 // Ignore
338 }
339 }
340 }
341 }
342
343 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
344 DataOutputStream out = null;
345 try {
346 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
347 out.writeUTF(configuration.locale);
348 out.writeInt(configuration.mcc);
349 out.writeInt(configuration.mnc);
350 out.flush();
351 } catch (FileNotFoundException e) {
352 // Ignore
353 } catch (IOException e) {
354 //noinspection ResultOfMethodCallIgnored
355 context.getFileStreamPath(PREFERENCES).delete();
356 } finally {
357 if (out != null) {
358 try {
359 out.close();
360 } catch (IOException e) {
361 // Ignore
362 }
363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 }
365 }
366
367 static int getScreen() {
368 synchronized (sLock) {
369 return sScreen;
370 }
371 }
372
373 static void setScreen(int screen) {
374 synchronized (sLock) {
375 sScreen = screen;
376 }
377 }
378
379 private void startLoaders() {
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700380 boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
381 sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 mRestoring = false;
384 }
385
386 private void setWallpaperDimension() {
387 IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE);
388 IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder);
389
390 Display display = getWindowManager().getDefaultDisplay();
391 boolean isPortrait = display.getWidth() < display.getHeight();
392
393 final int width = isPortrait ? display.getWidth() : display.getHeight();
394 final int height = isPortrait ? display.getHeight() : display.getWidth();
395 try {
396 wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height);
397 } catch (RemoteException e) {
398 // System is dead!
399 }
400 }
401
402 @Override
403 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700404 mWaitingForResult = false;
405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 // The pattern used here is that a user PICKs a specific application,
407 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
410 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700411
Romain Guy73b979d2009-06-09 12:57:21 -0700412 if (resultCode == RESULT_OK && (mAddItemCellInfo != null ||
413 ((requestCode == REQUEST_PICK_GESTURE_ACTION ||
414 requestCode == REQUEST_CREATE_GESTURE_ACTION ||
415 requestCode == REQUEST_CREATE_GESTURE_APPLICATION_ACTION) && mCurrentGesture != null))) {
416
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 switch (requestCode) {
418 case REQUEST_PICK_APPLICATION:
419 completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked);
420 break;
421 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700422 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 break;
424 case REQUEST_CREATE_SHORTCUT:
425 completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked);
426 break;
427 case REQUEST_PICK_LIVE_FOLDER:
428 addLiveFolder(data);
429 break;
430 case REQUEST_CREATE_LIVE_FOLDER:
431 completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked);
432 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 case REQUEST_PICK_APPWIDGET:
434 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700436 case REQUEST_CREATE_APPWIDGET:
437 completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 break;
Romain Guy73b979d2009-06-09 12:57:21 -0700439 case REQUEST_PICK_GESTURE_ACTION:
440 processShortcut(data, REQUEST_CREATE_GESTURE_APPLICATION_ACTION,
441 REQUEST_CREATE_GESTURE_ACTION);
442 break;
443 case REQUEST_CREATE_GESTURE_ACTION:
444 completeCreateGesture(data, true);
445 break;
446 case REQUEST_CREATE_GESTURE_APPLICATION_ACTION:
447 completeCreateGesture(data, false);
448 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700450 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700452 // Clean up the appWidgetId if we canceled
453 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
454 if (appWidgetId != -1) {
455 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 }
457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
459
460 @Override
461 protected void onResume() {
462 super.onResume();
463
464 if (mRestoring) {
465 startLoaders();
466 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700467
468 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
469 // onNewIntent), then close the search dialog if needed, because it probably
470 // came from the user pressing 'home' (rather than, for example, pressing 'back').
471 if (mIsNewIntent) {
472 // Post to a handler so that this happens after the search dialog tries to open
473 // itself again.
474 mWorkspace.post(new Runnable() {
475 public void run() {
476 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
477 ServiceManager.getService(Context.SEARCH_SERVICE));
478 try {
479 searchManagerService.stopSearch();
480 } catch (RemoteException e) {
481 e(LOG_TAG, "error stopping search", e);
482 }
483 }
484 });
485 }
486
487 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 }
489
490 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700491 protected void onPause() {
492 super.onPause();
Romain Guy73b979d2009-06-09 12:57:21 -0700493 if (mGesturesWindow != null) {
494 mGesturesWindow.setAnimationStyle(0);
495 mGesturesWindow.update();
496 }
Romain Guycbb89e42009-06-08 15:52:54 -0700497 closeDrawer(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700498 }
Romain Guycbb89e42009-06-08 15:52:54 -0700499
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700500 @Override
Romain Guy73b979d2009-06-09 12:57:21 -0700501 protected void onStop() {
502 super.onStop();
Romain Guyaad5ef42009-06-10 02:48:37 -0700503 if (mHideGesturesPanel) {
504 mHideGesturesPanel = false;
505 hideGesturesPanel();
506 }
Romain Guy73b979d2009-06-09 12:57:21 -0700507 }
508
509 @Override
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700510 public Object onRetainNonConfigurationInstance() {
511 // Flag any binder to stop early before switching
512 if (mBinder != null) {
513 mBinder.mTerminate = true;
514 }
Romain Guycbb89e42009-06-08 15:52:54 -0700515
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700516 if (PROFILE_ROTATE) {
517 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
518 }
519 return null;
520 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700521
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522 private boolean acceptFilter() {
523 final InputMethodManager inputManager = (InputMethodManager)
524 getSystemService(Context.INPUT_METHOD_SERVICE);
525 return !inputManager.isFullscreenMode();
526 }
527
528 @Override
529 public boolean onKeyDown(int keyCode, KeyEvent event) {
530 boolean handled = super.onKeyDown(keyCode, event);
531 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
532 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
533 keyCode, event);
534 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700535 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700536 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700537 // showSearchDialog()
538 // If there are multiple keystrokes before the search dialog takes focus,
539 // onSearchRequested() will be called for every keystroke,
540 // but it is idempotent, so it's fine.
541 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 }
543 }
544
545 return handled;
546 }
547
Karl Rosaen138a0412009-04-23 19:00:21 -0700548 private String getTypedText() {
549 return mDefaultKeySsb.toString();
550 }
551
552 private void clearTypedText() {
553 mDefaultKeySsb.clear();
554 mDefaultKeySsb.clearSpans();
555 Selection.setSelection(mDefaultKeySsb, 0);
556 }
557
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 /**
559 * Restores the previous state, if it exists.
560 *
561 * @param savedState The previous state.
562 */
563 private void restoreState(Bundle savedState) {
564 if (savedState == null) {
565 return;
566 }
567
568 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
569 if (currentScreen > -1) {
570 mWorkspace.setCurrentScreen(currentScreen);
571 }
572
573 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
574 if (addScreen > -1) {
575 mAddItemCellInfo = new CellLayout.CellInfo();
576 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
577 addItemCellInfo.valid = true;
578 addItemCellInfo.screen = addScreen;
579 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
580 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
581 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
582 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
583 addItemCellInfo.findVacantCellsFromOccupied(
584 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
585 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
586 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
587 mRestoring = true;
588 }
589
590 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
591 if (renameFolder) {
592 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
593 mFolderInfo = sModel.getFolderById(this, id);
594 mRestoring = true;
595 }
Romain Guy73b979d2009-06-09 12:57:21 -0700596
Romain Guy1ce1a242009-06-23 17:34:54 -0700597 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_PENDING_GESTURE);
598
Romain Guy3cf604f2009-06-16 13:12:53 -0700599 boolean gesturesShowing = savedState.getBoolean(RUNTIME_STATE_GESTURES_PANEL, false);
600 if (gesturesShowing) {
Romain Guyf38b3d52009-07-01 16:39:46 -0700601 if (mCurrentGesture == null) {
602 mCurrentGesture = (Gesture) savedState.get(RUNTIME_STATE_GESTURES_PANEL_GESTURE);
603 }
604 final Gesture gesture = mCurrentGesture;
Romain Guy3cf604f2009-06-16 13:12:53 -0700605 mWorkspace.post(new Runnable() {
606 public void run() {
607 showGesturesPanel(false);
608 mGesturesProcessor.matchGesture(gesture, false);
609 mWorkspace.post(new Runnable() {
610 public void run() {
611 if (gesture != null) {
612 mGesturesOverlay.setGesture(gesture);
613 }
614 }
615 });
616 }
617 });
618 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 }
620
621 /**
622 * Finds all the views we need and configure them properly.
623 */
624 private void setupViews() {
625 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
626 final DragLayer dragLayer = mDragLayer;
627
628 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
629 final Workspace workspace = mWorkspace;
630
631 mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer);
632 final SlidingDrawer drawer = mDrawer;
633
634 mAllAppsGrid = (AllAppsGridView) drawer.getContent();
635 final AllAppsGridView grid = mAllAppsGrid;
636
637 final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
638
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700639 mHandleView = (HandleView) drawer.findViewById(R.id.all_apps);
640 mHandleView.setLauncher(this);
641 mHandleIcon = (TransitionDrawable) mHandleView.getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 mHandleIcon.setCrossFadeEnabled(true);
643
644 drawer.lock();
645 final DrawerManager drawerManager = new DrawerManager();
646 drawer.setOnDrawerOpenListener(drawerManager);
647 drawer.setOnDrawerCloseListener(drawerManager);
648 drawer.setOnDrawerScrollListener(drawerManager);
649
Karl Rosaen138a0412009-04-23 19:00:21 -0700650 grid.setTextFilterEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 grid.setDragger(dragLayer);
652 grid.setLauncher(this);
653
654 workspace.setOnLongClickListener(this);
655 workspace.setDragger(dragLayer);
656 workspace.setLauncher(this);
657 loadWallpaper();
658
659 deleteZone.setLauncher(this);
660 deleteZone.setDragController(dragLayer);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700661 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662
663 dragLayer.setIgnoredDropTarget(grid);
664 dragLayer.setDragScoller(workspace);
665 dragLayer.setDragListener(deleteZone);
Romain Guy73b979d2009-06-09 12:57:21 -0700666
667 mGesturesPanel = mInflater.inflate(R.layout.gestures, mDragLayer, false);
668 final View gesturesPanel = mGesturesPanel;
669
670 mGesturesPrompt = (ViewSwitcher) gesturesPanel.findViewById(R.id.gestures_actions);
671 mGesturesAction = new GesturesAction();
672
673 mGesturesPrompt.getChildAt(0).setOnClickListener(mGesturesAction);
674 mGesturesPrompt.getChildAt(1).setOnClickListener(mGesturesAction);
675
676 mGesturesAdd = (ImageView) gesturesPanel.findViewById(R.id.gestures_add);
677 final ImageView gesturesAdd = mGesturesAdd;
678 gesturesAdd.setAlpha(128);
679 gesturesAdd.setEnabled(false);
680 gesturesAdd.setOnClickListener(new View.OnClickListener() {
681 public void onClick(View v) {
682 createGesture();
683 }
684 });
685
686 mGesturesOverlay = (GestureOverlayView) gesturesPanel.findViewById(R.id.gestures_overlay);
687 mGesturesProcessor = new GesturesProcessor();
688
689 final GestureOverlayView overlay = mGesturesOverlay;
Romain Guy73b979d2009-06-09 12:57:21 -0700690 overlay.addOnGestureListener(mGesturesProcessor);
Romain Guyb8734242009-06-10 11:53:57 -0700691 overlay.getGesturePaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
Romain Guy73b979d2009-06-09 12:57:21 -0700692 }
693
694 private void createGesture() {
Romain Guyf280f202009-06-26 10:31:07 -0700695 if (!mWaitingForResult) {
696 mCurrentGesture = mGesturesOverlay.getGesture();
697 mWaitingForResult = true;
698 pickShortcut(REQUEST_PICK_GESTURE_ACTION, R.string.title_select_shortcut);
699 }
Romain Guy73b979d2009-06-09 12:57:21 -0700700 }
701
702 private void completeCreateGesture(Intent data, boolean isShortcut) {
703 ApplicationInfo info;
704
705 if (isShortcut) {
706 info = infoFromShortcutIntent(this, data);
707 } else {
708 info = infoFromApplicationIntent(this, data);
709 }
710
711 boolean success = false;
712 if (info != null) {
713 info.isGesture = true;
714
715 if (LauncherModel.addGestureToDatabase(this, info, false)) {
716 mGesturesProcessor.addGesture(String.valueOf(info.id), mCurrentGesture);
717 mGesturesProcessor.update(info, mCurrentGesture);
718 Toast.makeText(this, getString(R.string.gestures_created, info.title),
719 Toast.LENGTH_SHORT).show();
720 success = true;
721 }
722 }
723
724 if (!success) {
725 Toast.makeText(this, getString(R.string.gestures_failed), Toast.LENGTH_SHORT).show();
726 }
727
728 mCurrentGesture = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 }
730
731 /**
732 * Creates a view representing a shortcut.
733 *
734 * @param info The data structure describing the shortcut.
735 *
736 * @return A View inflated from R.layout.application.
737 */
738 View createShortcut(ApplicationInfo info) {
739 return createShortcut(R.layout.application,
740 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
741 }
742
743 /**
744 * Creates a view representing a shortcut inflated from the specified resource.
745 *
746 * @param layoutResId The id of the XML layout used to create the shortcut.
747 * @param parent The group the shortcut belongs to.
748 * @param info The data structure describing the shortcut.
749 *
750 * @return A View inflated from layoutResId.
751 */
752 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
753 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
754
755 if (!info.filtered) {
756 info.icon = Utilities.createIconThumbnail(info.icon, this);
757 info.filtered = true;
758 }
759
760 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
761 favorite.setText(info.title);
762 favorite.setTag(info);
763 favorite.setOnClickListener(this);
764
765 return favorite;
766 }
767
768 /**
769 * Add an application shortcut to the workspace.
770 *
771 * @param data The intent describing the application.
772 * @param cellInfo The position on screen where to create the shortcut.
773 */
774 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo,
775 boolean insertAtFirst) {
776 cellInfo.screen = mWorkspace.getCurrentScreen();
777 if (!findSingleSlot(cellInfo)) return;
778
Romain Guy73b979d2009-06-09 12:57:21 -0700779 final ApplicationInfo info = infoFromApplicationIntent(context, data);
780 if (info != null) {
781 mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst);
782 }
783 }
784
785 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 ComponentName component = data.getComponent();
787 PackageManager packageManager = context.getPackageManager();
788 ActivityInfo activityInfo = null;
789 try {
790 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
791 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700792 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 }
Romain Guycbb89e42009-06-08 15:52:54 -0700794
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 if (activityInfo != null) {
796 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 itemInfo.title = activityInfo.loadLabel(packageManager);
799 if (itemInfo.title == null) {
800 itemInfo.title = activityInfo.name;
801 }
Romain Guycbb89e42009-06-08 15:52:54 -0700802
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
804 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
805 itemInfo.icon = activityInfo.loadIcon(packageManager);
806 itemInfo.container = ItemInfo.NO_ID;
807
Romain Guy73b979d2009-06-09 12:57:21 -0700808 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 }
Romain Guy73b979d2009-06-09 12:57:21 -0700810
811 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
Romain Guycbb89e42009-06-08 15:52:54 -0700813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 /**
815 * Add a shortcut to the workspace.
816 *
817 * @param data The intent describing the shortcut.
818 * @param cellInfo The position on screen where to create the shortcut.
819 * @param insertAtFirst
820 */
821 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo,
822 boolean insertAtFirst) {
823 cellInfo.screen = mWorkspace.getCurrentScreen();
824 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700825
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
827
828 if (!mRestoring) {
829 sModel.addDesktopItem(info);
830
831 final View view = createShortcut(info);
832 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
833 } else if (sModel.isDesktopLoaded()) {
834 sModel.addDesktopItem(info);
835 }
836 }
837
Romain Guycbb89e42009-06-08 15:52:54 -0700838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700840 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700842 * @param data The intent describing the appWidgetId.
843 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700845 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 boolean insertAtFirst) {
847
848 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700849 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700850
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700851 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700852
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700853 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700854
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700855 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700857 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 // Try finding open space on Launcher screen
860 final int[] xy = mCellCoordinates;
861 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
862
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700863 // Build Launcher-specific widget info and save to database
864 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 launcherInfo.spanX = spans[0];
866 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700867
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 LauncherModel.addItemToDatabase(this, launcherInfo,
869 LauncherSettings.Favorites.CONTAINER_DESKTOP,
870 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
871
872 if (!mRestoring) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700873 sModel.addDesktopAppWidget(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700874
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700876 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700877
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700878 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700880
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
882 launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
883 } else if (sModel.isDesktopLoaded()) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700884 sModel.addDesktopAppWidget(launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886 }
Romain Guycbb89e42009-06-08 15:52:54 -0700887
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700888 public LauncherAppWidgetHost getAppWidgetHost() {
889 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
Romain Guycbb89e42009-06-08 15:52:54 -0700891
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 static ApplicationInfo addShortcut(Context context, Intent data,
893 CellLayout.CellInfo cellInfo, boolean notify) {
894
Romain Guy73b979d2009-06-09 12:57:21 -0700895 final ApplicationInfo info = infoFromShortcutIntent(context, data);
896 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
897 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
898
899 return info;
900 }
901
902 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
904 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
905 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
906
907 Drawable icon = null;
908 boolean filtered = false;
909 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700910 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911
912 if (bitmap != null) {
913 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
914 filtered = true;
915 customIcon = true;
916 } else {
917 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700918 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700920 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 final PackageManager packageManager = context.getPackageManager();
922 Resources resources = packageManager.getResourcesForApplication(
923 iconResource.packageName);
924 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
925 icon = resources.getDrawable(id);
926 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700927 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 }
929 }
930 }
931
932 if (icon == null) {
933 icon = context.getPackageManager().getDefaultActivityIcon();
934 }
935
936 final ApplicationInfo info = new ApplicationInfo();
937 info.icon = icon;
938 info.filtered = filtered;
939 info.title = name;
940 info.intent = intent;
941 info.customIcon = customIcon;
942 info.iconResource = iconResource;
943
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 return info;
945 }
946
947 @Override
948 protected void onNewIntent(Intent intent) {
949 super.onNewIntent(intent);
950
951 // Close the menu
952 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
953 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700954
955 // Set this flag so that onResume knows to close the search dialog if it's open,
956 // because this was a new intent (thus a press of 'home' or some such) rather than
957 // for example onResume being called when the user pressed the 'back' button.
958 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959
960 try {
961 dismissDialog(DIALOG_CREATE_SHORTCUT);
962 // Unlock the workspace if the dialog was showing
963 mWorkspace.unlock();
964 } catch (Exception e) {
965 // An exception is thrown if the dialog is not visible, which is fine
966 }
967
968 try {
969 dismissDialog(DIALOG_RENAME_FOLDER);
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
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
977 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700978
Romain Guye3895ae2009-06-16 13:25:29 -0700979 if (mGesturesPanel != null && mDragLayer.getWindowVisibility() == View.VISIBLE &&
Romain Guy94406842009-06-17 16:18:58 -0700980 (mDragLayer.hasWindowFocus() ||
981 (mGesturesWindow != null && mGesturesWindow.isShowing()))) {
982
Romain Guyb8734242009-06-10 11:53:57 -0700983 SearchManager searchManager =
984 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Romain Guy94406842009-06-17 16:18:58 -0700985
Romain Guyb8734242009-06-10 11:53:57 -0700986 if (!searchManager.isVisible()) {
987 onHomeKeyPressed();
988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 }
990 closeDrawer();
Romain Guy73b979d2009-06-09 12:57:21 -0700991
992 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 if (v != null && v.getWindowToken() != null) {
994 InputMethodManager imm = (InputMethodManager)getSystemService(
995 INPUT_METHOD_SERVICE);
996 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
997 }
998 } else {
999 closeDrawer(false);
1000 }
1001 }
1002 }
1003
Romain Guy73b979d2009-06-09 12:57:21 -07001004 private void onHomeKeyPressed() {
1005 if (mGesturesWindow == null || !mGesturesWindow.isShowing()) {
1006 showGesturesPanel();
1007 } else {
1008 hideGesturesPanel();
1009 }
1010 }
1011
1012 private void showGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -07001013 showGesturesPanel(true);
1014 }
1015
1016 private void showGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07001017 resetGesturesPrompt();
1018
1019 mGesturesAdd.setEnabled(false);
1020 mGesturesAdd.setAlpha(128);
1021
1022 mGesturesOverlay.clear(false);
1023
1024 PopupWindow window;
1025 if (mGesturesWindow == null) {
1026 mGesturesWindow = new PopupWindow(this);
1027 window = mGesturesWindow;
1028 window.setFocusable(true);
1029 window.setTouchable(true);
1030 window.setBackgroundDrawable(null);
1031 window.setContentView(mGesturesPanel);
1032 } else {
1033 window = mGesturesWindow;
1034 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001035 window.setAnimationStyle(animate ? com.android.internal.R.style.Animation_SlidingCard : 0);
Romain Guy73b979d2009-06-09 12:57:21 -07001036
1037 final int[] xy = new int[2];
1038 final DragLayer dragLayer = mDragLayer;
1039 dragLayer.getLocationOnScreen(xy);
1040
1041 window.setWidth(dragLayer.getWidth());
1042 window.setHeight(dragLayer.getHeight() - 1);
1043 window.showAtLocation(dragLayer, Gravity.TOP | Gravity.LEFT, xy[0], xy[1] + 1);
1044 }
1045
1046 private void resetGesturesPrompt() {
1047 mGesturesAction.intent = null;
1048 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
1049 prompt.setText(R.string.gestures_instructions);
1050 prompt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
1051 prompt.setClickable(false);
1052 }
1053
1054 private void resetGesturesNextPrompt() {
1055 mGesturesAction.intent = null;
1056 setGesturesNextPrompt(null, getString(R.string.gestures_instructions));
1057 mGesturesPrompt.getNextView().setClickable(false);
1058 }
1059
1060 private void setGesturesNextPrompt(Drawable icon, CharSequence title) {
1061 final TextView prompt = (TextView) mGesturesPrompt.getNextView();
1062 prompt.setText(title);
1063 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
1064 prompt.setClickable(true);
1065 mGesturesPrompt.showNext();
1066 }
1067
Romain Guy3cf604f2009-06-16 13:12:53 -07001068 private void setGesturesPrompt(Drawable icon, CharSequence title) {
1069 final TextView prompt = (TextView) mGesturesPrompt.getCurrentView();
1070 prompt.setText(title);
1071 prompt.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
1072 prompt.setClickable(true);
1073 }
1074
Romain Guy73b979d2009-06-09 12:57:21 -07001075 void hideGesturesPanel() {
Romain Guy3cf604f2009-06-16 13:12:53 -07001076 hideGesturesPanel(true);
1077 }
1078
1079 void hideGesturesPanel(boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07001080 if (mGesturesWindow != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001081 final PopupWindow popupWindow = mGesturesWindow;
1082 popupWindow.setAnimationStyle(animate ?
1083 com.android.internal.R.style.Animation_SlidingCard : 0);
1084 popupWindow.update();
1085 popupWindow.dismiss();
Romain Guy73b979d2009-06-09 12:57:21 -07001086 }
1087 }
1088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 @Override
1090 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1091 // Do not call super here
1092 mSavedInstanceState = savedInstanceState;
1093 }
1094
1095 @Override
1096 protected void onSaveInstanceState(Bundle outState) {
1097 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1098
1099 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1100 if (folders.size() > 0) {
1101 final int count = folders.size();
1102 long[] ids = new long[count];
1103 for (int i = 0; i < count; i++) {
1104 final FolderInfo info = folders.get(i).getInfo();
1105 ids[i] = info.id;
1106 }
1107 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1108 } else {
1109 super.onSaveInstanceState(outState);
1110 }
1111
Romain Guy3cf604f2009-06-16 13:12:53 -07001112 final boolean isConfigurationChange = getChangingConfigurations() != 0;
1113
Romain Guy5a941392009-04-28 15:18:25 -07001114 // When the drawer is opened and we are saving the state because of a
1115 // configuration change
Romain Guy3cf604f2009-06-16 13:12:53 -07001116 if (mDrawer.isOpened() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001118 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119
1120 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1121 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1122 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1123
1124 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1125 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1126 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1127 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1128 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1129 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1130 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1131 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1132 layout.getOccupiedCells());
1133 }
1134
1135 if (mFolderInfo != null && mWaitingForResult) {
1136 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1137 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1138 }
Romain Guy73b979d2009-06-09 12:57:21 -07001139
1140 if (mCurrentGesture != null && mWaitingForResult) {
1141 outState.putParcelable(RUNTIME_STATE_PENDING_GESTURE, mCurrentGesture);
1142 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001143
Romain Guy1ce1a242009-06-23 17:34:54 -07001144 if (mGesturesWindow != null && mGesturesWindow.isShowing()) {
Romain Guy3cf604f2009-06-16 13:12:53 -07001145 outState.putBoolean(RUNTIME_STATE_GESTURES_PANEL, true);
1146
Romain Guy1ce1a242009-06-23 17:34:54 -07001147 if (mCurrentGesture == null || !mWaitingForResult) {
1148 final Gesture gesture = mGesturesOverlay.getGesture();
1149 if (gesture != null) {
1150 outState.putParcelable(RUNTIME_STATE_GESTURES_PANEL_GESTURE, gesture);
1151 }
Romain Guy3cf604f2009-06-16 13:12:53 -07001152 }
1153 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
1155
1156 @Override
1157 public void onDestroy() {
1158 mDestroyed = true;
1159
1160 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001161
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001163 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001165 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 }
1167
1168 TextKeyListener.getInstance().release();
1169
Romain Guy3cf604f2009-06-16 13:12:53 -07001170 hideGesturesPanel(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 mAllAppsGrid.clearTextFilter();
1172 mAllAppsGrid.setAdapter(null);
1173 sModel.unbind();
1174 sModel.abortLoaders();
1175
1176 getContentResolver().unregisterContentObserver(mObserver);
1177 unregisterReceiver(mApplicationsReceiver);
1178 }
1179
1180 @Override
1181 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001182 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 super.startActivityForResult(intent, requestCode);
1184 }
1185
1186 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001187 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001189
1190 closeDrawer(false);
Romain Guycbb89e42009-06-08 15:52:54 -07001191
Karl Rosaen138a0412009-04-23 19:00:21 -07001192 // Slide the search widget to the top, if it's on the current screen,
1193 // otherwise show the search dialog immediately.
1194 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1195 if (searchWidget == null) {
1196 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1197 } else {
1198 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1199 // show the currently typed text in the search widget while sliding
1200 searchWidget.setQuery(getTypedText());
1201 }
1202 }
Romain Guycbb89e42009-06-08 15:52:54 -07001203
Karl Rosaen138a0412009-04-23 19:00:21 -07001204 /**
1205 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -07001206 *
1207 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -07001208 */
Romain Guycbb89e42009-06-08 15:52:54 -07001209 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -07001210 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -07001211
Karl Rosaen138a0412009-04-23 19:00:21 -07001212 if (initialQuery == null) {
1213 // Use any text typed in the launcher as the initial query
1214 initialQuery = getTypedText();
1215 clearTypedText();
1216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 if (appSearchData == null) {
1218 appSearchData = new Bundle();
1219 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
1220 }
Romain Guycbb89e42009-06-08 15:52:54 -07001221
Karl Rosaen138a0412009-04-23 19:00:21 -07001222 final SearchManager searchManager =
1223 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1224
1225 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1226 if (searchWidget != null) {
1227 // This gets called when the user leaves the search dialog to go back to
1228 // the Launcher.
1229 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1230 public void onCancel() {
1231 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001232 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001233 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001234 });
1235 }
Romain Guycbb89e42009-06-08 15:52:54 -07001236
Karl Rosaen138a0412009-04-23 19:00:21 -07001237 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001238 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 }
1240
Karl Rosaen138a0412009-04-23 19:00:21 -07001241 /**
1242 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001243 */
Romain Guy5a941392009-04-28 15:18:25 -07001244 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001245 // Close search dialog
1246 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001247 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001248 // Restore search widget to its normal position
1249 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1250 if (searchWidget != null) {
1251 searchWidget.stopSearch(false);
1252 }
1253 }
Romain Guycbb89e42009-06-08 15:52:54 -07001254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 @Override
1256 public boolean onCreateOptionsMenu(Menu menu) {
1257 if (mDesktopLocked) return false;
1258
1259 super.onCreateOptionsMenu(menu);
1260 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1261 .setIcon(android.R.drawable.ic_menu_add)
1262 .setAlphabeticShortcut('A');
1263 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1264 .setIcon(android.R.drawable.ic_menu_gallery)
1265 .setAlphabeticShortcut('W');
1266 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1267 .setIcon(android.R.drawable.ic_search_category_default)
1268 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1269 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1270 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1271 .setAlphabeticShortcut('N');
1272
Romain Guy73b979d2009-06-09 12:57:21 -07001273 final Intent gestures = new Intent(this, GesturesActivity.class);
1274 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1275 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1276 .setIntent(gestures);
1277
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001279 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1280 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281
1282 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1283 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1284 .setIntent(settings);
1285
1286 return true;
1287 }
1288
1289 @Override
1290 public boolean onPrepareOptionsMenu(Menu menu) {
1291 super.onPrepareOptionsMenu(menu);
1292
1293 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1294 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1295
1296 return true;
1297 }
1298
1299 @Override
1300 public boolean onOptionsItemSelected(MenuItem item) {
1301 switch (item.getItemId()) {
1302 case MENU_ADD:
1303 addItems();
1304 return true;
1305 case MENU_WALLPAPER_SETTINGS:
1306 startWallpaper();
1307 return true;
1308 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001309 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 return true;
1311 case MENU_NOTIFICATIONS:
1312 showNotifications();
1313 return true;
1314 }
1315
1316 return super.onOptionsItemSelected(item);
1317 }
Romain Guycbb89e42009-06-08 15:52:54 -07001318
Karl Rosaen138a0412009-04-23 19:00:21 -07001319 /**
1320 * Indicates that we want global search for this activity by setting the globalSearch
1321 * argument for {@link #startSearch} to true.
1322 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001324 @Override
1325 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001326 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001327 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001328 }
1329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 private void addItems() {
1331 showAddDialog(mMenuAddInfo);
1332 }
1333
1334 private void removeShortcutsForPackage(String packageName) {
1335 if (packageName != null && packageName.length() > 0) {
1336 mWorkspace.removeShortcutsForPackage(packageName);
1337 }
1338 }
Romain Guycbb89e42009-06-08 15:52:54 -07001339
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001340 private void updateShortcutsForPackage(String packageName) {
1341 if (packageName != null && packageName.length() > 0) {
1342 mWorkspace.updateShortcutsForPackage(packageName);
1343 }
1344 }
1345
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001346 void addAppWidget(Intent data) {
1347 // TODO: catch bad widget exception when sent
1348 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001350 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1351 if (SEARCH_WIDGET.equals(customWidget)) {
1352 // We don't need this any more, since this isn't a real app widget.
1353 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1354 // add the search widget
1355 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001357 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1358
1359 if (appWidget.configure != null) {
1360 // Launch over to configure widget, if needed
1361 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1362 intent.setComponent(appWidget.configure);
1363 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1364
1365 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1366 } else {
1367 // Otherwise just add it
1368 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
1371 }
Romain Guycbb89e42009-06-08 15:52:54 -07001372
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 void addSearch() {
1374 final Widget info = Widget.makeSearch();
1375 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 final int[] xy = mCellCoordinates;
1378 final int spanX = info.spanX;
1379 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001382
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 sModel.addDesktopItem(info);
1384 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1385 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 final View view = mInflater.inflate(info.layoutResource, null);
1388 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001389 Search search = (Search) view.findViewById(R.id.widget_search);
1390 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1393 }
1394
Romain Guy73b979d2009-06-09 12:57:21 -07001395 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001396 // Handle case where user selected "Applications"
1397 String applicationName = getResources().getString(R.string.group_applications);
1398 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001399
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001400 if (applicationName != null && applicationName.equals(shortcutName)) {
1401 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1402 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001403
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001404 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1405 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001406 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001407 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001408 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 }
1411
1412 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001413 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001414 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001415 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001416
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001417 if (folderName != null && folderName.equals(shortcutName)) {
1418 addFolder(!mDesktopLocked);
1419 } else {
1420 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1421 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 }
1423
1424 void addFolder(boolean insertAtFirst) {
1425 UserFolderInfo folderInfo = new UserFolderInfo();
1426 folderInfo.title = getText(R.string.folder_name);
1427
1428 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1429 cellInfo.screen = mWorkspace.getCurrentScreen();
1430 if (!findSingleSlot(cellInfo)) return;
1431
1432 // Update the model
1433 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1434 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1435 sModel.addDesktopItem(folderInfo);
1436 sModel.addFolder(folderInfo);
1437
1438 // Create the view
1439 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1440 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1441 mWorkspace.addInCurrentScreen(newFolder,
1442 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1443 }
Romain Guycbb89e42009-06-08 15:52:54 -07001444
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1446 boolean insertAtFirst) {
1447 cellInfo.screen = mWorkspace.getCurrentScreen();
1448 if (!findSingleSlot(cellInfo)) return;
1449
1450 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1451
1452 if (!mRestoring) {
1453 sModel.addDesktopItem(info);
1454
1455 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1456 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1457 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1458 } else if (sModel.isDesktopLoaded()) {
1459 sModel.addDesktopItem(info);
1460 }
1461 }
1462
1463 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1464 CellLayout.CellInfo cellInfo, boolean notify) {
1465
1466 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1467 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1468
1469 Drawable icon = null;
1470 boolean filtered = false;
1471 Intent.ShortcutIconResource iconResource = null;
1472
1473 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1474 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1475 try {
1476 iconResource = (Intent.ShortcutIconResource) extra;
1477 final PackageManager packageManager = context.getPackageManager();
1478 Resources resources = packageManager.getResourcesForApplication(
1479 iconResource.packageName);
1480 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1481 icon = resources.getDrawable(id);
1482 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001483 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 }
1485 }
1486
1487 if (icon == null) {
1488 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1489 }
1490
1491 final LiveFolderInfo info = new LiveFolderInfo();
1492 info.icon = icon;
1493 info.filtered = filtered;
1494 info.title = name;
1495 info.iconResource = iconResource;
1496 info.uri = data.getData();
1497 info.baseIntent = baseIntent;
1498 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1499 LiveFolders.DISPLAY_MODE_GRID);
1500
1501 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1502 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1503 sModel.addFolder(info);
1504
1505 return info;
1506 }
1507
1508 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1509 final int[] xy = new int[2];
1510 if (findSlot(cellInfo, xy, 1, 1)) {
1511 cellInfo.cellX = xy[0];
1512 cellInfo.cellY = xy[1];
1513 return true;
1514 }
1515 return false;
1516 }
1517
1518 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1519 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1520 boolean[] occupied = mSavedState != null ?
1521 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1522 cellInfo = mWorkspace.findAllVacantCells(occupied);
1523 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1524 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1525 return false;
1526 }
1527 }
1528 return true;
1529 }
1530
1531 private void showNotifications() {
1532 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1533 if (statusBar != null) {
1534 statusBar.expand();
1535 }
1536 }
1537
1538 private void startWallpaper() {
1539 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1540 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1541 }
1542
1543 /**
1544 * Registers various intent receivers. The current implementation registers
1545 * only a wallpaper intent receiver to let other applications change the
1546 * wallpaper.
1547 */
1548 private void registerIntentReceivers() {
1549 if (sWallpaperReceiver == null) {
1550 final Application application = getApplication();
1551
1552 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1553
1554 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1555 application.registerReceiver(sWallpaperReceiver, filter);
1556 } else {
1557 sWallpaperReceiver.setLauncher(this);
1558 }
1559
1560 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1561 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1562 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1563 filter.addDataScheme("package");
1564 registerReceiver(mApplicationsReceiver, filter);
1565 }
1566
1567 /**
1568 * Registers various content observers. The current implementation registers
1569 * only a favorites observer to keep track of the favorites applications.
1570 */
1571 private void registerContentObservers() {
1572 ContentResolver resolver = getContentResolver();
1573 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1574 }
1575
1576 @Override
1577 public boolean dispatchKeyEvent(KeyEvent event) {
1578 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1579 switch (event.getKeyCode()) {
1580 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001581 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001582 if (mDrawer.isOpened()) {
1583 closeDrawer();
1584 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001585 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 return true;
1588 case KeyEvent.KEYCODE_HOME:
1589 return true;
1590 }
1591 }
1592
1593 return super.dispatchKeyEvent(event);
1594 }
1595
1596 private void closeDrawer() {
1597 closeDrawer(true);
1598 }
1599
1600 private void closeDrawer(boolean animated) {
1601 if (mDrawer.isOpened()) {
1602 if (animated) {
1603 mDrawer.animateClose();
1604 } else {
1605 mDrawer.close();
1606 }
1607 if (mDrawer.hasFocus()) {
1608 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1609 }
1610 }
1611 }
1612
1613 private void closeFolder() {
1614 Folder folder = mWorkspace.getOpenFolder();
1615 if (folder != null) {
1616 closeFolder(folder);
1617 }
1618 }
1619
1620 void closeFolder(Folder folder) {
1621 folder.getInfo().opened = false;
1622 ViewGroup parent = (ViewGroup) folder.getParent();
1623 if (parent != null) {
1624 parent.removeView(folder);
1625 }
1626 folder.onClose();
1627 }
1628
1629 /**
1630 * When the notification that favorites have changed is received, requests
1631 * a favorites list refresh.
1632 */
1633 private void onFavoritesChanged() {
1634 mDesktopLocked = true;
1635 mDrawer.lock();
1636 sModel.loadUserItems(false, this, false, false);
1637 }
1638
1639 void onDesktopItemsLoaded() {
1640 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 bindDesktopItems();
1642 }
Romain Guycbb89e42009-06-08 15:52:54 -07001643
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 /**
1645 * Refreshes the shortcuts shown on the workspace.
1646 */
1647 private void bindDesktopItems() {
1648 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001649 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001650 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1651 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 return;
1653 }
1654
1655 final Workspace workspace = mWorkspace;
1656 int count = workspace.getChildCount();
1657 for (int i = 0; i < count; i++) {
1658 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1659 }
Romain Guycbb89e42009-06-08 15:52:54 -07001660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 if (DEBUG_USER_INTERFACE) {
1662 android.widget.Button finishButton = new android.widget.Button(this);
1663 finishButton.setText("Finish");
1664 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1665
1666 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1667 public void onClick(View v) {
1668 finish();
1669 }
1670 });
1671 }
Romain Guycbb89e42009-06-08 15:52:54 -07001672
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001673 // Flag any old binder to terminate early
1674 if (mBinder != null) {
1675 mBinder.mTerminate = true;
1676 }
Romain Guycbb89e42009-06-08 15:52:54 -07001677
Karl Rosaen138a0412009-04-23 19:00:21 -07001678 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001679 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 }
1681
1682 private void bindItems(Launcher.DesktopBinder binder,
1683 ArrayList<ItemInfo> shortcuts, int start, int count) {
1684
1685 final Workspace workspace = mWorkspace;
1686 final boolean desktopLocked = mDesktopLocked;
1687
1688 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1689 int i = start;
1690
1691 for ( ; i < end; i++) {
1692 final ItemInfo item = shortcuts.get(i);
1693 switch (item.itemType) {
1694 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1695 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1696 final View shortcut = createShortcut((ApplicationInfo) item);
1697 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1698 !desktopLocked);
1699 break;
1700 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1701 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1702 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1703 (UserFolderInfo) item);
1704 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1705 !desktopLocked);
1706 break;
1707 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1708 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1709 R.layout.live_folder_icon, this,
1710 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1711 (LiveFolderInfo) item);
1712 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1713 !desktopLocked);
1714 break;
1715 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1716 final int screen = workspace.getCurrentScreen();
1717 final View view = mInflater.inflate(R.layout.widget_search,
1718 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001719
Karl Rosaen138a0412009-04-23 19:00:21 -07001720 Search search = (Search) view.findViewById(R.id.widget_search);
1721 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001722
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 final Widget widget = (Widget) item;
1724 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001725
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 workspace.addWidget(view, widget, !desktopLocked);
1727 break;
1728 }
1729 }
1730
1731 workspace.requestLayout();
1732
1733 if (end >= count) {
1734 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001735 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 } else {
1737 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1738 }
1739 }
1740
1741 private void finishBindDesktopItems() {
1742 if (mSavedState != null) {
1743 if (!mWorkspace.hasFocus()) {
1744 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1745 }
1746
1747 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1748 if (userFolders != null) {
1749 for (long folderId : userFolders) {
1750 final FolderInfo info = sModel.findFolderById(folderId);
1751 if (info != null) {
1752 openFolder(info);
1753 }
1754 }
1755 final Folder openFolder = mWorkspace.getOpenFolder();
1756 if (openFolder != null) {
1757 openFolder.requestFocus();
1758 }
1759 }
1760
1761 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1762 if (allApps) {
1763 mDrawer.open();
1764 }
1765
1766 mSavedState = null;
1767 }
1768
1769 if (mSavedInstanceState != null) {
1770 super.onRestoreInstanceState(mSavedInstanceState);
1771 mSavedInstanceState = null;
1772 }
1773
1774 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1775 mDrawer.requestFocus();
1776 }
1777
1778 mDesktopLocked = false;
1779 mDrawer.unlock();
1780 }
Romain Guycbb89e42009-06-08 15:52:54 -07001781
Karl Rosaen138a0412009-04-23 19:00:21 -07001782 private void bindDrawer(Launcher.DesktopBinder binder,
1783 ApplicationsAdapter drawerAdapter) {
1784 mAllAppsGrid.setAdapter(drawerAdapter);
1785 binder.startBindingAppWidgetsWhenIdle();
1786 }
Romain Guycbb89e42009-06-08 15:52:54 -07001787
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001788 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001789 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 final Workspace workspace = mWorkspace;
1792 final boolean desktopLocked = mDesktopLocked;
1793
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001794 if (!appWidgets.isEmpty()) {
1795 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001796
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001797 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001798 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001799 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001800
Karl Rosaen138a0412009-04-23 19:00:21 -07001801 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001802
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001803 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1804 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001805
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001806 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1807 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001808
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001809 workspace.requestLayout();
1810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001812 if (appWidgets.isEmpty()) {
1813 if (PROFILE_ROTATE) {
1814 android.os.Debug.stopMethodTracing();
1815 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001817 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 }
1819 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 DragController getDragController() {
1822 return mDragLayer;
1823 }
1824
1825 /**
1826 * Launches the intent referred by the clicked shortcut.
1827 *
1828 * @param v The view representing the clicked shortcut.
1829 */
1830 public void onClick(View v) {
1831 Object tag = v.getTag();
1832 if (tag instanceof ApplicationInfo) {
1833 // Open shortcut
1834 final Intent intent = ((ApplicationInfo) tag).intent;
1835 startActivitySafely(intent);
1836 } else if (tag instanceof FolderInfo) {
1837 handleFolderClick((FolderInfo) tag);
1838 }
1839 }
1840
1841 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001842 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1844 try {
1845 startActivity(intent);
1846 } catch (ActivityNotFoundException e) {
1847 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1848 } catch (SecurityException e) {
1849 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001850 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1852 "or use the exported attribute for this activity.", e);
1853 }
1854 }
1855
1856 private void handleFolderClick(FolderInfo folderInfo) {
1857 if (!folderInfo.opened) {
1858 // Close any open folder
1859 closeFolder();
1860 // Open the requested folder
1861 openFolder(folderInfo);
1862 } else {
1863 // Find the open folder...
1864 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1865 int folderScreen;
1866 if (openFolder != null) {
1867 folderScreen = mWorkspace.getScreenForView(openFolder);
1868 // .. and close it
1869 closeFolder(openFolder);
1870 if (folderScreen != mWorkspace.getCurrentScreen()) {
1871 // Close any folder open on the current screen
1872 closeFolder();
1873 // Pull the folder onto this screen
1874 openFolder(folderInfo);
1875 }
1876 }
1877 }
1878 }
1879
1880 private void loadWallpaper() {
1881 // The first time the application is started, we load the wallpaper from
1882 // the ApplicationContext
1883 if (sWallpaper == null) {
1884 final Drawable drawable = getWallpaper();
1885 if (drawable instanceof BitmapDrawable) {
1886 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1887 } else {
1888 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1889 }
1890 }
1891 mWorkspace.loadWallpaper(sWallpaper);
1892 }
1893
1894 /**
1895 * Opens the user fodler described by the specified tag. The opening of the folder
1896 * is animated relative to the specified View. If the View is null, no animation
1897 * is played.
1898 *
1899 * @param folderInfo The FolderInfo describing the folder to open.
1900 */
1901 private void openFolder(FolderInfo folderInfo) {
1902 Folder openFolder;
1903
1904 if (folderInfo instanceof UserFolderInfo) {
1905 openFolder = UserFolder.fromXml(this);
1906 } else if (folderInfo instanceof LiveFolderInfo) {
1907 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1908 } else {
1909 return;
1910 }
1911
1912 openFolder.setDragger(mDragLayer);
1913 openFolder.setLauncher(this);
1914
1915 openFolder.bind(folderInfo);
1916 folderInfo.opened = true;
1917
1918 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1919 openFolder.onOpen();
1920 }
1921
1922 /**
1923 * Returns true if the workspace is being loaded. When the workspace is loading,
1924 * no user interaction should be allowed to avoid any conflict.
1925 *
1926 * @return True if the workspace is locked, false otherwise.
1927 */
1928 boolean isWorkspaceLocked() {
1929 return mDesktopLocked;
1930 }
1931
1932 public boolean onLongClick(View v) {
1933 if (mDesktopLocked) {
1934 return false;
1935 }
1936
1937 if (!(v instanceof CellLayout)) {
1938 v = (View) v.getParent();
1939 }
1940
1941 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1942
1943 // This happens when long clicking an item with the dpad/trackball
1944 if (cellInfo == null) {
1945 return true;
1946 }
1947
1948 if (mWorkspace.allowLongPress()) {
1949 if (cellInfo.cell == null) {
1950 if (cellInfo.valid) {
1951 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001952 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 showAddDialog(cellInfo);
1954 }
1955 } else {
1956 if (!(cellInfo.cell instanceof Folder)) {
1957 // User long pressed on an item
1958 mWorkspace.startDrag(cellInfo);
1959 }
1960 }
1961 }
1962 return true;
1963 }
1964
1965 static LauncherModel getModel() {
1966 return sModel;
1967 }
1968
Romain Guy73b979d2009-06-09 12:57:21 -07001969 static GestureLibrary getGestureLibrary() {
1970 return sLibrary;
1971 }
1972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 void closeAllApplications() {
1974 mDrawer.close();
1975 }
1976
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001977 View getDrawerHandle() {
1978 return mHandleView;
1979 }
1980
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 boolean isDrawerDown() {
1982 return !mDrawer.isMoving() && !mDrawer.isOpened();
1983 }
1984
1985 boolean isDrawerUp() {
1986 return mDrawer.isOpened() && !mDrawer.isMoving();
1987 }
1988
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001989 boolean isDrawerMoving() {
1990 return mDrawer.isMoving();
1991 }
1992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 Workspace getWorkspace() {
1994 return mWorkspace;
1995 }
1996
1997 GridView getApplicationsGrid() {
1998 return mAllAppsGrid;
1999 }
2000
2001 @Override
2002 protected Dialog onCreateDialog(int id) {
2003 switch (id) {
2004 case DIALOG_CREATE_SHORTCUT:
2005 return new CreateShortcut().createDialog();
2006 case DIALOG_RENAME_FOLDER:
2007 return new RenameFolder().createDialog();
2008 }
2009
2010 return super.onCreateDialog(id);
2011 }
2012
2013 @Override
2014 protected void onPrepareDialog(int id, Dialog dialog) {
2015 switch (id) {
2016 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 break;
2018 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002019 if (mFolderInfo != null) {
2020 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2021 final CharSequence text = mFolderInfo.title;
2022 input.setText(text);
2023 input.setSelection(0, text.length());
2024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 break;
2026 }
2027 }
2028
2029 void showRenameDialog(FolderInfo info) {
2030 mFolderInfo = info;
2031 mWaitingForResult = true;
2032 showDialog(DIALOG_RENAME_FOLDER);
2033 }
2034
2035 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2036 mAddItemCellInfo = cellInfo;
2037 mWaitingForResult = true;
2038 showDialog(DIALOG_CREATE_SHORTCUT);
2039 }
2040
Romain Guy73b979d2009-06-09 12:57:21 -07002041 private void pickShortcut(int requestCode, int title) {
2042 Bundle bundle = new Bundle();
2043
2044 ArrayList<String> shortcutNames = new ArrayList<String>();
2045 shortcutNames.add(getString(R.string.group_applications));
2046 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2047
2048 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2049 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2050 R.drawable.ic_launcher_application));
2051 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2052
2053 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2054 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
2055 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
2056 pickIntent.putExtras(bundle);
2057
2058 startActivityForResult(pickIntent, requestCode);
2059 }
2060
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 private class RenameFolder {
2062 private EditText mInput;
2063
2064 Dialog createDialog() {
2065 mWaitingForResult = true;
2066 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2067 mInput = (EditText) layout.findViewById(R.id.folder_name);
2068
2069 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2070 builder.setIcon(0);
2071 builder.setTitle(getString(R.string.rename_folder_title));
2072 builder.setCancelable(true);
2073 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2074 public void onCancel(DialogInterface dialog) {
2075 cleanup();
2076 }
2077 });
2078 builder.setNegativeButton(getString(R.string.cancel_action),
2079 new Dialog.OnClickListener() {
2080 public void onClick(DialogInterface dialog, int which) {
2081 cleanup();
2082 }
2083 }
2084 );
2085 builder.setPositiveButton(getString(R.string.rename_action),
2086 new Dialog.OnClickListener() {
2087 public void onClick(DialogInterface dialog, int which) {
2088 changeFolderName();
2089 }
2090 }
2091 );
2092 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002093
2094 final AlertDialog dialog = builder.create();
2095 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2096 public void onShow(DialogInterface dialog) {
2097 mWorkspace.lock();
2098 }
2099 });
2100
2101 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 }
2103
2104 private void changeFolderName() {
2105 final String name = mInput.getText().toString();
2106 if (!TextUtils.isEmpty(name)) {
2107 // Make sure we have the right folder info
2108 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
2109 mFolderInfo.title = name;
2110 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2111
2112 if (mDesktopLocked) {
2113 mDrawer.lock();
2114 sModel.loadUserItems(false, Launcher.this, false, false);
2115 } else {
2116 final FolderIcon folderIcon = (FolderIcon)
2117 mWorkspace.getViewForTag(mFolderInfo);
2118 if (folderIcon != null) {
2119 folderIcon.setText(name);
2120 getWorkspace().requestLayout();
2121 } else {
2122 mDesktopLocked = true;
2123 mDrawer.lock();
2124 sModel.loadUserItems(false, Launcher.this, false, false);
2125 }
2126 }
2127 }
2128 cleanup();
2129 }
2130
2131 private void cleanup() {
2132 mWorkspace.unlock();
2133 dismissDialog(DIALOG_RENAME_FOLDER);
2134 mWaitingForResult = false;
2135 mFolderInfo = null;
2136 }
2137 }
2138
2139 /**
2140 * Displays the shortcut creation dialog and launches, if necessary, the
2141 * appropriate activity.
2142 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002143 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002144 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2145 DialogInterface.OnShowListener {
2146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002148
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 Dialog createDialog() {
2150 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002151
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002153
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2155 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002156 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 builder.setInverseBackgroundForced(true);
2159
2160 AlertDialog dialog = builder.create();
2161 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002162 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002163 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 return dialog;
2166 }
2167
2168 public void onCancel(DialogInterface dialog) {
2169 mWaitingForResult = false;
2170 cleanup();
2171 }
2172
Romain Guycbb89e42009-06-08 15:52:54 -07002173 public void onDismiss(DialogInterface dialog) {
2174 mWorkspace.unlock();
2175 }
2176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 private void cleanup() {
2178 mWorkspace.unlock();
2179 dismissDialog(DIALOG_CREATE_SHORTCUT);
2180 }
2181
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002182 /**
2183 * Handle the action clicked in the "Add to home" dialog.
2184 */
2185 public void onClick(DialogInterface dialog, int which) {
2186 Resources res = getResources();
2187 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002188
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002189 switch (which) {
2190 case AddAdapter.ITEM_SHORTCUT: {
2191 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002192 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002193 break;
2194 }
Romain Guycbb89e42009-06-08 15:52:54 -07002195
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002196 case AddAdapter.ITEM_APPWIDGET: {
2197 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002198
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002199 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2200 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2201 // add the search widget
2202 ArrayList<AppWidgetProviderInfo> customInfo =
2203 new ArrayList<AppWidgetProviderInfo>();
2204 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2205 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2206 info.label = getString(R.string.group_search);
2207 info.icon = R.drawable.ic_search_widget;
2208 customInfo.add(info);
2209 pickIntent.putParcelableArrayListExtra(
2210 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2211 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2212 Bundle b = new Bundle();
2213 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2214 customExtras.add(b);
2215 pickIntent.putParcelableArrayListExtra(
2216 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2217 // start the pick activity
2218 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2219 break;
2220 }
Romain Guycbb89e42009-06-08 15:52:54 -07002221
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002222 case AddAdapter.ITEM_LIVE_FOLDER: {
2223 // Insert extra item to handle inserting folder
2224 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002225
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002226 ArrayList<String> shortcutNames = new ArrayList<String>();
2227 shortcutNames.add(res.getString(R.string.group_folder));
2228 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002229
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002230 ArrayList<ShortcutIconResource> shortcutIcons =
2231 new ArrayList<ShortcutIconResource>();
2232 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2233 R.drawable.ic_launcher_folder));
2234 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2235
2236 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2237 pickIntent.putExtra(Intent.EXTRA_INTENT,
2238 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2239 pickIntent.putExtra(Intent.EXTRA_TITLE,
2240 getText(R.string.title_select_live_folder));
2241 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002242
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002243 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2244 break;
2245 }
2246
2247 case AddAdapter.ITEM_WALLPAPER: {
2248 startWallpaper();
2249 break;
2250 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 }
2252 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002253
2254 public void onShow(DialogInterface dialog) {
2255 mWorkspace.lock();
2256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 }
2258
2259 /**
2260 * Receives notifications when applications are added/removed.
2261 */
2262 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2263 @Override
2264 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002265 final String action = intent.getAction();
2266 final String packageName = intent.getData().getSchemeSpecificPart();
2267 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2268
2269 if (LauncherModel.DEBUG_LOADERS) {
2270 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2271 ", replacing=" + replacing);
2272 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002274
2275 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2276 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2277 if (!replacing) {
2278 removeShortcutsForPackage(packageName);
2279 if (LauncherModel.DEBUG_LOADERS) {
2280 d(LauncherModel.LOG_TAG, " --> remove package");
2281 }
2282 sModel.removePackage(Launcher.this, packageName);
2283 }
2284 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2285 // later, we will update the package at this time
2286 } else {
2287 if (!replacing) {
2288 if (LauncherModel.DEBUG_LOADERS) {
2289 d(LauncherModel.LOG_TAG, " --> add package");
2290 }
2291 sModel.addPackage(Launcher.this, packageName);
2292 } else {
2293 if (LauncherModel.DEBUG_LOADERS) {
2294 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2295 }
2296 sModel.updatePackage(Launcher.this, packageName);
2297 updateShortcutsForPackage(packageName);
2298 }
2299 }
2300 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002302 if (LauncherModel.DEBUG_LOADERS) {
2303 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2304 }
2305 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002306 }
2307 }
2308 }
2309
2310 /**
2311 * Receives notifications whenever the user favorites have changed.
2312 */
2313 private class FavoritesChangeObserver extends ContentObserver {
2314 public FavoritesChangeObserver() {
2315 super(new Handler());
2316 }
2317
2318 @Override
2319 public void onChange(boolean selfChange) {
2320 onFavoritesChanged();
2321 }
2322 }
2323
2324 /**
2325 * Receives intents from other applications to change the wallpaper.
2326 */
2327 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2328 private final Application mApplication;
2329 private WeakReference<Launcher> mLauncher;
2330
2331 WallpaperIntentReceiver(Application application, Launcher launcher) {
2332 mApplication = application;
2333 setLauncher(launcher);
2334 }
2335
2336 void setLauncher(Launcher launcher) {
2337 mLauncher = new WeakReference<Launcher>(launcher);
2338 }
2339
2340 @Override
2341 public void onReceive(Context context, Intent intent) {
2342 // Load the wallpaper from the ApplicationContext and store it locally
2343 // until the Launcher Activity is ready to use it
2344 final Drawable drawable = mApplication.getWallpaper();
2345 if (drawable instanceof BitmapDrawable) {
2346 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2347 } else {
2348 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2349 }
2350
2351 // If Launcher is alive, notify we have a new wallpaper
2352 if (mLauncher != null) {
2353 final Launcher launcher = mLauncher.get();
2354 if (launcher != null) {
2355 launcher.loadWallpaper();
2356 }
2357 }
2358 }
2359 }
2360
2361 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2362 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2363 private boolean mOpen;
2364
2365 public void onDrawerOpened() {
2366 if (!mOpen) {
2367 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002370 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371
2372 mOpen = true;
2373 }
2374 }
2375
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002376 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2377 view.getDrawingRect(bounds);
2378
2379 while (view != mDragLayer) {
2380 bounds.offset(view.getLeft(), view.getTop());
2381 view = (View) view.getParent();
2382 }
2383 }
2384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 public void onDrawerClosed() {
2386 if (mOpen) {
2387 mHandleIcon.reverseTransition(150);
2388 mWorkspace.mDrawerBounds.setEmpty();
2389 mOpen = false;
2390 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002392 mAllAppsGrid.setSelection(0);
2393 mAllAppsGrid.clearTextFilter();
2394 }
2395
2396 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002397 if (PROFILE_DRAWER) {
2398 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2399 }
2400
2401 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2402 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 }
2404
2405 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002406 if (PROFILE_DRAWER) {
2407 android.os.Debug.stopMethodTracing();
2408 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 }
2410 }
2411
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002412 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002414 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002415 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002416
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 // Number of items to bind in every pass
2418 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419
2420 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002421 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002422 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002424
Karl Rosaen138a0412009-04-23 19:00:21 -07002425 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426
2427 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002428 ArrayList<LauncherAppWidgetInfo> appWidgets,
2429 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002430
2431 mLauncher = new WeakReference<Launcher>(launcher);
2432 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002433 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002434
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002435 // Sort widgets so active workspace is bound first
2436 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2437 final int size = appWidgets.size();
2438 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002439
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002440 for (int i = 0; i < size; i++) {
2441 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2442 if (appWidgetInfo.screen == currentScreen) {
2443 mAppWidgets.addFirst(appWidgetInfo);
2444 } else {
2445 mAppWidgets.addLast(appWidgetInfo);
2446 }
2447 }
2448 }
Romain Guycbb89e42009-06-08 15:52:54 -07002449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 public void startBindingItems() {
2451 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2452 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002453
2454 public void startBindingDrawer() {
2455 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2456 }
Romain Guycbb89e42009-06-08 15:52:54 -07002457
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002458 public void startBindingAppWidgetsWhenIdle() {
2459 // Ask for notification when message queue becomes idle
2460 final MessageQueue messageQueue = Looper.myQueue();
2461 messageQueue.addIdleHandler(this);
2462 }
Romain Guycbb89e42009-06-08 15:52:54 -07002463
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002464 public boolean queueIdle() {
2465 // Queue is idle, so start binding items
2466 startBindingAppWidgets();
2467 return false;
2468 }
2469
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002470 public void startBindingAppWidgets() {
2471 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2472 }
2473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 @Override
2475 public void handleMessage(Message msg) {
2476 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002477 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 return;
2479 }
Romain Guycbb89e42009-06-08 15:52:54 -07002480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002481 switch (msg.what) {
2482 case MESSAGE_BIND_ITEMS: {
2483 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2484 break;
2485 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002486 case MESSAGE_BIND_DRAWER: {
2487 launcher.bindDrawer(this, mDrawerAdapter);
2488 break;
2489 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002490 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002491 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 break;
2493 }
2494 }
2495 }
2496 }
Romain Guy73b979d2009-06-09 12:57:21 -07002497
2498 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2499 GestureOverlayView.OnGesturePerformedListener {
2500
2501 private final GestureMatcher mMatcher = new GestureMatcher();
2502
2503 GesturesProcessor() {
2504 // TODO: Maybe the load should happen on a background thread?
2505 sLibrary.load();
2506 }
2507
2508 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002509 //noinspection PointlessBooleanExpression,ConstantConditions
2510 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2511 overlay.removeCallbacks(mMatcher);
2512 resetGesturesNextPrompt();
2513 }
Romain Guy73b979d2009-06-09 12:57:21 -07002514
2515 mGesturesAdd.setAlpha(128);
2516 mGesturesAdd.setEnabled(false);
2517 }
2518
2519 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2520 }
2521
2522 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2523 }
2524
2525 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002526 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2527 mMatcher.gesture = overlay.getGesture();
2528 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2529 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002530 if (mGesturesAction.intent != null) {
2531 mGesturesAction.intent = null;
2532 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2533 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002534 } else {
2535 mMatcher.run();
2536 }
Romain Guy73b979d2009-06-09 12:57:21 -07002537 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002538 overlay.removeCallbacks(mMatcher);
2539
2540 mMatcher.gesture = overlay.getGesture();
2541 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2542 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002543 if (mGesturesAction.intent != null) {
2544 mGesturesAction.intent = null;
2545 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2546 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002547 } else {
2548 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2549 }
Romain Guy73b979d2009-06-09 12:57:21 -07002550 }
2551 }
2552
Romain Guy3cf604f2009-06-16 13:12:53 -07002553 void matchGesture(Gesture gesture) {
2554 matchGesture(gesture, true);
2555 }
2556
2557 void matchGesture(Gesture gesture, boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07002558 mGesturesAdd.setAlpha(255);
2559 mGesturesAdd.setEnabled(true);
2560
2561 if (gesture != null) {
2562 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2563
2564 if (DEBUG_GESTURES) {
2565 for (Prediction p : predictions) {
2566 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2567 }
2568 }
2569
2570 boolean match = false;
2571 if (predictions.size() > 0) {
2572 final Prediction prediction = predictions.get(0);
2573 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2574 match = true;
2575
2576 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2577 if (info != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002578 updatePrompt(info, animate);
Romain Guy73b979d2009-06-09 12:57:21 -07002579 }
2580 }
2581 }
2582
2583 if (!match){
Romain Guy1ce1a242009-06-23 17:34:54 -07002584 mGesturesAction.intent = null;
Romain Guy3cf604f2009-06-16 13:12:53 -07002585 if (animate) {
2586 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2587 } else {
2588 setGesturesPrompt(null, getString(R.string.gestures_unknown));
2589 }
Romain Guy73b979d2009-06-09 12:57:21 -07002590 }
2591 }
2592 }
2593
2594 private void updatePrompt(ApplicationInfo info) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002595 updatePrompt(info, true);
2596 }
2597
2598 private void updatePrompt(ApplicationInfo info, boolean animate) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002599 if (mGesturesAction.intent != null &&
Romain Guy1ce1a242009-06-23 17:34:54 -07002600 info.intent.toUri(0).equals(mGesturesAction.intent.toUri(0)) &&
Romain Guy6fefcf12009-06-11 13:07:43 -07002601 info.title.equals(((TextView) mGesturesPrompt.getCurrentView()).getText())) {
2602 return;
2603 }
Romain Guy3cf604f2009-06-16 13:12:53 -07002604
2605 if (animate) {
2606 setGesturesNextPrompt(info.icon, info.title);
2607 } else {
2608 setGesturesPrompt(info.icon, info.title);
2609 }
2610
Romain Guy73b979d2009-06-09 12:57:21 -07002611 mGesturesAction.intent = info.intent;
2612 }
2613
2614 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002615 //noinspection PointlessBooleanExpression,ConstantConditions
2616 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2617 overlay.removeCallbacks(mMatcher);
2618 }
Romain Guy73b979d2009-06-09 12:57:21 -07002619 }
2620
2621 void addGesture(String name, Gesture gesture) {
2622 sLibrary.addGesture(name, gesture);
2623 // TODO: On a background thread?
2624 sLibrary.save();
2625 }
2626
2627 void update(ApplicationInfo info, Gesture gesture) {
2628 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002629 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002630 }
2631
2632 class GestureMatcher implements Runnable {
2633 Gesture gesture;
2634
2635 public void run() {
2636 if (gesture != null) {
2637 matchGesture(gesture);
2638 }
2639 }
2640 }
2641 }
2642
2643 private class GesturesAction implements View.OnClickListener {
2644 Intent intent;
2645
2646 public void onClick(View v) {
2647 if (intent != null) {
2648 startActivitySafely(intent);
2649 }
2650 }
2651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002652}
Karl Rosaen138a0412009-04-23 19:00:21 -07002653