blob: cb511bc8c922bb7fdc5d8b95149cf1ab89a04c73 [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);
1247 if (searchManager.isVisible()) {
1248 searchManager.stopSearch();
1249 }
1250 // Restore search widget to its normal position
1251 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1252 if (searchWidget != null) {
1253 searchWidget.stopSearch(false);
1254 }
1255 }
Romain Guycbb89e42009-06-08 15:52:54 -07001256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 @Override
1258 public boolean onCreateOptionsMenu(Menu menu) {
1259 if (mDesktopLocked) return false;
1260
1261 super.onCreateOptionsMenu(menu);
1262 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1263 .setIcon(android.R.drawable.ic_menu_add)
1264 .setAlphabeticShortcut('A');
1265 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1266 .setIcon(android.R.drawable.ic_menu_gallery)
1267 .setAlphabeticShortcut('W');
1268 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1269 .setIcon(android.R.drawable.ic_search_category_default)
1270 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1271 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1272 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1273 .setAlphabeticShortcut('N');
1274
Romain Guy73b979d2009-06-09 12:57:21 -07001275 final Intent gestures = new Intent(this, GesturesActivity.class);
1276 menu.add(0, MENU_GESTURES, 0, R.string.menu_gestures)
1277 .setIcon(com.android.internal.R.drawable.ic_menu_compose).setAlphabeticShortcut('G')
1278 .setIntent(gestures);
1279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001281 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1282 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283
1284 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1285 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1286 .setIntent(settings);
1287
1288 return true;
1289 }
1290
1291 @Override
1292 public boolean onPrepareOptionsMenu(Menu menu) {
1293 super.onPrepareOptionsMenu(menu);
1294
1295 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1296 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1297
1298 return true;
1299 }
1300
1301 @Override
1302 public boolean onOptionsItemSelected(MenuItem item) {
1303 switch (item.getItemId()) {
1304 case MENU_ADD:
1305 addItems();
1306 return true;
1307 case MENU_WALLPAPER_SETTINGS:
1308 startWallpaper();
1309 return true;
1310 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001311 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 return true;
1313 case MENU_NOTIFICATIONS:
1314 showNotifications();
1315 return true;
1316 }
1317
1318 return super.onOptionsItemSelected(item);
1319 }
Romain Guycbb89e42009-06-08 15:52:54 -07001320
Karl Rosaen138a0412009-04-23 19:00:21 -07001321 /**
1322 * Indicates that we want global search for this activity by setting the globalSearch
1323 * argument for {@link #startSearch} to true.
1324 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001326 @Override
1327 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001328 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001329 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001330 }
1331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 private void addItems() {
1333 showAddDialog(mMenuAddInfo);
1334 }
1335
1336 private void removeShortcutsForPackage(String packageName) {
1337 if (packageName != null && packageName.length() > 0) {
1338 mWorkspace.removeShortcutsForPackage(packageName);
1339 }
1340 }
Romain Guycbb89e42009-06-08 15:52:54 -07001341
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001342 private void updateShortcutsForPackage(String packageName) {
1343 if (packageName != null && packageName.length() > 0) {
1344 mWorkspace.updateShortcutsForPackage(packageName);
1345 }
1346 }
1347
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001348 void addAppWidget(Intent data) {
1349 // TODO: catch bad widget exception when sent
1350 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001352 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1353 if (SEARCH_WIDGET.equals(customWidget)) {
1354 // We don't need this any more, since this isn't a real app widget.
1355 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1356 // add the search widget
1357 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001359 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1360
1361 if (appWidget.configure != null) {
1362 // Launch over to configure widget, if needed
1363 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1364 intent.setComponent(appWidget.configure);
1365 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1366
1367 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1368 } else {
1369 // Otherwise just add it
1370 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1371 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 }
1373 }
Romain Guycbb89e42009-06-08 15:52:54 -07001374
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 void addSearch() {
1376 final Widget info = Widget.makeSearch();
1377 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001378
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 final int[] xy = mCellCoordinates;
1380 final int spanX = info.spanX;
1381 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001382
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 sModel.addDesktopItem(info);
1386 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1387 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 final View view = mInflater.inflate(info.layoutResource, null);
1390 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001391 Search search = (Search) view.findViewById(R.id.widget_search);
1392 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001393
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1395 }
1396
Romain Guy73b979d2009-06-09 12:57:21 -07001397 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001398 // Handle case where user selected "Applications"
1399 String applicationName = getResources().getString(R.string.group_applications);
1400 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001401
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001402 if (applicationName != null && applicationName.equals(shortcutName)) {
1403 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1404 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001405
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001406 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1407 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001408 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001409 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001410 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 }
1413
1414 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001415 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001416 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001417 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001418
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001419 if (folderName != null && folderName.equals(shortcutName)) {
1420 addFolder(!mDesktopLocked);
1421 } else {
1422 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1423 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 }
1425
1426 void addFolder(boolean insertAtFirst) {
1427 UserFolderInfo folderInfo = new UserFolderInfo();
1428 folderInfo.title = getText(R.string.folder_name);
1429
1430 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1431 cellInfo.screen = mWorkspace.getCurrentScreen();
1432 if (!findSingleSlot(cellInfo)) return;
1433
1434 // Update the model
1435 LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1436 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1437 sModel.addDesktopItem(folderInfo);
1438 sModel.addFolder(folderInfo);
1439
1440 // Create the view
1441 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1442 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1443 mWorkspace.addInCurrentScreen(newFolder,
1444 cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1445 }
Romain Guycbb89e42009-06-08 15:52:54 -07001446
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
1448 boolean insertAtFirst) {
1449 cellInfo.screen = mWorkspace.getCurrentScreen();
1450 if (!findSingleSlot(cellInfo)) return;
1451
1452 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1453
1454 if (!mRestoring) {
1455 sModel.addDesktopItem(info);
1456
1457 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1458 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1459 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
1460 } else if (sModel.isDesktopLoaded()) {
1461 sModel.addDesktopItem(info);
1462 }
1463 }
1464
1465 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1466 CellLayout.CellInfo cellInfo, boolean notify) {
1467
1468 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1469 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1470
1471 Drawable icon = null;
1472 boolean filtered = false;
1473 Intent.ShortcutIconResource iconResource = null;
1474
1475 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1476 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1477 try {
1478 iconResource = (Intent.ShortcutIconResource) extra;
1479 final PackageManager packageManager = context.getPackageManager();
1480 Resources resources = packageManager.getResourcesForApplication(
1481 iconResource.packageName);
1482 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1483 icon = resources.getDrawable(id);
1484 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001485 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 }
1487 }
1488
1489 if (icon == null) {
1490 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1491 }
1492
1493 final LiveFolderInfo info = new LiveFolderInfo();
1494 info.icon = icon;
1495 info.filtered = filtered;
1496 info.title = name;
1497 info.iconResource = iconResource;
1498 info.uri = data.getData();
1499 info.baseIntent = baseIntent;
1500 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1501 LiveFolders.DISPLAY_MODE_GRID);
1502
1503 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1504 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1505 sModel.addFolder(info);
1506
1507 return info;
1508 }
1509
1510 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1511 final int[] xy = new int[2];
1512 if (findSlot(cellInfo, xy, 1, 1)) {
1513 cellInfo.cellX = xy[0];
1514 cellInfo.cellY = xy[1];
1515 return true;
1516 }
1517 return false;
1518 }
1519
1520 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1521 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1522 boolean[] occupied = mSavedState != null ?
1523 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1524 cellInfo = mWorkspace.findAllVacantCells(occupied);
1525 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1526 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1527 return false;
1528 }
1529 }
1530 return true;
1531 }
1532
1533 private void showNotifications() {
1534 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1535 if (statusBar != null) {
1536 statusBar.expand();
1537 }
1538 }
1539
1540 private void startWallpaper() {
1541 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1542 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1543 }
1544
1545 /**
1546 * Registers various intent receivers. The current implementation registers
1547 * only a wallpaper intent receiver to let other applications change the
1548 * wallpaper.
1549 */
1550 private void registerIntentReceivers() {
1551 if (sWallpaperReceiver == null) {
1552 final Application application = getApplication();
1553
1554 sWallpaperReceiver = new WallpaperIntentReceiver(application, this);
1555
1556 IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
1557 application.registerReceiver(sWallpaperReceiver, filter);
1558 } else {
1559 sWallpaperReceiver.setLauncher(this);
1560 }
1561
1562 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
1563 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1564 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1565 filter.addDataScheme("package");
1566 registerReceiver(mApplicationsReceiver, filter);
1567 }
1568
1569 /**
1570 * Registers various content observers. The current implementation registers
1571 * only a favorites observer to keep track of the favorites applications.
1572 */
1573 private void registerContentObservers() {
1574 ContentResolver resolver = getContentResolver();
1575 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver);
1576 }
1577
1578 @Override
1579 public boolean dispatchKeyEvent(KeyEvent event) {
1580 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1581 switch (event.getKeyCode()) {
1582 case KeyEvent.KEYCODE_BACK:
Romain Guycbb89e42009-06-08 15:52:54 -07001583 mWorkspace.dispatchKeyEvent(event);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001584 if (mDrawer.isOpened()) {
1585 closeDrawer();
1586 } else {
Romain Guycbb89e42009-06-08 15:52:54 -07001587 closeFolder();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001588 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 return true;
1590 case KeyEvent.KEYCODE_HOME:
1591 return true;
1592 }
1593 }
1594
1595 return super.dispatchKeyEvent(event);
1596 }
1597
1598 private void closeDrawer() {
1599 closeDrawer(true);
1600 }
1601
1602 private void closeDrawer(boolean animated) {
1603 if (mDrawer.isOpened()) {
1604 if (animated) {
1605 mDrawer.animateClose();
1606 } else {
1607 mDrawer.close();
1608 }
1609 if (mDrawer.hasFocus()) {
1610 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1611 }
1612 }
1613 }
1614
1615 private void closeFolder() {
1616 Folder folder = mWorkspace.getOpenFolder();
1617 if (folder != null) {
1618 closeFolder(folder);
1619 }
1620 }
1621
1622 void closeFolder(Folder folder) {
1623 folder.getInfo().opened = false;
1624 ViewGroup parent = (ViewGroup) folder.getParent();
1625 if (parent != null) {
1626 parent.removeView(folder);
1627 }
1628 folder.onClose();
1629 }
1630
1631 /**
1632 * When the notification that favorites have changed is received, requests
1633 * a favorites list refresh.
1634 */
1635 private void onFavoritesChanged() {
1636 mDesktopLocked = true;
1637 mDrawer.lock();
1638 sModel.loadUserItems(false, this, false, false);
1639 }
1640
1641 void onDesktopItemsLoaded() {
1642 if (mDestroyed) return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 bindDesktopItems();
1644 }
Romain Guycbb89e42009-06-08 15:52:54 -07001645
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 /**
1647 * Refreshes the shortcuts shown on the workspace.
1648 */
1649 private void bindDesktopItems() {
1650 final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001651 final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001652 final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
1653 if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 return;
1655 }
1656
1657 final Workspace workspace = mWorkspace;
1658 int count = workspace.getChildCount();
1659 for (int i = 0; i < count; i++) {
1660 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1661 }
Romain Guycbb89e42009-06-08 15:52:54 -07001662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 if (DEBUG_USER_INTERFACE) {
1664 android.widget.Button finishButton = new android.widget.Button(this);
1665 finishButton.setText("Finish");
1666 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1667
1668 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1669 public void onClick(View v) {
1670 finish();
1671 }
1672 });
1673 }
Romain Guycbb89e42009-06-08 15:52:54 -07001674
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001675 // Flag any old binder to terminate early
1676 if (mBinder != null) {
1677 mBinder.mTerminate = true;
1678 }
Romain Guycbb89e42009-06-08 15:52:54 -07001679
Karl Rosaen138a0412009-04-23 19:00:21 -07001680 mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07001681 mBinder.startBindingItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 }
1683
1684 private void bindItems(Launcher.DesktopBinder binder,
1685 ArrayList<ItemInfo> shortcuts, int start, int count) {
1686
1687 final Workspace workspace = mWorkspace;
1688 final boolean desktopLocked = mDesktopLocked;
1689
1690 final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count);
1691 int i = start;
1692
1693 for ( ; i < end; i++) {
1694 final ItemInfo item = shortcuts.get(i);
1695 switch (item.itemType) {
1696 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1697 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1698 final View shortcut = createShortcut((ApplicationInfo) item);
1699 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1700 !desktopLocked);
1701 break;
1702 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1703 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1704 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1705 (UserFolderInfo) item);
1706 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1707 !desktopLocked);
1708 break;
1709 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1710 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1711 R.layout.live_folder_icon, this,
1712 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1713 (LiveFolderInfo) item);
1714 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1715 !desktopLocked);
1716 break;
1717 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1718 final int screen = workspace.getCurrentScreen();
1719 final View view = mInflater.inflate(R.layout.widget_search,
1720 (ViewGroup) workspace.getChildAt(screen), false);
Romain Guycbb89e42009-06-08 15:52:54 -07001721
Karl Rosaen138a0412009-04-23 19:00:21 -07001722 Search search = (Search) view.findViewById(R.id.widget_search);
1723 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001724
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 final Widget widget = (Widget) item;
1726 view.setTag(widget);
Romain Guycbb89e42009-06-08 15:52:54 -07001727
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 workspace.addWidget(view, widget, !desktopLocked);
1729 break;
1730 }
1731 }
1732
1733 workspace.requestLayout();
1734
1735 if (end >= count) {
1736 finishBindDesktopItems();
Karl Rosaen138a0412009-04-23 19:00:21 -07001737 binder.startBindingDrawer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 } else {
1739 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget();
1740 }
1741 }
1742
1743 private void finishBindDesktopItems() {
1744 if (mSavedState != null) {
1745 if (!mWorkspace.hasFocus()) {
1746 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1747 }
1748
1749 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1750 if (userFolders != null) {
1751 for (long folderId : userFolders) {
1752 final FolderInfo info = sModel.findFolderById(folderId);
1753 if (info != null) {
1754 openFolder(info);
1755 }
1756 }
1757 final Folder openFolder = mWorkspace.getOpenFolder();
1758 if (openFolder != null) {
1759 openFolder.requestFocus();
1760 }
1761 }
1762
1763 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1764 if (allApps) {
1765 mDrawer.open();
1766 }
1767
1768 mSavedState = null;
1769 }
1770
1771 if (mSavedInstanceState != null) {
1772 super.onRestoreInstanceState(mSavedInstanceState);
1773 mSavedInstanceState = null;
1774 }
1775
1776 if (mDrawer.isOpened() && !mDrawer.hasFocus()) {
1777 mDrawer.requestFocus();
1778 }
1779
1780 mDesktopLocked = false;
1781 mDrawer.unlock();
1782 }
Romain Guycbb89e42009-06-08 15:52:54 -07001783
Karl Rosaen138a0412009-04-23 19:00:21 -07001784 private void bindDrawer(Launcher.DesktopBinder binder,
1785 ApplicationsAdapter drawerAdapter) {
1786 mAllAppsGrid.setAdapter(drawerAdapter);
1787 binder.startBindingAppWidgetsWhenIdle();
1788 }
Romain Guycbb89e42009-06-08 15:52:54 -07001789
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001790 private void bindAppWidgets(Launcher.DesktopBinder binder,
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001791 LinkedList<LauncherAppWidgetInfo> appWidgets) {
Romain Guycbb89e42009-06-08 15:52:54 -07001792
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 final Workspace workspace = mWorkspace;
1794 final boolean desktopLocked = mDesktopLocked;
1795
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001796 if (!appWidgets.isEmpty()) {
1797 final LauncherAppWidgetInfo item = appWidgets.removeFirst();
Romain Guycbb89e42009-06-08 15:52:54 -07001798
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001799 final int appWidgetId = item.appWidgetId;
Karl Rosaen138a0412009-04-23 19:00:21 -07001800 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001801 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001802
Karl Rosaen138a0412009-04-23 19:00:21 -07001803 if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
Romain Guycbb89e42009-06-08 15:52:54 -07001804
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001805 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1806 item.hostView.setTag(item);
Romain Guycbb89e42009-06-08 15:52:54 -07001807
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001808 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1809 item.cellY, item.spanX, item.spanY, !desktopLocked);
Romain Guycbb89e42009-06-08 15:52:54 -07001810
Jeffrey Sharkeyd18299f2009-03-24 18:11:49 -07001811 workspace.requestLayout();
1812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001814 if (appWidgets.isEmpty()) {
1815 if (PROFILE_ROTATE) {
1816 android.os.Debug.stopMethodTracing();
1817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 } else {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001819 binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 }
1821 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 DragController getDragController() {
1824 return mDragLayer;
1825 }
1826
1827 /**
1828 * Launches the intent referred by the clicked shortcut.
1829 *
1830 * @param v The view representing the clicked shortcut.
1831 */
1832 public void onClick(View v) {
1833 Object tag = v.getTag();
1834 if (tag instanceof ApplicationInfo) {
1835 // Open shortcut
1836 final Intent intent = ((ApplicationInfo) tag).intent;
1837 startActivitySafely(intent);
1838 } else if (tag instanceof FolderInfo) {
1839 handleFolderClick((FolderInfo) tag);
1840 }
1841 }
1842
1843 void startActivitySafely(Intent intent) {
Romain Guyaad5ef42009-06-10 02:48:37 -07001844 mHideGesturesPanel = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1846 try {
1847 startActivity(intent);
1848 } catch (ActivityNotFoundException e) {
1849 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1850 } catch (SecurityException e) {
1851 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001852 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1854 "or use the exported attribute for this activity.", e);
1855 }
1856 }
1857
1858 private void handleFolderClick(FolderInfo folderInfo) {
1859 if (!folderInfo.opened) {
1860 // Close any open folder
1861 closeFolder();
1862 // Open the requested folder
1863 openFolder(folderInfo);
1864 } else {
1865 // Find the open folder...
1866 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1867 int folderScreen;
1868 if (openFolder != null) {
1869 folderScreen = mWorkspace.getScreenForView(openFolder);
1870 // .. and close it
1871 closeFolder(openFolder);
1872 if (folderScreen != mWorkspace.getCurrentScreen()) {
1873 // Close any folder open on the current screen
1874 closeFolder();
1875 // Pull the folder onto this screen
1876 openFolder(folderInfo);
1877 }
1878 }
1879 }
1880 }
1881
1882 private void loadWallpaper() {
1883 // The first time the application is started, we load the wallpaper from
1884 // the ApplicationContext
1885 if (sWallpaper == null) {
1886 final Drawable drawable = getWallpaper();
1887 if (drawable instanceof BitmapDrawable) {
1888 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
1889 } else {
1890 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
1891 }
1892 }
1893 mWorkspace.loadWallpaper(sWallpaper);
1894 }
1895
1896 /**
1897 * Opens the user fodler described by the specified tag. The opening of the folder
1898 * is animated relative to the specified View. If the View is null, no animation
1899 * is played.
1900 *
1901 * @param folderInfo The FolderInfo describing the folder to open.
1902 */
1903 private void openFolder(FolderInfo folderInfo) {
1904 Folder openFolder;
1905
1906 if (folderInfo instanceof UserFolderInfo) {
1907 openFolder = UserFolder.fromXml(this);
1908 } else if (folderInfo instanceof LiveFolderInfo) {
1909 openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo);
1910 } else {
1911 return;
1912 }
1913
1914 openFolder.setDragger(mDragLayer);
1915 openFolder.setLauncher(this);
1916
1917 openFolder.bind(folderInfo);
1918 folderInfo.opened = true;
1919
1920 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1921 openFolder.onOpen();
1922 }
1923
1924 /**
1925 * Returns true if the workspace is being loaded. When the workspace is loading,
1926 * no user interaction should be allowed to avoid any conflict.
1927 *
1928 * @return True if the workspace is locked, false otherwise.
1929 */
1930 boolean isWorkspaceLocked() {
1931 return mDesktopLocked;
1932 }
1933
1934 public boolean onLongClick(View v) {
1935 if (mDesktopLocked) {
1936 return false;
1937 }
1938
1939 if (!(v instanceof CellLayout)) {
1940 v = (View) v.getParent();
1941 }
1942
1943 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1944
1945 // This happens when long clicking an item with the dpad/trackball
1946 if (cellInfo == null) {
1947 return true;
1948 }
1949
1950 if (mWorkspace.allowLongPress()) {
1951 if (cellInfo.cell == null) {
1952 if (cellInfo.valid) {
1953 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001954 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 showAddDialog(cellInfo);
1956 }
1957 } else {
1958 if (!(cellInfo.cell instanceof Folder)) {
1959 // User long pressed on an item
1960 mWorkspace.startDrag(cellInfo);
1961 }
1962 }
1963 }
1964 return true;
1965 }
1966
1967 static LauncherModel getModel() {
1968 return sModel;
1969 }
1970
Romain Guy73b979d2009-06-09 12:57:21 -07001971 static GestureLibrary getGestureLibrary() {
1972 return sLibrary;
1973 }
1974
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 void closeAllApplications() {
1976 mDrawer.close();
1977 }
1978
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001979 View getDrawerHandle() {
1980 return mHandleView;
1981 }
1982
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 boolean isDrawerDown() {
1984 return !mDrawer.isMoving() && !mDrawer.isOpened();
1985 }
1986
1987 boolean isDrawerUp() {
1988 return mDrawer.isOpened() && !mDrawer.isMoving();
1989 }
1990
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001991 boolean isDrawerMoving() {
1992 return mDrawer.isMoving();
1993 }
1994
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 Workspace getWorkspace() {
1996 return mWorkspace;
1997 }
1998
1999 GridView getApplicationsGrid() {
2000 return mAllAppsGrid;
2001 }
2002
2003 @Override
2004 protected Dialog onCreateDialog(int id) {
2005 switch (id) {
2006 case DIALOG_CREATE_SHORTCUT:
2007 return new CreateShortcut().createDialog();
2008 case DIALOG_RENAME_FOLDER:
2009 return new RenameFolder().createDialog();
2010 }
2011
2012 return super.onCreateDialog(id);
2013 }
2014
2015 @Override
2016 protected void onPrepareDialog(int id, Dialog dialog) {
2017 switch (id) {
2018 case DIALOG_CREATE_SHORTCUT:
2019 mWorkspace.lock();
2020 break;
2021 case DIALOG_RENAME_FOLDER:
2022 mWorkspace.lock();
2023 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2024 final CharSequence text = mFolderInfo.title;
2025 input.setText(text);
Romain Guycbb89e42009-06-08 15:52:54 -07002026 input.setSelection(0, text.length());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 break;
2028 }
2029 }
2030
2031 void showRenameDialog(FolderInfo info) {
2032 mFolderInfo = info;
2033 mWaitingForResult = true;
2034 showDialog(DIALOG_RENAME_FOLDER);
2035 }
2036
2037 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2038 mAddItemCellInfo = cellInfo;
2039 mWaitingForResult = true;
2040 showDialog(DIALOG_CREATE_SHORTCUT);
2041 }
2042
Romain Guy73b979d2009-06-09 12:57:21 -07002043 private void pickShortcut(int requestCode, int title) {
2044 Bundle bundle = new Bundle();
2045
2046 ArrayList<String> shortcutNames = new ArrayList<String>();
2047 shortcutNames.add(getString(R.string.group_applications));
2048 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2049
2050 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2051 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2052 R.drawable.ic_launcher_application));
2053 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2054
2055 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2056 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
2057 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
2058 pickIntent.putExtras(bundle);
2059
2060 startActivityForResult(pickIntent, requestCode);
2061 }
2062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 private class RenameFolder {
2064 private EditText mInput;
2065
2066 Dialog createDialog() {
2067 mWaitingForResult = true;
2068 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2069 mInput = (EditText) layout.findViewById(R.id.folder_name);
2070
2071 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2072 builder.setIcon(0);
2073 builder.setTitle(getString(R.string.rename_folder_title));
2074 builder.setCancelable(true);
2075 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2076 public void onCancel(DialogInterface dialog) {
2077 cleanup();
2078 }
2079 });
2080 builder.setNegativeButton(getString(R.string.cancel_action),
2081 new Dialog.OnClickListener() {
2082 public void onClick(DialogInterface dialog, int which) {
2083 cleanup();
2084 }
2085 }
2086 );
2087 builder.setPositiveButton(getString(R.string.rename_action),
2088 new Dialog.OnClickListener() {
2089 public void onClick(DialogInterface dialog, int which) {
2090 changeFolderName();
2091 }
2092 }
2093 );
2094 builder.setView(layout);
2095 return builder.create();
2096 }
2097
2098 private void changeFolderName() {
2099 final String name = mInput.getText().toString();
2100 if (!TextUtils.isEmpty(name)) {
2101 // Make sure we have the right folder info
2102 mFolderInfo = sModel.findFolderById(mFolderInfo.id);
2103 mFolderInfo.title = name;
2104 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2105
2106 if (mDesktopLocked) {
2107 mDrawer.lock();
2108 sModel.loadUserItems(false, Launcher.this, false, false);
2109 } else {
2110 final FolderIcon folderIcon = (FolderIcon)
2111 mWorkspace.getViewForTag(mFolderInfo);
2112 if (folderIcon != null) {
2113 folderIcon.setText(name);
2114 getWorkspace().requestLayout();
2115 } else {
2116 mDesktopLocked = true;
2117 mDrawer.lock();
2118 sModel.loadUserItems(false, Launcher.this, false, false);
2119 }
2120 }
2121 }
2122 cleanup();
2123 }
2124
2125 private void cleanup() {
2126 mWorkspace.unlock();
2127 dismissDialog(DIALOG_RENAME_FOLDER);
2128 mWaitingForResult = false;
2129 mFolderInfo = null;
2130 }
2131 }
2132
2133 /**
2134 * Displays the shortcut creation dialog and launches, if necessary, the
2135 * appropriate activity.
2136 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002137 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guycbb89e42009-06-08 15:52:54 -07002138 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 Dialog createDialog() {
2142 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07002143
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2147 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002148 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 builder.setInverseBackgroundForced(true);
2151
2152 AlertDialog dialog = builder.create();
2153 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002154 dialog.setOnDismissListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 return dialog;
2157 }
2158
2159 public void onCancel(DialogInterface dialog) {
2160 mWaitingForResult = false;
2161 cleanup();
2162 }
2163
Romain Guycbb89e42009-06-08 15:52:54 -07002164 public void onDismiss(DialogInterface dialog) {
2165 mWorkspace.unlock();
2166 }
2167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 private void cleanup() {
2169 mWorkspace.unlock();
2170 dismissDialog(DIALOG_CREATE_SHORTCUT);
2171 }
2172
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002173 /**
2174 * Handle the action clicked in the "Add to home" dialog.
2175 */
2176 public void onClick(DialogInterface dialog, int which) {
2177 Resources res = getResources();
2178 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002179
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002180 switch (which) {
2181 case AddAdapter.ITEM_SHORTCUT: {
2182 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002183 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002184 break;
2185 }
Romain Guycbb89e42009-06-08 15:52:54 -07002186
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002187 case AddAdapter.ITEM_APPWIDGET: {
2188 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002189
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002190 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2191 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2192 // add the search widget
2193 ArrayList<AppWidgetProviderInfo> customInfo =
2194 new ArrayList<AppWidgetProviderInfo>();
2195 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
2196 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
2197 info.label = getString(R.string.group_search);
2198 info.icon = R.drawable.ic_search_widget;
2199 customInfo.add(info);
2200 pickIntent.putParcelableArrayListExtra(
2201 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
2202 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
2203 Bundle b = new Bundle();
2204 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
2205 customExtras.add(b);
2206 pickIntent.putParcelableArrayListExtra(
2207 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
2208 // start the pick activity
2209 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2210 break;
2211 }
Romain Guycbb89e42009-06-08 15:52:54 -07002212
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002213 case AddAdapter.ITEM_LIVE_FOLDER: {
2214 // Insert extra item to handle inserting folder
2215 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002216
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002217 ArrayList<String> shortcutNames = new ArrayList<String>();
2218 shortcutNames.add(res.getString(R.string.group_folder));
2219 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002220
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002221 ArrayList<ShortcutIconResource> shortcutIcons =
2222 new ArrayList<ShortcutIconResource>();
2223 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2224 R.drawable.ic_launcher_folder));
2225 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2226
2227 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2228 pickIntent.putExtra(Intent.EXTRA_INTENT,
2229 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2230 pickIntent.putExtra(Intent.EXTRA_TITLE,
2231 getText(R.string.title_select_live_folder));
2232 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002233
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002234 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2235 break;
2236 }
2237
2238 case AddAdapter.ITEM_WALLPAPER: {
2239 startWallpaper();
2240 break;
2241 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 }
2243 }
2244 }
2245
2246 /**
2247 * Receives notifications when applications are added/removed.
2248 */
2249 private class ApplicationsIntentReceiver extends BroadcastReceiver {
2250 @Override
2251 public void onReceive(Context context, Intent intent) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002252 final String action = intent.getAction();
2253 final String packageName = intent.getData().getSchemeSpecificPart();
2254 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
2255
2256 if (LauncherModel.DEBUG_LOADERS) {
2257 d(LauncherModel.LOG_TAG, "application intent received: " + action +
2258 ", replacing=" + replacing);
2259 d(LauncherModel.LOG_TAG, " --> " + intent.getData());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002261
2262 if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
2263 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
2264 if (!replacing) {
2265 removeShortcutsForPackage(packageName);
2266 if (LauncherModel.DEBUG_LOADERS) {
2267 d(LauncherModel.LOG_TAG, " --> remove package");
2268 }
2269 sModel.removePackage(Launcher.this, packageName);
2270 }
2271 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
2272 // later, we will update the package at this time
2273 } else {
2274 if (!replacing) {
2275 if (LauncherModel.DEBUG_LOADERS) {
2276 d(LauncherModel.LOG_TAG, " --> add package");
2277 }
2278 sModel.addPackage(Launcher.this, packageName);
2279 } else {
2280 if (LauncherModel.DEBUG_LOADERS) {
2281 d(LauncherModel.LOG_TAG, " --> update package " + packageName);
2282 }
2283 sModel.updatePackage(Launcher.this, packageName);
2284 updateShortcutsForPackage(packageName);
2285 }
2286 }
2287 removeDialog(DIALOG_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002289 if (LauncherModel.DEBUG_LOADERS) {
2290 d(LauncherModel.LOG_TAG, " --> sync package " + packageName);
2291 }
2292 sModel.syncPackage(Launcher.this, packageName);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 }
2294 }
2295 }
2296
2297 /**
2298 * Receives notifications whenever the user favorites have changed.
2299 */
2300 private class FavoritesChangeObserver extends ContentObserver {
2301 public FavoritesChangeObserver() {
2302 super(new Handler());
2303 }
2304
2305 @Override
2306 public void onChange(boolean selfChange) {
2307 onFavoritesChanged();
2308 }
2309 }
2310
2311 /**
2312 * Receives intents from other applications to change the wallpaper.
2313 */
2314 private static class WallpaperIntentReceiver extends BroadcastReceiver {
2315 private final Application mApplication;
2316 private WeakReference<Launcher> mLauncher;
2317
2318 WallpaperIntentReceiver(Application application, Launcher launcher) {
2319 mApplication = application;
2320 setLauncher(launcher);
2321 }
2322
2323 void setLauncher(Launcher launcher) {
2324 mLauncher = new WeakReference<Launcher>(launcher);
2325 }
2326
2327 @Override
2328 public void onReceive(Context context, Intent intent) {
2329 // Load the wallpaper from the ApplicationContext and store it locally
2330 // until the Launcher Activity is ready to use it
2331 final Drawable drawable = mApplication.getWallpaper();
2332 if (drawable instanceof BitmapDrawable) {
2333 sWallpaper = ((BitmapDrawable) drawable).getBitmap();
2334 } else {
2335 throw new IllegalStateException("The wallpaper must be a BitmapDrawable.");
2336 }
2337
2338 // If Launcher is alive, notify we have a new wallpaper
2339 if (mLauncher != null) {
2340 final Launcher launcher = mLauncher.get();
2341 if (launcher != null) {
2342 launcher.loadWallpaper();
2343 }
2344 }
2345 }
2346 }
2347
2348 private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
2349 SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener {
2350 private boolean mOpen;
2351
2352 public void onDrawerOpened() {
2353 if (!mOpen) {
2354 mHandleIcon.reverseTransition(150);
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 final Rect bounds = mWorkspace.mDrawerBounds;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002357 offsetBoundsToDragLayer(bounds, mAllAppsGrid);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358
2359 mOpen = true;
2360 }
2361 }
2362
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002363 private void offsetBoundsToDragLayer(Rect bounds, View view) {
2364 view.getDrawingRect(bounds);
2365
2366 while (view != mDragLayer) {
2367 bounds.offset(view.getLeft(), view.getTop());
2368 view = (View) view.getParent();
2369 }
2370 }
2371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 public void onDrawerClosed() {
2373 if (mOpen) {
2374 mHandleIcon.reverseTransition(150);
2375 mWorkspace.mDrawerBounds.setEmpty();
2376 mOpen = false;
2377 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002378
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002379 mAllAppsGrid.setSelection(0);
2380 mAllAppsGrid.clearTextFilter();
2381 }
2382
2383 public void onScrollStarted() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002384 if (PROFILE_DRAWER) {
2385 android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
2386 }
2387
2388 mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth();
2389 mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 }
2391
2392 public void onScrollEnded() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002393 if (PROFILE_DRAWER) {
2394 android.os.Debug.stopMethodTracing();
2395 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 }
2397 }
2398
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002399 private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 static final int MESSAGE_BIND_ITEMS = 0x1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002401 static final int MESSAGE_BIND_APPWIDGETS = 0x2;
Karl Rosaen138a0412009-04-23 19:00:21 -07002402 static final int MESSAGE_BIND_DRAWER = 0x3;
Romain Guycbb89e42009-06-08 15:52:54 -07002403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 // Number of items to bind in every pass
2405 static final int ITEMS_COUNT = 6;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406
2407 private final ArrayList<ItemInfo> mShortcuts;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002408 private final LinkedList<LauncherAppWidgetInfo> mAppWidgets;
Karl Rosaen138a0412009-04-23 19:00:21 -07002409 private final ApplicationsAdapter mDrawerAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 private final WeakReference<Launcher> mLauncher;
Romain Guycbb89e42009-06-08 15:52:54 -07002411
Karl Rosaen138a0412009-04-23 19:00:21 -07002412 public boolean mTerminate = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413
2414 DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts,
Karl Rosaen138a0412009-04-23 19:00:21 -07002415 ArrayList<LauncherAppWidgetInfo> appWidgets,
2416 ApplicationsAdapter drawerAdapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417
2418 mLauncher = new WeakReference<Launcher>(launcher);
2419 mShortcuts = shortcuts;
Karl Rosaen138a0412009-04-23 19:00:21 -07002420 mDrawerAdapter = drawerAdapter;
Romain Guycbb89e42009-06-08 15:52:54 -07002421
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002422 // Sort widgets so active workspace is bound first
2423 final int currentScreen = launcher.mWorkspace.getCurrentScreen();
2424 final int size = appWidgets.size();
2425 mAppWidgets = new LinkedList<LauncherAppWidgetInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -07002426
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002427 for (int i = 0; i < size; i++) {
2428 LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i);
2429 if (appWidgetInfo.screen == currentScreen) {
2430 mAppWidgets.addFirst(appWidgetInfo);
2431 } else {
2432 mAppWidgets.addLast(appWidgetInfo);
2433 }
2434 }
2435 }
Romain Guycbb89e42009-06-08 15:52:54 -07002436
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 public void startBindingItems() {
2438 obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
2439 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002440
2441 public void startBindingDrawer() {
2442 obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget();
2443 }
Romain Guycbb89e42009-06-08 15:52:54 -07002444
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002445 public void startBindingAppWidgetsWhenIdle() {
2446 // Ask for notification when message queue becomes idle
2447 final MessageQueue messageQueue = Looper.myQueue();
2448 messageQueue.addIdleHandler(this);
2449 }
Romain Guycbb89e42009-06-08 15:52:54 -07002450
Jeffrey Sharkey2d132af2009-03-24 21:21:09 -07002451 public boolean queueIdle() {
2452 // Queue is idle, so start binding items
2453 startBindingAppWidgets();
2454 return false;
2455 }
2456
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002457 public void startBindingAppWidgets() {
2458 obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget();
2459 }
2460
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002461 @Override
2462 public void handleMessage(Message msg) {
2463 Launcher launcher = mLauncher.get();
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002464 if (launcher == null || mTerminate) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 return;
2466 }
Romain Guycbb89e42009-06-08 15:52:54 -07002467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 switch (msg.what) {
2469 case MESSAGE_BIND_ITEMS: {
2470 launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
2471 break;
2472 }
Karl Rosaen138a0412009-04-23 19:00:21 -07002473 case MESSAGE_BIND_DRAWER: {
2474 launcher.bindDrawer(this, mDrawerAdapter);
2475 break;
2476 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002477 case MESSAGE_BIND_APPWIDGETS: {
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002478 launcher.bindAppWidgets(this, mAppWidgets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002479 break;
2480 }
2481 }
2482 }
2483 }
Romain Guy73b979d2009-06-09 12:57:21 -07002484
2485 private class GesturesProcessor implements GestureOverlayView.OnGestureListener,
2486 GestureOverlayView.OnGesturePerformedListener {
2487
2488 private final GestureMatcher mMatcher = new GestureMatcher();
2489
2490 GesturesProcessor() {
2491 // TODO: Maybe the load should happen on a background thread?
2492 sLibrary.load();
2493 }
2494
2495 public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002496 //noinspection PointlessBooleanExpression,ConstantConditions
2497 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2498 overlay.removeCallbacks(mMatcher);
2499 resetGesturesNextPrompt();
2500 }
Romain Guy73b979d2009-06-09 12:57:21 -07002501
2502 mGesturesAdd.setAlpha(128);
2503 mGesturesAdd.setEnabled(false);
2504 }
2505
2506 public void onGesture(GestureOverlayView overlay, MotionEvent event) {
2507 }
2508
2509 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
2510 }
2511
2512 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002513 if (CONFIG_GESTURES_IMMEDIATE_MODE) {
2514 mMatcher.gesture = overlay.getGesture();
2515 if (mMatcher.gesture.getLength() < GesturesConstants.LENGTH_THRESHOLD) {
2516 overlay.clear(false);
Romain Guy1ce1a242009-06-23 17:34:54 -07002517 if (mGesturesAction.intent != null) {
2518 mGesturesAction.intent = null;
2519 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2520 }
Romain Guy6fefcf12009-06-11 13:07:43 -07002521 } else {
2522 mMatcher.run();
2523 }
Romain Guy73b979d2009-06-09 12:57:21 -07002524 } else {
Romain Guy6fefcf12009-06-11 13:07:43 -07002525 overlay.removeCallbacks(mMatcher);
2526
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 overlay.postDelayed(mMatcher, GesturesConstants.MATCH_DELAY);
2536 }
Romain Guy73b979d2009-06-09 12:57:21 -07002537 }
2538 }
2539
Romain Guy3cf604f2009-06-16 13:12:53 -07002540 void matchGesture(Gesture gesture) {
2541 matchGesture(gesture, true);
2542 }
2543
2544 void matchGesture(Gesture gesture, boolean animate) {
Romain Guy73b979d2009-06-09 12:57:21 -07002545 mGesturesAdd.setAlpha(255);
2546 mGesturesAdd.setEnabled(true);
2547
2548 if (gesture != null) {
2549 final ArrayList<Prediction> predictions = sLibrary.recognize(gesture);
2550
2551 if (DEBUG_GESTURES) {
2552 for (Prediction p : predictions) {
2553 d(LOG_TAG, String.format("name=%s, score=%f", p.name, p.score));
2554 }
2555 }
2556
2557 boolean match = false;
2558 if (predictions.size() > 0) {
2559 final Prediction prediction = predictions.get(0);
2560 if (prediction.score > GesturesConstants.PREDICTION_THRESHOLD) {
2561 match = true;
2562
2563 ApplicationInfo info = sModel.queryGesture(Launcher.this, prediction.name);
2564 if (info != null) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002565 updatePrompt(info, animate);
Romain Guy73b979d2009-06-09 12:57:21 -07002566 }
2567 }
2568 }
2569
2570 if (!match){
Romain Guy1ce1a242009-06-23 17:34:54 -07002571 mGesturesAction.intent = null;
Romain Guy3cf604f2009-06-16 13:12:53 -07002572 if (animate) {
2573 setGesturesNextPrompt(null, getString(R.string.gestures_unknown));
2574 } else {
2575 setGesturesPrompt(null, getString(R.string.gestures_unknown));
2576 }
Romain Guy73b979d2009-06-09 12:57:21 -07002577 }
2578 }
2579 }
2580
2581 private void updatePrompt(ApplicationInfo info) {
Romain Guy3cf604f2009-06-16 13:12:53 -07002582 updatePrompt(info, true);
2583 }
2584
2585 private void updatePrompt(ApplicationInfo info, boolean animate) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002586 if (mGesturesAction.intent != null &&
Romain Guy1ce1a242009-06-23 17:34:54 -07002587 info.intent.toUri(0).equals(mGesturesAction.intent.toUri(0)) &&
Romain Guy6fefcf12009-06-11 13:07:43 -07002588 info.title.equals(((TextView) mGesturesPrompt.getCurrentView()).getText())) {
2589 return;
2590 }
Romain Guy3cf604f2009-06-16 13:12:53 -07002591
2592 if (animate) {
2593 setGesturesNextPrompt(info.icon, info.title);
2594 } else {
2595 setGesturesPrompt(info.icon, info.title);
2596 }
2597
Romain Guy73b979d2009-06-09 12:57:21 -07002598 mGesturesAction.intent = info.intent;
2599 }
2600
2601 public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Romain Guy6fefcf12009-06-11 13:07:43 -07002602 //noinspection PointlessBooleanExpression,ConstantConditions
2603 if (!CONFIG_GESTURES_IMMEDIATE_MODE) {
2604 overlay.removeCallbacks(mMatcher);
2605 }
Romain Guy73b979d2009-06-09 12:57:21 -07002606 }
2607
2608 void addGesture(String name, Gesture gesture) {
2609 sLibrary.addGesture(name, gesture);
2610 // TODO: On a background thread?
2611 sLibrary.save();
2612 }
2613
2614 void update(ApplicationInfo info, Gesture gesture) {
2615 mGesturesOverlay.setGesture(gesture);
Romain Guyb8734242009-06-10 11:53:57 -07002616 updatePrompt(info);
Romain Guy73b979d2009-06-09 12:57:21 -07002617 }
2618
2619 class GestureMatcher implements Runnable {
2620 Gesture gesture;
2621
2622 public void run() {
2623 if (gesture != null) {
2624 matchGesture(gesture);
2625 }
2626 }
2627 }
2628 }
2629
2630 private class GesturesAction implements View.OnClickListener {
2631 Intent intent;
2632
2633 public void onClick(View v) {
2634 if (intent != null) {
2635 startActivitySafely(intent);
2636 }
2637 }
2638 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002639}
Karl Rosaen138a0412009-04-23 19:00:21 -07002640