blob: bddc9ac3e33418bab3ddcc6b2004b7226be60b7f [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 Onorato7bb17492009-09-24 17:51:01 -070085 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040087 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 static final boolean LOGD = false;
89
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 static final boolean PROFILE_STARTUP = false;
91 static final boolean PROFILE_ROTATE = false;
92 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 private static final int WALLPAPER_SCREENS_SPAN = 2;
95
96 private static final int MENU_GROUP_ADD = 1;
97 private static final int MENU_ADD = Menu.FIRST + 1;
98 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
99 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
100 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700101 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
103 private static final int REQUEST_CREATE_SHORTCUT = 1;
104 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700105 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 private static final int REQUEST_PICK_APPLICATION = 6;
107 private static final int REQUEST_PICK_SHORTCUT = 7;
108 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700109 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
111 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
112
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700113 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
114 static final String SEARCH_WIDGET = "search_widget";
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 static final int SCREEN_COUNT = 3;
117 static final int DEFAULT_SCREN = 1;
118 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700119 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Joe Onorato7c312c12009-08-13 21:36:53 -0700121 static final int DIALOG_CREATE_SHORTCUT = 1;
122 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Romain Guy98d01652009-06-30 16:21:04 -0700124 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 // Type: int
127 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
128 // Type: boolean
129 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
130 // Type: long
131 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
146 // Type: int[]
147 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
148 // Type: boolean
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
150 // Type: long
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
152
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final Object sLock = new Object();
156 private static int sScreen = DEFAULT_SCREN;
157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private LayoutInflater mInflater;
159
Joe Onorato00acb122009-08-04 16:04:30 -0400160 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700162
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700163 private AppWidgetManager mAppWidgetManager;
164 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private CellLayout.CellInfo mAddItemCellInfo;
167 private CellLayout.CellInfo mMenuAddInfo;
168 private final int[] mCellCoordinates = new int[2];
169 private FolderInfo mFolderInfo;
170
Joe Onorato7c312c12009-08-13 21:36:53 -0700171 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700172 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700173 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private Bundle mSavedState;
176
177 private SpannableStringBuilder mDefaultKeySsb = null;
178
Mike LeBeau736cf282009-07-02 17:46:59 -0700179 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 private boolean mWorkspaceLoading = true;
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private boolean mRestoring;
184 private boolean mWaitingForResult;
185 private boolean mLocaleChanged;
Joe Onoratoff3862d2009-10-07 10:35:51 -0700186 private boolean mExitingBecauseOfLaunch;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Dianne Hackborn67800862009-07-24 17:15:20 -0700188 private boolean mHomeDown;
189 private boolean mBackDown;
Jason Samsfd22dac2009-09-20 17:24:16 -0700190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private Bundle mSavedInstanceState;
192
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 private LauncherModel mModel;
194
195 private ArrayList<ItemInfo> mDesktopItems = new ArrayList();
196 private static HashMap<Long, FolderInfo> mFolders = new HashMap();
197 private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList();
Romain Guycbb89e42009-06-08 15:52:54 -0700198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 @Override
200 protected void onCreate(Bundle savedInstanceState) {
201 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700206 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700207
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
209 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 if (PROFILE_STARTUP) {
212 android.os.Debug.startMethodTracing("/sdcard/launcher");
213 }
214
215 checkForLocaleChange();
216 setWallpaperDimension();
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 setContentView(R.layout.launcher);
219 setupViews();
220
Joe Onorato7c312c12009-08-13 21:36:53 -0700221 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 mSavedState = savedInstanceState;
224 restoreState(mSavedState);
225
226 if (PROFILE_STARTUP) {
227 android.os.Debug.stopMethodTracing();
228 }
229
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700230 // We have a new AllAppsView, we need to re-bind everything, and it could have
231 // changed in our absence.
232 mModel.setAllAppsDirty();
233 mModel.setWorkspaceDirty();
234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 }
238
239 // For handling default keys
240 mDefaultKeySsb = new SpannableStringBuilder();
241 Selection.setSelection(mDefaultKeySsb, 0);
242 }
Romain Guycbb89e42009-06-08 15:52:54 -0700243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700245 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
246 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 final Configuration configuration = getResources().getConfiguration();
249
Romain Guy98d01652009-06-30 16:21:04 -0700250 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 final String locale = configuration.locale.toString();
252
Romain Guy98d01652009-06-30 16:21:04 -0700253 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 final int mcc = configuration.mcc;
255
Romain Guy98d01652009-06-30 16:21:04 -0700256 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 final int mnc = configuration.mnc;
258
259 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
260
261 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700262 localeConfiguration.locale = locale;
263 localeConfiguration.mcc = mcc;
264 localeConfiguration.mnc = mnc;
265
266 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400267 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700268 }
269 }
270
271 private static class LocaleConfiguration {
272 public String locale;
273 public int mcc = -1;
274 public int mnc = -1;
275 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700276
Romain Guy98d01652009-06-30 16:21:04 -0700277 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
278 DataInputStream in = null;
279 try {
280 in = new DataInputStream(context.openFileInput(PREFERENCES));
281 configuration.locale = in.readUTF();
282 configuration.mcc = in.readInt();
283 configuration.mnc = in.readInt();
284 } catch (FileNotFoundException e) {
285 // Ignore
286 } catch (IOException e) {
287 // Ignore
288 } finally {
289 if (in != null) {
290 try {
291 in.close();
292 } catch (IOException e) {
293 // Ignore
294 }
295 }
296 }
297 }
298
299 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
300 DataOutputStream out = null;
301 try {
302 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
303 out.writeUTF(configuration.locale);
304 out.writeInt(configuration.mcc);
305 out.writeInt(configuration.mnc);
306 out.flush();
307 } catch (FileNotFoundException e) {
308 // Ignore
309 } catch (IOException e) {
310 //noinspection ResultOfMethodCallIgnored
311 context.getFileStreamPath(PREFERENCES).delete();
312 } finally {
313 if (out != null) {
314 try {
315 out.close();
316 } catch (IOException e) {
317 // Ignore
318 }
319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 }
321 }
322
323 static int getScreen() {
324 synchronized (sLock) {
325 return sScreen;
326 }
327 }
328
329 static void setScreen(int screen) {
330 synchronized (sLock) {
331 sScreen = screen;
332 }
333 }
334
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700336 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337
338 Display display = getWindowManager().getDefaultDisplay();
339 boolean isPortrait = display.getWidth() < display.getHeight();
340
341 final int width = isPortrait ? display.getWidth() : display.getHeight();
342 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700343 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 }
345
346 @Override
347 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700348 mWaitingForResult = false;
349
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 // The pattern used here is that a user PICKs a specific application,
351 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
354 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700355
Romain Guy94dabf12009-07-21 10:55:43 -0700356 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 switch (requestCode) {
358 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400359 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 break;
361 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700362 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 break;
364 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400365 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 break;
367 case REQUEST_PICK_LIVE_FOLDER:
368 addLiveFolder(data);
369 break;
370 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400371 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700373 case REQUEST_PICK_APPWIDGET:
374 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700376 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400377 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 break;
379 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700380 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 // Clean up the appWidgetId if we canceled
383 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
384 if (appWidgetId != -1) {
385 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 }
387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 }
389
390 @Override
391 protected void onResume() {
392 super.onResume();
393
394 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400395 mWorkspaceLoading = true;
396 mModel.startLoader(this, true);
397 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700399
Mike LeBeau736cf282009-07-02 17:46:59 -0700400 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
401 // onNewIntent), then close the search dialog if needed, because it probably
402 // came from the user pressing 'home' (rather than, for example, pressing 'back').
403 if (mIsNewIntent) {
404 // Post to a handler so that this happens after the search dialog tries to open
405 // itself again.
406 mWorkspace.post(new Runnable() {
407 public void run() {
408 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
409 ServiceManager.getService(Context.SEARCH_SERVICE));
410 try {
411 searchManagerService.stopSearch();
412 } catch (RemoteException e) {
413 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700414 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700415 }
416 });
417 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700418
Mike LeBeau736cf282009-07-02 17:46:59 -0700419 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 }
421
422 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700423 protected void onPause() {
424 super.onPause();
Joe Onoratoff3862d2009-10-07 10:35:51 -0700425 if (mExitingBecauseOfLaunch) {
426 mExitingBecauseOfLaunch = false;
427 closeAllApps(false);
428 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700429 }
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700431 @Override
432 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400433 // Flag the loader to stop early before switching
434 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700436 if (PROFILE_ROTATE) {
437 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
438 }
439 return null;
440 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 private boolean acceptFilter() {
443 final InputMethodManager inputManager = (InputMethodManager)
444 getSystemService(Context.INPUT_METHOD_SERVICE);
445 return !inputManager.isFullscreenMode();
446 }
447
448 @Override
449 public boolean onKeyDown(int keyCode, KeyEvent event) {
450 boolean handled = super.onKeyDown(keyCode, event);
451 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
452 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
453 keyCode, event);
454 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700455 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700456 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700457 // showSearchDialog()
458 // If there are multiple keystrokes before the search dialog takes focus,
459 // onSearchRequested() will be called for every keystroke,
460 // but it is idempotent, so it's fine.
461 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 }
463 }
464
465 return handled;
466 }
467
Karl Rosaen138a0412009-04-23 19:00:21 -0700468 private String getTypedText() {
469 return mDefaultKeySsb.toString();
470 }
471
472 private void clearTypedText() {
473 mDefaultKeySsb.clear();
474 mDefaultKeySsb.clearSpans();
475 Selection.setSelection(mDefaultKeySsb, 0);
476 }
477
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 /**
479 * Restores the previous state, if it exists.
480 *
481 * @param savedState The previous state.
482 */
483 private void restoreState(Bundle savedState) {
484 if (savedState == null) {
485 return;
486 }
487
488 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
489 if (currentScreen > -1) {
490 mWorkspace.setCurrentScreen(currentScreen);
491 }
492
493 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
494 if (addScreen > -1) {
495 mAddItemCellInfo = new CellLayout.CellInfo();
496 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
497 addItemCellInfo.valid = true;
498 addItemCellInfo.screen = addScreen;
499 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
500 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
501 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
502 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
503 addItemCellInfo.findVacantCellsFromOccupied(
504 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
505 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
506 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
507 mRestoring = true;
508 }
509
510 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
511 if (renameFolder) {
512 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400513 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514 mRestoring = true;
515 }
516 }
517
518 /**
519 * Finds all the views we need and configure them properly.
520 */
521 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400522 mDragController = new DragController(this);
523 DragController dragController = mDragController;
524
Romain Guyb1b69f52009-08-10 15:10:15 -0700525 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400526 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527
Joe Onorato7c312c12009-08-13 21:36:53 -0700528 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700529 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700530 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700531 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Joe Onorato7c312c12009-08-13 21:36:53 -0700532
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
534 final Workspace workspace = mWorkspace;
535
Joe Onorato7c312c12009-08-13 21:36:53 -0700536 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
537 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538
Joe Onorato7c312c12009-08-13 21:36:53 -0700539 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700540 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700541 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700542 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700543 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700544 handleIocon.setCrossFadeEnabled(true);
545 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400548 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550
551 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400552 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700553 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554
Joe Onorato00acb122009-08-04 16:04:30 -0400555 dragController.setDragScoller(workspace);
556 dragController.setDragListener(deleteZone);
557 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700558
Joe Onorato00acb122009-08-04 16:04:30 -0400559 // The order here is bottom to top.
560 dragController.addDropTarget(workspace);
561 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 }
563
564 /**
565 * Creates a view representing a shortcut.
566 *
567 * @param info The data structure describing the shortcut.
568 *
569 * @return A View inflated from R.layout.application.
570 */
571 View createShortcut(ApplicationInfo info) {
572 return createShortcut(R.layout.application,
573 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
574 }
575
576 /**
577 * Creates a view representing a shortcut inflated from the specified resource.
578 *
579 * @param layoutResId The id of the XML layout used to create the shortcut.
580 * @param parent The group the shortcut belongs to.
581 * @param info The data structure describing the shortcut.
582 *
583 * @return A View inflated from layoutResId.
584 */
585 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
586 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
587
Joe Onorato5162ea92009-09-03 09:39:42 -0700588 if (info.icon == null) {
589 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
590 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700592 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 info.filtered = true;
594 }
595
596 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
597 favorite.setText(info.title);
598 favorite.setTag(info);
599 favorite.setOnClickListener(this);
600
601 return favorite;
602 }
603
604 /**
605 * Add an application shortcut to the workspace.
606 *
607 * @param data The intent describing the application.
608 * @param cellInfo The position on screen where to create the shortcut.
609 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400610 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 cellInfo.screen = mWorkspace.getCurrentScreen();
612 if (!findSingleSlot(cellInfo)) return;
613
Romain Guy73b979d2009-06-09 12:57:21 -0700614 final ApplicationInfo info = infoFromApplicationIntent(context, data);
615 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700617 }
618 }
619
620 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 ComponentName component = data.getComponent();
622 PackageManager packageManager = context.getPackageManager();
623 ActivityInfo activityInfo = null;
624 try {
625 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
626 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700627 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 }
Romain Guycbb89e42009-06-08 15:52:54 -0700629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 if (activityInfo != null) {
631 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 itemInfo.title = activityInfo.loadLabel(packageManager);
634 if (itemInfo.title == null) {
635 itemInfo.title = activityInfo.name;
636 }
Romain Guycbb89e42009-06-08 15:52:54 -0700637
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
639 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
640 itemInfo.icon = activityInfo.loadIcon(packageManager);
641 itemInfo.container = ItemInfo.NO_ID;
642
Romain Guy73b979d2009-06-09 12:57:21 -0700643 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 }
Romain Guy73b979d2009-06-09 12:57:21 -0700645
646 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 }
Romain Guycbb89e42009-06-08 15:52:54 -0700648
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 /**
650 * Add a shortcut to the workspace.
651 *
652 * @param data The intent describing the shortcut.
653 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 cellInfo.screen = mWorkspace.getCurrentScreen();
657 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700658
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
660
661 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400663 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
664 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
666 }
667
Romain Guycbb89e42009-06-08 15:52:54 -0700668
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700670 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700672 * @param data The intent describing the appWidgetId.
673 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400675 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700678
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700679 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700680
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700681 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700682
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700685 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700686
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 // Try finding open space on Launcher screen
688 final int[] xy = mCellCoordinates;
689 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
690
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 // Build Launcher-specific widget info and save to database
692 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 launcherInfo.spanX = spans[0];
694 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700695
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 LauncherModel.addItemToDatabase(this, launcherInfo,
697 LauncherSettings.Favorites.CONTAINER_DESKTOP,
698 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
699
700 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700702
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700704 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700705
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 }
712 }
Romain Guycbb89e42009-06-08 15:52:54 -0700713
Joe Onorato9c1289c2009-08-17 11:03:03 -0400714 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
715 mDesktopItems.remove(launcherInfo);
716 launcherInfo.hostView = null;
717 }
718
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700719 public LauncherAppWidgetHost getAppWidgetHost() {
720 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 }
Romain Guycbb89e42009-06-08 15:52:54 -0700722
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 static ApplicationInfo addShortcut(Context context, Intent data,
724 CellLayout.CellInfo cellInfo, boolean notify) {
725
Romain Guy73b979d2009-06-09 12:57:21 -0700726 final ApplicationInfo info = infoFromShortcutIntent(context, data);
727 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
728 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
729
730 return info;
731 }
732
733 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
735 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
736 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
737
738 Drawable icon = null;
739 boolean filtered = false;
740 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700741 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742
743 if (bitmap != null) {
744 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
745 filtered = true;
746 customIcon = true;
747 } else {
748 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700749 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700751 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 final PackageManager packageManager = context.getPackageManager();
753 Resources resources = packageManager.getResourcesForApplication(
754 iconResource.packageName);
755 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
756 icon = resources.getDrawable(id);
757 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700758 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
760 }
761 }
762
763 if (icon == null) {
764 icon = context.getPackageManager().getDefaultActivityIcon();
765 }
766
767 final ApplicationInfo info = new ApplicationInfo();
768 info.icon = icon;
769 info.filtered = filtered;
770 info.title = name;
771 info.intent = intent;
772 info.customIcon = customIcon;
773 info.iconResource = iconResource;
774
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 return info;
776 }
777
778 @Override
779 protected void onNewIntent(Intent intent) {
780 super.onNewIntent(intent);
781
782 // Close the menu
783 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
784 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700785
Joe Onorato742966b2009-10-07 10:26:17 -0700786 // Whatever we were doing is hereby canceled.
787 mWaitingForResult = false;
788
Mike LeBeau736cf282009-07-02 17:46:59 -0700789 // Set this flag so that onResume knows to close the search dialog if it's open,
790 // because this was a new intent (thus a press of 'home' or some such) rather than
791 // for example onResume being called when the user pressed the 'back' button.
792 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793
794 try {
795 dismissDialog(DIALOG_CREATE_SHORTCUT);
796 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 } catch (Exception e) {
798 // An exception is thrown if the dialog is not visible, which is fine
799 }
800
801 try {
802 dismissDialog(DIALOG_RENAME_FOLDER);
803 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 } catch (Exception e) {
805 // An exception is thrown if the dialog is not visible, which is fine
806 }
807
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
809 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700810
Romain Guy94dabf12009-07-21 10:55:43 -0700811 if (!mWorkspace.isDefaultScreenShowing()) {
812 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700814
Joe Onorato7bb17492009-09-24 17:51:01 -0700815 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700816
817 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 if (v != null && v.getWindowToken() != null) {
819 InputMethodManager imm = (InputMethodManager)getSystemService(
820 INPUT_METHOD_SERVICE);
821 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
822 }
823 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700824 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 }
826 }
827 }
828
829 @Override
830 protected void onRestoreInstanceState(Bundle savedInstanceState) {
831 // Do not call super here
832 mSavedInstanceState = savedInstanceState;
833 }
834
835 @Override
836 protected void onSaveInstanceState(Bundle outState) {
837 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
838
839 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
840 if (folders.size() > 0) {
841 final int count = folders.size();
842 long[] ids = new long[count];
843 for (int i = 0; i < count; i++) {
844 final FolderInfo info = folders.get(i).getInfo();
845 ids[i] = info.id;
846 }
847 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
848 } else {
849 super.onSaveInstanceState(outState);
850 }
851
Romain Guy3cf604f2009-06-16 13:12:53 -0700852 final boolean isConfigurationChange = getChangingConfigurations() != 0;
853
Romain Guy5a941392009-04-28 15:18:25 -0700854 // When the drawer is opened and we are saving the state because of a
855 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400856 // TODO should not do this if the drawer is currently closing.
857 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700859 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860
861 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
862 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
863 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
864
865 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
866 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
867 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
868 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
869 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
870 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
871 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
872 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
873 layout.getOccupiedCells());
874 }
875
876 if (mFolderInfo != null && mWaitingForResult) {
877 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
878 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
879 }
880 }
881
882 @Override
883 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700885
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700887 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700889 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
891
892 TextKeyListener.getInstance().release();
893
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895
Joe Onorato9c1289c2009-08-17 11:03:03 -0400896 unbindDesktopItems();
897 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
899
900 @Override
901 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700902 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 super.startActivityForResult(intent, requestCode);
904 }
905
906 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700907 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700909
Joe Onorato7bb17492009-09-24 17:51:01 -0700910 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700911
Karl Rosaen138a0412009-04-23 19:00:21 -0700912 // Slide the search widget to the top, if it's on the current screen,
913 // otherwise show the search dialog immediately.
914 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
915 if (searchWidget == null) {
916 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
917 } else {
918 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
919 // show the currently typed text in the search widget while sliding
920 searchWidget.setQuery(getTypedText());
921 }
922 }
Romain Guycbb89e42009-06-08 15:52:54 -0700923
Karl Rosaen138a0412009-04-23 19:00:21 -0700924 /**
925 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700926 *
927 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700928 */
Romain Guycbb89e42009-06-08 15:52:54 -0700929 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700930 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700931
Karl Rosaen138a0412009-04-23 19:00:21 -0700932 if (initialQuery == null) {
933 // Use any text typed in the launcher as the initial query
934 initialQuery = getTypedText();
935 clearTypedText();
936 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 if (appSearchData == null) {
938 appSearchData = new Bundle();
939 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
940 }
Romain Guycbb89e42009-06-08 15:52:54 -0700941
Karl Rosaen138a0412009-04-23 19:00:21 -0700942 final SearchManager searchManager =
943 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
944
945 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
946 if (searchWidget != null) {
947 // This gets called when the user leaves the search dialog to go back to
948 // the Launcher.
949 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
950 public void onCancel() {
951 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700952 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700953 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700954 });
955 }
Romain Guycbb89e42009-06-08 15:52:54 -0700956
Karl Rosaen138a0412009-04-23 19:00:21 -0700957 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700958 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 }
960
Karl Rosaen138a0412009-04-23 19:00:21 -0700961 /**
962 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 */
Romain Guy5a941392009-04-28 15:18:25 -0700964 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700965 // Close search dialog
966 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100967 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700968 // Restore search widget to its normal position
969 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
970 if (searchWidget != null) {
971 searchWidget.stopSearch(false);
972 }
973 }
Romain Guycbb89e42009-06-08 15:52:54 -0700974
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 @Override
976 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400977 if (isWorkspaceLocked()) {
978 return false;
979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980
981 super.onCreateOptionsMenu(menu);
982 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
983 .setIcon(android.R.drawable.ic_menu_add)
984 .setAlphabeticShortcut('A');
985 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
986 .setIcon(android.R.drawable.ic_menu_gallery)
987 .setAlphabeticShortcut('W');
988 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
989 .setIcon(android.R.drawable.ic_search_category_default)
990 .setAlphabeticShortcut(SearchManager.MENU_KEY);
991 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
992 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
993 .setAlphabeticShortcut('N');
994
995 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700996 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
997 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998
999 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1000 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1001 .setIntent(settings);
1002
1003 return true;
1004 }
1005
1006 @Override
1007 public boolean onPrepareOptionsMenu(Menu menu) {
1008 super.onPrepareOptionsMenu(menu);
1009
1010 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1011 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1012
1013 return true;
1014 }
1015
1016 @Override
1017 public boolean onOptionsItemSelected(MenuItem item) {
1018 switch (item.getItemId()) {
1019 case MENU_ADD:
1020 addItems();
1021 return true;
1022 case MENU_WALLPAPER_SETTINGS:
1023 startWallpaper();
1024 return true;
1025 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001026 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 return true;
1028 case MENU_NOTIFICATIONS:
1029 showNotifications();
1030 return true;
1031 }
1032
1033 return super.onOptionsItemSelected(item);
1034 }
Romain Guycbb89e42009-06-08 15:52:54 -07001035
Karl Rosaen138a0412009-04-23 19:00:21 -07001036 /**
1037 * Indicates that we want global search for this activity by setting the globalSearch
1038 * argument for {@link #startSearch} to true.
1039 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001041 @Override
1042 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001043 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001044 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001045 }
1046
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047 public boolean isWorkspaceLocked() {
1048 return mWorkspaceLoading || mWaitingForResult;
1049 }
1050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 private void addItems() {
1052 showAddDialog(mMenuAddInfo);
1053 }
1054
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001055 void addAppWidget(Intent data) {
1056 // TODO: catch bad widget exception when sent
1057 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001059 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1060 if (SEARCH_WIDGET.equals(customWidget)) {
1061 // We don't need this any more, since this isn't a real app widget.
1062 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1063 // add the search widget
1064 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001066 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1067
1068 if (appWidget.configure != null) {
1069 // Launch over to configure widget, if needed
1070 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1071 intent.setComponent(appWidget.configure);
1072 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1073
1074 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1075 } else {
1076 // Otherwise just add it
1077 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 }
1080 }
Romain Guycbb89e42009-06-08 15:52:54 -07001081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 void addSearch() {
1083 final Widget info = Widget.makeSearch();
1084 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 final int[] xy = mCellCoordinates;
1087 final int spanX = info.spanX;
1088 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1093 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 final View view = mInflater.inflate(info.layoutResource, null);
1096 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001097 Search search = (Search) view.findViewById(R.id.widget_search);
1098 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1101 }
1102
Romain Guy73b979d2009-06-09 12:57:21 -07001103 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001104 // Handle case where user selected "Applications"
1105 String applicationName = getResources().getString(R.string.group_applications);
1106 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001107
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001108 if (applicationName != null && applicationName.equals(shortcutName)) {
1109 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1110 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001111
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001112 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1113 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001114 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001115 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001116 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
1119
1120 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001121 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001122 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001123 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001124
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001125 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001127 } else {
1128 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 }
1131
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 UserFolderInfo folderInfo = new UserFolderInfo();
1134 folderInfo.title = getText(R.string.folder_name);
1135
1136 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1137 cellInfo.screen = mWorkspace.getCurrentScreen();
1138 if (!findSingleSlot(cellInfo)) return;
1139
1140 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 LauncherModel.addItemToDatabase(this, folderInfo,
1142 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001144 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145
1146 // Create the view
1147 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1148 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1149 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001150 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 }
Romain Guycbb89e42009-06-08 15:52:54 -07001152
Joe Onorato9c1289c2009-08-17 11:03:03 -04001153 void removeFolder(FolderInfo folder) {
1154 mFolders.remove(folder.id);
1155 }
1156
1157 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 cellInfo.screen = mWorkspace.getCurrentScreen();
1159 if (!findSingleSlot(cellInfo)) return;
1160
1161 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1162
1163 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1165 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001166 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1167 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 }
1169 }
1170
1171 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1172 CellLayout.CellInfo cellInfo, boolean notify) {
1173
1174 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1175 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1176
1177 Drawable icon = null;
1178 boolean filtered = false;
1179 Intent.ShortcutIconResource iconResource = null;
1180
1181 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1182 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1183 try {
1184 iconResource = (Intent.ShortcutIconResource) extra;
1185 final PackageManager packageManager = context.getPackageManager();
1186 Resources resources = packageManager.getResourcesForApplication(
1187 iconResource.packageName);
1188 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1189 icon = resources.getDrawable(id);
1190 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001191 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 }
1193 }
1194
1195 if (icon == null) {
1196 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1197 }
1198
1199 final LiveFolderInfo info = new LiveFolderInfo();
1200 info.icon = icon;
1201 info.filtered = filtered;
1202 info.title = name;
1203 info.iconResource = iconResource;
1204 info.uri = data.getData();
1205 info.baseIntent = baseIntent;
1206 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1207 LiveFolders.DISPLAY_MODE_GRID);
1208
1209 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1210 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212
1213 return info;
1214 }
1215
1216 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1217 final int[] xy = new int[2];
1218 if (findSlot(cellInfo, xy, 1, 1)) {
1219 cellInfo.cellX = xy[0];
1220 cellInfo.cellY = xy[1];
1221 return true;
1222 }
1223 return false;
1224 }
1225
1226 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1227 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1228 boolean[] occupied = mSavedState != null ?
1229 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1230 cellInfo = mWorkspace.findAllVacantCells(occupied);
1231 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1232 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1233 return false;
1234 }
1235 }
1236 return true;
1237 }
1238
1239 private void showNotifications() {
1240 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1241 if (statusBar != null) {
1242 statusBar.expand();
1243 }
1244 }
1245
1246 private void startWallpaper() {
1247 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001248 Intent chooser = Intent.createChooser(pickWallpaper,
1249 getText(R.string.chooser_wallpaper));
1250 WallpaperManager wm = (WallpaperManager)
1251 getSystemService(Context.WALLPAPER_SERVICE);
1252 WallpaperInfo wi = wm.getWallpaperInfo();
1253 if (wi != null && wi.getSettingsActivity() != null) {
1254 LabeledIntent li = new LabeledIntent(getPackageName(),
1255 R.string.configure_wallpaper, 0);
1256 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1257 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1258 }
1259 startActivity(chooser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
1261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001263 public void onWindowFocusChanged(boolean hasFocus) {
1264 super.onWindowFocusChanged(hasFocus);
1265 if (!hasFocus) {
1266 mBackDown = mHomeDown = false;
1267 }
1268 }
1269
1270 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 public boolean dispatchKeyEvent(KeyEvent event) {
1272 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1273 switch (event.getKeyCode()) {
1274 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001275 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 return true;
1277 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001278 mHomeDown = true;
1279 return true;
1280 }
1281 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1282 switch (event.getKeyCode()) {
1283 case KeyEvent.KEYCODE_BACK:
1284 if (!event.isCanceled()) {
1285 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001286 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001287 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001288 } else {
1289 closeFolder();
1290 }
1291 }
1292 mBackDown = false;
1293 return true;
1294 case KeyEvent.KEYCODE_HOME:
1295 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 return true;
1297 }
1298 }
1299
1300 return super.dispatchKeyEvent(event);
1301 }
1302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 private void closeFolder() {
1304 Folder folder = mWorkspace.getOpenFolder();
1305 if (folder != null) {
1306 closeFolder(folder);
1307 }
1308 }
1309
1310 void closeFolder(Folder folder) {
1311 folder.getInfo().opened = false;
1312 ViewGroup parent = (ViewGroup) folder.getParent();
1313 if (parent != null) {
1314 parent.removeView(folder);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315 // TODO: this line crashes.
1316 //mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
1318 folder.onClose();
1319 }
1320
1321 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001322 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1323 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325 private void unbindDesktopItems() {
1326 for (ItemInfo item: mDesktopItems) {
1327 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 }
1329 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001330
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 /**
1332 * Launches the intent referred by the clicked shortcut.
1333 *
1334 * @param v The view representing the clicked shortcut.
1335 */
1336 public void onClick(View v) {
1337 Object tag = v.getTag();
1338 if (tag instanceof ApplicationInfo) {
1339 // Open shortcut
1340 final Intent intent = ((ApplicationInfo) tag).intent;
1341 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001342 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 } else if (tag instanceof FolderInfo) {
1344 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001345 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001346 Log.d(TAG, "onClick");
1347 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001348 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001349 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001350 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001351 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 }
1353 }
1354
1355 void startActivitySafely(Intent intent) {
1356 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1357 try {
1358 startActivity(intent);
1359 } catch (ActivityNotFoundException e) {
1360 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1361 } catch (SecurityException e) {
1362 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001363 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1365 "or use the exported attribute for this activity.", e);
1366 }
1367 }
1368
1369 private void handleFolderClick(FolderInfo folderInfo) {
1370 if (!folderInfo.opened) {
1371 // Close any open folder
1372 closeFolder();
1373 // Open the requested folder
1374 openFolder(folderInfo);
1375 } else {
1376 // Find the open folder...
1377 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1378 int folderScreen;
1379 if (openFolder != null) {
1380 folderScreen = mWorkspace.getScreenForView(openFolder);
1381 // .. and close it
1382 closeFolder(openFolder);
1383 if (folderScreen != mWorkspace.getCurrentScreen()) {
1384 // Close any folder open on the current screen
1385 closeFolder();
1386 // Pull the folder onto this screen
1387 openFolder(folderInfo);
1388 }
1389 }
1390 }
1391 }
1392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 /**
1394 * Opens the user fodler described by the specified tag. The opening of the folder
1395 * is animated relative to the specified View. If the View is null, no animation
1396 * is played.
1397 *
1398 * @param folderInfo The FolderInfo describing the folder to open.
1399 */
1400 private void openFolder(FolderInfo folderInfo) {
1401 Folder openFolder;
1402
1403 if (folderInfo instanceof UserFolderInfo) {
1404 openFolder = UserFolder.fromXml(this);
1405 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001406 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 } else {
1408 return;
1409 }
1410
Joe Onorato00acb122009-08-04 16:04:30 -04001411 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 openFolder.setLauncher(this);
1413
1414 openFolder.bind(folderInfo);
1415 folderInfo.opened = true;
1416
1417 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1418 openFolder.onOpen();
1419 }
1420
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001422 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 return false;
1424 }
1425
1426 if (!(v instanceof CellLayout)) {
1427 v = (View) v.getParent();
1428 }
1429
1430 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1431
1432 // This happens when long clicking an item with the dpad/trackball
1433 if (cellInfo == null) {
1434 return true;
1435 }
1436
1437 if (mWorkspace.allowLongPress()) {
1438 if (cellInfo.cell == null) {
1439 if (cellInfo.valid) {
1440 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001441 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 showAddDialog(cellInfo);
1443 }
1444 } else {
1445 if (!(cellInfo.cell instanceof Folder)) {
1446 // User long pressed on an item
1447 mWorkspace.startDrag(cellInfo);
1448 }
1449 }
1450 }
1451 return true;
1452 }
1453
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001454 View getDrawerHandle() {
1455 return mHandleView;
1456 }
1457
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 Workspace getWorkspace() {
1459 return mWorkspace;
1460 }
1461
Joe Onorato7404ee42009-07-31 11:54:44 -07001462 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 GridView getApplicationsGrid() {
1464 return mAllAppsGrid;
1465 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001466 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467
1468 @Override
1469 protected Dialog onCreateDialog(int id) {
1470 switch (id) {
1471 case DIALOG_CREATE_SHORTCUT:
1472 return new CreateShortcut().createDialog();
1473 case DIALOG_RENAME_FOLDER:
1474 return new RenameFolder().createDialog();
1475 }
1476
1477 return super.onCreateDialog(id);
1478 }
1479
1480 @Override
1481 protected void onPrepareDialog(int id, Dialog dialog) {
1482 switch (id) {
1483 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 break;
1485 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001486 if (mFolderInfo != null) {
1487 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1488 final CharSequence text = mFolderInfo.title;
1489 input.setText(text);
1490 input.setSelection(0, text.length());
1491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 break;
1493 }
1494 }
1495
1496 void showRenameDialog(FolderInfo info) {
1497 mFolderInfo = info;
1498 mWaitingForResult = true;
1499 showDialog(DIALOG_RENAME_FOLDER);
1500 }
1501
1502 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1503 mAddItemCellInfo = cellInfo;
1504 mWaitingForResult = true;
1505 showDialog(DIALOG_CREATE_SHORTCUT);
1506 }
1507
Romain Guy73b979d2009-06-09 12:57:21 -07001508 private void pickShortcut(int requestCode, int title) {
1509 Bundle bundle = new Bundle();
1510
1511 ArrayList<String> shortcutNames = new ArrayList<String>();
1512 shortcutNames.add(getString(R.string.group_applications));
1513 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1514
1515 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1516 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1517 R.drawable.ic_launcher_application));
1518 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1519
1520 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1521 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1522 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1523 pickIntent.putExtras(bundle);
1524
1525 startActivityForResult(pickIntent, requestCode);
1526 }
1527
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 private class RenameFolder {
1529 private EditText mInput;
1530
1531 Dialog createDialog() {
1532 mWaitingForResult = true;
1533 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1534 mInput = (EditText) layout.findViewById(R.id.folder_name);
1535
1536 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1537 builder.setIcon(0);
1538 builder.setTitle(getString(R.string.rename_folder_title));
1539 builder.setCancelable(true);
1540 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1541 public void onCancel(DialogInterface dialog) {
1542 cleanup();
1543 }
1544 });
1545 builder.setNegativeButton(getString(R.string.cancel_action),
1546 new Dialog.OnClickListener() {
1547 public void onClick(DialogInterface dialog, int which) {
1548 cleanup();
1549 }
1550 }
1551 );
1552 builder.setPositiveButton(getString(R.string.rename_action),
1553 new Dialog.OnClickListener() {
1554 public void onClick(DialogInterface dialog, int which) {
1555 changeFolderName();
1556 }
1557 }
1558 );
1559 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001560
1561 final AlertDialog dialog = builder.create();
1562 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1563 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001564 }
1565 });
1566
1567 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
1569
1570 private void changeFolderName() {
1571 final String name = mInput.getText().toString();
1572 if (!TextUtils.isEmpty(name)) {
1573 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001574 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 mFolderInfo.title = name;
1576 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1577
Joe Onorato9c1289c2009-08-17 11:03:03 -04001578 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001579 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001580 mModel.setWorkspaceDirty();
1581 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 } else {
1583 final FolderIcon folderIcon = (FolderIcon)
1584 mWorkspace.getViewForTag(mFolderInfo);
1585 if (folderIcon != null) {
1586 folderIcon.setText(name);
1587 getWorkspace().requestLayout();
1588 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001589 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001590 mModel.setWorkspaceDirty();
1591 mWorkspaceLoading = true;
1592 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
1594 }
1595 }
1596 cleanup();
1597 }
1598
1599 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 dismissDialog(DIALOG_RENAME_FOLDER);
1601 mWaitingForResult = false;
1602 mFolderInfo = null;
1603 }
1604 }
1605
Joe Onoratofb0ca672009-09-14 17:55:46 -04001606 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001607 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001608 }
1609
Jason Samsfd22dac2009-09-20 17:24:16 -07001610 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001611 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001612 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001613
Joe Onorato7c312c12009-08-13 21:36:53 -07001614 // TODO: fade these two too
1615 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001616 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001617 }
1618
Joe Onorato7bb17492009-09-24 17:51:01 -07001619 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001620 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001621 mAllAppsGrid.zoom(0.0f);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001622 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001623
1624 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001625 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001626 mDeleteZone.setVisibility(View.VISIBLE);
1627 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001628 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001629 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001630 }
1631
Joe Onorato7c312c12009-08-13 21:36:53 -07001632 void lockAllApps() {
1633 // TODO
1634 }
1635
1636 void unlockAllApps() {
1637 // TODO
1638 }
1639
Joe Onorato7404ee42009-07-31 11:54:44 -07001640 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 * Displays the shortcut creation dialog and launches, if necessary, the
1642 * appropriate activity.
1643 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001644 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001645 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1646 DialogInterface.OnShowListener {
1647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001649
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 Dialog createDialog() {
1651 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1656 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001657 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 builder.setInverseBackgroundForced(true);
1660
1661 AlertDialog dialog = builder.create();
1662 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001663 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001664 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 return dialog;
1667 }
1668
1669 public void onCancel(DialogInterface dialog) {
1670 mWaitingForResult = false;
1671 cleanup();
1672 }
1673
Romain Guycbb89e42009-06-08 15:52:54 -07001674 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001675 }
1676
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 dismissDialog(DIALOG_CREATE_SHORTCUT);
1679 }
1680
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001681 /**
1682 * Handle the action clicked in the "Add to home" dialog.
1683 */
1684 public void onClick(DialogInterface dialog, int which) {
1685 Resources res = getResources();
1686 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001687
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001688 switch (which) {
1689 case AddAdapter.ITEM_SHORTCUT: {
1690 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001691 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001692 break;
1693 }
Romain Guycbb89e42009-06-08 15:52:54 -07001694
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001695 case AddAdapter.ITEM_APPWIDGET: {
1696 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001697
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001698 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1699 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1700 // add the search widget
1701 ArrayList<AppWidgetProviderInfo> customInfo =
1702 new ArrayList<AppWidgetProviderInfo>();
1703 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1704 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1705 info.label = getString(R.string.group_search);
1706 info.icon = R.drawable.ic_search_widget;
1707 customInfo.add(info);
1708 pickIntent.putParcelableArrayListExtra(
1709 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1710 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1711 Bundle b = new Bundle();
1712 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1713 customExtras.add(b);
1714 pickIntent.putParcelableArrayListExtra(
1715 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1716 // start the pick activity
1717 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1718 break;
1719 }
Romain Guycbb89e42009-06-08 15:52:54 -07001720
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001721 case AddAdapter.ITEM_LIVE_FOLDER: {
1722 // Insert extra item to handle inserting folder
1723 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001724
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001725 ArrayList<String> shortcutNames = new ArrayList<String>();
1726 shortcutNames.add(res.getString(R.string.group_folder));
1727 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001728
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001729 ArrayList<ShortcutIconResource> shortcutIcons =
1730 new ArrayList<ShortcutIconResource>();
1731 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1732 R.drawable.ic_launcher_folder));
1733 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1734
1735 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1736 pickIntent.putExtra(Intent.EXTRA_INTENT,
1737 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1738 pickIntent.putExtra(Intent.EXTRA_TITLE,
1739 getText(R.string.title_select_live_folder));
1740 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001741
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001742 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1743 break;
1744 }
1745
1746 case AddAdapter.ITEM_WALLPAPER: {
1747 startWallpaper();
1748 break;
1749 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 }
1751 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001752
1753 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001754 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 }
1756
1757 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001758 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001760 public int getCurrentWorkspaceScreen() {
1761 return mWorkspace.getCurrentScreen();
1762 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001763
Joe Onorato9c1289c2009-08-17 11:03:03 -04001764 /**
1765 * Refreshes the shortcuts shown on the workspace.
1766 *
1767 * Implementation of the method from LauncherModel.Callbacks.
1768 */
1769 public void startBinding() {
1770 final Workspace workspace = mWorkspace;
1771 int count = workspace.getChildCount();
1772 for (int i = 0; i < count; i++) {
1773 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1774 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001775
Joe Onorato9c1289c2009-08-17 11:03:03 -04001776 if (DEBUG_USER_INTERFACE) {
1777 android.widget.Button finishButton = new android.widget.Button(this);
1778 finishButton.setText("Finish");
1779 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1780
1781 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1782 public void onClick(View v) {
1783 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001784 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001785 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 }
1787 }
1788
1789 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001790 * Bind the items start-end from the list.
1791 *
1792 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001794 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1795
1796 final Workspace workspace = mWorkspace;
1797
1798 for (int i=start; i<end; i++) {
1799 final ItemInfo item = shortcuts.get(i);
1800 mDesktopItems.add(item);
1801 switch (item.itemType) {
1802 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1803 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1804 final View shortcut = createShortcut((ApplicationInfo) item);
1805 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1806 false);
1807 break;
1808 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1809 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1810 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1811 (UserFolderInfo) item);
1812 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1813 false);
1814 break;
1815 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1816 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1817 R.layout.live_folder_icon, this,
1818 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1819 (LiveFolderInfo) item);
1820 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1821 false);
1822 break;
1823 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1824 final int screen = workspace.getCurrentScreen();
1825 final View view = mInflater.inflate(R.layout.widget_search,
1826 (ViewGroup) workspace.getChildAt(screen), false);
1827
1828 Search search = (Search) view.findViewById(R.id.widget_search);
1829 search.setLauncher(this);
1830
1831 final Widget widget = (Widget) item;
1832 view.setTag(widget);
1833
1834 workspace.addWidget(view, widget, false);
1835 break;
1836 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 }
1838
Joe Onorato9c1289c2009-08-17 11:03:03 -04001839 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 }
1841
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 /**
1843 * Implementation of the method from LauncherModel.Callbacks.
1844 */
1845 void bindFolders(HashMap<Long, FolderInfo> folders) {
1846 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848
1849 /**
1850 * Add the views for a widget to the workspace.
1851 *
1852 * Implementation of the method from LauncherModel.Callbacks.
1853 */
1854 public void bindAppWidget(LauncherAppWidgetInfo item) {
1855 final Workspace workspace = mWorkspace;
1856
1857 final int appWidgetId = item.appWidgetId;
1858 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1859 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1860
1861 if (true) {
1862 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1863 appWidgetId, appWidgetInfo));
1864 }
1865
1866 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1867 item.hostView.setTag(item);
1868
1869 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1870 item.cellY, item.spanX, item.spanY, false);
1871
1872 workspace.requestLayout();
1873
1874 mDesktopItems.add(item);
1875 }
1876
1877 /**
1878 * Callback saying that there aren't any more items to bind.
1879 *
1880 * Implementation of the method from LauncherModel.Callbacks.
1881 */
1882 public void finishBindingItems() {
1883 if (mSavedState != null) {
1884 if (!mWorkspace.hasFocus()) {
1885 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1886 }
1887
1888 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1889 if (userFolders != null) {
1890 for (long folderId : userFolders) {
1891 final FolderInfo info = mFolders.get(folderId);
1892 if (info != null) {
1893 openFolder(info);
1894 }
1895 }
1896 final Folder openFolder = mWorkspace.getOpenFolder();
1897 if (openFolder != null) {
1898 openFolder.requestFocus();
1899 }
1900 }
1901
1902 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1903 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001904 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001905 }
1906
1907 mSavedState = null;
1908 }
1909
1910 if (mSavedInstanceState != null) {
1911 super.onRestoreInstanceState(mSavedInstanceState);
1912 mSavedInstanceState = null;
1913 }
1914
1915 /* TODO
1916 if (mAllAppsVisible && !mDrawer.hasFocus()) {
1917 mDrawer.requestFocus();
1918 }
1919 */
1920
1921 Log.d(TAG, "finishBindingItems done");
1922 mWorkspaceLoading = false;
1923 }
1924
1925 /**
1926 * Add the icons for all apps.
1927 *
1928 * Implementation of the method from LauncherModel.Callbacks.
1929 */
1930 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001931 mAllAppsList = apps;
1932 mAllAppsGrid.setApps(mAllAppsList);
1933 }
1934
1935 /**
1936 * A package was installed.
1937 *
1938 * Implementation of the method from LauncherModel.Callbacks.
1939 */
1940 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1941 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001942 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001943 }
1944
1945 /**
1946 * A package was updated.
1947 *
1948 * Implementation of the method from LauncherModel.Callbacks.
1949 */
1950 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1951 removeDialog(DIALOG_CREATE_SHORTCUT);
1952 mWorkspace.updateShortcutsForPackage(packageName);
1953 }
1954
1955 /**
1956 * A package was uninstalled.
1957 *
1958 * Implementation of the method from LauncherModel.Callbacks.
1959 */
1960 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1961 removeDialog(DIALOG_CREATE_SHORTCUT);
1962 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001963 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965}