blob: 7be2a1bcc75bba58fed0670fe84d9dbfcad558fd [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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
20import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070022import android.app.ISearchManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.SearchManager;
24import android.app.StatusBarManager;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070025import android.app.WallpaperInfo;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
30import android.content.DialogInterface;
31import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070032import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ActivityInfo;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070034import android.content.pm.LabeledIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.PackageManager;
36import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070038import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.graphics.drawable.Drawable;
41import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Bundle;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070043import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070044import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070045import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.text.Selection;
51import android.text.SpannableStringBuilder;
52import android.text.TextUtils;
53import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070054import static android.util.Log.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070055import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.KeyEvent;
58import android.view.LayoutInflater;
59import android.view.Menu;
60import android.view.MenuItem;
61import android.view.View;
62import android.view.ViewGroup;
Romain Guyb1b69f52009-08-10 15:10:15 -070063import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.view.View.OnLongClickListener;
65import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.widget.TextView;
68import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070069import android.appwidget.AppWidgetManager;
70import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071
72import java.lang.ref.WeakReference;
73import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074import java.util.HashMap;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070075import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070076import java.io.DataOutputStream;
77import java.io.FileNotFoundException;
78import java.io.IOException;
79import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
81/**
82 * Default launcher application.
83 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040084public final class Launcher extends Activity
Joe Onorato85a02a82009-09-08 12:34:22 -070085 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
86 SwipeController.SwipeListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040088 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 static final boolean LOGD = false;
90
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 static final boolean PROFILE_STARTUP = false;
92 static final boolean PROFILE_ROTATE = false;
93 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070094
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 private static final int WALLPAPER_SCREENS_SPAN = 2;
96
97 private static final int MENU_GROUP_ADD = 1;
98 private static final int MENU_ADD = Menu.FIRST + 1;
99 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
100 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
101 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700102 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103
104 private static final int REQUEST_CREATE_SHORTCUT = 1;
105 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700106 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 private static final int REQUEST_PICK_APPLICATION = 6;
108 private static final int REQUEST_PICK_SHORTCUT = 7;
109 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700110 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Joe Onorato85a02a82009-09-08 12:34:22 -0700112 private static final int MODE_WORKSPACE = 0;
113 private static final int MODE_ALL_APPS = 1;
114 private static final int MODE_ALL_APPS_ZOOMED = 2;
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
117
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700118 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
119 static final String SEARCH_WIDGET = "search_widget";
120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 static final int SCREEN_COUNT = 3;
122 static final int DEFAULT_SCREN = 1;
123 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700124 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Joe Onorato7c312c12009-08-13 21:36:53 -0700126 static final int DIALOG_CREATE_SHORTCUT = 1;
127 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Romain Guy98d01652009-06-30 16:21:04 -0700129 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
131 // Type: int
132 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
133 // Type: boolean
134 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
135 // Type: long
136 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
149 // Type: int
150 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
151 // Type: int[]
152 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
153 // Type: boolean
154 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
155 // Type: long
156 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
157
Joe Onorato9c1289c2009-08-17 11:03:03 -0400158 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 private static final Object sLock = new Object();
161 private static int sScreen = DEFAULT_SCREN;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private LayoutInflater mInflater;
164
Joe Onorato00acb122009-08-04 16:04:30 -0400165 private DragController mDragController;
Joe Onorato85a02a82009-09-08 12:34:22 -0700166 private SwipeController mSwipeController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private AppWidgetManager mAppWidgetManager;
170 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private CellLayout.CellInfo mAddItemCellInfo;
173 private CellLayout.CellInfo mMenuAddInfo;
174 private final int[] mCellCoordinates = new int[2];
175 private FolderInfo mFolderInfo;
176
Joe Onorato7c312c12009-08-13 21:36:53 -0700177 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700179 private AllAppsView mAllAppsGrid;
Joe Onorato85a02a82009-09-08 12:34:22 -0700180 private int mMode = MODE_WORKSPACE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private Bundle mSavedState;
183
184 private SpannableStringBuilder mDefaultKeySsb = null;
185
Mike LeBeau736cf282009-07-02 17:46:59 -0700186 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 private boolean mWorkspaceLoading = true;
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private boolean mRestoring;
191 private boolean mWaitingForResult;
192 private boolean mLocaleChanged;
193
Dianne Hackborn67800862009-07-24 17:15:20 -0700194 private boolean mHomeDown;
195 private boolean mBackDown;
196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private Bundle mSavedInstanceState;
198
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 private LauncherModel mModel;
200
201 private ArrayList<ItemInfo> mDesktopItems = new ArrayList();
202 private static HashMap<Long, FolderInfo> mFolders = new HashMap();
203 private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList();
Romain Guycbb89e42009-06-08 15:52:54 -0700204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 @Override
206 protected void onCreate(Bundle savedInstanceState) {
207 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700211
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700213
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
215 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 if (PROFILE_STARTUP) {
218 android.os.Debug.startMethodTracing("/sdcard/launcher");
219 }
220
221 checkForLocaleChange();
222 setWallpaperDimension();
223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 setContentView(R.layout.launcher);
225 setupViews();
226
Joe Onorato7c312c12009-08-13 21:36:53 -0700227 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 mSavedState = savedInstanceState;
230 restoreState(mSavedState);
231
232 if (PROFILE_STARTUP) {
233 android.os.Debug.stopMethodTracing();
234 }
235
236 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400237 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238 }
239
240 // For handling default keys
241 mDefaultKeySsb = new SpannableStringBuilder();
242 Selection.setSelection(mDefaultKeySsb, 0);
243 }
Romain Guycbb89e42009-06-08 15:52:54 -0700244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700246 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
247 readConfiguration(this, localeConfiguration);
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 final Configuration configuration = getResources().getConfiguration();
250
Romain Guy98d01652009-06-30 16:21:04 -0700251 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 final String locale = configuration.locale.toString();
253
Romain Guy98d01652009-06-30 16:21:04 -0700254 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 final int mcc = configuration.mcc;
256
Romain Guy98d01652009-06-30 16:21:04 -0700257 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 final int mnc = configuration.mnc;
259
260 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
261
262 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700263 localeConfiguration.locale = locale;
264 localeConfiguration.mcc = mcc;
265 localeConfiguration.mnc = mnc;
266
267 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700269 }
270 }
271
272 private static class LocaleConfiguration {
273 public String locale;
274 public int mcc = -1;
275 public int mnc = -1;
276 }
277
278 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
279 DataInputStream in = null;
280 try {
281 in = new DataInputStream(context.openFileInput(PREFERENCES));
282 configuration.locale = in.readUTF();
283 configuration.mcc = in.readInt();
284 configuration.mnc = in.readInt();
285 } catch (FileNotFoundException e) {
286 // Ignore
287 } catch (IOException e) {
288 // Ignore
289 } finally {
290 if (in != null) {
291 try {
292 in.close();
293 } catch (IOException e) {
294 // Ignore
295 }
296 }
297 }
298 }
299
300 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
301 DataOutputStream out = null;
302 try {
303 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
304 out.writeUTF(configuration.locale);
305 out.writeInt(configuration.mcc);
306 out.writeInt(configuration.mnc);
307 out.flush();
308 } catch (FileNotFoundException e) {
309 // Ignore
310 } catch (IOException e) {
311 //noinspection ResultOfMethodCallIgnored
312 context.getFileStreamPath(PREFERENCES).delete();
313 } finally {
314 if (out != null) {
315 try {
316 out.close();
317 } catch (IOException e) {
318 // Ignore
319 }
320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
322 }
323
324 static int getScreen() {
325 synchronized (sLock) {
326 return sScreen;
327 }
328 }
329
330 static void setScreen(int screen) {
331 synchronized (sLock) {
332 sScreen = screen;
333 }
334 }
335
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700337 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338
339 Display display = getWindowManager().getDefaultDisplay();
340 boolean isPortrait = display.getWidth() < display.getHeight();
341
342 final int width = isPortrait ? display.getWidth() : display.getHeight();
343 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700344 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 }
346
347 @Override
348 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700349 mWaitingForResult = false;
350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 // The pattern used here is that a user PICKs a specific application,
352 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
355 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700356
Romain Guy94dabf12009-07-21 10:55:43 -0700357 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 switch (requestCode) {
359 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400360 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 break;
362 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700363 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 break;
365 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 break;
368 case REQUEST_PICK_LIVE_FOLDER:
369 addLiveFolder(data);
370 break;
371 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400372 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700374 case REQUEST_PICK_APPWIDGET:
375 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700377 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 break;
380 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700381 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700383 // Clean up the appWidgetId if we canceled
384 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
385 if (appWidgetId != -1) {
386 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 }
388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 }
390
391 @Override
392 protected void onResume() {
393 super.onResume();
394
395 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400396 mWorkspaceLoading = true;
397 mModel.startLoader(this, true);
398 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700400
401 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
402 // onNewIntent), then close the search dialog if needed, because it probably
403 // came from the user pressing 'home' (rather than, for example, pressing 'back').
404 if (mIsNewIntent) {
405 // Post to a handler so that this happens after the search dialog tries to open
406 // itself again.
407 mWorkspace.post(new Runnable() {
408 public void run() {
409 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
410 ServiceManager.getService(Context.SEARCH_SERVICE));
411 try {
412 searchManagerService.stopSearch();
413 } catch (RemoteException e) {
414 e(LOG_TAG, "error stopping search", e);
415 }
416 }
417 });
418 }
419
420 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 }
422
423 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700424 protected void onPause() {
425 super.onPause();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400426 closeAllApps(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 }
Romain Guycbb89e42009-06-08 15:52:54 -0700428
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700429 @Override
430 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400431 // Flag the loader to stop early before switching
432 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700433
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700434 if (PROFILE_ROTATE) {
435 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
436 }
437 return null;
438 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 private boolean acceptFilter() {
441 final InputMethodManager inputManager = (InputMethodManager)
442 getSystemService(Context.INPUT_METHOD_SERVICE);
443 return !inputManager.isFullscreenMode();
444 }
445
446 @Override
447 public boolean onKeyDown(int keyCode, KeyEvent event) {
448 boolean handled = super.onKeyDown(keyCode, event);
449 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
450 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
451 keyCode, event);
452 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700453 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700454 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700455 // showSearchDialog()
456 // If there are multiple keystrokes before the search dialog takes focus,
457 // onSearchRequested() will be called for every keystroke,
458 // but it is idempotent, so it's fine.
459 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461 }
462
463 return handled;
464 }
465
Karl Rosaen138a0412009-04-23 19:00:21 -0700466 private String getTypedText() {
467 return mDefaultKeySsb.toString();
468 }
469
470 private void clearTypedText() {
471 mDefaultKeySsb.clear();
472 mDefaultKeySsb.clearSpans();
473 Selection.setSelection(mDefaultKeySsb, 0);
474 }
475
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 /**
477 * Restores the previous state, if it exists.
478 *
479 * @param savedState The previous state.
480 */
481 private void restoreState(Bundle savedState) {
482 if (savedState == null) {
483 return;
484 }
485
486 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
487 if (currentScreen > -1) {
488 mWorkspace.setCurrentScreen(currentScreen);
489 }
490
491 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
492 if (addScreen > -1) {
493 mAddItemCellInfo = new CellLayout.CellInfo();
494 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
495 addItemCellInfo.valid = true;
496 addItemCellInfo.screen = addScreen;
497 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
498 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
499 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
500 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
501 addItemCellInfo.findVacantCellsFromOccupied(
502 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
503 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
504 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
505 mRestoring = true;
506 }
507
508 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
509 if (renameFolder) {
510 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400511 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 mRestoring = true;
513 }
514 }
515
516 /**
517 * Finds all the views we need and configure them properly.
518 */
519 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400520 mDragController = new DragController(this);
521 DragController dragController = mDragController;
Joe Onorato85a02a82009-09-08 12:34:22 -0700522 mSwipeController = new SwipeController(this, this);
523 SwipeController swipeController = mSwipeController;
524 swipeController.setRange(-1, 0);
Joe Onorato00acb122009-08-04 16:04:30 -0400525
Romain Guyb1b69f52009-08-10 15:10:15 -0700526 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400527 dragLayer.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700528 dragLayer.setSwipeController(swipeController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529
Joe Onorato7c312c12009-08-13 21:36:53 -0700530 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700531 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700532 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700533 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Joe Onorato7c312c12009-08-13 21:36:53 -0700534
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
536 final Workspace workspace = mWorkspace;
537
Joe Onorato7c312c12009-08-13 21:36:53 -0700538 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
539 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540
Joe Onorato7c312c12009-08-13 21:36:53 -0700541 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700542 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700543 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700544 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700545 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700546 handleIocon.setCrossFadeEnabled(true);
547 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400550 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552
553 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400554 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700555 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556
Joe Onorato00acb122009-08-04 16:04:30 -0400557 dragController.setDragScoller(workspace);
558 dragController.setDragListener(deleteZone);
559 dragController.setScrollView(dragLayer);
560
561 // The order here is bottom to top.
562 dragController.addDropTarget(workspace);
563 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 }
565
566 /**
567 * Creates a view representing a shortcut.
568 *
569 * @param info The data structure describing the shortcut.
570 *
571 * @return A View inflated from R.layout.application.
572 */
573 View createShortcut(ApplicationInfo info) {
574 return createShortcut(R.layout.application,
575 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
576 }
577
578 /**
579 * Creates a view representing a shortcut inflated from the specified resource.
580 *
581 * @param layoutResId The id of the XML layout used to create the shortcut.
582 * @param parent The group the shortcut belongs to.
583 * @param info The data structure describing the shortcut.
584 *
585 * @return A View inflated from layoutResId.
586 */
587 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
588 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
589
Joe Onorato5162ea92009-09-03 09:39:42 -0700590 if (info.icon == null) {
591 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700594 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 info.filtered = true;
596 }
597
598 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
599 favorite.setText(info.title);
600 favorite.setTag(info);
601 favorite.setOnClickListener(this);
602
603 return favorite;
604 }
605
606 /**
607 * Add an application shortcut to the workspace.
608 *
609 * @param data The intent describing the application.
610 * @param cellInfo The position on screen where to create the shortcut.
611 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400612 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 cellInfo.screen = mWorkspace.getCurrentScreen();
614 if (!findSingleSlot(cellInfo)) return;
615
Romain Guy73b979d2009-06-09 12:57:21 -0700616 final ApplicationInfo info = infoFromApplicationIntent(context, data);
617 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400618 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700619 }
620 }
621
622 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 ComponentName component = data.getComponent();
624 PackageManager packageManager = context.getPackageManager();
625 ActivityInfo activityInfo = null;
626 try {
627 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
628 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700629 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 }
Romain Guycbb89e42009-06-08 15:52:54 -0700631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 if (activityInfo != null) {
633 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 itemInfo.title = activityInfo.loadLabel(packageManager);
636 if (itemInfo.title == null) {
637 itemInfo.title = activityInfo.name;
638 }
Romain Guycbb89e42009-06-08 15:52:54 -0700639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
641 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
642 itemInfo.icon = activityInfo.loadIcon(packageManager);
643 itemInfo.container = ItemInfo.NO_ID;
644
Romain Guy73b979d2009-06-09 12:57:21 -0700645 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
Romain Guy73b979d2009-06-09 12:57:21 -0700647
648 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
Romain Guycbb89e42009-06-08 15:52:54 -0700650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 /**
652 * Add a shortcut to the workspace.
653 *
654 * @param data The intent describing the shortcut.
655 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 cellInfo.screen = mWorkspace.getCurrentScreen();
659 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
662
663 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400665 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
666 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 }
668 }
669
Romain Guycbb89e42009-06-08 15:52:54 -0700670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700672 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700674 * @param data The intent describing the appWidgetId.
675 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400677 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700679 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700680
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700681 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700682
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700684
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700685 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700688
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 // Try finding open space on Launcher screen
690 final int[] xy = mCellCoordinates;
691 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
692
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700693 // Build Launcher-specific widget info and save to database
694 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 launcherInfo.spanX = spans[0];
696 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 LauncherModel.addItemToDatabase(this, launcherInfo,
699 LauncherSettings.Favorites.CONTAINER_DESKTOP,
700 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
701
702 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700707
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400712 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
714 }
Romain Guycbb89e42009-06-08 15:52:54 -0700715
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
717 mDesktopItems.remove(launcherInfo);
718 launcherInfo.hostView = null;
719 }
720
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700721 public LauncherAppWidgetHost getAppWidgetHost() {
722 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 }
Romain Guycbb89e42009-06-08 15:52:54 -0700724
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 static ApplicationInfo addShortcut(Context context, Intent data,
726 CellLayout.CellInfo cellInfo, boolean notify) {
727
Romain Guy73b979d2009-06-09 12:57:21 -0700728 final ApplicationInfo info = infoFromShortcutIntent(context, data);
729 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
730 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
731
732 return info;
733 }
734
735 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
737 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
738 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
739
740 Drawable icon = null;
741 boolean filtered = false;
742 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700743 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744
745 if (bitmap != null) {
746 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
747 filtered = true;
748 customIcon = true;
749 } else {
750 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700751 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700753 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 final PackageManager packageManager = context.getPackageManager();
755 Resources resources = packageManager.getResourcesForApplication(
756 iconResource.packageName);
757 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
758 icon = resources.getDrawable(id);
759 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700760 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 }
762 }
763 }
764
765 if (icon == null) {
766 icon = context.getPackageManager().getDefaultActivityIcon();
767 }
768
769 final ApplicationInfo info = new ApplicationInfo();
770 info.icon = icon;
771 info.filtered = filtered;
772 info.title = name;
773 info.intent = intent;
774 info.customIcon = customIcon;
775 info.iconResource = iconResource;
776
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 return info;
778 }
779
780 @Override
781 protected void onNewIntent(Intent intent) {
782 super.onNewIntent(intent);
783
784 // Close the menu
785 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
786 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700787
788 // Set this flag so that onResume knows to close the search dialog if it's open,
789 // because this was a new intent (thus a press of 'home' or some such) rather than
790 // for example onResume being called when the user pressed the 'back' button.
791 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792
793 try {
794 dismissDialog(DIALOG_CREATE_SHORTCUT);
795 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 } catch (Exception e) {
797 // An exception is thrown if the dialog is not visible, which is fine
798 }
799
800 try {
801 dismissDialog(DIALOG_RENAME_FOLDER);
802 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 } catch (Exception e) {
804 // An exception is thrown if the dialog is not visible, which is fine
805 }
806
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
808 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700809
Romain Guy94dabf12009-07-21 10:55:43 -0700810 if (!mWorkspace.isDefaultScreenShowing()) {
811 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700813
Joe Onoratofb0ca672009-09-14 17:55:46 -0400814 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700815
816 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 if (v != null && v.getWindowToken() != null) {
818 InputMethodManager imm = (InputMethodManager)getSystemService(
819 INPUT_METHOD_SERVICE);
820 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
821 }
822 } else {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400823 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 }
825 }
826 }
827
828 @Override
829 protected void onRestoreInstanceState(Bundle savedInstanceState) {
830 // Do not call super here
831 mSavedInstanceState = savedInstanceState;
832 }
833
834 @Override
835 protected void onSaveInstanceState(Bundle outState) {
836 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
837
838 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
839 if (folders.size() > 0) {
840 final int count = folders.size();
841 long[] ids = new long[count];
842 for (int i = 0; i < count; i++) {
843 final FolderInfo info = folders.get(i).getInfo();
844 ids[i] = info.id;
845 }
846 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
847 } else {
848 super.onSaveInstanceState(outState);
849 }
850
Romain Guy3cf604f2009-06-16 13:12:53 -0700851 final boolean isConfigurationChange = getChangingConfigurations() != 0;
852
Romain Guy5a941392009-04-28 15:18:25 -0700853 // When the drawer is opened and we are saving the state because of a
854 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400855 // TODO should not do this if the drawer is currently closing.
856 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700858 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859
860 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
861 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
862 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
863
864 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
865 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
866 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
867 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
868 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
869 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
870 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
871 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
872 layout.getOccupiedCells());
873 }
874
875 if (mFolderInfo != null && mWaitingForResult) {
876 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
877 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
878 }
879 }
880
881 @Override
882 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700884
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700886 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700888 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
890
891 TextKeyListener.getInstance().release();
892
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894
Joe Onorato9c1289c2009-08-17 11:03:03 -0400895 unbindDesktopItems();
896 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 }
898
899 @Override
900 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700901 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 super.startActivityForResult(intent, requestCode);
903 }
904
905 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700906 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700908
Joe Onoratofb0ca672009-09-14 17:55:46 -0400909 closeAllApps(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700910
Karl Rosaen138a0412009-04-23 19:00:21 -0700911 // Slide the search widget to the top, if it's on the current screen,
912 // otherwise show the search dialog immediately.
913 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
914 if (searchWidget == null) {
915 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
916 } else {
917 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
918 // show the currently typed text in the search widget while sliding
919 searchWidget.setQuery(getTypedText());
920 }
921 }
Romain Guycbb89e42009-06-08 15:52:54 -0700922
Karl Rosaen138a0412009-04-23 19:00:21 -0700923 /**
924 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700925 *
926 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700927 */
Romain Guycbb89e42009-06-08 15:52:54 -0700928 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700929 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700930
Karl Rosaen138a0412009-04-23 19:00:21 -0700931 if (initialQuery == null) {
932 // Use any text typed in the launcher as the initial query
933 initialQuery = getTypedText();
934 clearTypedText();
935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 if (appSearchData == null) {
937 appSearchData = new Bundle();
938 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
939 }
Romain Guycbb89e42009-06-08 15:52:54 -0700940
Karl Rosaen138a0412009-04-23 19:00:21 -0700941 final SearchManager searchManager =
942 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
943
944 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
945 if (searchWidget != null) {
946 // This gets called when the user leaves the search dialog to go back to
947 // the Launcher.
948 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
949 public void onCancel() {
950 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700951 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700952 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700953 });
954 }
Romain Guycbb89e42009-06-08 15:52:54 -0700955
Karl Rosaen138a0412009-04-23 19:00:21 -0700956 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700957 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
959
Karl Rosaen138a0412009-04-23 19:00:21 -0700960 /**
961 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 */
Romain Guy5a941392009-04-28 15:18:25 -0700963 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700964 // Close search dialog
965 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100966 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 // Restore search widget to its normal position
968 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
969 if (searchWidget != null) {
970 searchWidget.stopSearch(false);
971 }
972 }
Romain Guycbb89e42009-06-08 15:52:54 -0700973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 @Override
975 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 if (isWorkspaceLocked()) {
977 return false;
978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979
980 super.onCreateOptionsMenu(menu);
981 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
982 .setIcon(android.R.drawable.ic_menu_add)
983 .setAlphabeticShortcut('A');
984 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
985 .setIcon(android.R.drawable.ic_menu_gallery)
986 .setAlphabeticShortcut('W');
987 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
988 .setIcon(android.R.drawable.ic_search_category_default)
989 .setAlphabeticShortcut(SearchManager.MENU_KEY);
990 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
991 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
992 .setAlphabeticShortcut('N');
993
994 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700995 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
996 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997
998 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
999 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1000 .setIntent(settings);
1001
1002 return true;
1003 }
1004
1005 @Override
1006 public boolean onPrepareOptionsMenu(Menu menu) {
1007 super.onPrepareOptionsMenu(menu);
1008
1009 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1010 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1011
1012 return true;
1013 }
1014
1015 @Override
1016 public boolean onOptionsItemSelected(MenuItem item) {
1017 switch (item.getItemId()) {
1018 case MENU_ADD:
1019 addItems();
1020 return true;
1021 case MENU_WALLPAPER_SETTINGS:
1022 startWallpaper();
1023 return true;
1024 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001025 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 return true;
1027 case MENU_NOTIFICATIONS:
1028 showNotifications();
1029 return true;
1030 }
1031
1032 return super.onOptionsItemSelected(item);
1033 }
Romain Guycbb89e42009-06-08 15:52:54 -07001034
Karl Rosaen138a0412009-04-23 19:00:21 -07001035 /**
1036 * Indicates that we want global search for this activity by setting the globalSearch
1037 * argument for {@link #startSearch} to true.
1038 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001040 @Override
1041 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001042 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001043 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001044 }
1045
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046 public boolean isWorkspaceLocked() {
1047 return mWorkspaceLoading || mWaitingForResult;
1048 }
1049
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 private void addItems() {
1051 showAddDialog(mMenuAddInfo);
1052 }
1053
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001054 void addAppWidget(Intent data) {
1055 // TODO: catch bad widget exception when sent
1056 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001058 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1059 if (SEARCH_WIDGET.equals(customWidget)) {
1060 // We don't need this any more, since this isn't a real app widget.
1061 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1062 // add the search widget
1063 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001065 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1066
1067 if (appWidget.configure != null) {
1068 // Launch over to configure widget, if needed
1069 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1070 intent.setComponent(appWidget.configure);
1071 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1072
1073 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1074 } else {
1075 // Otherwise just add it
1076 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 }
1079 }
Romain Guycbb89e42009-06-08 15:52:54 -07001080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 void addSearch() {
1082 final Widget info = Widget.makeSearch();
1083 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 final int[] xy = mCellCoordinates;
1086 final int spanX = info.spanX;
1087 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1092 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 final View view = mInflater.inflate(info.layoutResource, null);
1095 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001096 Search search = (Search) view.findViewById(R.id.widget_search);
1097 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1100 }
1101
Romain Guy73b979d2009-06-09 12:57:21 -07001102 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001103 // Handle case where user selected "Applications"
1104 String applicationName = getResources().getString(R.string.group_applications);
1105 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001106
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001107 if (applicationName != null && applicationName.equals(shortcutName)) {
1108 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1109 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001110
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001111 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1112 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001113 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001114 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001115 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
1118
1119 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001120 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001121 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001122 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001123
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001124 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001125 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001126 } else {
1127 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 }
1130
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 UserFolderInfo folderInfo = new UserFolderInfo();
1133 folderInfo.title = getText(R.string.folder_name);
1134
1135 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1136 cellInfo.screen = mWorkspace.getCurrentScreen();
1137 if (!findSingleSlot(cellInfo)) return;
1138
1139 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140 LauncherModel.addItemToDatabase(this, folderInfo,
1141 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144
1145 // Create the view
1146 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1147 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1148 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001149 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 }
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Joe Onorato9c1289c2009-08-17 11:03:03 -04001152 void removeFolder(FolderInfo folder) {
1153 mFolders.remove(folder.id);
1154 }
1155
1156 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 cellInfo.screen = mWorkspace.getCurrentScreen();
1158 if (!findSingleSlot(cellInfo)) return;
1159
1160 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1161
1162 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1164 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001165 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1166 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 }
1168 }
1169
1170 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1171 CellLayout.CellInfo cellInfo, boolean notify) {
1172
1173 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1174 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1175
1176 Drawable icon = null;
1177 boolean filtered = false;
1178 Intent.ShortcutIconResource iconResource = null;
1179
1180 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1181 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1182 try {
1183 iconResource = (Intent.ShortcutIconResource) extra;
1184 final PackageManager packageManager = context.getPackageManager();
1185 Resources resources = packageManager.getResourcesForApplication(
1186 iconResource.packageName);
1187 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1188 icon = resources.getDrawable(id);
1189 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001190 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 }
1192 }
1193
1194 if (icon == null) {
1195 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1196 }
1197
1198 final LiveFolderInfo info = new LiveFolderInfo();
1199 info.icon = icon;
1200 info.filtered = filtered;
1201 info.title = name;
1202 info.iconResource = iconResource;
1203 info.uri = data.getData();
1204 info.baseIntent = baseIntent;
1205 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1206 LiveFolders.DISPLAY_MODE_GRID);
1207
1208 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1209 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211
1212 return info;
1213 }
1214
1215 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1216 final int[] xy = new int[2];
1217 if (findSlot(cellInfo, xy, 1, 1)) {
1218 cellInfo.cellX = xy[0];
1219 cellInfo.cellY = xy[1];
1220 return true;
1221 }
1222 return false;
1223 }
1224
1225 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1226 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1227 boolean[] occupied = mSavedState != null ?
1228 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1229 cellInfo = mWorkspace.findAllVacantCells(occupied);
1230 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1231 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1232 return false;
1233 }
1234 }
1235 return true;
1236 }
1237
1238 private void showNotifications() {
1239 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1240 if (statusBar != null) {
1241 statusBar.expand();
1242 }
1243 }
1244
1245 private void startWallpaper() {
1246 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001247 Intent chooser = Intent.createChooser(pickWallpaper,
1248 getText(R.string.chooser_wallpaper));
1249 WallpaperManager wm = (WallpaperManager)
1250 getSystemService(Context.WALLPAPER_SERVICE);
1251 WallpaperInfo wi = wm.getWallpaperInfo();
1252 if (wi != null && wi.getSettingsActivity() != null) {
1253 LabeledIntent li = new LabeledIntent(getPackageName(),
1254 R.string.configure_wallpaper, 0);
1255 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1256 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1257 }
1258 startActivity(chooser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 }
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001262 public void onWindowFocusChanged(boolean hasFocus) {
1263 super.onWindowFocusChanged(hasFocus);
1264 if (!hasFocus) {
1265 mBackDown = mHomeDown = false;
1266 }
1267 }
1268
1269 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 public boolean dispatchKeyEvent(KeyEvent event) {
1271 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1272 switch (event.getKeyCode()) {
1273 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001274 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 return true;
1276 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001277 mHomeDown = true;
1278 return true;
1279 }
1280 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1281 switch (event.getKeyCode()) {
1282 case KeyEvent.KEYCODE_BACK:
1283 if (!event.isCanceled()) {
1284 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001285 if (isAllAppsVisible()) {
1286 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001287 } else {
1288 closeFolder();
1289 }
1290 }
1291 mBackDown = false;
1292 return true;
1293 case KeyEvent.KEYCODE_HOME:
1294 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 return true;
1296 }
1297 }
1298
1299 return super.dispatchKeyEvent(event);
1300 }
1301
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 private void closeFolder() {
1303 Folder folder = mWorkspace.getOpenFolder();
1304 if (folder != null) {
1305 closeFolder(folder);
1306 }
1307 }
1308
1309 void closeFolder(Folder folder) {
1310 folder.getInfo().opened = false;
1311 ViewGroup parent = (ViewGroup) folder.getParent();
1312 if (parent != null) {
1313 parent.removeView(folder);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314 // TODO: this line crashes.
1315 //mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
1317 folder.onClose();
1318 }
1319
1320 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001321 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1322 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001324 private void unbindDesktopItems() {
1325 for (ItemInfo item: mDesktopItems) {
1326 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 /**
1331 * Launches the intent referred by the clicked shortcut.
1332 *
1333 * @param v The view representing the clicked shortcut.
1334 */
1335 public void onClick(View v) {
1336 Object tag = v.getTag();
1337 if (tag instanceof ApplicationInfo) {
1338 // Open shortcut
1339 final Intent intent = ((ApplicationInfo) tag).intent;
1340 startActivitySafely(intent);
1341 } else if (tag instanceof FolderInfo) {
1342 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001343 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001344 Log.d(TAG, "onClick");
1345 if (isAllAppsVisible()) {
1346 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001347 } else {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001348 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001349 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 }
1351 }
1352
1353 void startActivitySafely(Intent intent) {
1354 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1355 try {
1356 startActivity(intent);
1357 } catch (ActivityNotFoundException e) {
1358 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1359 } catch (SecurityException e) {
1360 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001361 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1363 "or use the exported attribute for this activity.", e);
1364 }
1365 }
1366
1367 private void handleFolderClick(FolderInfo folderInfo) {
1368 if (!folderInfo.opened) {
1369 // Close any open folder
1370 closeFolder();
1371 // Open the requested folder
1372 openFolder(folderInfo);
1373 } else {
1374 // Find the open folder...
1375 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1376 int folderScreen;
1377 if (openFolder != null) {
1378 folderScreen = mWorkspace.getScreenForView(openFolder);
1379 // .. and close it
1380 closeFolder(openFolder);
1381 if (folderScreen != mWorkspace.getCurrentScreen()) {
1382 // Close any folder open on the current screen
1383 closeFolder();
1384 // Pull the folder onto this screen
1385 openFolder(folderInfo);
1386 }
1387 }
1388 }
1389 }
1390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 /**
1392 * Opens the user fodler described by the specified tag. The opening of the folder
1393 * is animated relative to the specified View. If the View is null, no animation
1394 * is played.
1395 *
1396 * @param folderInfo The FolderInfo describing the folder to open.
1397 */
1398 private void openFolder(FolderInfo folderInfo) {
1399 Folder openFolder;
1400
1401 if (folderInfo instanceof UserFolderInfo) {
1402 openFolder = UserFolder.fromXml(this);
1403 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001404 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 } else {
1406 return;
1407 }
1408
Joe Onorato00acb122009-08-04 16:04:30 -04001409 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 openFolder.setLauncher(this);
1411
1412 openFolder.bind(folderInfo);
1413 folderInfo.opened = true;
1414
1415 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1416 openFolder.onOpen();
1417 }
1418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001420 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 return false;
1422 }
1423
1424 if (!(v instanceof CellLayout)) {
1425 v = (View) v.getParent();
1426 }
1427
1428 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1429
1430 // This happens when long clicking an item with the dpad/trackball
1431 if (cellInfo == null) {
1432 return true;
1433 }
1434
1435 if (mWorkspace.allowLongPress()) {
Joe Onorato85a02a82009-09-08 12:34:22 -07001436 mSwipeController.cancelSwipe();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 if (cellInfo.cell == null) {
1438 if (cellInfo.valid) {
1439 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001440 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 showAddDialog(cellInfo);
1442 }
1443 } else {
1444 if (!(cellInfo.cell instanceof Folder)) {
1445 // User long pressed on an item
1446 mWorkspace.startDrag(cellInfo);
1447 }
1448 }
1449 }
1450 return true;
1451 }
1452
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001453 View getDrawerHandle() {
1454 return mHandleView;
1455 }
1456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 Workspace getWorkspace() {
1458 return mWorkspace;
1459 }
1460
Joe Onorato7404ee42009-07-31 11:54:44 -07001461 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 GridView getApplicationsGrid() {
1463 return mAllAppsGrid;
1464 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001465 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466
1467 @Override
1468 protected Dialog onCreateDialog(int id) {
1469 switch (id) {
1470 case DIALOG_CREATE_SHORTCUT:
1471 return new CreateShortcut().createDialog();
1472 case DIALOG_RENAME_FOLDER:
1473 return new RenameFolder().createDialog();
1474 }
1475
1476 return super.onCreateDialog(id);
1477 }
1478
1479 @Override
1480 protected void onPrepareDialog(int id, Dialog dialog) {
1481 switch (id) {
1482 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 break;
1484 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001485 if (mFolderInfo != null) {
1486 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1487 final CharSequence text = mFolderInfo.title;
1488 input.setText(text);
1489 input.setSelection(0, text.length());
1490 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 break;
1492 }
1493 }
1494
1495 void showRenameDialog(FolderInfo info) {
1496 mFolderInfo = info;
1497 mWaitingForResult = true;
1498 showDialog(DIALOG_RENAME_FOLDER);
1499 }
1500
1501 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1502 mAddItemCellInfo = cellInfo;
1503 mWaitingForResult = true;
1504 showDialog(DIALOG_CREATE_SHORTCUT);
1505 }
1506
Romain Guy73b979d2009-06-09 12:57:21 -07001507 private void pickShortcut(int requestCode, int title) {
1508 Bundle bundle = new Bundle();
1509
1510 ArrayList<String> shortcutNames = new ArrayList<String>();
1511 shortcutNames.add(getString(R.string.group_applications));
1512 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1513
1514 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1515 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1516 R.drawable.ic_launcher_application));
1517 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1518
1519 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1520 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1521 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1522 pickIntent.putExtras(bundle);
1523
1524 startActivityForResult(pickIntent, requestCode);
1525 }
1526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 private class RenameFolder {
1528 private EditText mInput;
1529
1530 Dialog createDialog() {
1531 mWaitingForResult = true;
1532 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1533 mInput = (EditText) layout.findViewById(R.id.folder_name);
1534
1535 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1536 builder.setIcon(0);
1537 builder.setTitle(getString(R.string.rename_folder_title));
1538 builder.setCancelable(true);
1539 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1540 public void onCancel(DialogInterface dialog) {
1541 cleanup();
1542 }
1543 });
1544 builder.setNegativeButton(getString(R.string.cancel_action),
1545 new Dialog.OnClickListener() {
1546 public void onClick(DialogInterface dialog, int which) {
1547 cleanup();
1548 }
1549 }
1550 );
1551 builder.setPositiveButton(getString(R.string.rename_action),
1552 new Dialog.OnClickListener() {
1553 public void onClick(DialogInterface dialog, int which) {
1554 changeFolderName();
1555 }
1556 }
1557 );
1558 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001559
1560 final AlertDialog dialog = builder.create();
1561 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1562 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001563 }
1564 });
1565
1566 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
1568
1569 private void changeFolderName() {
1570 final String name = mInput.getText().toString();
1571 if (!TextUtils.isEmpty(name)) {
1572 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001573 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 mFolderInfo.title = name;
1575 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1576
Joe Onorato9c1289c2009-08-17 11:03:03 -04001577 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001578 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001579 mModel.setWorkspaceDirty();
1580 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 } else {
1582 final FolderIcon folderIcon = (FolderIcon)
1583 mWorkspace.getViewForTag(mFolderInfo);
1584 if (folderIcon != null) {
1585 folderIcon.setText(name);
1586 getWorkspace().requestLayout();
1587 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001588 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001589 mModel.setWorkspaceDirty();
1590 mWorkspaceLoading = true;
1591 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
1593 }
1594 }
1595 cleanup();
1596 }
1597
1598 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 dismissDialog(DIALOG_RENAME_FOLDER);
1600 mWaitingForResult = false;
1601 mFolderInfo = null;
1602 }
1603 }
1604
Joe Onoratofb0ca672009-09-14 17:55:46 -04001605 boolean isAllAppsVisible() {
1606 return mAllAppsGrid.isZooming() || mAllAppsGrid.isVisible();
1607 }
1608
1609 void showAllApps(boolean animated) {
1610 if (mMode == MODE_ALL_APPS) {
1611 return;
1612 }
1613
1614 mSwipeController.setRange(-1, 0);
1615 if (animated) {
1616 mSwipeController.animate(-1);
1617 } else {
1618 mSwipeController.setImmediate(-1);
1619 }
1620
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001621 mWorkspace.hide();
Joe Onorato7c312c12009-08-13 21:36:53 -07001622
1623 // TODO: fade these two too
1624 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001625 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001626 }
1627
Joe Onoratofb0ca672009-09-14 17:55:46 -04001628 void closeAllApps(boolean animated) {
1629 if (mAllAppsGrid.isVisible()) {
1630 mSwipeController.setRange(0, 1);
1631 if (animated) {
1632 mSwipeController.animate(1);
1633 } else {
1634 mSwipeController.setImmediate(1);
1635 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001636 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001637
1638 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001639 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001640 mDeleteZone.setVisibility(View.VISIBLE);
1641 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001642 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001643 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001644 }
1645
Joe Onorato7c312c12009-08-13 21:36:53 -07001646 void lockAllApps() {
1647 // TODO
1648 }
1649
1650 void unlockAllApps() {
1651 // TODO
1652 }
1653
Joe Onorato7404ee42009-07-31 11:54:44 -07001654 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 * Displays the shortcut creation dialog and launches, if necessary, the
1656 * appropriate activity.
1657 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001658 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001659 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1660 DialogInterface.OnShowListener {
1661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 Dialog createDialog() {
1665 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001666
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1670 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001671 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001672
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 builder.setInverseBackgroundForced(true);
1674
1675 AlertDialog dialog = builder.create();
1676 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001677 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001678 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 return dialog;
1681 }
1682
1683 public void onCancel(DialogInterface dialog) {
1684 mWaitingForResult = false;
1685 cleanup();
1686 }
1687
Romain Guycbb89e42009-06-08 15:52:54 -07001688 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001689 }
1690
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 dismissDialog(DIALOG_CREATE_SHORTCUT);
1693 }
1694
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001695 /**
1696 * Handle the action clicked in the "Add to home" dialog.
1697 */
1698 public void onClick(DialogInterface dialog, int which) {
1699 Resources res = getResources();
1700 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001701
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001702 switch (which) {
1703 case AddAdapter.ITEM_SHORTCUT: {
1704 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001705 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001706 break;
1707 }
Romain Guycbb89e42009-06-08 15:52:54 -07001708
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001709 case AddAdapter.ITEM_APPWIDGET: {
1710 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001711
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001712 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1713 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1714 // add the search widget
1715 ArrayList<AppWidgetProviderInfo> customInfo =
1716 new ArrayList<AppWidgetProviderInfo>();
1717 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1718 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1719 info.label = getString(R.string.group_search);
1720 info.icon = R.drawable.ic_search_widget;
1721 customInfo.add(info);
1722 pickIntent.putParcelableArrayListExtra(
1723 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1724 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1725 Bundle b = new Bundle();
1726 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1727 customExtras.add(b);
1728 pickIntent.putParcelableArrayListExtra(
1729 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1730 // start the pick activity
1731 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1732 break;
1733 }
Romain Guycbb89e42009-06-08 15:52:54 -07001734
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001735 case AddAdapter.ITEM_LIVE_FOLDER: {
1736 // Insert extra item to handle inserting folder
1737 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001738
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001739 ArrayList<String> shortcutNames = new ArrayList<String>();
1740 shortcutNames.add(res.getString(R.string.group_folder));
1741 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001742
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001743 ArrayList<ShortcutIconResource> shortcutIcons =
1744 new ArrayList<ShortcutIconResource>();
1745 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1746 R.drawable.ic_launcher_folder));
1747 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1748
1749 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1750 pickIntent.putExtra(Intent.EXTRA_INTENT,
1751 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1752 pickIntent.putExtra(Intent.EXTRA_TITLE,
1753 getText(R.string.title_select_live_folder));
1754 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001755
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001756 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1757 break;
1758 }
1759
1760 case AddAdapter.ITEM_WALLPAPER: {
1761 startWallpaper();
1762 break;
1763 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 }
1765 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001766
1767 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 }
1770
1771 /**
Joe Onorato85a02a82009-09-08 12:34:22 -07001772 * Implementation of the method from SwipeController.SwipeListener.
1773 */
1774 public void onStartSwipe() {
1775 switch (mMode) {
1776 case MODE_WORKSPACE:
1777 mWorkspace.enableChildrenCache();
1778 break;
1779 case MODE_ALL_APPS:
1780 break;
1781 case MODE_ALL_APPS_ZOOMED:
1782 break;
1783 }
1784 }
1785
1786 /**
1787 * Implementation of the method from SwipeController.SwipeListener.
1788 *
1789 * @param amount The final value of the swipe (-1, 0 or 1)
1790 */
1791 public void onFinishSwipe(int amount) {
1792 switch (mMode) {
1793 case MODE_WORKSPACE:
1794 if (amount == -1) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001795 setWorkspaceAndAllAppsScale(-amount);
Joe Onorato85a02a82009-09-08 12:34:22 -07001796 mWorkspace.clearChildrenCache();
1797 mMode = MODE_ALL_APPS;
1798 mSwipeController.setRange(0, 1);
1799 }
1800 break;
1801 case MODE_ALL_APPS:
1802 if (amount == 1) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001803 setWorkspaceAndAllAppsScale(1-amount);
Joe Onorato85a02a82009-09-08 12:34:22 -07001804 mWorkspace.clearChildrenCache();
1805 mMode = MODE_WORKSPACE;
1806 mSwipeController.setRange(-1, 0);
1807 }
1808 break;
1809 case MODE_ALL_APPS_ZOOMED:
1810 break;
1811 }
1812 }
1813
1814 /**
1815 * Implementation of the method from SwipeController.SwipeListener.
1816 */
1817 public void onSwipe(float amount) {
1818 switch (mMode) {
1819 case MODE_WORKSPACE:
1820 // We can open the all apps view.
1821 // 0 == workspace is showing
1822 // -1 == all apps is showing
1823 setWorkspaceAndAllAppsScale(-amount);
1824 break;
1825 case MODE_ALL_APPS:
1826 // We can close it, or (someday) zoom it further
1827 // 0 == all apps showing
1828 // 1 == workspace is showing
1829 setWorkspaceAndAllAppsScale(1-amount);
1830 break;
1831 }
1832 }
1833
1834 /**
1835 * Set the scale factor for the workspace and the all apps grid.
1836 *
1837 * @param amount A float between 0 and 1, where:
1838 * 0 == workspace is showing and
1839 * 1 == the all apps grid is showing.
1840 */
1841 private void setWorkspaceAndAllAppsScale(float amount) {
1842 //Log.d("setWorkspaceAndAllAppsScale", "setWorkspaceAndAllAppsScale amount=" + amount);
1843 if (amount < 0.001f) {
1844 amount = 0.0f;
1845 }
1846 if (amount > 0.999f) {
1847 amount = 1.0f;
1848 mWorkspace.setVisibility(View.INVISIBLE);
1849 } else {
1850 mWorkspace.setVisibility(View.VISIBLE);
1851 }
1852 mWorkspace.setScale(1-amount);
1853 mAllAppsGrid.setScale(amount);
1854 }
1855
1856 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001857 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859 public int getCurrentWorkspaceScreen() {
1860 return mWorkspace.getCurrentScreen();
1861 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001862
Joe Onorato9c1289c2009-08-17 11:03:03 -04001863 /**
1864 * Refreshes the shortcuts shown on the workspace.
1865 *
1866 * Implementation of the method from LauncherModel.Callbacks.
1867 */
1868 public void startBinding() {
1869 final Workspace workspace = mWorkspace;
1870 int count = workspace.getChildCount();
1871 for (int i = 0; i < count; i++) {
1872 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1873 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001874
Joe Onorato9c1289c2009-08-17 11:03:03 -04001875 if (DEBUG_USER_INTERFACE) {
1876 android.widget.Button finishButton = new android.widget.Button(this);
1877 finishButton.setText("Finish");
1878 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1879
1880 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1881 public void onClick(View v) {
1882 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001883 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001884 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
1886 }
1887
1888 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001889 * Bind the items start-end from the list.
1890 *
1891 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001893 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1894
1895 final Workspace workspace = mWorkspace;
1896
1897 for (int i=start; i<end; i++) {
1898 final ItemInfo item = shortcuts.get(i);
1899 mDesktopItems.add(item);
1900 switch (item.itemType) {
1901 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1902 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1903 final View shortcut = createShortcut((ApplicationInfo) item);
1904 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1905 false);
1906 break;
1907 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1908 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1909 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1910 (UserFolderInfo) item);
1911 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1912 false);
1913 break;
1914 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1915 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1916 R.layout.live_folder_icon, this,
1917 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1918 (LiveFolderInfo) item);
1919 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1920 false);
1921 break;
1922 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1923 final int screen = workspace.getCurrentScreen();
1924 final View view = mInflater.inflate(R.layout.widget_search,
1925 (ViewGroup) workspace.getChildAt(screen), false);
1926
1927 Search search = (Search) view.findViewById(R.id.widget_search);
1928 search.setLauncher(this);
1929
1930 final Widget widget = (Widget) item;
1931 view.setTag(widget);
1932
1933 workspace.addWidget(view, widget, false);
1934 break;
1935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
1937
Joe Onorato9c1289c2009-08-17 11:03:03 -04001938 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 }
1940
Joe Onorato9c1289c2009-08-17 11:03:03 -04001941 /**
1942 * Implementation of the method from LauncherModel.Callbacks.
1943 */
1944 void bindFolders(HashMap<Long, FolderInfo> folders) {
1945 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001947
1948 /**
1949 * Add the views for a widget to the workspace.
1950 *
1951 * Implementation of the method from LauncherModel.Callbacks.
1952 */
1953 public void bindAppWidget(LauncherAppWidgetInfo item) {
1954 final Workspace workspace = mWorkspace;
1955
1956 final int appWidgetId = item.appWidgetId;
1957 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1958 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1959
1960 if (true) {
1961 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1962 appWidgetId, appWidgetInfo));
1963 }
1964
1965 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1966 item.hostView.setTag(item);
1967
1968 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1969 item.cellY, item.spanX, item.spanY, false);
1970
1971 workspace.requestLayout();
1972
1973 mDesktopItems.add(item);
1974 }
1975
1976 /**
1977 * Callback saying that there aren't any more items to bind.
1978 *
1979 * Implementation of the method from LauncherModel.Callbacks.
1980 */
1981 public void finishBindingItems() {
1982 if (mSavedState != null) {
1983 if (!mWorkspace.hasFocus()) {
1984 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1985 }
1986
1987 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1988 if (userFolders != null) {
1989 for (long folderId : userFolders) {
1990 final FolderInfo info = mFolders.get(folderId);
1991 if (info != null) {
1992 openFolder(info);
1993 }
1994 }
1995 final Folder openFolder = mWorkspace.getOpenFolder();
1996 if (openFolder != null) {
1997 openFolder.requestFocus();
1998 }
1999 }
2000
2001 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
2002 if (allApps) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002003 showAllApps(false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002004 }
2005
2006 mSavedState = null;
2007 }
2008
2009 if (mSavedInstanceState != null) {
2010 super.onRestoreInstanceState(mSavedInstanceState);
2011 mSavedInstanceState = null;
2012 }
2013
2014 /* TODO
2015 if (mAllAppsVisible && !mDrawer.hasFocus()) {
2016 mDrawer.requestFocus();
2017 }
2018 */
2019
2020 Log.d(TAG, "finishBindingItems done");
2021 mWorkspaceLoading = false;
2022 }
2023
2024 /**
2025 * Add the icons for all apps.
2026 *
2027 * Implementation of the method from LauncherModel.Callbacks.
2028 */
2029 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
2030 Log.d(LOG_TAG, "got info for " + apps.size() + " apps");
2031 mAllAppsList = apps;
2032 mAllAppsGrid.setApps(mAllAppsList);
2033 }
2034
2035 /**
2036 * A package was installed.
2037 *
2038 * Implementation of the method from LauncherModel.Callbacks.
2039 */
2040 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2041 removeDialog(DIALOG_CREATE_SHORTCUT);
2042 }
2043
2044 /**
2045 * A package was updated.
2046 *
2047 * Implementation of the method from LauncherModel.Callbacks.
2048 */
2049 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2050 removeDialog(DIALOG_CREATE_SHORTCUT);
2051 mWorkspace.updateShortcutsForPackage(packageName);
2052 }
2053
2054 /**
2055 * A package was uninstalled.
2056 *
2057 * Implementation of the method from LauncherModel.Callbacks.
2058 */
2059 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2060 removeDialog(DIALOG_CREATE_SHORTCUT);
2061 mWorkspace.removeShortcutsForPackage(packageName);
2062 }
2063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064}