blob: 851636681ddf697ce498531de58e66341e7de341 [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.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700532 // Manage focusability manually since this thing is always visible
533 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700534
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
536 final Workspace workspace = mWorkspace;
537
Joe Onorato7c312c12009-08-13 21:36:53 -0700538 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
539 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540
Joe Onorato7c312c12009-08-13 21:36:53 -0700541 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700542 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700543 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700544 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700545 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700546 handleIocon.setCrossFadeEnabled(true);
547 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400550 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552
553 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400554 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700555 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556
Joe Onorato00acb122009-08-04 16:04:30 -0400557 dragController.setDragScoller(workspace);
558 dragController.setDragListener(deleteZone);
559 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700560
Joe Onorato00acb122009-08-04 16:04:30 -0400561 // The order here is bottom to top.
562 dragController.addDropTarget(workspace);
563 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 }
565
566 /**
567 * Creates a view representing a shortcut.
568 *
569 * @param info The data structure describing the shortcut.
570 *
571 * @return A View inflated from R.layout.application.
572 */
573 View createShortcut(ApplicationInfo info) {
574 return createShortcut(R.layout.application,
575 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
576 }
577
578 /**
579 * Creates a view representing a shortcut inflated from the specified resource.
580 *
581 * @param layoutResId The id of the XML layout used to create the shortcut.
582 * @param parent The group the shortcut belongs to.
583 * @param info The data structure describing the shortcut.
584 *
585 * @return A View inflated from layoutResId.
586 */
587 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
588 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
589
Joe Onorato5162ea92009-09-03 09:39:42 -0700590 if (info.icon == null) {
591 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700594 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 info.filtered = true;
596 }
597
598 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
599 favorite.setText(info.title);
600 favorite.setTag(info);
601 favorite.setOnClickListener(this);
602
603 return favorite;
604 }
605
606 /**
607 * Add an application shortcut to the workspace.
608 *
609 * @param data The intent describing the application.
610 * @param cellInfo The position on screen where to create the shortcut.
611 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400612 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 cellInfo.screen = mWorkspace.getCurrentScreen();
614 if (!findSingleSlot(cellInfo)) return;
615
Romain Guy73b979d2009-06-09 12:57:21 -0700616 final ApplicationInfo info = infoFromApplicationIntent(context, data);
617 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400618 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700619 }
620 }
621
622 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 ComponentName component = data.getComponent();
624 PackageManager packageManager = context.getPackageManager();
625 ActivityInfo activityInfo = null;
626 try {
627 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
628 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700629 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 }
Romain Guycbb89e42009-06-08 15:52:54 -0700631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 if (activityInfo != null) {
633 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 itemInfo.title = activityInfo.loadLabel(packageManager);
636 if (itemInfo.title == null) {
637 itemInfo.title = activityInfo.name;
638 }
Romain Guycbb89e42009-06-08 15:52:54 -0700639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
641 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
642 itemInfo.icon = activityInfo.loadIcon(packageManager);
643 itemInfo.container = ItemInfo.NO_ID;
644
Romain Guy73b979d2009-06-09 12:57:21 -0700645 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
Romain Guy73b979d2009-06-09 12:57:21 -0700647
648 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
Romain Guycbb89e42009-06-08 15:52:54 -0700650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 /**
652 * Add a shortcut to the workspace.
653 *
654 * @param data The intent describing the shortcut.
655 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 cellInfo.screen = mWorkspace.getCurrentScreen();
659 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
662
663 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400665 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
666 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 }
668 }
669
Romain Guycbb89e42009-06-08 15:52:54 -0700670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700672 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700674 * @param data The intent describing the appWidgetId.
675 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400677 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700679 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700680
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700681 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700682
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700684
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700685 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700688
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 // Try finding open space on Launcher screen
690 final int[] xy = mCellCoordinates;
691 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
692
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700693 // Build Launcher-specific widget info and save to database
694 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 launcherInfo.spanX = spans[0];
696 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 LauncherModel.addItemToDatabase(this, launcherInfo,
699 LauncherSettings.Favorites.CONTAINER_DESKTOP,
700 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
701
702 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700707
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400712 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
714 }
Romain Guycbb89e42009-06-08 15:52:54 -0700715
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
717 mDesktopItems.remove(launcherInfo);
718 launcherInfo.hostView = null;
719 }
720
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700721 public LauncherAppWidgetHost getAppWidgetHost() {
722 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 }
Romain Guycbb89e42009-06-08 15:52:54 -0700724
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 static ApplicationInfo addShortcut(Context context, Intent data,
726 CellLayout.CellInfo cellInfo, boolean notify) {
727
Romain Guy73b979d2009-06-09 12:57:21 -0700728 final ApplicationInfo info = infoFromShortcutIntent(context, data);
729 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
730 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
731
732 return info;
733 }
734
735 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
737 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
738 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
739
740 Drawable icon = null;
741 boolean filtered = false;
742 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700743 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744
745 if (bitmap != null) {
746 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
747 filtered = true;
748 customIcon = true;
749 } else {
750 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700751 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700753 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 final PackageManager packageManager = context.getPackageManager();
755 Resources resources = packageManager.getResourcesForApplication(
756 iconResource.packageName);
757 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
758 icon = resources.getDrawable(id);
759 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700760 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 }
762 }
763 }
764
765 if (icon == null) {
766 icon = context.getPackageManager().getDefaultActivityIcon();
767 }
768
769 final ApplicationInfo info = new ApplicationInfo();
770 info.icon = icon;
771 info.filtered = filtered;
772 info.title = name;
773 info.intent = intent;
774 info.customIcon = customIcon;
775 info.iconResource = iconResource;
776
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 return info;
778 }
779
780 @Override
781 protected void onNewIntent(Intent intent) {
782 super.onNewIntent(intent);
783
784 // Close the menu
785 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
786 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700787
Joe Onorato742966b2009-10-07 10:26:17 -0700788 // Whatever we were doing is hereby canceled.
789 mWaitingForResult = false;
790
Mike LeBeau736cf282009-07-02 17:46:59 -0700791 // Set this flag so that onResume knows to close the search dialog if it's open,
792 // because this was a new intent (thus a press of 'home' or some such) rather than
793 // for example onResume being called when the user pressed the 'back' button.
794 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795
796 try {
797 dismissDialog(DIALOG_CREATE_SHORTCUT);
798 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 } catch (Exception e) {
800 // An exception is thrown if the dialog is not visible, which is fine
801 }
802
803 try {
804 dismissDialog(DIALOG_RENAME_FOLDER);
805 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 } catch (Exception e) {
807 // An exception is thrown if the dialog is not visible, which is fine
808 }
809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
811 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700812
Romain Guy94dabf12009-07-21 10:55:43 -0700813 if (!mWorkspace.isDefaultScreenShowing()) {
814 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700816
Joe Onorato7bb17492009-09-24 17:51:01 -0700817 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700818
819 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 if (v != null && v.getWindowToken() != null) {
821 InputMethodManager imm = (InputMethodManager)getSystemService(
822 INPUT_METHOD_SERVICE);
823 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
824 }
825 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700826 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 }
828 }
829 }
830
831 @Override
832 protected void onRestoreInstanceState(Bundle savedInstanceState) {
833 // Do not call super here
834 mSavedInstanceState = savedInstanceState;
835 }
836
837 @Override
838 protected void onSaveInstanceState(Bundle outState) {
839 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
840
841 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
842 if (folders.size() > 0) {
843 final int count = folders.size();
844 long[] ids = new long[count];
845 for (int i = 0; i < count; i++) {
846 final FolderInfo info = folders.get(i).getInfo();
847 ids[i] = info.id;
848 }
849 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
850 } else {
851 super.onSaveInstanceState(outState);
852 }
853
Romain Guy3cf604f2009-06-16 13:12:53 -0700854 final boolean isConfigurationChange = getChangingConfigurations() != 0;
855
Romain Guy5a941392009-04-28 15:18:25 -0700856 // When the drawer is opened and we are saving the state because of a
857 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400858 // TODO should not do this if the drawer is currently closing.
859 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700861 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862
863 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
864 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
865 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
866
867 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
868 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
869 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
870 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
871 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
872 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
873 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
874 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
875 layout.getOccupiedCells());
876 }
877
878 if (mFolderInfo != null && mWaitingForResult) {
879 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
880 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
881 }
882 }
883
884 @Override
885 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700887
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700889 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700891 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
893
894 TextKeyListener.getInstance().release();
895
Joe Onorato9c1289c2009-08-17 11:03:03 -0400896 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897
Joe Onorato9c1289c2009-08-17 11:03:03 -0400898 unbindDesktopItems();
899 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 }
901
902 @Override
903 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700904 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 super.startActivityForResult(intent, requestCode);
906 }
907
908 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700909 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700911
Joe Onorato7bb17492009-09-24 17:51:01 -0700912 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700913
Karl Rosaen138a0412009-04-23 19:00:21 -0700914 // Slide the search widget to the top, if it's on the current screen,
915 // otherwise show the search dialog immediately.
916 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
917 if (searchWidget == null) {
918 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
919 } else {
920 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
921 // show the currently typed text in the search widget while sliding
922 searchWidget.setQuery(getTypedText());
923 }
924 }
Romain Guycbb89e42009-06-08 15:52:54 -0700925
Karl Rosaen138a0412009-04-23 19:00:21 -0700926 /**
927 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700928 *
929 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700930 */
Romain Guycbb89e42009-06-08 15:52:54 -0700931 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700932 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700933
Karl Rosaen138a0412009-04-23 19:00:21 -0700934 if (initialQuery == null) {
935 // Use any text typed in the launcher as the initial query
936 initialQuery = getTypedText();
937 clearTypedText();
938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 if (appSearchData == null) {
940 appSearchData = new Bundle();
941 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
942 }
Romain Guycbb89e42009-06-08 15:52:54 -0700943
Karl Rosaen138a0412009-04-23 19:00:21 -0700944 final SearchManager searchManager =
945 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
946
947 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
948 if (searchWidget != null) {
949 // This gets called when the user leaves the search dialog to go back to
950 // the Launcher.
951 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
952 public void onCancel() {
953 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700954 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700955 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700956 });
957 }
Romain Guycbb89e42009-06-08 15:52:54 -0700958
Karl Rosaen138a0412009-04-23 19:00:21 -0700959 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700960 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 }
962
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 /**
964 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700965 */
Romain Guy5a941392009-04-28 15:18:25 -0700966 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 // Close search dialog
968 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100969 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 // Restore search widget to its normal position
971 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
972 if (searchWidget != null) {
973 searchWidget.stopSearch(false);
974 }
975 }
Romain Guycbb89e42009-06-08 15:52:54 -0700976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 @Override
978 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 if (isWorkspaceLocked()) {
980 return false;
981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982
983 super.onCreateOptionsMenu(menu);
984 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
985 .setIcon(android.R.drawable.ic_menu_add)
986 .setAlphabeticShortcut('A');
987 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
988 .setIcon(android.R.drawable.ic_menu_gallery)
989 .setAlphabeticShortcut('W');
990 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
991 .setIcon(android.R.drawable.ic_search_category_default)
992 .setAlphabeticShortcut(SearchManager.MENU_KEY);
993 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
994 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
995 .setAlphabeticShortcut('N');
996
997 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700998 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
999 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000
1001 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1002 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1003 .setIntent(settings);
1004
1005 return true;
1006 }
1007
1008 @Override
1009 public boolean onPrepareOptionsMenu(Menu menu) {
1010 super.onPrepareOptionsMenu(menu);
1011
1012 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1013 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1014
1015 return true;
1016 }
1017
1018 @Override
1019 public boolean onOptionsItemSelected(MenuItem item) {
1020 switch (item.getItemId()) {
1021 case MENU_ADD:
1022 addItems();
1023 return true;
1024 case MENU_WALLPAPER_SETTINGS:
1025 startWallpaper();
1026 return true;
1027 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001028 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 return true;
1030 case MENU_NOTIFICATIONS:
1031 showNotifications();
1032 return true;
1033 }
1034
1035 return super.onOptionsItemSelected(item);
1036 }
Romain Guycbb89e42009-06-08 15:52:54 -07001037
Karl Rosaen138a0412009-04-23 19:00:21 -07001038 /**
1039 * Indicates that we want global search for this activity by setting the globalSearch
1040 * argument for {@link #startSearch} to true.
1041 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001043 @Override
1044 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001045 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001046 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001047 }
1048
Joe Onorato9c1289c2009-08-17 11:03:03 -04001049 public boolean isWorkspaceLocked() {
1050 return mWorkspaceLoading || mWaitingForResult;
1051 }
1052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 private void addItems() {
1054 showAddDialog(mMenuAddInfo);
1055 }
1056
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001057 void addAppWidget(Intent data) {
1058 // TODO: catch bad widget exception when sent
1059 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001061 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1062 if (SEARCH_WIDGET.equals(customWidget)) {
1063 // We don't need this any more, since this isn't a real app widget.
1064 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1065 // add the search widget
1066 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001068 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1069
1070 if (appWidget.configure != null) {
1071 // Launch over to configure widget, if needed
1072 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1073 intent.setComponent(appWidget.configure);
1074 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1075
1076 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1077 } else {
1078 // Otherwise just add it
1079 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1080 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 }
1082 }
Romain Guycbb89e42009-06-08 15:52:54 -07001083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 void addSearch() {
1085 final Widget info = Widget.makeSearch();
1086 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 final int[] xy = mCellCoordinates;
1089 final int spanX = info.spanX;
1090 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1095 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 final View view = mInflater.inflate(info.layoutResource, null);
1098 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001099 Search search = (Search) view.findViewById(R.id.widget_search);
1100 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1103 }
1104
Romain Guy73b979d2009-06-09 12:57:21 -07001105 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001106 // Handle case where user selected "Applications"
1107 String applicationName = getResources().getString(R.string.group_applications);
1108 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001109
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001110 if (applicationName != null && applicationName.equals(shortcutName)) {
1111 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1112 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001113
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001114 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1115 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001116 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001117 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001118 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001119 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 }
1121
1122 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001123 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001124 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001125 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001126
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001127 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001128 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001129 } else {
1130 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 }
1133
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 UserFolderInfo folderInfo = new UserFolderInfo();
1136 folderInfo.title = getText(R.string.folder_name);
1137
1138 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1139 cellInfo.screen = mWorkspace.getCurrentScreen();
1140 if (!findSingleSlot(cellInfo)) return;
1141
1142 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143 LauncherModel.addItemToDatabase(this, folderInfo,
1144 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001146 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147
1148 // Create the view
1149 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1150 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1151 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001152 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 }
Romain Guycbb89e42009-06-08 15:52:54 -07001154
Joe Onorato9c1289c2009-08-17 11:03:03 -04001155 void removeFolder(FolderInfo folder) {
1156 mFolders.remove(folder.id);
1157 }
1158
1159 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 cellInfo.screen = mWorkspace.getCurrentScreen();
1161 if (!findSingleSlot(cellInfo)) return;
1162
1163 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1164
1165 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1167 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001168 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1169 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 }
1171 }
1172
1173 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1174 CellLayout.CellInfo cellInfo, boolean notify) {
1175
1176 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1177 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1178
1179 Drawable icon = null;
1180 boolean filtered = false;
1181 Intent.ShortcutIconResource iconResource = null;
1182
1183 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1184 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1185 try {
1186 iconResource = (Intent.ShortcutIconResource) extra;
1187 final PackageManager packageManager = context.getPackageManager();
1188 Resources resources = packageManager.getResourcesForApplication(
1189 iconResource.packageName);
1190 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1191 icon = resources.getDrawable(id);
1192 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001193 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 }
1195 }
1196
1197 if (icon == null) {
1198 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1199 }
1200
1201 final LiveFolderInfo info = new LiveFolderInfo();
1202 info.icon = icon;
1203 info.filtered = filtered;
1204 info.title = name;
1205 info.iconResource = iconResource;
1206 info.uri = data.getData();
1207 info.baseIntent = baseIntent;
1208 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1209 LiveFolders.DISPLAY_MODE_GRID);
1210
1211 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1212 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001213 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214
1215 return info;
1216 }
1217
1218 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1219 final int[] xy = new int[2];
1220 if (findSlot(cellInfo, xy, 1, 1)) {
1221 cellInfo.cellX = xy[0];
1222 cellInfo.cellY = xy[1];
1223 return true;
1224 }
1225 return false;
1226 }
1227
1228 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1229 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1230 boolean[] occupied = mSavedState != null ?
1231 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1232 cellInfo = mWorkspace.findAllVacantCells(occupied);
1233 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1234 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1235 return false;
1236 }
1237 }
1238 return true;
1239 }
1240
1241 private void showNotifications() {
1242 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1243 if (statusBar != null) {
1244 statusBar.expand();
1245 }
1246 }
1247
1248 private void startWallpaper() {
1249 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001250 Intent chooser = Intent.createChooser(pickWallpaper,
1251 getText(R.string.chooser_wallpaper));
1252 WallpaperManager wm = (WallpaperManager)
1253 getSystemService(Context.WALLPAPER_SERVICE);
1254 WallpaperInfo wi = wm.getWallpaperInfo();
1255 if (wi != null && wi.getSettingsActivity() != null) {
1256 LabeledIntent li = new LabeledIntent(getPackageName(),
1257 R.string.configure_wallpaper, 0);
1258 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1259 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1260 }
1261 startActivity(chooser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
1263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001265 public void onWindowFocusChanged(boolean hasFocus) {
1266 super.onWindowFocusChanged(hasFocus);
1267 if (!hasFocus) {
1268 mBackDown = mHomeDown = false;
1269 }
1270 }
1271
1272 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 public boolean dispatchKeyEvent(KeyEvent event) {
1274 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1275 switch (event.getKeyCode()) {
1276 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001277 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 return true;
1279 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001280 mHomeDown = true;
1281 return true;
1282 }
1283 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1284 switch (event.getKeyCode()) {
1285 case KeyEvent.KEYCODE_BACK:
1286 if (!event.isCanceled()) {
1287 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001288 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001289 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001290 } else {
1291 closeFolder();
1292 }
1293 }
1294 mBackDown = false;
1295 return true;
1296 case KeyEvent.KEYCODE_HOME:
1297 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 return true;
1299 }
1300 }
1301
1302 return super.dispatchKeyEvent(event);
1303 }
1304
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 private void closeFolder() {
1306 Folder folder = mWorkspace.getOpenFolder();
1307 if (folder != null) {
1308 closeFolder(folder);
1309 }
1310 }
1311
1312 void closeFolder(Folder folder) {
1313 folder.getInfo().opened = false;
1314 ViewGroup parent = (ViewGroup) folder.getParent();
1315 if (parent != null) {
1316 parent.removeView(folder);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317 // TODO: this line crashes.
1318 //mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320 folder.onClose();
1321 }
1322
1323 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001324 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1325 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001327 private void unbindDesktopItems() {
1328 for (ItemInfo item: mDesktopItems) {
1329 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
1331 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 /**
1334 * Launches the intent referred by the clicked shortcut.
1335 *
1336 * @param v The view representing the clicked shortcut.
1337 */
1338 public void onClick(View v) {
1339 Object tag = v.getTag();
1340 if (tag instanceof ApplicationInfo) {
1341 // Open shortcut
1342 final Intent intent = ((ApplicationInfo) tag).intent;
1343 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001344 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 } else if (tag instanceof FolderInfo) {
1346 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001347 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001348 Log.d(TAG, "onClick");
1349 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001350 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001351 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001352 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001353 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 }
1355 }
1356
1357 void startActivitySafely(Intent intent) {
1358 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1359 try {
1360 startActivity(intent);
1361 } catch (ActivityNotFoundException e) {
1362 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1363 } catch (SecurityException e) {
1364 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001365 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1367 "or use the exported attribute for this activity.", e);
1368 }
1369 }
1370
1371 private void handleFolderClick(FolderInfo folderInfo) {
1372 if (!folderInfo.opened) {
1373 // Close any open folder
1374 closeFolder();
1375 // Open the requested folder
1376 openFolder(folderInfo);
1377 } else {
1378 // Find the open folder...
1379 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1380 int folderScreen;
1381 if (openFolder != null) {
1382 folderScreen = mWorkspace.getScreenForView(openFolder);
1383 // .. and close it
1384 closeFolder(openFolder);
1385 if (folderScreen != mWorkspace.getCurrentScreen()) {
1386 // Close any folder open on the current screen
1387 closeFolder();
1388 // Pull the folder onto this screen
1389 openFolder(folderInfo);
1390 }
1391 }
1392 }
1393 }
1394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 /**
1396 * Opens the user fodler described by the specified tag. The opening of the folder
1397 * is animated relative to the specified View. If the View is null, no animation
1398 * is played.
1399 *
1400 * @param folderInfo The FolderInfo describing the folder to open.
1401 */
1402 private void openFolder(FolderInfo folderInfo) {
1403 Folder openFolder;
1404
1405 if (folderInfo instanceof UserFolderInfo) {
1406 openFolder = UserFolder.fromXml(this);
1407 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001408 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 } else {
1410 return;
1411 }
1412
Joe Onorato00acb122009-08-04 16:04:30 -04001413 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 openFolder.setLauncher(this);
1415
1416 openFolder.bind(folderInfo);
1417 folderInfo.opened = true;
1418
1419 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1420 openFolder.onOpen();
1421 }
1422
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001424 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 return false;
1426 }
1427
1428 if (!(v instanceof CellLayout)) {
1429 v = (View) v.getParent();
1430 }
1431
1432 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1433
1434 // This happens when long clicking an item with the dpad/trackball
1435 if (cellInfo == null) {
1436 return true;
1437 }
1438
1439 if (mWorkspace.allowLongPress()) {
1440 if (cellInfo.cell == null) {
1441 if (cellInfo.valid) {
1442 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001443 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 showAddDialog(cellInfo);
1445 }
1446 } else {
1447 if (!(cellInfo.cell instanceof Folder)) {
1448 // User long pressed on an item
1449 mWorkspace.startDrag(cellInfo);
1450 }
1451 }
1452 }
1453 return true;
1454 }
1455
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001456 View getDrawerHandle() {
1457 return mHandleView;
1458 }
1459
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 Workspace getWorkspace() {
1461 return mWorkspace;
1462 }
1463
Joe Onorato7404ee42009-07-31 11:54:44 -07001464 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 GridView getApplicationsGrid() {
1466 return mAllAppsGrid;
1467 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001468 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469
1470 @Override
1471 protected Dialog onCreateDialog(int id) {
1472 switch (id) {
1473 case DIALOG_CREATE_SHORTCUT:
1474 return new CreateShortcut().createDialog();
1475 case DIALOG_RENAME_FOLDER:
1476 return new RenameFolder().createDialog();
1477 }
1478
1479 return super.onCreateDialog(id);
1480 }
1481
1482 @Override
1483 protected void onPrepareDialog(int id, Dialog dialog) {
1484 switch (id) {
1485 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 break;
1487 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001488 if (mFolderInfo != null) {
1489 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1490 final CharSequence text = mFolderInfo.title;
1491 input.setText(text);
1492 input.setSelection(0, text.length());
1493 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 break;
1495 }
1496 }
1497
1498 void showRenameDialog(FolderInfo info) {
1499 mFolderInfo = info;
1500 mWaitingForResult = true;
1501 showDialog(DIALOG_RENAME_FOLDER);
1502 }
1503
1504 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1505 mAddItemCellInfo = cellInfo;
1506 mWaitingForResult = true;
1507 showDialog(DIALOG_CREATE_SHORTCUT);
1508 }
1509
Romain Guy73b979d2009-06-09 12:57:21 -07001510 private void pickShortcut(int requestCode, int title) {
1511 Bundle bundle = new Bundle();
1512
1513 ArrayList<String> shortcutNames = new ArrayList<String>();
1514 shortcutNames.add(getString(R.string.group_applications));
1515 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1516
1517 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1518 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1519 R.drawable.ic_launcher_application));
1520 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1521
1522 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1523 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1524 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1525 pickIntent.putExtras(bundle);
1526
1527 startActivityForResult(pickIntent, requestCode);
1528 }
1529
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 private class RenameFolder {
1531 private EditText mInput;
1532
1533 Dialog createDialog() {
1534 mWaitingForResult = true;
1535 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1536 mInput = (EditText) layout.findViewById(R.id.folder_name);
1537
1538 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1539 builder.setIcon(0);
1540 builder.setTitle(getString(R.string.rename_folder_title));
1541 builder.setCancelable(true);
1542 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1543 public void onCancel(DialogInterface dialog) {
1544 cleanup();
1545 }
1546 });
1547 builder.setNegativeButton(getString(R.string.cancel_action),
1548 new Dialog.OnClickListener() {
1549 public void onClick(DialogInterface dialog, int which) {
1550 cleanup();
1551 }
1552 }
1553 );
1554 builder.setPositiveButton(getString(R.string.rename_action),
1555 new Dialog.OnClickListener() {
1556 public void onClick(DialogInterface dialog, int which) {
1557 changeFolderName();
1558 }
1559 }
1560 );
1561 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001562
1563 final AlertDialog dialog = builder.create();
1564 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1565 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001566 }
1567 });
1568
1569 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
1571
1572 private void changeFolderName() {
1573 final String name = mInput.getText().toString();
1574 if (!TextUtils.isEmpty(name)) {
1575 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001576 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 mFolderInfo.title = name;
1578 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1579
Joe Onorato9c1289c2009-08-17 11:03:03 -04001580 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001581 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001582 mModel.setWorkspaceDirty();
1583 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 } else {
1585 final FolderIcon folderIcon = (FolderIcon)
1586 mWorkspace.getViewForTag(mFolderInfo);
1587 if (folderIcon != null) {
1588 folderIcon.setText(name);
1589 getWorkspace().requestLayout();
1590 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001591 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001592 mModel.setWorkspaceDirty();
1593 mWorkspaceLoading = true;
1594 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
1596 }
1597 }
1598 cleanup();
1599 }
1600
1601 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 dismissDialog(DIALOG_RENAME_FOLDER);
1603 mWaitingForResult = false;
1604 mFolderInfo = null;
1605 }
1606 }
1607
Joe Onoratofb0ca672009-09-14 17:55:46 -04001608 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001609 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001610 }
1611
Jason Samsfd22dac2009-09-20 17:24:16 -07001612 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001613 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001614 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001615
Mike Cleronb6082fa02009-10-19 17:03:36 -07001616 mAllAppsGrid.setFocusable(true);
1617
Joe Onorato7c312c12009-08-13 21:36:53 -07001618 // TODO: fade these two too
1619 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001620 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001621 }
1622
Joe Onorato7bb17492009-09-24 17:51:01 -07001623 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001624 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001625 mAllAppsGrid.zoom(0.0f);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001626 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001627 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001628
Mike Cleronb6082fa02009-10-19 17:03:36 -07001629
Joe Onorato7c312c12009-08-13 21:36:53 -07001630 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001631 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001632 mDeleteZone.setVisibility(View.VISIBLE);
1633 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001634 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001635 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001636 }
1637
Joe Onorato7c312c12009-08-13 21:36:53 -07001638 void lockAllApps() {
1639 // TODO
1640 }
1641
1642 void unlockAllApps() {
1643 // TODO
1644 }
1645
Joe Onorato7404ee42009-07-31 11:54:44 -07001646 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 * Displays the shortcut creation dialog and launches, if necessary, the
1648 * appropriate activity.
1649 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001650 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001651 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1652 DialogInterface.OnShowListener {
1653
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001655
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 Dialog createDialog() {
1657 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1662 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001663 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001664
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 builder.setInverseBackgroundForced(true);
1666
1667 AlertDialog dialog = builder.create();
1668 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001669 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001670 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 return dialog;
1673 }
1674
1675 public void onCancel(DialogInterface dialog) {
1676 mWaitingForResult = false;
1677 cleanup();
1678 }
1679
Romain Guycbb89e42009-06-08 15:52:54 -07001680 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001681 }
1682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 dismissDialog(DIALOG_CREATE_SHORTCUT);
1685 }
1686
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001687 /**
1688 * Handle the action clicked in the "Add to home" dialog.
1689 */
1690 public void onClick(DialogInterface dialog, int which) {
1691 Resources res = getResources();
1692 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001693
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001694 switch (which) {
1695 case AddAdapter.ITEM_SHORTCUT: {
1696 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001697 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001698 break;
1699 }
Romain Guycbb89e42009-06-08 15:52:54 -07001700
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001701 case AddAdapter.ITEM_APPWIDGET: {
1702 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001703
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001704 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1705 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1706 // add the search widget
1707 ArrayList<AppWidgetProviderInfo> customInfo =
1708 new ArrayList<AppWidgetProviderInfo>();
1709 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1710 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1711 info.label = getString(R.string.group_search);
1712 info.icon = R.drawable.ic_search_widget;
1713 customInfo.add(info);
1714 pickIntent.putParcelableArrayListExtra(
1715 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1716 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1717 Bundle b = new Bundle();
1718 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1719 customExtras.add(b);
1720 pickIntent.putParcelableArrayListExtra(
1721 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1722 // start the pick activity
1723 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1724 break;
1725 }
Romain Guycbb89e42009-06-08 15:52:54 -07001726
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001727 case AddAdapter.ITEM_LIVE_FOLDER: {
1728 // Insert extra item to handle inserting folder
1729 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001730
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001731 ArrayList<String> shortcutNames = new ArrayList<String>();
1732 shortcutNames.add(res.getString(R.string.group_folder));
1733 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001734
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001735 ArrayList<ShortcutIconResource> shortcutIcons =
1736 new ArrayList<ShortcutIconResource>();
1737 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1738 R.drawable.ic_launcher_folder));
1739 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1740
1741 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1742 pickIntent.putExtra(Intent.EXTRA_INTENT,
1743 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1744 pickIntent.putExtra(Intent.EXTRA_TITLE,
1745 getText(R.string.title_select_live_folder));
1746 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001747
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001748 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1749 break;
1750 }
1751
1752 case AddAdapter.ITEM_WALLPAPER: {
1753 startWallpaper();
1754 break;
1755 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 }
1757 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001758
1759 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 }
1762
1763 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001764 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766 public int getCurrentWorkspaceScreen() {
1767 return mWorkspace.getCurrentScreen();
1768 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001769
Joe Onorato9c1289c2009-08-17 11:03:03 -04001770 /**
1771 * Refreshes the shortcuts shown on the workspace.
1772 *
1773 * Implementation of the method from LauncherModel.Callbacks.
1774 */
1775 public void startBinding() {
1776 final Workspace workspace = mWorkspace;
1777 int count = workspace.getChildCount();
1778 for (int i = 0; i < count; i++) {
1779 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1780 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001781
Joe Onorato9c1289c2009-08-17 11:03:03 -04001782 if (DEBUG_USER_INTERFACE) {
1783 android.widget.Button finishButton = new android.widget.Button(this);
1784 finishButton.setText("Finish");
1785 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1786
1787 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1788 public void onClick(View v) {
1789 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001790 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001791 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 }
1793 }
1794
1795 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001796 * Bind the items start-end from the list.
1797 *
1798 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001800 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1801
1802 final Workspace workspace = mWorkspace;
1803
1804 for (int i=start; i<end; i++) {
1805 final ItemInfo item = shortcuts.get(i);
1806 mDesktopItems.add(item);
1807 switch (item.itemType) {
1808 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1809 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1810 final View shortcut = createShortcut((ApplicationInfo) item);
1811 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1812 false);
1813 break;
1814 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1815 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1816 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1817 (UserFolderInfo) item);
1818 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1819 false);
1820 break;
1821 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1822 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1823 R.layout.live_folder_icon, this,
1824 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1825 (LiveFolderInfo) item);
1826 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1827 false);
1828 break;
1829 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1830 final int screen = workspace.getCurrentScreen();
1831 final View view = mInflater.inflate(R.layout.widget_search,
1832 (ViewGroup) workspace.getChildAt(screen), false);
1833
1834 Search search = (Search) view.findViewById(R.id.widget_search);
1835 search.setLauncher(this);
1836
1837 final Widget widget = (Widget) item;
1838 view.setTag(widget);
1839
1840 workspace.addWidget(view, widget, false);
1841 break;
1842 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 }
1844
Joe Onorato9c1289c2009-08-17 11:03:03 -04001845 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 }
1847
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 /**
1849 * Implementation of the method from LauncherModel.Callbacks.
1850 */
1851 void bindFolders(HashMap<Long, FolderInfo> folders) {
1852 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001854
1855 /**
1856 * Add the views for a widget to the workspace.
1857 *
1858 * Implementation of the method from LauncherModel.Callbacks.
1859 */
1860 public void bindAppWidget(LauncherAppWidgetInfo item) {
1861 final Workspace workspace = mWorkspace;
1862
1863 final int appWidgetId = item.appWidgetId;
1864 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1865 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1866
1867 if (true) {
1868 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1869 appWidgetId, appWidgetInfo));
1870 }
1871
1872 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1873 item.hostView.setTag(item);
1874
1875 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1876 item.cellY, item.spanX, item.spanY, false);
1877
1878 workspace.requestLayout();
1879
1880 mDesktopItems.add(item);
1881 }
1882
1883 /**
1884 * Callback saying that there aren't any more items to bind.
1885 *
1886 * Implementation of the method from LauncherModel.Callbacks.
1887 */
1888 public void finishBindingItems() {
1889 if (mSavedState != null) {
1890 if (!mWorkspace.hasFocus()) {
1891 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1892 }
1893
1894 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1895 if (userFolders != null) {
1896 for (long folderId : userFolders) {
1897 final FolderInfo info = mFolders.get(folderId);
1898 if (info != null) {
1899 openFolder(info);
1900 }
1901 }
1902 final Folder openFolder = mWorkspace.getOpenFolder();
1903 if (openFolder != null) {
1904 openFolder.requestFocus();
1905 }
1906 }
1907
1908 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1909 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001910 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001911 }
1912
1913 mSavedState = null;
1914 }
1915
1916 if (mSavedInstanceState != null) {
1917 super.onRestoreInstanceState(mSavedInstanceState);
1918 mSavedInstanceState = null;
1919 }
1920
1921 /* TODO
1922 if (mAllAppsVisible && !mDrawer.hasFocus()) {
1923 mDrawer.requestFocus();
1924 }
1925 */
1926
1927 Log.d(TAG, "finishBindingItems done");
1928 mWorkspaceLoading = false;
1929 }
1930
1931 /**
1932 * Add the icons for all apps.
1933 *
1934 * Implementation of the method from LauncherModel.Callbacks.
1935 */
1936 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001937 mAllAppsList = apps;
1938 mAllAppsGrid.setApps(mAllAppsList);
1939 }
1940
1941 /**
1942 * A package was installed.
1943 *
1944 * Implementation of the method from LauncherModel.Callbacks.
1945 */
1946 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1947 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001948 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001949 }
1950
1951 /**
1952 * A package was updated.
1953 *
1954 * Implementation of the method from LauncherModel.Callbacks.
1955 */
1956 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1957 removeDialog(DIALOG_CREATE_SHORTCUT);
1958 mWorkspace.updateShortcutsForPackage(packageName);
1959 }
1960
1961 /**
1962 * A package was uninstalled.
1963 *
1964 * Implementation of the method from LauncherModel.Callbacks.
1965 */
1966 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1967 removeDialog(DIALOG_CREATE_SHORTCUT);
1968 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001969 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971}