blob: 93ce9d543b7dbffd521b26f7493917c440b0b0ee [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
20import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070022import android.app.ISearchManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.SearchManager;
24import android.app.StatusBarManager;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070025import android.app.WallpaperInfo;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
30import android.content.DialogInterface;
31import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070032import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ActivityInfo;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070034import android.content.pm.LabeledIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.PackageManager;
36import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070038import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.graphics.drawable.Drawable;
41import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Bundle;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070043import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070044import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070045import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.text.Selection;
51import android.text.SpannableStringBuilder;
52import android.text.TextUtils;
53import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070054import static android.util.Log.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070055import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.KeyEvent;
58import android.view.LayoutInflater;
59import android.view.Menu;
60import android.view.MenuItem;
61import android.view.View;
62import android.view.ViewGroup;
Romain Guyb1b69f52009-08-10 15:10:15 -070063import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.view.View.OnLongClickListener;
65import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.widget.TextView;
68import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070069import android.appwidget.AppWidgetManager;
70import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071
72import java.lang.ref.WeakReference;
73import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074import java.util.HashMap;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070075import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070076import java.io.DataOutputStream;
77import java.io.FileNotFoundException;
78import java.io.IOException;
79import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
81/**
82 * Default launcher application.
83 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040084public final class Launcher extends Activity
Joe Onorato7bb17492009-09-24 17:51:01 -070085 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040087 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 static final boolean LOGD = false;
89
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 static final boolean PROFILE_STARTUP = false;
91 static final boolean PROFILE_ROTATE = false;
92 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 private static final int WALLPAPER_SCREENS_SPAN = 2;
95
96 private static final int MENU_GROUP_ADD = 1;
97 private static final int MENU_ADD = Menu.FIRST + 1;
98 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
99 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
100 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700101 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
103 private static final int REQUEST_CREATE_SHORTCUT = 1;
104 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700105 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 private static final int REQUEST_PICK_APPLICATION = 6;
107 private static final int REQUEST_PICK_SHORTCUT = 7;
108 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700109 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
111 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
112
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700113 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
114 static final String SEARCH_WIDGET = "search_widget";
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 static final int SCREEN_COUNT = 3;
117 static final int DEFAULT_SCREN = 1;
118 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700119 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Joe Onorato7c312c12009-08-13 21:36:53 -0700121 static final int DIALOG_CREATE_SHORTCUT = 1;
122 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Romain Guy98d01652009-06-30 16:21:04 -0700124 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 // Type: int
127 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
128 // Type: boolean
129 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
130 // Type: long
131 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
146 // Type: int[]
147 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
148 // Type: boolean
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
150 // Type: long
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
152
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final Object sLock = new Object();
156 private static int sScreen = DEFAULT_SCREN;
157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private LayoutInflater mInflater;
159
Joe Onorato00acb122009-08-04 16:04:30 -0400160 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700162
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700163 private AppWidgetManager mAppWidgetManager;
164 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private CellLayout.CellInfo mAddItemCellInfo;
167 private CellLayout.CellInfo mMenuAddInfo;
168 private final int[] mCellCoordinates = new int[2];
169 private FolderInfo mFolderInfo;
170
Joe Onorato7c312c12009-08-13 21:36:53 -0700171 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700172 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700173 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private Bundle mSavedState;
176
177 private SpannableStringBuilder mDefaultKeySsb = null;
178
Mike LeBeau736cf282009-07-02 17:46:59 -0700179 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 private boolean mWorkspaceLoading = true;
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private boolean mRestoring;
184 private boolean mWaitingForResult;
185 private boolean mLocaleChanged;
Joe Onoratoff3862d2009-10-07 10:35:51 -0700186 private boolean mExitingBecauseOfLaunch;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Dianne Hackborn67800862009-07-24 17:15:20 -0700188 private boolean mHomeDown;
189 private boolean mBackDown;
Jason Samsfd22dac2009-09-20 17:24:16 -0700190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private Bundle mSavedInstanceState;
192
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 private LauncherModel mModel;
194
195 private ArrayList<ItemInfo> mDesktopItems = new ArrayList();
196 private static HashMap<Long, FolderInfo> mFolders = new HashMap();
197 private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList();
Romain Guycbb89e42009-06-08 15:52:54 -0700198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 @Override
200 protected void onCreate(Bundle savedInstanceState) {
201 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700206 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700207
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
209 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 if (PROFILE_STARTUP) {
212 android.os.Debug.startMethodTracing("/sdcard/launcher");
213 }
214
215 checkForLocaleChange();
216 setWallpaperDimension();
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 setContentView(R.layout.launcher);
219 setupViews();
220
Joe Onorato7c312c12009-08-13 21:36:53 -0700221 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 mSavedState = savedInstanceState;
224 restoreState(mSavedState);
225
226 if (PROFILE_STARTUP) {
227 android.os.Debug.stopMethodTracing();
228 }
229
230 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 }
233
234 // For handling default keys
235 mDefaultKeySsb = new SpannableStringBuilder();
236 Selection.setSelection(mDefaultKeySsb, 0);
237 }
Romain Guycbb89e42009-06-08 15:52:54 -0700238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700240 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
241 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 final Configuration configuration = getResources().getConfiguration();
244
Romain Guy98d01652009-06-30 16:21:04 -0700245 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 final String locale = configuration.locale.toString();
247
Romain Guy98d01652009-06-30 16:21:04 -0700248 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 final int mcc = configuration.mcc;
250
Romain Guy98d01652009-06-30 16:21:04 -0700251 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 final int mnc = configuration.mnc;
253
254 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
255
256 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700257 localeConfiguration.locale = locale;
258 localeConfiguration.mcc = mcc;
259 localeConfiguration.mnc = mnc;
260
261 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700263 }
264 }
265
266 private static class LocaleConfiguration {
267 public String locale;
268 public int mcc = -1;
269 public int mnc = -1;
270 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700271
Romain Guy98d01652009-06-30 16:21:04 -0700272 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
273 DataInputStream in = null;
274 try {
275 in = new DataInputStream(context.openFileInput(PREFERENCES));
276 configuration.locale = in.readUTF();
277 configuration.mcc = in.readInt();
278 configuration.mnc = in.readInt();
279 } catch (FileNotFoundException e) {
280 // Ignore
281 } catch (IOException e) {
282 // Ignore
283 } finally {
284 if (in != null) {
285 try {
286 in.close();
287 } catch (IOException e) {
288 // Ignore
289 }
290 }
291 }
292 }
293
294 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
295 DataOutputStream out = null;
296 try {
297 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
298 out.writeUTF(configuration.locale);
299 out.writeInt(configuration.mcc);
300 out.writeInt(configuration.mnc);
301 out.flush();
302 } catch (FileNotFoundException e) {
303 // Ignore
304 } catch (IOException e) {
305 //noinspection ResultOfMethodCallIgnored
306 context.getFileStreamPath(PREFERENCES).delete();
307 } finally {
308 if (out != null) {
309 try {
310 out.close();
311 } catch (IOException e) {
312 // Ignore
313 }
314 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 }
316 }
317
318 static int getScreen() {
319 synchronized (sLock) {
320 return sScreen;
321 }
322 }
323
324 static void setScreen(int screen) {
325 synchronized (sLock) {
326 sScreen = screen;
327 }
328 }
329
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700331 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332
333 Display display = getWindowManager().getDefaultDisplay();
334 boolean isPortrait = display.getWidth() < display.getHeight();
335
336 final int width = isPortrait ? display.getWidth() : display.getHeight();
337 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700338 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 }
340
341 @Override
342 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700343 mWaitingForResult = false;
344
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 // The pattern used here is that a user PICKs a specific application,
346 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
349 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700350
Romain Guy94dabf12009-07-21 10:55:43 -0700351 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 switch (requestCode) {
353 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400354 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 break;
356 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700357 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 break;
359 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400360 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 break;
362 case REQUEST_PICK_LIVE_FOLDER:
363 addLiveFolder(data);
364 break;
365 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700368 case REQUEST_PICK_APPWIDGET:
369 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700371 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400372 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 break;
374 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700375 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700377 // Clean up the appWidgetId if we canceled
378 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
379 if (appWidgetId != -1) {
380 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 }
382 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
384
385 @Override
386 protected void onResume() {
387 super.onResume();
388
389 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390 mWorkspaceLoading = true;
391 mModel.startLoader(this, true);
392 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700394
Mike LeBeau736cf282009-07-02 17:46:59 -0700395 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
396 // onNewIntent), then close the search dialog if needed, because it probably
397 // came from the user pressing 'home' (rather than, for example, pressing 'back').
398 if (mIsNewIntent) {
399 // Post to a handler so that this happens after the search dialog tries to open
400 // itself again.
401 mWorkspace.post(new Runnable() {
402 public void run() {
403 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
404 ServiceManager.getService(Context.SEARCH_SERVICE));
405 try {
406 searchManagerService.stopSearch();
407 } catch (RemoteException e) {
408 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700409 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700410 }
411 });
412 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700413
Mike LeBeau736cf282009-07-02 17:46:59 -0700414 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 }
416
417 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700418 protected void onPause() {
419 super.onPause();
Joe Onoratoff3862d2009-10-07 10:35:51 -0700420 if (mExitingBecauseOfLaunch) {
421 mExitingBecauseOfLaunch = false;
422 closeAllApps(false);
423 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700424 }
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700426 @Override
427 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400428 // Flag the loader to stop early before switching
429 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700431 if (PROFILE_ROTATE) {
432 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
433 }
434 return null;
435 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 private boolean acceptFilter() {
438 final InputMethodManager inputManager = (InputMethodManager)
439 getSystemService(Context.INPUT_METHOD_SERVICE);
440 return !inputManager.isFullscreenMode();
441 }
442
443 @Override
444 public boolean onKeyDown(int keyCode, KeyEvent event) {
445 boolean handled = super.onKeyDown(keyCode, event);
446 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
447 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
448 keyCode, event);
449 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700450 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700451 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700452 // showSearchDialog()
453 // If there are multiple keystrokes before the search dialog takes focus,
454 // onSearchRequested() will be called for every keystroke,
455 // but it is idempotent, so it's fine.
456 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
458 }
459
460 return handled;
461 }
462
Karl Rosaen138a0412009-04-23 19:00:21 -0700463 private String getTypedText() {
464 return mDefaultKeySsb.toString();
465 }
466
467 private void clearTypedText() {
468 mDefaultKeySsb.clear();
469 mDefaultKeySsb.clearSpans();
470 Selection.setSelection(mDefaultKeySsb, 0);
471 }
472
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 /**
474 * Restores the previous state, if it exists.
475 *
476 * @param savedState The previous state.
477 */
478 private void restoreState(Bundle savedState) {
479 if (savedState == null) {
480 return;
481 }
482
483 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
484 if (currentScreen > -1) {
485 mWorkspace.setCurrentScreen(currentScreen);
486 }
487
488 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
489 if (addScreen > -1) {
490 mAddItemCellInfo = new CellLayout.CellInfo();
491 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
492 addItemCellInfo.valid = true;
493 addItemCellInfo.screen = addScreen;
494 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
495 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
496 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
497 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
498 addItemCellInfo.findVacantCellsFromOccupied(
499 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
500 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
501 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
502 mRestoring = true;
503 }
504
505 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
506 if (renameFolder) {
507 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400508 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509 mRestoring = true;
510 }
511 }
512
513 /**
514 * Finds all the views we need and configure them properly.
515 */
516 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400517 mDragController = new DragController(this);
518 DragController dragController = mDragController;
519
Romain Guyb1b69f52009-08-10 15:10:15 -0700520 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400521 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522
Joe Onorato7c312c12009-08-13 21:36:53 -0700523 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700524 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700525 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700526 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Joe Onorato7c312c12009-08-13 21:36:53 -0700527
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800528 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
529 final Workspace workspace = mWorkspace;
530
Joe Onorato7c312c12009-08-13 21:36:53 -0700531 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
532 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533
Joe Onorato7c312c12009-08-13 21:36:53 -0700534 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700535 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700536 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700537 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700538 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700539 handleIocon.setCrossFadeEnabled(true);
540 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400543 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545
546 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400547 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700548 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549
Joe Onorato00acb122009-08-04 16:04:30 -0400550 dragController.setDragScoller(workspace);
551 dragController.setDragListener(deleteZone);
552 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700553
Joe Onorato00acb122009-08-04 16:04:30 -0400554 // The order here is bottom to top.
555 dragController.addDropTarget(workspace);
556 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 }
558
559 /**
560 * Creates a view representing a shortcut.
561 *
562 * @param info The data structure describing the shortcut.
563 *
564 * @return A View inflated from R.layout.application.
565 */
566 View createShortcut(ApplicationInfo info) {
567 return createShortcut(R.layout.application,
568 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
569 }
570
571 /**
572 * Creates a view representing a shortcut inflated from the specified resource.
573 *
574 * @param layoutResId The id of the XML layout used to create the shortcut.
575 * @param parent The group the shortcut belongs to.
576 * @param info The data structure describing the shortcut.
577 *
578 * @return A View inflated from layoutResId.
579 */
580 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
581 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
582
Joe Onorato5162ea92009-09-03 09:39:42 -0700583 if (info.icon == null) {
584 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
585 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700587 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 info.filtered = true;
589 }
590
591 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
592 favorite.setText(info.title);
593 favorite.setTag(info);
594 favorite.setOnClickListener(this);
595
596 return favorite;
597 }
598
599 /**
600 * Add an application shortcut to the workspace.
601 *
602 * @param data The intent describing the application.
603 * @param cellInfo The position on screen where to create the shortcut.
604 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400605 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 cellInfo.screen = mWorkspace.getCurrentScreen();
607 if (!findSingleSlot(cellInfo)) return;
608
Romain Guy73b979d2009-06-09 12:57:21 -0700609 final ApplicationInfo info = infoFromApplicationIntent(context, data);
610 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400611 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700612 }
613 }
614
615 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 ComponentName component = data.getComponent();
617 PackageManager packageManager = context.getPackageManager();
618 ActivityInfo activityInfo = null;
619 try {
620 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
621 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700622 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 }
Romain Guycbb89e42009-06-08 15:52:54 -0700624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 if (activityInfo != null) {
626 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700627
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 itemInfo.title = activityInfo.loadLabel(packageManager);
629 if (itemInfo.title == null) {
630 itemInfo.title = activityInfo.name;
631 }
Romain Guycbb89e42009-06-08 15:52:54 -0700632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
634 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
635 itemInfo.icon = activityInfo.loadIcon(packageManager);
636 itemInfo.container = ItemInfo.NO_ID;
637
Romain Guy73b979d2009-06-09 12:57:21 -0700638 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 }
Romain Guy73b979d2009-06-09 12:57:21 -0700640
641 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 }
Romain Guycbb89e42009-06-08 15:52:54 -0700643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 /**
645 * Add a shortcut to the workspace.
646 *
647 * @param data The intent describing the shortcut.
648 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 cellInfo.screen = mWorkspace.getCurrentScreen();
652 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
655
656 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
659 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
661 }
662
Romain Guycbb89e42009-06-08 15:52:54 -0700663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700665 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700667 * @param data The intent describing the appWidgetId.
668 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400670 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700672 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700673
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700674 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700675
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700677
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700678 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700680 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700681
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 // Try finding open space on Launcher screen
683 final int[] xy = mCellCoordinates;
684 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
685
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686 // Build Launcher-specific widget info and save to database
687 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 launcherInfo.spanX = spans[0];
689 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700690
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 LauncherModel.addItemToDatabase(this, launcherInfo,
692 LauncherSettings.Favorites.CONTAINER_DESKTOP,
693 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
694
695 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700699 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700700
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700701 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400705 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 }
707 }
Romain Guycbb89e42009-06-08 15:52:54 -0700708
Joe Onorato9c1289c2009-08-17 11:03:03 -0400709 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
710 mDesktopItems.remove(launcherInfo);
711 launcherInfo.hostView = null;
712 }
713
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700714 public LauncherAppWidgetHost getAppWidgetHost() {
715 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
Romain Guycbb89e42009-06-08 15:52:54 -0700717
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 static ApplicationInfo addShortcut(Context context, Intent data,
719 CellLayout.CellInfo cellInfo, boolean notify) {
720
Romain Guy73b979d2009-06-09 12:57:21 -0700721 final ApplicationInfo info = infoFromShortcutIntent(context, data);
722 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
723 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
724
725 return info;
726 }
727
728 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
730 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
731 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
732
733 Drawable icon = null;
734 boolean filtered = false;
735 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700736 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737
738 if (bitmap != null) {
739 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
740 filtered = true;
741 customIcon = true;
742 } else {
743 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700744 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700746 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 final PackageManager packageManager = context.getPackageManager();
748 Resources resources = packageManager.getResourcesForApplication(
749 iconResource.packageName);
750 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
751 icon = resources.getDrawable(id);
752 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700753 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 }
755 }
756 }
757
758 if (icon == null) {
759 icon = context.getPackageManager().getDefaultActivityIcon();
760 }
761
762 final ApplicationInfo info = new ApplicationInfo();
763 info.icon = icon;
764 info.filtered = filtered;
765 info.title = name;
766 info.intent = intent;
767 info.customIcon = customIcon;
768 info.iconResource = iconResource;
769
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 return info;
771 }
772
773 @Override
774 protected void onNewIntent(Intent intent) {
775 super.onNewIntent(intent);
776
777 // Close the menu
778 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
779 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700780
Joe Onorato742966b2009-10-07 10:26:17 -0700781 // Whatever we were doing is hereby canceled.
782 mWaitingForResult = false;
783
Mike LeBeau736cf282009-07-02 17:46:59 -0700784 // Set this flag so that onResume knows to close the search dialog if it's open,
785 // because this was a new intent (thus a press of 'home' or some such) rather than
786 // for example onResume being called when the user pressed the 'back' button.
787 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788
789 try {
790 dismissDialog(DIALOG_CREATE_SHORTCUT);
791 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 } catch (Exception e) {
793 // An exception is thrown if the dialog is not visible, which is fine
794 }
795
796 try {
797 dismissDialog(DIALOG_RENAME_FOLDER);
798 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 } catch (Exception e) {
800 // An exception is thrown if the dialog is not visible, which is fine
801 }
802
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
804 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700805
Romain Guy94dabf12009-07-21 10:55:43 -0700806 if (!mWorkspace.isDefaultScreenShowing()) {
807 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700809
Joe Onorato7bb17492009-09-24 17:51:01 -0700810 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700811
812 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 if (v != null && v.getWindowToken() != null) {
814 InputMethodManager imm = (InputMethodManager)getSystemService(
815 INPUT_METHOD_SERVICE);
816 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
817 }
818 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700819 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 }
821 }
822 }
823
824 @Override
825 protected void onRestoreInstanceState(Bundle savedInstanceState) {
826 // Do not call super here
827 mSavedInstanceState = savedInstanceState;
828 }
829
830 @Override
831 protected void onSaveInstanceState(Bundle outState) {
832 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
833
834 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
835 if (folders.size() > 0) {
836 final int count = folders.size();
837 long[] ids = new long[count];
838 for (int i = 0; i < count; i++) {
839 final FolderInfo info = folders.get(i).getInfo();
840 ids[i] = info.id;
841 }
842 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
843 } else {
844 super.onSaveInstanceState(outState);
845 }
846
Romain Guy3cf604f2009-06-16 13:12:53 -0700847 final boolean isConfigurationChange = getChangingConfigurations() != 0;
848
Romain Guy5a941392009-04-28 15:18:25 -0700849 // When the drawer is opened and we are saving the state because of a
850 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400851 // TODO should not do this if the drawer is currently closing.
852 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700854 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855
856 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
857 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
858 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
859
860 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
861 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
862 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
863 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
864 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
865 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
866 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
867 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
868 layout.getOccupiedCells());
869 }
870
871 if (mFolderInfo != null && mWaitingForResult) {
872 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
873 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
874 }
875 }
876
877 @Override
878 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700880
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700882 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700884 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886
887 TextKeyListener.getInstance().release();
888
Joe Onorato9c1289c2009-08-17 11:03:03 -0400889 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890
Joe Onorato9c1289c2009-08-17 11:03:03 -0400891 unbindDesktopItems();
892 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 }
894
895 @Override
896 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700897 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 super.startActivityForResult(intent, requestCode);
899 }
900
901 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700902 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700904
Joe Onorato7bb17492009-09-24 17:51:01 -0700905 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700906
Karl Rosaen138a0412009-04-23 19:00:21 -0700907 // Slide the search widget to the top, if it's on the current screen,
908 // otherwise show the search dialog immediately.
909 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
910 if (searchWidget == null) {
911 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
912 } else {
913 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
914 // show the currently typed text in the search widget while sliding
915 searchWidget.setQuery(getTypedText());
916 }
917 }
Romain Guycbb89e42009-06-08 15:52:54 -0700918
Karl Rosaen138a0412009-04-23 19:00:21 -0700919 /**
920 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700921 *
922 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700923 */
Romain Guycbb89e42009-06-08 15:52:54 -0700924 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700925 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700926
Karl Rosaen138a0412009-04-23 19:00:21 -0700927 if (initialQuery == null) {
928 // Use any text typed in the launcher as the initial query
929 initialQuery = getTypedText();
930 clearTypedText();
931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 if (appSearchData == null) {
933 appSearchData = new Bundle();
934 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
935 }
Romain Guycbb89e42009-06-08 15:52:54 -0700936
Karl Rosaen138a0412009-04-23 19:00:21 -0700937 final SearchManager searchManager =
938 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
939
940 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
941 if (searchWidget != null) {
942 // This gets called when the user leaves the search dialog to go back to
943 // the Launcher.
944 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
945 public void onCancel() {
946 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700947 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700948 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700949 });
950 }
Romain Guycbb89e42009-06-08 15:52:54 -0700951
Karl Rosaen138a0412009-04-23 19:00:21 -0700952 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700953 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 }
955
Karl Rosaen138a0412009-04-23 19:00:21 -0700956 /**
957 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700958 */
Romain Guy5a941392009-04-28 15:18:25 -0700959 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700960 // Close search dialog
961 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100962 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 // Restore search widget to its normal position
964 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
965 if (searchWidget != null) {
966 searchWidget.stopSearch(false);
967 }
968 }
Romain Guycbb89e42009-06-08 15:52:54 -0700969
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 @Override
971 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 if (isWorkspaceLocked()) {
973 return false;
974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975
976 super.onCreateOptionsMenu(menu);
977 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
978 .setIcon(android.R.drawable.ic_menu_add)
979 .setAlphabeticShortcut('A');
980 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
981 .setIcon(android.R.drawable.ic_menu_gallery)
982 .setAlphabeticShortcut('W');
983 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
984 .setIcon(android.R.drawable.ic_search_category_default)
985 .setAlphabeticShortcut(SearchManager.MENU_KEY);
986 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
987 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
988 .setAlphabeticShortcut('N');
989
990 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700991 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
992 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993
994 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
995 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
996 .setIntent(settings);
997
998 return true;
999 }
1000
1001 @Override
1002 public boolean onPrepareOptionsMenu(Menu menu) {
1003 super.onPrepareOptionsMenu(menu);
1004
1005 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1006 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1007
1008 return true;
1009 }
1010
1011 @Override
1012 public boolean onOptionsItemSelected(MenuItem item) {
1013 switch (item.getItemId()) {
1014 case MENU_ADD:
1015 addItems();
1016 return true;
1017 case MENU_WALLPAPER_SETTINGS:
1018 startWallpaper();
1019 return true;
1020 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001021 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 return true;
1023 case MENU_NOTIFICATIONS:
1024 showNotifications();
1025 return true;
1026 }
1027
1028 return super.onOptionsItemSelected(item);
1029 }
Romain Guycbb89e42009-06-08 15:52:54 -07001030
Karl Rosaen138a0412009-04-23 19:00:21 -07001031 /**
1032 * Indicates that we want global search for this activity by setting the globalSearch
1033 * argument for {@link #startSearch} to true.
1034 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001036 @Override
1037 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001038 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001039 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001040 }
1041
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042 public boolean isWorkspaceLocked() {
1043 return mWorkspaceLoading || mWaitingForResult;
1044 }
1045
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 private void addItems() {
1047 showAddDialog(mMenuAddInfo);
1048 }
1049
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001050 void addAppWidget(Intent data) {
1051 // TODO: catch bad widget exception when sent
1052 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001054 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1055 if (SEARCH_WIDGET.equals(customWidget)) {
1056 // We don't need this any more, since this isn't a real app widget.
1057 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1058 // add the search widget
1059 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001061 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1062
1063 if (appWidget.configure != null) {
1064 // Launch over to configure widget, if needed
1065 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1066 intent.setComponent(appWidget.configure);
1067 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1068
1069 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1070 } else {
1071 // Otherwise just add it
1072 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 }
1075 }
Romain Guycbb89e42009-06-08 15:52:54 -07001076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 void addSearch() {
1078 final Widget info = Widget.makeSearch();
1079 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 final int[] xy = mCellCoordinates;
1082 final int spanX = info.spanX;
1083 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1088 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 final View view = mInflater.inflate(info.layoutResource, null);
1091 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001092 Search search = (Search) view.findViewById(R.id.widget_search);
1093 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1096 }
1097
Romain Guy73b979d2009-06-09 12:57:21 -07001098 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001099 // Handle case where user selected "Applications"
1100 String applicationName = getResources().getString(R.string.group_applications);
1101 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001102
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001103 if (applicationName != null && applicationName.equals(shortcutName)) {
1104 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1105 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001106
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001107 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1108 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001109 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001110 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001111 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 }
1114
1115 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001116 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001117 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001118 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001119
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001120 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001121 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001122 } else {
1123 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
1126
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 UserFolderInfo folderInfo = new UserFolderInfo();
1129 folderInfo.title = getText(R.string.folder_name);
1130
1131 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1132 cellInfo.screen = mWorkspace.getCurrentScreen();
1133 if (!findSingleSlot(cellInfo)) return;
1134
1135 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136 LauncherModel.addItemToDatabase(this, folderInfo,
1137 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140
1141 // Create the view
1142 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1143 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1144 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001145 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 }
Romain Guycbb89e42009-06-08 15:52:54 -07001147
Joe Onorato9c1289c2009-08-17 11:03:03 -04001148 void removeFolder(FolderInfo folder) {
1149 mFolders.remove(folder.id);
1150 }
1151
1152 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 cellInfo.screen = mWorkspace.getCurrentScreen();
1154 if (!findSingleSlot(cellInfo)) return;
1155
1156 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1157
1158 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1160 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001161 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1162 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 }
1164 }
1165
1166 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1167 CellLayout.CellInfo cellInfo, boolean notify) {
1168
1169 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1170 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1171
1172 Drawable icon = null;
1173 boolean filtered = false;
1174 Intent.ShortcutIconResource iconResource = null;
1175
1176 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1177 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1178 try {
1179 iconResource = (Intent.ShortcutIconResource) extra;
1180 final PackageManager packageManager = context.getPackageManager();
1181 Resources resources = packageManager.getResourcesForApplication(
1182 iconResource.packageName);
1183 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1184 icon = resources.getDrawable(id);
1185 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001186 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 }
1188 }
1189
1190 if (icon == null) {
1191 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1192 }
1193
1194 final LiveFolderInfo info = new LiveFolderInfo();
1195 info.icon = icon;
1196 info.filtered = filtered;
1197 info.title = name;
1198 info.iconResource = iconResource;
1199 info.uri = data.getData();
1200 info.baseIntent = baseIntent;
1201 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1202 LiveFolders.DISPLAY_MODE_GRID);
1203
1204 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1205 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207
1208 return info;
1209 }
1210
1211 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1212 final int[] xy = new int[2];
1213 if (findSlot(cellInfo, xy, 1, 1)) {
1214 cellInfo.cellX = xy[0];
1215 cellInfo.cellY = xy[1];
1216 return true;
1217 }
1218 return false;
1219 }
1220
1221 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1222 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1223 boolean[] occupied = mSavedState != null ?
1224 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1225 cellInfo = mWorkspace.findAllVacantCells(occupied);
1226 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1227 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1228 return false;
1229 }
1230 }
1231 return true;
1232 }
1233
1234 private void showNotifications() {
1235 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1236 if (statusBar != null) {
1237 statusBar.expand();
1238 }
1239 }
1240
1241 private void startWallpaper() {
1242 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001243 Intent chooser = Intent.createChooser(pickWallpaper,
1244 getText(R.string.chooser_wallpaper));
1245 WallpaperManager wm = (WallpaperManager)
1246 getSystemService(Context.WALLPAPER_SERVICE);
1247 WallpaperInfo wi = wm.getWallpaperInfo();
1248 if (wi != null && wi.getSettingsActivity() != null) {
1249 LabeledIntent li = new LabeledIntent(getPackageName(),
1250 R.string.configure_wallpaper, 0);
1251 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1252 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1253 }
1254 startActivity(chooser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
1256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001258 public void onWindowFocusChanged(boolean hasFocus) {
1259 super.onWindowFocusChanged(hasFocus);
1260 if (!hasFocus) {
1261 mBackDown = mHomeDown = false;
1262 }
1263 }
1264
1265 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 public boolean dispatchKeyEvent(KeyEvent event) {
1267 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1268 switch (event.getKeyCode()) {
1269 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001270 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 return true;
1272 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001273 mHomeDown = true;
1274 return true;
1275 }
1276 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1277 switch (event.getKeyCode()) {
1278 case KeyEvent.KEYCODE_BACK:
1279 if (!event.isCanceled()) {
1280 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001281 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001282 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001283 } else {
1284 closeFolder();
1285 }
1286 }
1287 mBackDown = false;
1288 return true;
1289 case KeyEvent.KEYCODE_HOME:
1290 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 return true;
1292 }
1293 }
1294
1295 return super.dispatchKeyEvent(event);
1296 }
1297
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 private void closeFolder() {
1299 Folder folder = mWorkspace.getOpenFolder();
1300 if (folder != null) {
1301 closeFolder(folder);
1302 }
1303 }
1304
1305 void closeFolder(Folder folder) {
1306 folder.getInfo().opened = false;
1307 ViewGroup parent = (ViewGroup) folder.getParent();
1308 if (parent != null) {
1309 parent.removeView(folder);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001310 // TODO: this line crashes.
1311 //mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
1313 folder.onClose();
1314 }
1315
1316 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1318 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001320 private void unbindDesktopItems() {
1321 for (ItemInfo item: mDesktopItems) {
1322 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
1324 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001325
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 /**
1327 * Launches the intent referred by the clicked shortcut.
1328 *
1329 * @param v The view representing the clicked shortcut.
1330 */
1331 public void onClick(View v) {
1332 Object tag = v.getTag();
1333 if (tag instanceof ApplicationInfo) {
1334 // Open shortcut
1335 final Intent intent = ((ApplicationInfo) tag).intent;
1336 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001337 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 } else if (tag instanceof FolderInfo) {
1339 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001340 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001341 Log.d(TAG, "onClick");
1342 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001343 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001344 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001345 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001346 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 }
1348 }
1349
1350 void startActivitySafely(Intent intent) {
1351 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1352 try {
1353 startActivity(intent);
1354 } catch (ActivityNotFoundException e) {
1355 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1356 } catch (SecurityException e) {
1357 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001358 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1360 "or use the exported attribute for this activity.", e);
1361 }
1362 }
1363
1364 private void handleFolderClick(FolderInfo folderInfo) {
1365 if (!folderInfo.opened) {
1366 // Close any open folder
1367 closeFolder();
1368 // Open the requested folder
1369 openFolder(folderInfo);
1370 } else {
1371 // Find the open folder...
1372 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1373 int folderScreen;
1374 if (openFolder != null) {
1375 folderScreen = mWorkspace.getScreenForView(openFolder);
1376 // .. and close it
1377 closeFolder(openFolder);
1378 if (folderScreen != mWorkspace.getCurrentScreen()) {
1379 // Close any folder open on the current screen
1380 closeFolder();
1381 // Pull the folder onto this screen
1382 openFolder(folderInfo);
1383 }
1384 }
1385 }
1386 }
1387
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 /**
1389 * Opens the user fodler described by the specified tag. The opening of the folder
1390 * is animated relative to the specified View. If the View is null, no animation
1391 * is played.
1392 *
1393 * @param folderInfo The FolderInfo describing the folder to open.
1394 */
1395 private void openFolder(FolderInfo folderInfo) {
1396 Folder openFolder;
1397
1398 if (folderInfo instanceof UserFolderInfo) {
1399 openFolder = UserFolder.fromXml(this);
1400 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001401 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 } else {
1403 return;
1404 }
1405
Joe Onorato00acb122009-08-04 16:04:30 -04001406 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 openFolder.setLauncher(this);
1408
1409 openFolder.bind(folderInfo);
1410 folderInfo.opened = true;
1411
1412 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1413 openFolder.onOpen();
1414 }
1415
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001417 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 return false;
1419 }
1420
1421 if (!(v instanceof CellLayout)) {
1422 v = (View) v.getParent();
1423 }
1424
1425 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1426
1427 // This happens when long clicking an item with the dpad/trackball
1428 if (cellInfo == null) {
1429 return true;
1430 }
1431
1432 if (mWorkspace.allowLongPress()) {
1433 if (cellInfo.cell == null) {
1434 if (cellInfo.valid) {
1435 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001436 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 showAddDialog(cellInfo);
1438 }
1439 } else {
1440 if (!(cellInfo.cell instanceof Folder)) {
1441 // User long pressed on an item
1442 mWorkspace.startDrag(cellInfo);
1443 }
1444 }
1445 }
1446 return true;
1447 }
1448
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001449 View getDrawerHandle() {
1450 return mHandleView;
1451 }
1452
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 Workspace getWorkspace() {
1454 return mWorkspace;
1455 }
1456
Joe Onorato7404ee42009-07-31 11:54:44 -07001457 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 GridView getApplicationsGrid() {
1459 return mAllAppsGrid;
1460 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001461 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462
1463 @Override
1464 protected Dialog onCreateDialog(int id) {
1465 switch (id) {
1466 case DIALOG_CREATE_SHORTCUT:
1467 return new CreateShortcut().createDialog();
1468 case DIALOG_RENAME_FOLDER:
1469 return new RenameFolder().createDialog();
1470 }
1471
1472 return super.onCreateDialog(id);
1473 }
1474
1475 @Override
1476 protected void onPrepareDialog(int id, Dialog dialog) {
1477 switch (id) {
1478 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 break;
1480 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001481 if (mFolderInfo != null) {
1482 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1483 final CharSequence text = mFolderInfo.title;
1484 input.setText(text);
1485 input.setSelection(0, text.length());
1486 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 break;
1488 }
1489 }
1490
1491 void showRenameDialog(FolderInfo info) {
1492 mFolderInfo = info;
1493 mWaitingForResult = true;
1494 showDialog(DIALOG_RENAME_FOLDER);
1495 }
1496
1497 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1498 mAddItemCellInfo = cellInfo;
1499 mWaitingForResult = true;
1500 showDialog(DIALOG_CREATE_SHORTCUT);
1501 }
1502
Romain Guy73b979d2009-06-09 12:57:21 -07001503 private void pickShortcut(int requestCode, int title) {
1504 Bundle bundle = new Bundle();
1505
1506 ArrayList<String> shortcutNames = new ArrayList<String>();
1507 shortcutNames.add(getString(R.string.group_applications));
1508 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1509
1510 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1511 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1512 R.drawable.ic_launcher_application));
1513 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1514
1515 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1516 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1517 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1518 pickIntent.putExtras(bundle);
1519
1520 startActivityForResult(pickIntent, requestCode);
1521 }
1522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 private class RenameFolder {
1524 private EditText mInput;
1525
1526 Dialog createDialog() {
1527 mWaitingForResult = true;
1528 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1529 mInput = (EditText) layout.findViewById(R.id.folder_name);
1530
1531 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1532 builder.setIcon(0);
1533 builder.setTitle(getString(R.string.rename_folder_title));
1534 builder.setCancelable(true);
1535 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1536 public void onCancel(DialogInterface dialog) {
1537 cleanup();
1538 }
1539 });
1540 builder.setNegativeButton(getString(R.string.cancel_action),
1541 new Dialog.OnClickListener() {
1542 public void onClick(DialogInterface dialog, int which) {
1543 cleanup();
1544 }
1545 }
1546 );
1547 builder.setPositiveButton(getString(R.string.rename_action),
1548 new Dialog.OnClickListener() {
1549 public void onClick(DialogInterface dialog, int which) {
1550 changeFolderName();
1551 }
1552 }
1553 );
1554 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001555
1556 final AlertDialog dialog = builder.create();
1557 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1558 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001559 }
1560 });
1561
1562 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
1564
1565 private void changeFolderName() {
1566 final String name = mInput.getText().toString();
1567 if (!TextUtils.isEmpty(name)) {
1568 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001569 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 mFolderInfo.title = name;
1571 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1572
Joe Onorato9c1289c2009-08-17 11:03:03 -04001573 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001574 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001575 mModel.setWorkspaceDirty();
1576 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 } else {
1578 final FolderIcon folderIcon = (FolderIcon)
1579 mWorkspace.getViewForTag(mFolderInfo);
1580 if (folderIcon != null) {
1581 folderIcon.setText(name);
1582 getWorkspace().requestLayout();
1583 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001584 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001585 mModel.setWorkspaceDirty();
1586 mWorkspaceLoading = true;
1587 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
1589 }
1590 }
1591 cleanup();
1592 }
1593
1594 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 dismissDialog(DIALOG_RENAME_FOLDER);
1596 mWaitingForResult = false;
1597 mFolderInfo = null;
1598 }
1599 }
1600
Joe Onoratofb0ca672009-09-14 17:55:46 -04001601 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001602 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001603 }
1604
Jason Samsfd22dac2009-09-20 17:24:16 -07001605 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001606 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001607 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001608
Joe Onorato7c312c12009-08-13 21:36:53 -07001609 // TODO: fade these two too
1610 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001611 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001612 }
1613
Joe Onorato7bb17492009-09-24 17:51:01 -07001614 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001615 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001616 mAllAppsGrid.zoom(0.0f);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001617 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001618
1619 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001620 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001621 mDeleteZone.setVisibility(View.VISIBLE);
1622 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001623 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001624 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001625 }
1626
Joe Onorato7c312c12009-08-13 21:36:53 -07001627 void lockAllApps() {
1628 // TODO
1629 }
1630
1631 void unlockAllApps() {
1632 // TODO
1633 }
1634
Joe Onorato7404ee42009-07-31 11:54:44 -07001635 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 * Displays the shortcut creation dialog and launches, if necessary, the
1637 * appropriate activity.
1638 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001639 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001640 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1641 DialogInterface.OnShowListener {
1642
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 Dialog createDialog() {
1646 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001649
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1651 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001652 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001653
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 builder.setInverseBackgroundForced(true);
1655
1656 AlertDialog dialog = builder.create();
1657 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001658 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001659 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 return dialog;
1662 }
1663
1664 public void onCancel(DialogInterface dialog) {
1665 mWaitingForResult = false;
1666 cleanup();
1667 }
1668
Romain Guycbb89e42009-06-08 15:52:54 -07001669 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001670 }
1671
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 dismissDialog(DIALOG_CREATE_SHORTCUT);
1674 }
1675
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001676 /**
1677 * Handle the action clicked in the "Add to home" dialog.
1678 */
1679 public void onClick(DialogInterface dialog, int which) {
1680 Resources res = getResources();
1681 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001682
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001683 switch (which) {
1684 case AddAdapter.ITEM_SHORTCUT: {
1685 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001686 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001687 break;
1688 }
Romain Guycbb89e42009-06-08 15:52:54 -07001689
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001690 case AddAdapter.ITEM_APPWIDGET: {
1691 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001692
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001693 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1694 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1695 // add the search widget
1696 ArrayList<AppWidgetProviderInfo> customInfo =
1697 new ArrayList<AppWidgetProviderInfo>();
1698 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1699 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1700 info.label = getString(R.string.group_search);
1701 info.icon = R.drawable.ic_search_widget;
1702 customInfo.add(info);
1703 pickIntent.putParcelableArrayListExtra(
1704 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1705 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1706 Bundle b = new Bundle();
1707 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1708 customExtras.add(b);
1709 pickIntent.putParcelableArrayListExtra(
1710 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1711 // start the pick activity
1712 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1713 break;
1714 }
Romain Guycbb89e42009-06-08 15:52:54 -07001715
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001716 case AddAdapter.ITEM_LIVE_FOLDER: {
1717 // Insert extra item to handle inserting folder
1718 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001719
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001720 ArrayList<String> shortcutNames = new ArrayList<String>();
1721 shortcutNames.add(res.getString(R.string.group_folder));
1722 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001723
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001724 ArrayList<ShortcutIconResource> shortcutIcons =
1725 new ArrayList<ShortcutIconResource>();
1726 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1727 R.drawable.ic_launcher_folder));
1728 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1729
1730 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1731 pickIntent.putExtra(Intent.EXTRA_INTENT,
1732 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1733 pickIntent.putExtra(Intent.EXTRA_TITLE,
1734 getText(R.string.title_select_live_folder));
1735 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001736
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001737 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1738 break;
1739 }
1740
1741 case AddAdapter.ITEM_WALLPAPER: {
1742 startWallpaper();
1743 break;
1744 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 }
1746 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001747
1748 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001749 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 }
1751
1752 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001753 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001755 public int getCurrentWorkspaceScreen() {
1756 return mWorkspace.getCurrentScreen();
1757 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001758
Joe Onorato9c1289c2009-08-17 11:03:03 -04001759 /**
1760 * Refreshes the shortcuts shown on the workspace.
1761 *
1762 * Implementation of the method from LauncherModel.Callbacks.
1763 */
1764 public void startBinding() {
1765 final Workspace workspace = mWorkspace;
1766 int count = workspace.getChildCount();
1767 for (int i = 0; i < count; i++) {
1768 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1769 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001770
Joe Onorato9c1289c2009-08-17 11:03:03 -04001771 if (DEBUG_USER_INTERFACE) {
1772 android.widget.Button finishButton = new android.widget.Button(this);
1773 finishButton.setText("Finish");
1774 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1775
1776 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1777 public void onClick(View v) {
1778 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001779 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001780 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 }
1782 }
1783
1784 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001785 * Bind the items start-end from the list.
1786 *
1787 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1790
1791 final Workspace workspace = mWorkspace;
1792
1793 for (int i=start; i<end; i++) {
1794 final ItemInfo item = shortcuts.get(i);
1795 mDesktopItems.add(item);
1796 switch (item.itemType) {
1797 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1798 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1799 final View shortcut = createShortcut((ApplicationInfo) item);
1800 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1801 false);
1802 break;
1803 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1804 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1805 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1806 (UserFolderInfo) item);
1807 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1808 false);
1809 break;
1810 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1811 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1812 R.layout.live_folder_icon, this,
1813 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1814 (LiveFolderInfo) item);
1815 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1816 false);
1817 break;
1818 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1819 final int screen = workspace.getCurrentScreen();
1820 final View view = mInflater.inflate(R.layout.widget_search,
1821 (ViewGroup) workspace.getChildAt(screen), false);
1822
1823 Search search = (Search) view.findViewById(R.id.widget_search);
1824 search.setLauncher(this);
1825
1826 final Widget widget = (Widget) item;
1827 view.setTag(widget);
1828
1829 workspace.addWidget(view, widget, false);
1830 break;
1831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 }
1833
Joe Onorato9c1289c2009-08-17 11:03:03 -04001834 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 }
1836
Joe Onorato9c1289c2009-08-17 11:03:03 -04001837 /**
1838 * Implementation of the method from LauncherModel.Callbacks.
1839 */
1840 void bindFolders(HashMap<Long, FolderInfo> folders) {
1841 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843
1844 /**
1845 * Add the views for a widget to the workspace.
1846 *
1847 * Implementation of the method from LauncherModel.Callbacks.
1848 */
1849 public void bindAppWidget(LauncherAppWidgetInfo item) {
1850 final Workspace workspace = mWorkspace;
1851
1852 final int appWidgetId = item.appWidgetId;
1853 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1854 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1855
1856 if (true) {
1857 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1858 appWidgetId, appWidgetInfo));
1859 }
1860
1861 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1862 item.hostView.setTag(item);
1863
1864 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1865 item.cellY, item.spanX, item.spanY, false);
1866
1867 workspace.requestLayout();
1868
1869 mDesktopItems.add(item);
1870 }
1871
1872 /**
1873 * Callback saying that there aren't any more items to bind.
1874 *
1875 * Implementation of the method from LauncherModel.Callbacks.
1876 */
1877 public void finishBindingItems() {
1878 if (mSavedState != null) {
1879 if (!mWorkspace.hasFocus()) {
1880 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1881 }
1882
1883 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1884 if (userFolders != null) {
1885 for (long folderId : userFolders) {
1886 final FolderInfo info = mFolders.get(folderId);
1887 if (info != null) {
1888 openFolder(info);
1889 }
1890 }
1891 final Folder openFolder = mWorkspace.getOpenFolder();
1892 if (openFolder != null) {
1893 openFolder.requestFocus();
1894 }
1895 }
1896
1897 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1898 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001899 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001900 }
1901
1902 mSavedState = null;
1903 }
1904
1905 if (mSavedInstanceState != null) {
1906 super.onRestoreInstanceState(mSavedInstanceState);
1907 mSavedInstanceState = null;
1908 }
1909
1910 /* TODO
1911 if (mAllAppsVisible && !mDrawer.hasFocus()) {
1912 mDrawer.requestFocus();
1913 }
1914 */
1915
1916 Log.d(TAG, "finishBindingItems done");
1917 mWorkspaceLoading = false;
1918 }
1919
1920 /**
1921 * Add the icons for all apps.
1922 *
1923 * Implementation of the method from LauncherModel.Callbacks.
1924 */
1925 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
1926 Log.d(LOG_TAG, "got info for " + apps.size() + " apps");
1927 mAllAppsList = apps;
1928 mAllAppsGrid.setApps(mAllAppsList);
1929 }
1930
1931 /**
1932 * A package was installed.
1933 *
1934 * Implementation of the method from LauncherModel.Callbacks.
1935 */
1936 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1937 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001938 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001939 }
1940
1941 /**
1942 * A package was updated.
1943 *
1944 * Implementation of the method from LauncherModel.Callbacks.
1945 */
1946 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1947 removeDialog(DIALOG_CREATE_SHORTCUT);
1948 mWorkspace.updateShortcutsForPackage(packageName);
1949 }
1950
1951 /**
1952 * A package was uninstalled.
1953 *
1954 * Implementation of the method from LauncherModel.Callbacks.
1955 */
1956 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1957 removeDialog(DIALOG_CREATE_SHORTCUT);
1958 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001959 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961}