blob: 4fe655eb21d8b368893bf528989bd1d643ffe317 [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;
186
Dianne Hackborn67800862009-07-24 17:15:20 -0700187 private boolean mHomeDown;
188 private boolean mBackDown;
Jason Samsfd22dac2009-09-20 17:24:16 -0700189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private Bundle mSavedInstanceState;
191
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 private LauncherModel mModel;
193
194 private ArrayList<ItemInfo> mDesktopItems = new ArrayList();
195 private static HashMap<Long, FolderInfo> mFolders = new HashMap();
196 private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList();
Romain Guycbb89e42009-06-08 15:52:54 -0700197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 @Override
199 protected void onCreate(Bundle savedInstanceState) {
200 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700201
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700204
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700205 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700206
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700207 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
208 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 if (PROFILE_STARTUP) {
211 android.os.Debug.startMethodTracing("/sdcard/launcher");
212 }
213
214 checkForLocaleChange();
215 setWallpaperDimension();
216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 setContentView(R.layout.launcher);
218 setupViews();
219
Joe Onorato7c312c12009-08-13 21:36:53 -0700220 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 mSavedState = savedInstanceState;
223 restoreState(mSavedState);
224
225 if (PROFILE_STARTUP) {
226 android.os.Debug.stopMethodTracing();
227 }
228
229 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 }
232
233 // For handling default keys
234 mDefaultKeySsb = new SpannableStringBuilder();
235 Selection.setSelection(mDefaultKeySsb, 0);
236 }
Romain Guycbb89e42009-06-08 15:52:54 -0700237
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700239 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
240 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 final Configuration configuration = getResources().getConfiguration();
243
Romain Guy98d01652009-06-30 16:21:04 -0700244 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 final String locale = configuration.locale.toString();
246
Romain Guy98d01652009-06-30 16:21:04 -0700247 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 final int mcc = configuration.mcc;
249
Romain Guy98d01652009-06-30 16:21:04 -0700250 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 final int mnc = configuration.mnc;
252
253 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
254
255 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700256 localeConfiguration.locale = locale;
257 localeConfiguration.mcc = mcc;
258 localeConfiguration.mnc = mnc;
259
260 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400261 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700262 }
263 }
264
265 private static class LocaleConfiguration {
266 public String locale;
267 public int mcc = -1;
268 public int mnc = -1;
269 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700270
Romain Guy98d01652009-06-30 16:21:04 -0700271 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
272 DataInputStream in = null;
273 try {
274 in = new DataInputStream(context.openFileInput(PREFERENCES));
275 configuration.locale = in.readUTF();
276 configuration.mcc = in.readInt();
277 configuration.mnc = in.readInt();
278 } catch (FileNotFoundException e) {
279 // Ignore
280 } catch (IOException e) {
281 // Ignore
282 } finally {
283 if (in != null) {
284 try {
285 in.close();
286 } catch (IOException e) {
287 // Ignore
288 }
289 }
290 }
291 }
292
293 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
294 DataOutputStream out = null;
295 try {
296 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
297 out.writeUTF(configuration.locale);
298 out.writeInt(configuration.mcc);
299 out.writeInt(configuration.mnc);
300 out.flush();
301 } catch (FileNotFoundException e) {
302 // Ignore
303 } catch (IOException e) {
304 //noinspection ResultOfMethodCallIgnored
305 context.getFileStreamPath(PREFERENCES).delete();
306 } finally {
307 if (out != null) {
308 try {
309 out.close();
310 } catch (IOException e) {
311 // Ignore
312 }
313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 }
315 }
316
317 static int getScreen() {
318 synchronized (sLock) {
319 return sScreen;
320 }
321 }
322
323 static void setScreen(int screen) {
324 synchronized (sLock) {
325 sScreen = screen;
326 }
327 }
328
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700330 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331
332 Display display = getWindowManager().getDefaultDisplay();
333 boolean isPortrait = display.getWidth() < display.getHeight();
334
335 final int width = isPortrait ? display.getWidth() : display.getHeight();
336 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700337 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 }
339
340 @Override
341 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700342 mWaitingForResult = false;
343
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 // The pattern used here is that a user PICKs a specific application,
345 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700346
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
348 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700349
Romain Guy94dabf12009-07-21 10:55:43 -0700350 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 switch (requestCode) {
352 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400353 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 break;
355 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700356 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 break;
358 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400359 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 break;
361 case REQUEST_PICK_LIVE_FOLDER:
362 addLiveFolder(data);
363 break;
364 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400365 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700367 case REQUEST_PICK_APPWIDGET:
368 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700370 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400371 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 break;
373 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700374 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700376 // Clean up the appWidgetId if we canceled
377 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
378 if (appWidgetId != -1) {
379 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 }
381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 }
383
384 @Override
385 protected void onResume() {
386 super.onResume();
387
388 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400389 mWorkspaceLoading = true;
390 mModel.startLoader(this, true);
391 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700393
Mike LeBeau736cf282009-07-02 17:46:59 -0700394 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
395 // onNewIntent), then close the search dialog if needed, because it probably
396 // came from the user pressing 'home' (rather than, for example, pressing 'back').
397 if (mIsNewIntent) {
398 // Post to a handler so that this happens after the search dialog tries to open
399 // itself again.
400 mWorkspace.post(new Runnable() {
401 public void run() {
402 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
403 ServiceManager.getService(Context.SEARCH_SERVICE));
404 try {
405 searchManagerService.stopSearch();
406 } catch (RemoteException e) {
407 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700408 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700409 }
410 });
411 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700412
Mike LeBeau736cf282009-07-02 17:46:59 -0700413 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 }
415
416 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700417 protected void onPause() {
418 super.onPause();
Joe Onorato7bb17492009-09-24 17:51:01 -0700419 closeAllApps(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700420 }
Romain Guycbb89e42009-06-08 15:52:54 -0700421
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700422 @Override
423 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400424 // Flag the loader to stop early before switching
425 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700427 if (PROFILE_ROTATE) {
428 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
429 }
430 return null;
431 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700432
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 private boolean acceptFilter() {
434 final InputMethodManager inputManager = (InputMethodManager)
435 getSystemService(Context.INPUT_METHOD_SERVICE);
436 return !inputManager.isFullscreenMode();
437 }
438
439 @Override
440 public boolean onKeyDown(int keyCode, KeyEvent event) {
441 boolean handled = super.onKeyDown(keyCode, event);
442 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
443 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
444 keyCode, event);
445 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700446 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700447 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700448 // showSearchDialog()
449 // If there are multiple keystrokes before the search dialog takes focus,
450 // onSearchRequested() will be called for every keystroke,
451 // but it is idempotent, so it's fine.
452 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454 }
455
456 return handled;
457 }
458
Karl Rosaen138a0412009-04-23 19:00:21 -0700459 private String getTypedText() {
460 return mDefaultKeySsb.toString();
461 }
462
463 private void clearTypedText() {
464 mDefaultKeySsb.clear();
465 mDefaultKeySsb.clearSpans();
466 Selection.setSelection(mDefaultKeySsb, 0);
467 }
468
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 /**
470 * Restores the previous state, if it exists.
471 *
472 * @param savedState The previous state.
473 */
474 private void restoreState(Bundle savedState) {
475 if (savedState == null) {
476 return;
477 }
478
479 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
480 if (currentScreen > -1) {
481 mWorkspace.setCurrentScreen(currentScreen);
482 }
483
484 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
485 if (addScreen > -1) {
486 mAddItemCellInfo = new CellLayout.CellInfo();
487 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
488 addItemCellInfo.valid = true;
489 addItemCellInfo.screen = addScreen;
490 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
491 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
492 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
493 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
494 addItemCellInfo.findVacantCellsFromOccupied(
495 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
496 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
497 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
498 mRestoring = true;
499 }
500
501 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
502 if (renameFolder) {
503 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400504 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 mRestoring = true;
506 }
507 }
508
509 /**
510 * Finds all the views we need and configure them properly.
511 */
512 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400513 mDragController = new DragController(this);
514 DragController dragController = mDragController;
515
Romain Guyb1b69f52009-08-10 15:10:15 -0700516 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400517 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518
Joe Onorato7c312c12009-08-13 21:36:53 -0700519 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700520 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700521 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700522 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Joe Onorato7c312c12009-08-13 21:36:53 -0700523
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
525 final Workspace workspace = mWorkspace;
526
Joe Onorato7c312c12009-08-13 21:36:53 -0700527 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
528 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529
Joe Onorato7c312c12009-08-13 21:36:53 -0700530 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700531 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700532 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700533 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700534 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700535 handleIocon.setCrossFadeEnabled(true);
536 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400539 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541
542 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400543 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700544 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545
Joe Onorato00acb122009-08-04 16:04:30 -0400546 dragController.setDragScoller(workspace);
547 dragController.setDragListener(deleteZone);
548 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700549
Joe Onorato00acb122009-08-04 16:04:30 -0400550 // The order here is bottom to top.
551 dragController.addDropTarget(workspace);
552 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 }
554
555 /**
556 * Creates a view representing a shortcut.
557 *
558 * @param info The data structure describing the shortcut.
559 *
560 * @return A View inflated from R.layout.application.
561 */
562 View createShortcut(ApplicationInfo info) {
563 return createShortcut(R.layout.application,
564 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
565 }
566
567 /**
568 * Creates a view representing a shortcut inflated from the specified resource.
569 *
570 * @param layoutResId The id of the XML layout used to create the shortcut.
571 * @param parent The group the shortcut belongs to.
572 * @param info The data structure describing the shortcut.
573 *
574 * @return A View inflated from layoutResId.
575 */
576 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
577 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
578
Joe Onorato5162ea92009-09-03 09:39:42 -0700579 if (info.icon == null) {
580 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
581 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700583 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 info.filtered = true;
585 }
586
587 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
588 favorite.setText(info.title);
589 favorite.setTag(info);
590 favorite.setOnClickListener(this);
591
592 return favorite;
593 }
594
595 /**
596 * Add an application shortcut to the workspace.
597 *
598 * @param data The intent describing the application.
599 * @param cellInfo The position on screen where to create the shortcut.
600 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400601 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 cellInfo.screen = mWorkspace.getCurrentScreen();
603 if (!findSingleSlot(cellInfo)) return;
604
Romain Guy73b979d2009-06-09 12:57:21 -0700605 final ApplicationInfo info = infoFromApplicationIntent(context, data);
606 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400607 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700608 }
609 }
610
611 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 ComponentName component = data.getComponent();
613 PackageManager packageManager = context.getPackageManager();
614 ActivityInfo activityInfo = null;
615 try {
616 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
617 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700618 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 }
Romain Guycbb89e42009-06-08 15:52:54 -0700620
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 if (activityInfo != null) {
622 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 itemInfo.title = activityInfo.loadLabel(packageManager);
625 if (itemInfo.title == null) {
626 itemInfo.title = activityInfo.name;
627 }
Romain Guycbb89e42009-06-08 15:52:54 -0700628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
630 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
631 itemInfo.icon = activityInfo.loadIcon(packageManager);
632 itemInfo.container = ItemInfo.NO_ID;
633
Romain Guy73b979d2009-06-09 12:57:21 -0700634 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 }
Romain Guy73b979d2009-06-09 12:57:21 -0700636
637 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 }
Romain Guycbb89e42009-06-08 15:52:54 -0700639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 /**
641 * Add a shortcut to the workspace.
642 *
643 * @param data The intent describing the shortcut.
644 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400646 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 cellInfo.screen = mWorkspace.getCurrentScreen();
648 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
651
652 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
655 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 }
657 }
658
Romain Guycbb89e42009-06-08 15:52:54 -0700659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700661 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700663 * @param data The intent describing the appWidgetId.
664 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700668 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700669
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700670 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700671
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700672 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700673
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700674 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700677
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 // Try finding open space on Launcher screen
679 final int[] xy = mCellCoordinates;
680 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
681
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700682 // Build Launcher-specific widget info and save to database
683 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 launcherInfo.spanX = spans[0];
685 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700686
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 LauncherModel.addItemToDatabase(this, launcherInfo,
688 LauncherSettings.Favorites.CONTAINER_DESKTOP,
689 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
690
691 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400692 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700695 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700696
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700699
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 }
703 }
Romain Guycbb89e42009-06-08 15:52:54 -0700704
Joe Onorato9c1289c2009-08-17 11:03:03 -0400705 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
706 mDesktopItems.remove(launcherInfo);
707 launcherInfo.hostView = null;
708 }
709
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700710 public LauncherAppWidgetHost getAppWidgetHost() {
711 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 static ApplicationInfo addShortcut(Context context, Intent data,
715 CellLayout.CellInfo cellInfo, boolean notify) {
716
Romain Guy73b979d2009-06-09 12:57:21 -0700717 final ApplicationInfo info = infoFromShortcutIntent(context, data);
718 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
719 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
720
721 return info;
722 }
723
724 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
726 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
727 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
728
729 Drawable icon = null;
730 boolean filtered = false;
731 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700732 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733
734 if (bitmap != null) {
735 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
736 filtered = true;
737 customIcon = true;
738 } else {
739 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700740 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700742 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 final PackageManager packageManager = context.getPackageManager();
744 Resources resources = packageManager.getResourcesForApplication(
745 iconResource.packageName);
746 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
747 icon = resources.getDrawable(id);
748 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700749 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
751 }
752 }
753
754 if (icon == null) {
755 icon = context.getPackageManager().getDefaultActivityIcon();
756 }
757
758 final ApplicationInfo info = new ApplicationInfo();
759 info.icon = icon;
760 info.filtered = filtered;
761 info.title = name;
762 info.intent = intent;
763 info.customIcon = customIcon;
764 info.iconResource = iconResource;
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 return info;
767 }
768
769 @Override
770 protected void onNewIntent(Intent intent) {
771 super.onNewIntent(intent);
772
773 // Close the menu
774 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
775 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700776
Joe Onorato742966b2009-10-07 10:26:17 -0700777 // Whatever we were doing is hereby canceled.
778 mWaitingForResult = false;
779
Mike LeBeau736cf282009-07-02 17:46:59 -0700780 // Set this flag so that onResume knows to close the search dialog if it's open,
781 // because this was a new intent (thus a press of 'home' or some such) rather than
782 // for example onResume being called when the user pressed the 'back' button.
783 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784
785 try {
786 dismissDialog(DIALOG_CREATE_SHORTCUT);
787 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 } catch (Exception e) {
789 // An exception is thrown if the dialog is not visible, which is fine
790 }
791
792 try {
793 dismissDialog(DIALOG_RENAME_FOLDER);
794 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 } catch (Exception e) {
796 // An exception is thrown if the dialog is not visible, which is fine
797 }
798
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
800 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700801
Romain Guy94dabf12009-07-21 10:55:43 -0700802 if (!mWorkspace.isDefaultScreenShowing()) {
803 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700805
Joe Onorato7bb17492009-09-24 17:51:01 -0700806 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700807
808 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 if (v != null && v.getWindowToken() != null) {
810 InputMethodManager imm = (InputMethodManager)getSystemService(
811 INPUT_METHOD_SERVICE);
812 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
813 }
814 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700815 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 }
817 }
818 }
819
820 @Override
821 protected void onRestoreInstanceState(Bundle savedInstanceState) {
822 // Do not call super here
823 mSavedInstanceState = savedInstanceState;
824 }
825
826 @Override
827 protected void onSaveInstanceState(Bundle outState) {
828 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
829
830 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
831 if (folders.size() > 0) {
832 final int count = folders.size();
833 long[] ids = new long[count];
834 for (int i = 0; i < count; i++) {
835 final FolderInfo info = folders.get(i).getInfo();
836 ids[i] = info.id;
837 }
838 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
839 } else {
840 super.onSaveInstanceState(outState);
841 }
842
Romain Guy3cf604f2009-06-16 13:12:53 -0700843 final boolean isConfigurationChange = getChangingConfigurations() != 0;
844
Romain Guy5a941392009-04-28 15:18:25 -0700845 // When the drawer is opened and we are saving the state because of a
846 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400847 // TODO should not do this if the drawer is currently closing.
848 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851
852 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
853 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
854 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
855
856 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
857 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
858 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
859 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
860 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
861 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
862 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
863 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
864 layout.getOccupiedCells());
865 }
866
867 if (mFolderInfo != null && mWaitingForResult) {
868 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
869 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
870 }
871 }
872
873 @Override
874 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700876
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700878 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700880 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 }
882
883 TextKeyListener.getInstance().release();
884
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886
Joe Onorato9c1289c2009-08-17 11:03:03 -0400887 unbindDesktopItems();
888 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
890
891 @Override
892 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700893 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 super.startActivityForResult(intent, requestCode);
895 }
896
897 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700898 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700900
Joe Onorato7bb17492009-09-24 17:51:01 -0700901 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700902
Karl Rosaen138a0412009-04-23 19:00:21 -0700903 // Slide the search widget to the top, if it's on the current screen,
904 // otherwise show the search dialog immediately.
905 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
906 if (searchWidget == null) {
907 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
908 } else {
909 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
910 // show the currently typed text in the search widget while sliding
911 searchWidget.setQuery(getTypedText());
912 }
913 }
Romain Guycbb89e42009-06-08 15:52:54 -0700914
Karl Rosaen138a0412009-04-23 19:00:21 -0700915 /**
916 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700917 *
918 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700919 */
Romain Guycbb89e42009-06-08 15:52:54 -0700920 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700921 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700922
Karl Rosaen138a0412009-04-23 19:00:21 -0700923 if (initialQuery == null) {
924 // Use any text typed in the launcher as the initial query
925 initialQuery = getTypedText();
926 clearTypedText();
927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 if (appSearchData == null) {
929 appSearchData = new Bundle();
930 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
931 }
Romain Guycbb89e42009-06-08 15:52:54 -0700932
Karl Rosaen138a0412009-04-23 19:00:21 -0700933 final SearchManager searchManager =
934 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
935
936 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
937 if (searchWidget != null) {
938 // This gets called when the user leaves the search dialog to go back to
939 // the Launcher.
940 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
941 public void onCancel() {
942 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700943 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700944 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700945 });
946 }
Romain Guycbb89e42009-06-08 15:52:54 -0700947
Karl Rosaen138a0412009-04-23 19:00:21 -0700948 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700949 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 }
951
Karl Rosaen138a0412009-04-23 19:00:21 -0700952 /**
953 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700954 */
Romain Guy5a941392009-04-28 15:18:25 -0700955 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700956 // Close search dialog
957 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100958 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700959 // Restore search widget to its normal position
960 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
961 if (searchWidget != null) {
962 searchWidget.stopSearch(false);
963 }
964 }
Romain Guycbb89e42009-06-08 15:52:54 -0700965
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 @Override
967 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 if (isWorkspaceLocked()) {
969 return false;
970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971
972 super.onCreateOptionsMenu(menu);
973 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
974 .setIcon(android.R.drawable.ic_menu_add)
975 .setAlphabeticShortcut('A');
976 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
977 .setIcon(android.R.drawable.ic_menu_gallery)
978 .setAlphabeticShortcut('W');
979 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
980 .setIcon(android.R.drawable.ic_search_category_default)
981 .setAlphabeticShortcut(SearchManager.MENU_KEY);
982 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
983 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
984 .setAlphabeticShortcut('N');
985
986 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700987 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
988 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989
990 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
991 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
992 .setIntent(settings);
993
994 return true;
995 }
996
997 @Override
998 public boolean onPrepareOptionsMenu(Menu menu) {
999 super.onPrepareOptionsMenu(menu);
1000
1001 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1002 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1003
1004 return true;
1005 }
1006
1007 @Override
1008 public boolean onOptionsItemSelected(MenuItem item) {
1009 switch (item.getItemId()) {
1010 case MENU_ADD:
1011 addItems();
1012 return true;
1013 case MENU_WALLPAPER_SETTINGS:
1014 startWallpaper();
1015 return true;
1016 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001017 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 return true;
1019 case MENU_NOTIFICATIONS:
1020 showNotifications();
1021 return true;
1022 }
1023
1024 return super.onOptionsItemSelected(item);
1025 }
Romain Guycbb89e42009-06-08 15:52:54 -07001026
Karl Rosaen138a0412009-04-23 19:00:21 -07001027 /**
1028 * Indicates that we want global search for this activity by setting the globalSearch
1029 * argument for {@link #startSearch} to true.
1030 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001032 @Override
1033 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001034 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001035 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001036 }
1037
Joe Onorato9c1289c2009-08-17 11:03:03 -04001038 public boolean isWorkspaceLocked() {
1039 return mWorkspaceLoading || mWaitingForResult;
1040 }
1041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 private void addItems() {
1043 showAddDialog(mMenuAddInfo);
1044 }
1045
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001046 void addAppWidget(Intent data) {
1047 // TODO: catch bad widget exception when sent
1048 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001050 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1051 if (SEARCH_WIDGET.equals(customWidget)) {
1052 // We don't need this any more, since this isn't a real app widget.
1053 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1054 // add the search widget
1055 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001057 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1058
1059 if (appWidget.configure != null) {
1060 // Launch over to configure widget, if needed
1061 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1062 intent.setComponent(appWidget.configure);
1063 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1064
1065 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1066 } else {
1067 // Otherwise just add it
1068 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1069 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 }
1071 }
Romain Guycbb89e42009-06-08 15:52:54 -07001072
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 void addSearch() {
1074 final Widget info = Widget.makeSearch();
1075 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 final int[] xy = mCellCoordinates;
1078 final int spanX = info.spanX;
1079 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001082
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1084 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 final View view = mInflater.inflate(info.layoutResource, null);
1087 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001088 Search search = (Search) view.findViewById(R.id.widget_search);
1089 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1092 }
1093
Romain Guy73b979d2009-06-09 12:57:21 -07001094 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001095 // Handle case where user selected "Applications"
1096 String applicationName = getResources().getString(R.string.group_applications);
1097 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001098
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001099 if (applicationName != null && applicationName.equals(shortcutName)) {
1100 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1101 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001102
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001103 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1104 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001105 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001106 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001107 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001108 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 }
1110
1111 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001112 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001113 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001114 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001115
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001116 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001117 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001118 } else {
1119 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
1122
Joe Onorato9c1289c2009-08-17 11:03:03 -04001123 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 UserFolderInfo folderInfo = new UserFolderInfo();
1125 folderInfo.title = getText(R.string.folder_name);
1126
1127 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1128 cellInfo.screen = mWorkspace.getCurrentScreen();
1129 if (!findSingleSlot(cellInfo)) return;
1130
1131 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 LauncherModel.addItemToDatabase(this, folderInfo,
1133 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001135 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136
1137 // Create the view
1138 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1139 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1140 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 }
Romain Guycbb89e42009-06-08 15:52:54 -07001143
Joe Onorato9c1289c2009-08-17 11:03:03 -04001144 void removeFolder(FolderInfo folder) {
1145 mFolders.remove(folder.id);
1146 }
1147
1148 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 cellInfo.screen = mWorkspace.getCurrentScreen();
1150 if (!findSingleSlot(cellInfo)) return;
1151
1152 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1153
1154 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1156 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001157 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1158 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 }
1160 }
1161
1162 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1163 CellLayout.CellInfo cellInfo, boolean notify) {
1164
1165 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1166 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1167
1168 Drawable icon = null;
1169 boolean filtered = false;
1170 Intent.ShortcutIconResource iconResource = null;
1171
1172 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1173 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1174 try {
1175 iconResource = (Intent.ShortcutIconResource) extra;
1176 final PackageManager packageManager = context.getPackageManager();
1177 Resources resources = packageManager.getResourcesForApplication(
1178 iconResource.packageName);
1179 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1180 icon = resources.getDrawable(id);
1181 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001182 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 }
1184 }
1185
1186 if (icon == null) {
1187 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1188 }
1189
1190 final LiveFolderInfo info = new LiveFolderInfo();
1191 info.icon = icon;
1192 info.filtered = filtered;
1193 info.title = name;
1194 info.iconResource = iconResource;
1195 info.uri = data.getData();
1196 info.baseIntent = baseIntent;
1197 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1198 LiveFolders.DISPLAY_MODE_GRID);
1199
1200 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1201 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203
1204 return info;
1205 }
1206
1207 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1208 final int[] xy = new int[2];
1209 if (findSlot(cellInfo, xy, 1, 1)) {
1210 cellInfo.cellX = xy[0];
1211 cellInfo.cellY = xy[1];
1212 return true;
1213 }
1214 return false;
1215 }
1216
1217 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1218 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1219 boolean[] occupied = mSavedState != null ?
1220 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1221 cellInfo = mWorkspace.findAllVacantCells(occupied);
1222 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1223 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1224 return false;
1225 }
1226 }
1227 return true;
1228 }
1229
1230 private void showNotifications() {
1231 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1232 if (statusBar != null) {
1233 statusBar.expand();
1234 }
1235 }
1236
1237 private void startWallpaper() {
1238 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001239 Intent chooser = Intent.createChooser(pickWallpaper,
1240 getText(R.string.chooser_wallpaper));
1241 WallpaperManager wm = (WallpaperManager)
1242 getSystemService(Context.WALLPAPER_SERVICE);
1243 WallpaperInfo wi = wm.getWallpaperInfo();
1244 if (wi != null && wi.getSettingsActivity() != null) {
1245 LabeledIntent li = new LabeledIntent(getPackageName(),
1246 R.string.configure_wallpaper, 0);
1247 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1248 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1249 }
1250 startActivity(chooser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 }
1252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001254 public void onWindowFocusChanged(boolean hasFocus) {
1255 super.onWindowFocusChanged(hasFocus);
1256 if (!hasFocus) {
1257 mBackDown = mHomeDown = false;
1258 }
1259 }
1260
1261 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 public boolean dispatchKeyEvent(KeyEvent event) {
1263 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1264 switch (event.getKeyCode()) {
1265 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001266 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 return true;
1268 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001269 mHomeDown = true;
1270 return true;
1271 }
1272 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1273 switch (event.getKeyCode()) {
1274 case KeyEvent.KEYCODE_BACK:
1275 if (!event.isCanceled()) {
1276 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001277 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001278 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001279 } else {
1280 closeFolder();
1281 }
1282 }
1283 mBackDown = false;
1284 return true;
1285 case KeyEvent.KEYCODE_HOME:
1286 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 return true;
1288 }
1289 }
1290
1291 return super.dispatchKeyEvent(event);
1292 }
1293
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 private void closeFolder() {
1295 Folder folder = mWorkspace.getOpenFolder();
1296 if (folder != null) {
1297 closeFolder(folder);
1298 }
1299 }
1300
1301 void closeFolder(Folder folder) {
1302 folder.getInfo().opened = false;
1303 ViewGroup parent = (ViewGroup) folder.getParent();
1304 if (parent != null) {
1305 parent.removeView(folder);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001306 // TODO: this line crashes.
1307 //mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
1309 folder.onClose();
1310 }
1311
1312 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001313 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1314 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316 private void unbindDesktopItems() {
1317 for (ItemInfo item: mDesktopItems) {
1318 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001321
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 /**
1323 * Launches the intent referred by the clicked shortcut.
1324 *
1325 * @param v The view representing the clicked shortcut.
1326 */
1327 public void onClick(View v) {
1328 Object tag = v.getTag();
1329 if (tag instanceof ApplicationInfo) {
1330 // Open shortcut
1331 final Intent intent = ((ApplicationInfo) tag).intent;
1332 startActivitySafely(intent);
1333 } else if (tag instanceof FolderInfo) {
1334 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001335 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001336 Log.d(TAG, "onClick");
1337 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001338 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001339 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001340 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 }
1343 }
1344
1345 void startActivitySafely(Intent intent) {
1346 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1347 try {
1348 startActivity(intent);
1349 } catch (ActivityNotFoundException e) {
1350 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1351 } catch (SecurityException e) {
1352 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001353 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1355 "or use the exported attribute for this activity.", e);
1356 }
1357 }
1358
1359 private void handleFolderClick(FolderInfo folderInfo) {
1360 if (!folderInfo.opened) {
1361 // Close any open folder
1362 closeFolder();
1363 // Open the requested folder
1364 openFolder(folderInfo);
1365 } else {
1366 // Find the open folder...
1367 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1368 int folderScreen;
1369 if (openFolder != null) {
1370 folderScreen = mWorkspace.getScreenForView(openFolder);
1371 // .. and close it
1372 closeFolder(openFolder);
1373 if (folderScreen != mWorkspace.getCurrentScreen()) {
1374 // Close any folder open on the current screen
1375 closeFolder();
1376 // Pull the folder onto this screen
1377 openFolder(folderInfo);
1378 }
1379 }
1380 }
1381 }
1382
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 /**
1384 * Opens the user fodler described by the specified tag. The opening of the folder
1385 * is animated relative to the specified View. If the View is null, no animation
1386 * is played.
1387 *
1388 * @param folderInfo The FolderInfo describing the folder to open.
1389 */
1390 private void openFolder(FolderInfo folderInfo) {
1391 Folder openFolder;
1392
1393 if (folderInfo instanceof UserFolderInfo) {
1394 openFolder = UserFolder.fromXml(this);
1395 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001396 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 } else {
1398 return;
1399 }
1400
Joe Onorato00acb122009-08-04 16:04:30 -04001401 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 openFolder.setLauncher(this);
1403
1404 openFolder.bind(folderInfo);
1405 folderInfo.opened = true;
1406
1407 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1408 openFolder.onOpen();
1409 }
1410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001412 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 return false;
1414 }
1415
1416 if (!(v instanceof CellLayout)) {
1417 v = (View) v.getParent();
1418 }
1419
1420 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1421
1422 // This happens when long clicking an item with the dpad/trackball
1423 if (cellInfo == null) {
1424 return true;
1425 }
1426
1427 if (mWorkspace.allowLongPress()) {
1428 if (cellInfo.cell == null) {
1429 if (cellInfo.valid) {
1430 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001431 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 showAddDialog(cellInfo);
1433 }
1434 } else {
1435 if (!(cellInfo.cell instanceof Folder)) {
1436 // User long pressed on an item
1437 mWorkspace.startDrag(cellInfo);
1438 }
1439 }
1440 }
1441 return true;
1442 }
1443
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001444 View getDrawerHandle() {
1445 return mHandleView;
1446 }
1447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 Workspace getWorkspace() {
1449 return mWorkspace;
1450 }
1451
Joe Onorato7404ee42009-07-31 11:54:44 -07001452 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 GridView getApplicationsGrid() {
1454 return mAllAppsGrid;
1455 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001456 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457
1458 @Override
1459 protected Dialog onCreateDialog(int id) {
1460 switch (id) {
1461 case DIALOG_CREATE_SHORTCUT:
1462 return new CreateShortcut().createDialog();
1463 case DIALOG_RENAME_FOLDER:
1464 return new RenameFolder().createDialog();
1465 }
1466
1467 return super.onCreateDialog(id);
1468 }
1469
1470 @Override
1471 protected void onPrepareDialog(int id, Dialog dialog) {
1472 switch (id) {
1473 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 break;
1475 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001476 if (mFolderInfo != null) {
1477 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1478 final CharSequence text = mFolderInfo.title;
1479 input.setText(text);
1480 input.setSelection(0, text.length());
1481 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 break;
1483 }
1484 }
1485
1486 void showRenameDialog(FolderInfo info) {
1487 mFolderInfo = info;
1488 mWaitingForResult = true;
1489 showDialog(DIALOG_RENAME_FOLDER);
1490 }
1491
1492 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1493 mAddItemCellInfo = cellInfo;
1494 mWaitingForResult = true;
1495 showDialog(DIALOG_CREATE_SHORTCUT);
1496 }
1497
Romain Guy73b979d2009-06-09 12:57:21 -07001498 private void pickShortcut(int requestCode, int title) {
1499 Bundle bundle = new Bundle();
1500
1501 ArrayList<String> shortcutNames = new ArrayList<String>();
1502 shortcutNames.add(getString(R.string.group_applications));
1503 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1504
1505 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1506 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1507 R.drawable.ic_launcher_application));
1508 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1509
1510 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1511 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1512 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1513 pickIntent.putExtras(bundle);
1514
1515 startActivityForResult(pickIntent, requestCode);
1516 }
1517
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 private class RenameFolder {
1519 private EditText mInput;
1520
1521 Dialog createDialog() {
1522 mWaitingForResult = true;
1523 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1524 mInput = (EditText) layout.findViewById(R.id.folder_name);
1525
1526 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1527 builder.setIcon(0);
1528 builder.setTitle(getString(R.string.rename_folder_title));
1529 builder.setCancelable(true);
1530 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1531 public void onCancel(DialogInterface dialog) {
1532 cleanup();
1533 }
1534 });
1535 builder.setNegativeButton(getString(R.string.cancel_action),
1536 new Dialog.OnClickListener() {
1537 public void onClick(DialogInterface dialog, int which) {
1538 cleanup();
1539 }
1540 }
1541 );
1542 builder.setPositiveButton(getString(R.string.rename_action),
1543 new Dialog.OnClickListener() {
1544 public void onClick(DialogInterface dialog, int which) {
1545 changeFolderName();
1546 }
1547 }
1548 );
1549 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001550
1551 final AlertDialog dialog = builder.create();
1552 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1553 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001554 }
1555 });
1556
1557 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 }
1559
1560 private void changeFolderName() {
1561 final String name = mInput.getText().toString();
1562 if (!TextUtils.isEmpty(name)) {
1563 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001564 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 mFolderInfo.title = name;
1566 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1567
Joe Onorato9c1289c2009-08-17 11:03:03 -04001568 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001569 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001570 mModel.setWorkspaceDirty();
1571 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 } else {
1573 final FolderIcon folderIcon = (FolderIcon)
1574 mWorkspace.getViewForTag(mFolderInfo);
1575 if (folderIcon != null) {
1576 folderIcon.setText(name);
1577 getWorkspace().requestLayout();
1578 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001579 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001580 mModel.setWorkspaceDirty();
1581 mWorkspaceLoading = true;
1582 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
1584 }
1585 }
1586 cleanup();
1587 }
1588
1589 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 dismissDialog(DIALOG_RENAME_FOLDER);
1591 mWaitingForResult = false;
1592 mFolderInfo = null;
1593 }
1594 }
1595
Joe Onoratofb0ca672009-09-14 17:55:46 -04001596 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001597 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001598 }
1599
Jason Samsfd22dac2009-09-20 17:24:16 -07001600 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001601 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001602 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001603
Joe Onorato7c312c12009-08-13 21:36:53 -07001604 // TODO: fade these two too
1605 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001606 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001607 }
1608
Joe Onorato7bb17492009-09-24 17:51:01 -07001609 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001610 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001611 mAllAppsGrid.zoom(0.0f);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001612 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001613
1614 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001615 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001616 mDeleteZone.setVisibility(View.VISIBLE);
1617 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001618 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001619 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001620 }
1621
Joe Onorato7c312c12009-08-13 21:36:53 -07001622 void lockAllApps() {
1623 // TODO
1624 }
1625
1626 void unlockAllApps() {
1627 // TODO
1628 }
1629
Joe Onorato7404ee42009-07-31 11:54:44 -07001630 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 * Displays the shortcut creation dialog and launches, if necessary, the
1632 * appropriate activity.
1633 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001634 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001635 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1636 DialogInterface.OnShowListener {
1637
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001639
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 Dialog createDialog() {
1641 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001642
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1646 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001647 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 builder.setInverseBackgroundForced(true);
1650
1651 AlertDialog dialog = builder.create();
1652 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001653 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001654 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 return dialog;
1657 }
1658
1659 public void onCancel(DialogInterface dialog) {
1660 mWaitingForResult = false;
1661 cleanup();
1662 }
1663
Romain Guycbb89e42009-06-08 15:52:54 -07001664 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001665 }
1666
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 dismissDialog(DIALOG_CREATE_SHORTCUT);
1669 }
1670
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001671 /**
1672 * Handle the action clicked in the "Add to home" dialog.
1673 */
1674 public void onClick(DialogInterface dialog, int which) {
1675 Resources res = getResources();
1676 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001677
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001678 switch (which) {
1679 case AddAdapter.ITEM_SHORTCUT: {
1680 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001681 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001682 break;
1683 }
Romain Guycbb89e42009-06-08 15:52:54 -07001684
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001685 case AddAdapter.ITEM_APPWIDGET: {
1686 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001687
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001688 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1689 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1690 // add the search widget
1691 ArrayList<AppWidgetProviderInfo> customInfo =
1692 new ArrayList<AppWidgetProviderInfo>();
1693 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1694 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1695 info.label = getString(R.string.group_search);
1696 info.icon = R.drawable.ic_search_widget;
1697 customInfo.add(info);
1698 pickIntent.putParcelableArrayListExtra(
1699 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1700 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1701 Bundle b = new Bundle();
1702 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1703 customExtras.add(b);
1704 pickIntent.putParcelableArrayListExtra(
1705 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1706 // start the pick activity
1707 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1708 break;
1709 }
Romain Guycbb89e42009-06-08 15:52:54 -07001710
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001711 case AddAdapter.ITEM_LIVE_FOLDER: {
1712 // Insert extra item to handle inserting folder
1713 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001714
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001715 ArrayList<String> shortcutNames = new ArrayList<String>();
1716 shortcutNames.add(res.getString(R.string.group_folder));
1717 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001718
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001719 ArrayList<ShortcutIconResource> shortcutIcons =
1720 new ArrayList<ShortcutIconResource>();
1721 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1722 R.drawable.ic_launcher_folder));
1723 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1724
1725 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1726 pickIntent.putExtra(Intent.EXTRA_INTENT,
1727 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1728 pickIntent.putExtra(Intent.EXTRA_TITLE,
1729 getText(R.string.title_select_live_folder));
1730 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001731
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001732 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1733 break;
1734 }
1735
1736 case AddAdapter.ITEM_WALLPAPER: {
1737 startWallpaper();
1738 break;
1739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 }
1741 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001742
1743 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001744 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 }
1746
1747 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001748 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001750 public int getCurrentWorkspaceScreen() {
1751 return mWorkspace.getCurrentScreen();
1752 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001753
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754 /**
1755 * Refreshes the shortcuts shown on the workspace.
1756 *
1757 * Implementation of the method from LauncherModel.Callbacks.
1758 */
1759 public void startBinding() {
1760 final Workspace workspace = mWorkspace;
1761 int count = workspace.getChildCount();
1762 for (int i = 0; i < count; i++) {
1763 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1764 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001765
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766 if (DEBUG_USER_INTERFACE) {
1767 android.widget.Button finishButton = new android.widget.Button(this);
1768 finishButton.setText("Finish");
1769 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1770
1771 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1772 public void onClick(View v) {
1773 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001774 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001775 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 }
1777 }
1778
1779 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001780 * Bind the items start-end from the list.
1781 *
1782 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001784 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1785
1786 final Workspace workspace = mWorkspace;
1787
1788 for (int i=start; i<end; i++) {
1789 final ItemInfo item = shortcuts.get(i);
1790 mDesktopItems.add(item);
1791 switch (item.itemType) {
1792 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1793 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1794 final View shortcut = createShortcut((ApplicationInfo) item);
1795 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1796 false);
1797 break;
1798 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1799 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1800 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1801 (UserFolderInfo) item);
1802 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1803 false);
1804 break;
1805 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1806 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1807 R.layout.live_folder_icon, this,
1808 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1809 (LiveFolderInfo) item);
1810 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1811 false);
1812 break;
1813 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1814 final int screen = workspace.getCurrentScreen();
1815 final View view = mInflater.inflate(R.layout.widget_search,
1816 (ViewGroup) workspace.getChildAt(screen), false);
1817
1818 Search search = (Search) view.findViewById(R.id.widget_search);
1819 search.setLauncher(this);
1820
1821 final Widget widget = (Widget) item;
1822 view.setTag(widget);
1823
1824 workspace.addWidget(view, widget, false);
1825 break;
1826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 }
1828
Joe Onorato9c1289c2009-08-17 11:03:03 -04001829 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 }
1831
Joe Onorato9c1289c2009-08-17 11:03:03 -04001832 /**
1833 * Implementation of the method from LauncherModel.Callbacks.
1834 */
1835 void bindFolders(HashMap<Long, FolderInfo> folders) {
1836 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838
1839 /**
1840 * Add the views for a widget to the workspace.
1841 *
1842 * Implementation of the method from LauncherModel.Callbacks.
1843 */
1844 public void bindAppWidget(LauncherAppWidgetInfo item) {
1845 final Workspace workspace = mWorkspace;
1846
1847 final int appWidgetId = item.appWidgetId;
1848 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1849 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1850
1851 if (true) {
1852 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1853 appWidgetId, appWidgetInfo));
1854 }
1855
1856 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1857 item.hostView.setTag(item);
1858
1859 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1860 item.cellY, item.spanX, item.spanY, false);
1861
1862 workspace.requestLayout();
1863
1864 mDesktopItems.add(item);
1865 }
1866
1867 /**
1868 * Callback saying that there aren't any more items to bind.
1869 *
1870 * Implementation of the method from LauncherModel.Callbacks.
1871 */
1872 public void finishBindingItems() {
1873 if (mSavedState != null) {
1874 if (!mWorkspace.hasFocus()) {
1875 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1876 }
1877
1878 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1879 if (userFolders != null) {
1880 for (long folderId : userFolders) {
1881 final FolderInfo info = mFolders.get(folderId);
1882 if (info != null) {
1883 openFolder(info);
1884 }
1885 }
1886 final Folder openFolder = mWorkspace.getOpenFolder();
1887 if (openFolder != null) {
1888 openFolder.requestFocus();
1889 }
1890 }
1891
1892 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1893 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001894 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001895 }
1896
1897 mSavedState = null;
1898 }
1899
1900 if (mSavedInstanceState != null) {
1901 super.onRestoreInstanceState(mSavedInstanceState);
1902 mSavedInstanceState = null;
1903 }
1904
1905 /* TODO
1906 if (mAllAppsVisible && !mDrawer.hasFocus()) {
1907 mDrawer.requestFocus();
1908 }
1909 */
1910
1911 Log.d(TAG, "finishBindingItems done");
1912 mWorkspaceLoading = false;
1913 }
1914
1915 /**
1916 * Add the icons for all apps.
1917 *
1918 * Implementation of the method from LauncherModel.Callbacks.
1919 */
1920 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
1921 Log.d(LOG_TAG, "got info for " + apps.size() + " apps");
1922 mAllAppsList = apps;
1923 mAllAppsGrid.setApps(mAllAppsList);
1924 }
1925
1926 /**
1927 * A package was installed.
1928 *
1929 * Implementation of the method from LauncherModel.Callbacks.
1930 */
1931 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1932 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001933 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001934 }
1935
1936 /**
1937 * A package was updated.
1938 *
1939 * Implementation of the method from LauncherModel.Callbacks.
1940 */
1941 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1942 removeDialog(DIALOG_CREATE_SHORTCUT);
1943 mWorkspace.updateShortcutsForPackage(packageName);
1944 }
1945
1946 /**
1947 * A package was uninstalled.
1948 *
1949 * Implementation of the method from LauncherModel.Callbacks.
1950 */
1951 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1952 removeDialog(DIALOG_CREATE_SHORTCUT);
1953 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001954 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956}