blob: 531e10c123dffd6c666bb57e4e4a20c2a9344ed0 [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;
Joe Onorato080d9b62009-11-02 12:01:11 -050049import android.os.SystemClock;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.text.Selection;
52import android.text.SpannableStringBuilder;
53import android.text.TextUtils;
54import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070055import static android.util.Log.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070056import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.view.KeyEvent;
59import android.view.LayoutInflater;
60import android.view.Menu;
61import android.view.MenuItem;
62import android.view.View;
63import android.view.ViewGroup;
Romain Guyb1b69f52009-08-10 15:10:15 -070064import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.View.OnLongClickListener;
66import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.widget.TextView;
69import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070070import android.appwidget.AppWidgetManager;
71import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
73import java.lang.ref.WeakReference;
74import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040075import java.util.HashMap;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070076import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070077import java.io.DataOutputStream;
78import java.io.FileNotFoundException;
79import java.io.IOException;
80import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081
82/**
83 * Default launcher application.
84 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040085public final class Launcher extends Activity
Joe Onorato7bb17492009-09-24 17:51:01 -070086 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040088 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 static final boolean LOGD = false;
90
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 static final boolean PROFILE_STARTUP = false;
92 static final boolean PROFILE_ROTATE = false;
93 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070094
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 private static final int WALLPAPER_SCREENS_SPAN = 2;
96
97 private static final int MENU_GROUP_ADD = 1;
98 private static final int MENU_ADD = Menu.FIRST + 1;
99 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
100 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
101 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700102 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103
104 private static final int REQUEST_CREATE_SHORTCUT = 1;
105 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700106 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 private static final int REQUEST_PICK_APPLICATION = 6;
108 private static final int REQUEST_PICK_SHORTCUT = 7;
109 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700110 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700111 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
113 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
114
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700115 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
116 static final String SEARCH_WIDGET = "search_widget";
117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 static final int SCREEN_COUNT = 3;
119 static final int DEFAULT_SCREN = 1;
120 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700121 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato7c312c12009-08-13 21:36:53 -0700123 static final int DIALOG_CREATE_SHORTCUT = 1;
124 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Romain Guy98d01652009-06-30 16:21:04 -0700126 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
128 // Type: int
129 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
130 // Type: boolean
131 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
132 // Type: long
133 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
148 // Type: int[]
149 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
150 // Type: boolean
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
152 // Type: long
153 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
154
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 private static final Object sLock = new Object();
158 private static int sScreen = DEFAULT_SCREN;
159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 private LayoutInflater mInflater;
161
Joe Onorato00acb122009-08-04 16:04:30 -0400162 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700164
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700165 private AppWidgetManager mAppWidgetManager;
166 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private CellLayout.CellInfo mAddItemCellInfo;
169 private CellLayout.CellInfo mMenuAddInfo;
170 private final int[] mCellCoordinates = new int[2];
171 private FolderInfo mFolderInfo;
172
Joe Onorato7c312c12009-08-13 21:36:53 -0700173 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700174 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700175 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private Bundle mSavedState;
178
179 private SpannableStringBuilder mDefaultKeySsb = null;
180
Mike LeBeau736cf282009-07-02 17:46:59 -0700181 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 private boolean mWorkspaceLoading = true;
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private boolean mRestoring;
186 private boolean mWaitingForResult;
187 private boolean mLocaleChanged;
Joe Onoratoff3862d2009-10-07 10:35:51 -0700188 private boolean mExitingBecauseOfLaunch;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
Dianne Hackborn67800862009-07-24 17:15:20 -0700190 private boolean mHomeDown;
191 private boolean mBackDown;
Jason Samsfd22dac2009-09-20 17:24:16 -0700192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private Bundle mSavedInstanceState;
194
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 private LauncherModel mModel;
196
197 private ArrayList<ItemInfo> mDesktopItems = new ArrayList();
198 private static HashMap<Long, FolderInfo> mFolders = new HashMap();
199 private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList();
Romain Guycbb89e42009-06-08 15:52:54 -0700200
Joe Onorato080d9b62009-11-02 12:01:11 -0500201 public static long lastStartTime;
202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 @Override
204 protected void onCreate(Bundle savedInstanceState) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500205 lastStartTime = SystemClock.uptimeMillis();
206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400210 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700212
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700214
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
216 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 if (PROFILE_STARTUP) {
219 android.os.Debug.startMethodTracing("/sdcard/launcher");
220 }
221
222 checkForLocaleChange();
223 setWallpaperDimension();
224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 setContentView(R.layout.launcher);
226 setupViews();
227
Joe Onorato7c312c12009-08-13 21:36:53 -0700228 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 mSavedState = savedInstanceState;
231 restoreState(mSavedState);
232
233 if (PROFILE_STARTUP) {
234 android.os.Debug.stopMethodTracing();
235 }
236
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700237 // We have a new AllAppsView, we need to re-bind everything, and it could have
238 // changed in our absence.
239 mModel.setAllAppsDirty();
240 mModel.setWorkspaceDirty();
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400243 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 }
245
246 // For handling default keys
247 mDefaultKeySsb = new SpannableStringBuilder();
248 Selection.setSelection(mDefaultKeySsb, 0);
249 }
Romain Guycbb89e42009-06-08 15:52:54 -0700250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700252 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
253 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 final Configuration configuration = getResources().getConfiguration();
256
Romain Guy98d01652009-06-30 16:21:04 -0700257 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 final String locale = configuration.locale.toString();
259
Romain Guy98d01652009-06-30 16:21:04 -0700260 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 final int mcc = configuration.mcc;
262
Romain Guy98d01652009-06-30 16:21:04 -0700263 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 final int mnc = configuration.mnc;
265
266 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
267
268 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700269 localeConfiguration.locale = locale;
270 localeConfiguration.mcc = mcc;
271 localeConfiguration.mnc = mnc;
272
273 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700275 }
276 }
277
278 private static class LocaleConfiguration {
279 public String locale;
280 public int mcc = -1;
281 public int mnc = -1;
282 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700283
Romain Guy98d01652009-06-30 16:21:04 -0700284 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
285 DataInputStream in = null;
286 try {
287 in = new DataInputStream(context.openFileInput(PREFERENCES));
288 configuration.locale = in.readUTF();
289 configuration.mcc = in.readInt();
290 configuration.mnc = in.readInt();
291 } catch (FileNotFoundException e) {
292 // Ignore
293 } catch (IOException e) {
294 // Ignore
295 } finally {
296 if (in != null) {
297 try {
298 in.close();
299 } catch (IOException e) {
300 // Ignore
301 }
302 }
303 }
304 }
305
306 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
307 DataOutputStream out = null;
308 try {
309 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
310 out.writeUTF(configuration.locale);
311 out.writeInt(configuration.mcc);
312 out.writeInt(configuration.mnc);
313 out.flush();
314 } catch (FileNotFoundException e) {
315 // Ignore
316 } catch (IOException e) {
317 //noinspection ResultOfMethodCallIgnored
318 context.getFileStreamPath(PREFERENCES).delete();
319 } finally {
320 if (out != null) {
321 try {
322 out.close();
323 } catch (IOException e) {
324 // Ignore
325 }
326 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 }
328 }
329
330 static int getScreen() {
331 synchronized (sLock) {
332 return sScreen;
333 }
334 }
335
336 static void setScreen(int screen) {
337 synchronized (sLock) {
338 sScreen = screen;
339 }
340 }
341
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700343 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344
345 Display display = getWindowManager().getDefaultDisplay();
346 boolean isPortrait = display.getWidth() < display.getHeight();
347
348 final int width = isPortrait ? display.getWidth() : display.getHeight();
349 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700350 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 }
352
353 @Override
354 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700355 mWaitingForResult = false;
356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 // The pattern used here is that a user PICKs a specific application,
358 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
361 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700362
Romain Guy94dabf12009-07-21 10:55:43 -0700363 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 switch (requestCode) {
365 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 break;
368 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700369 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 break;
371 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400372 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 break;
374 case REQUEST_PICK_LIVE_FOLDER:
375 addLiveFolder(data);
376 break;
377 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700380 case REQUEST_PICK_APPWIDGET:
381 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700383 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400384 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700386 case REQUEST_PICK_WALLPAPER:
387 // We just wanted the activity result here so we can clear mWaitingForResult
388 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700390 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700392 // Clean up the appWidgetId if we canceled
393 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
394 if (appWidgetId != -1) {
395 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 }
397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
399
400 @Override
401 protected void onResume() {
Joe Onorato080d9b62009-11-02 12:01:11 -0500402 if (lastStartTime == 0) {
403 lastStartTime = SystemClock.uptimeMillis();
404 }
405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 super.onResume();
407
408 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400409 mWorkspaceLoading = true;
410 mModel.startLoader(this, true);
411 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700413
Mike LeBeau736cf282009-07-02 17:46:59 -0700414 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
415 // onNewIntent), then close the search dialog if needed, because it probably
416 // came from the user pressing 'home' (rather than, for example, pressing 'back').
417 if (mIsNewIntent) {
418 // Post to a handler so that this happens after the search dialog tries to open
419 // itself again.
420 mWorkspace.post(new Runnable() {
421 public void run() {
422 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
423 ServiceManager.getService(Context.SEARCH_SERVICE));
424 try {
425 searchManagerService.stopSearch();
426 } catch (RemoteException e) {
427 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700428 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700429 }
430 });
431 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700432
Mike LeBeau736cf282009-07-02 17:46:59 -0700433 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 }
435
436 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700437 protected void onPause() {
438 super.onPause();
Joe Onoratoff3862d2009-10-07 10:35:51 -0700439 if (mExitingBecauseOfLaunch) {
440 mExitingBecauseOfLaunch = false;
441 closeAllApps(false);
442 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700443 }
Romain Guycbb89e42009-06-08 15:52:54 -0700444
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700445 @Override
446 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400447 // Flag the loader to stop early before switching
448 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700449
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700450 if (PROFILE_ROTATE) {
451 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
452 }
453 return null;
454 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 private boolean acceptFilter() {
457 final InputMethodManager inputManager = (InputMethodManager)
458 getSystemService(Context.INPUT_METHOD_SERVICE);
459 return !inputManager.isFullscreenMode();
460 }
461
462 @Override
463 public boolean onKeyDown(int keyCode, KeyEvent event) {
464 boolean handled = super.onKeyDown(keyCode, event);
465 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
466 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
467 keyCode, event);
468 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700469 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700470 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700471 // showSearchDialog()
472 // If there are multiple keystrokes before the search dialog takes focus,
473 // onSearchRequested() will be called for every keystroke,
474 // but it is idempotent, so it's fine.
475 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 }
477 }
478
479 return handled;
480 }
481
Karl Rosaen138a0412009-04-23 19:00:21 -0700482 private String getTypedText() {
483 return mDefaultKeySsb.toString();
484 }
485
486 private void clearTypedText() {
487 mDefaultKeySsb.clear();
488 mDefaultKeySsb.clearSpans();
489 Selection.setSelection(mDefaultKeySsb, 0);
490 }
491
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 /**
493 * Restores the previous state, if it exists.
494 *
495 * @param savedState The previous state.
496 */
497 private void restoreState(Bundle savedState) {
498 if (savedState == null) {
499 return;
500 }
501
502 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
503 if (currentScreen > -1) {
504 mWorkspace.setCurrentScreen(currentScreen);
505 }
506
507 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
508 if (addScreen > -1) {
509 mAddItemCellInfo = new CellLayout.CellInfo();
510 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
511 addItemCellInfo.valid = true;
512 addItemCellInfo.screen = addScreen;
513 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
514 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
515 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
516 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
517 addItemCellInfo.findVacantCellsFromOccupied(
518 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
519 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
520 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
521 mRestoring = true;
522 }
523
524 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
525 if (renameFolder) {
526 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400527 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800528 mRestoring = true;
529 }
530 }
531
532 /**
533 * Finds all the views we need and configure them properly.
534 */
535 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400536 DragController dragController = mDragController;
537
Romain Guyb1b69f52009-08-10 15:10:15 -0700538 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400539 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540
Joe Onorato7c312c12009-08-13 21:36:53 -0700541 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700542 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700543 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700544 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700545 // Manage focusability manually since this thing is always visible
546 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700547
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
549 final Workspace workspace = mWorkspace;
550
Joe Onorato7c312c12009-08-13 21:36:53 -0700551 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
552 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553
Joe Onorato7c312c12009-08-13 21:36:53 -0700554 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700555 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700556 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700557 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700558 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700559 handleIocon.setCrossFadeEnabled(true);
560 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400563 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565
566 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400567 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700568 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569
Joe Onorato00acb122009-08-04 16:04:30 -0400570 dragController.setDragScoller(workspace);
571 dragController.setDragListener(deleteZone);
572 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700573
Joe Onorato00acb122009-08-04 16:04:30 -0400574 // The order here is bottom to top.
575 dragController.addDropTarget(workspace);
576 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 }
578
579 /**
580 * Creates a view representing a shortcut.
581 *
582 * @param info The data structure describing the shortcut.
583 *
584 * @return A View inflated from R.layout.application.
585 */
586 View createShortcut(ApplicationInfo info) {
587 return createShortcut(R.layout.application,
588 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
589 }
590
591 /**
592 * Creates a view representing a shortcut inflated from the specified resource.
593 *
594 * @param layoutResId The id of the XML layout used to create the shortcut.
595 * @param parent The group the shortcut belongs to.
596 * @param info The data structure describing the shortcut.
597 *
598 * @return A View inflated from layoutResId.
599 */
600 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
601 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
602
Joe Onorato5162ea92009-09-03 09:39:42 -0700603 if (info.icon == null) {
604 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
605 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700607 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 info.filtered = true;
609 }
610
611 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
612 favorite.setText(info.title);
613 favorite.setTag(info);
614 favorite.setOnClickListener(this);
615
616 return favorite;
617 }
618
619 /**
620 * Add an application shortcut to the workspace.
621 *
622 * @param data The intent describing the application.
623 * @param cellInfo The position on screen where to create the shortcut.
624 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 cellInfo.screen = mWorkspace.getCurrentScreen();
627 if (!findSingleSlot(cellInfo)) return;
628
Romain Guy73b979d2009-06-09 12:57:21 -0700629 final ApplicationInfo info = infoFromApplicationIntent(context, data);
630 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700632 }
633 }
634
635 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 ComponentName component = data.getComponent();
637 PackageManager packageManager = context.getPackageManager();
638 ActivityInfo activityInfo = null;
639 try {
640 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
641 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700642 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 }
Romain Guycbb89e42009-06-08 15:52:54 -0700644
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 if (activityInfo != null) {
646 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700647
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 itemInfo.title = activityInfo.loadLabel(packageManager);
649 if (itemInfo.title == null) {
650 itemInfo.title = activityInfo.name;
651 }
Romain Guycbb89e42009-06-08 15:52:54 -0700652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
654 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
655 itemInfo.icon = activityInfo.loadIcon(packageManager);
656 itemInfo.container = ItemInfo.NO_ID;
657
Romain Guy73b979d2009-06-09 12:57:21 -0700658 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
Romain Guy73b979d2009-06-09 12:57:21 -0700660
661 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
Romain Guycbb89e42009-06-08 15:52:54 -0700663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 /**
665 * Add a shortcut to the workspace.
666 *
667 * @param data The intent describing the shortcut.
668 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400670 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 cellInfo.screen = mWorkspace.getCurrentScreen();
672 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
675
676 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400678 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
679 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
681 }
682
Romain Guycbb89e42009-06-08 15:52:54 -0700683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700685 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 * @param data The intent describing the appWidgetId.
688 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700692 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700693
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700694 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700695
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700696 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700697
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700698 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700700 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 // Try finding open space on Launcher screen
703 final int[] xy = mCellCoordinates;
704 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
705
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 // Build Launcher-specific widget info and save to database
707 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 launcherInfo.spanX = spans[0];
709 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 LauncherModel.addItemToDatabase(this, launcherInfo,
712 LauncherSettings.Favorites.CONTAINER_DESKTOP,
713 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
714
715 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700717
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700719 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700720
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700721 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700723
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400725 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 }
727 }
Romain Guycbb89e42009-06-08 15:52:54 -0700728
Joe Onorato9c1289c2009-08-17 11:03:03 -0400729 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
730 mDesktopItems.remove(launcherInfo);
731 launcherInfo.hostView = null;
732 }
733
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700734 public LauncherAppWidgetHost getAppWidgetHost() {
735 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 }
Romain Guycbb89e42009-06-08 15:52:54 -0700737
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 static ApplicationInfo addShortcut(Context context, Intent data,
739 CellLayout.CellInfo cellInfo, boolean notify) {
740
Romain Guy73b979d2009-06-09 12:57:21 -0700741 final ApplicationInfo info = infoFromShortcutIntent(context, data);
742 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
743 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
744
745 return info;
746 }
747
748 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
750 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
751 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
752
753 Drawable icon = null;
754 boolean filtered = false;
755 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700756 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757
758 if (bitmap != null) {
759 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
760 filtered = true;
761 customIcon = true;
762 } else {
763 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700764 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700766 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 final PackageManager packageManager = context.getPackageManager();
768 Resources resources = packageManager.getResourcesForApplication(
769 iconResource.packageName);
770 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
771 icon = resources.getDrawable(id);
772 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700773 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 }
775 }
776 }
777
778 if (icon == null) {
779 icon = context.getPackageManager().getDefaultActivityIcon();
780 }
781
782 final ApplicationInfo info = new ApplicationInfo();
783 info.icon = icon;
784 info.filtered = filtered;
785 info.title = name;
786 info.intent = intent;
787 info.customIcon = customIcon;
788 info.iconResource = iconResource;
789
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 return info;
791 }
792
793 @Override
794 protected void onNewIntent(Intent intent) {
795 super.onNewIntent(intent);
796
797 // Close the menu
798 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
799 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700800
Joe Onorato742966b2009-10-07 10:26:17 -0700801 // Whatever we were doing is hereby canceled.
802 mWaitingForResult = false;
803
Mike LeBeau736cf282009-07-02 17:46:59 -0700804 // Set this flag so that onResume knows to close the search dialog if it's open,
805 // because this was a new intent (thus a press of 'home' or some such) rather than
806 // for example onResume being called when the user pressed the 'back' button.
807 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808
809 try {
810 dismissDialog(DIALOG_CREATE_SHORTCUT);
811 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 } catch (Exception e) {
813 // An exception is thrown if the dialog is not visible, which is fine
814 }
815
816 try {
817 dismissDialog(DIALOG_RENAME_FOLDER);
818 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 } catch (Exception e) {
820 // An exception is thrown if the dialog is not visible, which is fine
821 }
822
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
824 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700825
Romain Guy94dabf12009-07-21 10:55:43 -0700826 if (!mWorkspace.isDefaultScreenShowing()) {
827 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700829
Joe Onorato7bb17492009-09-24 17:51:01 -0700830 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700831
832 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 if (v != null && v.getWindowToken() != null) {
834 InputMethodManager imm = (InputMethodManager)getSystemService(
835 INPUT_METHOD_SERVICE);
836 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
837 }
838 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700839 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 }
841 }
842 }
843
844 @Override
845 protected void onRestoreInstanceState(Bundle savedInstanceState) {
846 // Do not call super here
847 mSavedInstanceState = savedInstanceState;
848 }
849
850 @Override
851 protected void onSaveInstanceState(Bundle outState) {
852 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
853
854 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
855 if (folders.size() > 0) {
856 final int count = folders.size();
857 long[] ids = new long[count];
858 for (int i = 0; i < count; i++) {
859 final FolderInfo info = folders.get(i).getInfo();
860 ids[i] = info.id;
861 }
862 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
863 } else {
864 super.onSaveInstanceState(outState);
865 }
866
Romain Guy3cf604f2009-06-16 13:12:53 -0700867 final boolean isConfigurationChange = getChangingConfigurations() != 0;
868
Romain Guy5a941392009-04-28 15:18:25 -0700869 // When the drawer is opened and we are saving the state because of a
870 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400871 // TODO should not do this if the drawer is currently closing.
872 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875
876 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
877 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
878 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
879
880 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
881 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
882 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
883 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
884 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
885 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
886 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
887 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
888 layout.getOccupiedCells());
889 }
890
891 if (mFolderInfo != null && mWaitingForResult) {
892 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
893 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
894 }
895 }
896
897 @Override
898 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700900
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700902 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700904 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
906
907 TextKeyListener.getInstance().release();
908
Joe Onorato9c1289c2009-08-17 11:03:03 -0400909 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910
Joe Onorato9c1289c2009-08-17 11:03:03 -0400911 unbindDesktopItems();
912 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 }
914
915 @Override
916 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700917 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 super.startActivityForResult(intent, requestCode);
919 }
920
921 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700922 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700924
Joe Onorato7bb17492009-09-24 17:51:01 -0700925 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700926
Karl Rosaen138a0412009-04-23 19:00:21 -0700927 // Slide the search widget to the top, if it's on the current screen,
928 // otherwise show the search dialog immediately.
929 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
930 if (searchWidget == null) {
931 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
932 } else {
933 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
934 // show the currently typed text in the search widget while sliding
935 searchWidget.setQuery(getTypedText());
936 }
937 }
Romain Guycbb89e42009-06-08 15:52:54 -0700938
Karl Rosaen138a0412009-04-23 19:00:21 -0700939 /**
940 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700941 *
942 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700943 */
Romain Guycbb89e42009-06-08 15:52:54 -0700944 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700945 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700946
Karl Rosaen138a0412009-04-23 19:00:21 -0700947 if (initialQuery == null) {
948 // Use any text typed in the launcher as the initial query
949 initialQuery = getTypedText();
950 clearTypedText();
951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 if (appSearchData == null) {
953 appSearchData = new Bundle();
954 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
955 }
Romain Guycbb89e42009-06-08 15:52:54 -0700956
Karl Rosaen138a0412009-04-23 19:00:21 -0700957 final SearchManager searchManager =
958 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
959
960 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
961 if (searchWidget != null) {
962 // This gets called when the user leaves the search dialog to go back to
963 // the Launcher.
964 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
965 public void onCancel() {
966 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700967 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700968 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 });
970 }
Romain Guycbb89e42009-06-08 15:52:54 -0700971
Karl Rosaen138a0412009-04-23 19:00:21 -0700972 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700973 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 }
975
Karl Rosaen138a0412009-04-23 19:00:21 -0700976 /**
977 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700978 */
Romain Guy5a941392009-04-28 15:18:25 -0700979 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700980 // Close search dialog
981 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100982 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700983 // Restore search widget to its normal position
984 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
985 if (searchWidget != null) {
986 searchWidget.stopSearch(false);
987 }
988 }
Romain Guycbb89e42009-06-08 15:52:54 -0700989
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 @Override
991 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 if (isWorkspaceLocked()) {
993 return false;
994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995
996 super.onCreateOptionsMenu(menu);
997 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
998 .setIcon(android.R.drawable.ic_menu_add)
999 .setAlphabeticShortcut('A');
1000 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1001 .setIcon(android.R.drawable.ic_menu_gallery)
1002 .setAlphabeticShortcut('W');
1003 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1004 .setIcon(android.R.drawable.ic_search_category_default)
1005 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1006 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1007 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1008 .setAlphabeticShortcut('N');
1009
1010 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001011 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1012 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013
1014 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1015 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1016 .setIntent(settings);
1017
1018 return true;
1019 }
1020
1021 @Override
1022 public boolean onPrepareOptionsMenu(Menu menu) {
1023 super.onPrepareOptionsMenu(menu);
1024
1025 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1026 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1027
1028 return true;
1029 }
1030
1031 @Override
1032 public boolean onOptionsItemSelected(MenuItem item) {
1033 switch (item.getItemId()) {
1034 case MENU_ADD:
1035 addItems();
1036 return true;
1037 case MENU_WALLPAPER_SETTINGS:
1038 startWallpaper();
1039 return true;
1040 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001041 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 return true;
1043 case MENU_NOTIFICATIONS:
1044 showNotifications();
1045 return true;
1046 }
1047
1048 return super.onOptionsItemSelected(item);
1049 }
Romain Guycbb89e42009-06-08 15:52:54 -07001050
Karl Rosaen138a0412009-04-23 19:00:21 -07001051 /**
1052 * Indicates that we want global search for this activity by setting the globalSearch
1053 * argument for {@link #startSearch} to true.
1054 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001056 @Override
1057 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001058 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001059 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001060 }
1061
Joe Onorato9c1289c2009-08-17 11:03:03 -04001062 public boolean isWorkspaceLocked() {
1063 return mWorkspaceLoading || mWaitingForResult;
1064 }
1065
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 private void addItems() {
1067 showAddDialog(mMenuAddInfo);
1068 }
1069
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001070 void addAppWidget(Intent data) {
1071 // TODO: catch bad widget exception when sent
1072 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001074 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1075 if (SEARCH_WIDGET.equals(customWidget)) {
1076 // We don't need this any more, since this isn't a real app widget.
1077 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1078 // add the search widget
1079 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001081 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1082
1083 if (appWidget.configure != null) {
1084 // Launch over to configure widget, if needed
1085 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1086 intent.setComponent(appWidget.configure);
1087 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1088
1089 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1090 } else {
1091 // Otherwise just add it
1092 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1093 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 }
1095 }
Romain Guycbb89e42009-06-08 15:52:54 -07001096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 void addSearch() {
1098 final Widget info = Widget.makeSearch();
1099 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 final int[] xy = mCellCoordinates;
1102 final int spanX = info.spanX;
1103 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1108 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 final View view = mInflater.inflate(info.layoutResource, null);
1111 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001112 Search search = (Search) view.findViewById(R.id.widget_search);
1113 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1116 }
1117
Romain Guy73b979d2009-06-09 12:57:21 -07001118 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001119 // Handle case where user selected "Applications"
1120 String applicationName = getResources().getString(R.string.group_applications);
1121 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001122
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001123 if (applicationName != null && applicationName.equals(shortcutName)) {
1124 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1125 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001126
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001127 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1128 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001129 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001130 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001131 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 }
1134
1135 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001136 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001137 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001138 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001139
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001140 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001142 } else {
1143 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 }
1146
Joe Onorato9c1289c2009-08-17 11:03:03 -04001147 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 UserFolderInfo folderInfo = new UserFolderInfo();
1149 folderInfo.title = getText(R.string.folder_name);
1150
1151 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1152 cellInfo.screen = mWorkspace.getCurrentScreen();
1153 if (!findSingleSlot(cellInfo)) return;
1154
1155 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001156 LauncherModel.addItemToDatabase(this, folderInfo,
1157 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001159 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160
1161 // Create the view
1162 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1163 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1164 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001165 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 }
Romain Guycbb89e42009-06-08 15:52:54 -07001167
Joe Onorato9c1289c2009-08-17 11:03:03 -04001168 void removeFolder(FolderInfo folder) {
1169 mFolders.remove(folder.id);
1170 }
1171
1172 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 cellInfo.screen = mWorkspace.getCurrentScreen();
1174 if (!findSingleSlot(cellInfo)) return;
1175
1176 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1177
1178 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1180 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001181 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1182 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 }
1184 }
1185
1186 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1187 CellLayout.CellInfo cellInfo, boolean notify) {
1188
1189 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1190 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1191
1192 Drawable icon = null;
1193 boolean filtered = false;
1194 Intent.ShortcutIconResource iconResource = null;
1195
1196 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1197 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1198 try {
1199 iconResource = (Intent.ShortcutIconResource) extra;
1200 final PackageManager packageManager = context.getPackageManager();
1201 Resources resources = packageManager.getResourcesForApplication(
1202 iconResource.packageName);
1203 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1204 icon = resources.getDrawable(id);
1205 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001206 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 }
1208 }
1209
1210 if (icon == null) {
1211 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1212 }
1213
1214 final LiveFolderInfo info = new LiveFolderInfo();
1215 info.icon = icon;
1216 info.filtered = filtered;
1217 info.title = name;
1218 info.iconResource = iconResource;
1219 info.uri = data.getData();
1220 info.baseIntent = baseIntent;
1221 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1222 LiveFolders.DISPLAY_MODE_GRID);
1223
1224 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1225 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001226 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227
1228 return info;
1229 }
1230
1231 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1232 final int[] xy = new int[2];
1233 if (findSlot(cellInfo, xy, 1, 1)) {
1234 cellInfo.cellX = xy[0];
1235 cellInfo.cellY = xy[1];
1236 return true;
1237 }
1238 return false;
1239 }
1240
1241 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1242 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1243 boolean[] occupied = mSavedState != null ?
1244 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1245 cellInfo = mWorkspace.findAllVacantCells(occupied);
1246 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1247 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1248 return false;
1249 }
1250 }
1251 return true;
1252 }
1253
1254 private void showNotifications() {
1255 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1256 if (statusBar != null) {
1257 statusBar.expand();
1258 }
1259 }
1260
1261 private void startWallpaper() {
1262 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001263 Intent chooser = Intent.createChooser(pickWallpaper,
1264 getText(R.string.chooser_wallpaper));
1265 WallpaperManager wm = (WallpaperManager)
1266 getSystemService(Context.WALLPAPER_SERVICE);
1267 WallpaperInfo wi = wm.getWallpaperInfo();
1268 if (wi != null && wi.getSettingsActivity() != null) {
1269 LabeledIntent li = new LabeledIntent(getPackageName(),
1270 R.string.configure_wallpaper, 0);
1271 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1272 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1273 }
Mike Clerona0618e42009-10-22 13:55:21 -07001274 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 }
1276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001278 public void onWindowFocusChanged(boolean hasFocus) {
1279 super.onWindowFocusChanged(hasFocus);
1280 if (!hasFocus) {
1281 mBackDown = mHomeDown = false;
1282 }
1283 }
1284
1285 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 public boolean dispatchKeyEvent(KeyEvent event) {
1287 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1288 switch (event.getKeyCode()) {
1289 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001290 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 return true;
1292 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001293 mHomeDown = true;
1294 return true;
1295 }
1296 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1297 switch (event.getKeyCode()) {
1298 case KeyEvent.KEYCODE_BACK:
1299 if (!event.isCanceled()) {
1300 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001301 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001302 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001303 } else {
1304 closeFolder();
1305 }
1306 }
1307 mBackDown = false;
1308 return true;
1309 case KeyEvent.KEYCODE_HOME:
1310 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 return true;
1312 }
1313 }
1314
1315 return super.dispatchKeyEvent(event);
1316 }
1317
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 private void closeFolder() {
1319 Folder folder = mWorkspace.getOpenFolder();
1320 if (folder != null) {
1321 closeFolder(folder);
1322 }
1323 }
1324
1325 void closeFolder(Folder folder) {
1326 folder.getInfo().opened = false;
1327 ViewGroup parent = (ViewGroup) folder.getParent();
1328 if (parent != null) {
1329 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001330 if (folder instanceof DropTarget) {
1331 // Live folders aren't DropTargets.
1332 mDragController.removeDropTarget((DropTarget)folder);
1333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335 folder.onClose();
1336 }
1337
1338 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1340 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001342 private void unbindDesktopItems() {
1343 for (ItemInfo item: mDesktopItems) {
1344 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001347
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 /**
1349 * Launches the intent referred by the clicked shortcut.
1350 *
1351 * @param v The view representing the clicked shortcut.
1352 */
1353 public void onClick(View v) {
1354 Object tag = v.getTag();
1355 if (tag instanceof ApplicationInfo) {
1356 // Open shortcut
1357 final Intent intent = ((ApplicationInfo) tag).intent;
1358 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001359 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 } else if (tag instanceof FolderInfo) {
1361 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001362 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001363 Log.d(TAG, "onClick");
1364 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001365 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001366 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001367 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001368 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 }
1370 }
1371
1372 void startActivitySafely(Intent intent) {
1373 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1374 try {
1375 startActivity(intent);
1376 } catch (ActivityNotFoundException e) {
1377 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1378 } catch (SecurityException e) {
1379 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001380 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1382 "or use the exported attribute for this activity.", e);
1383 }
1384 }
1385
1386 private void handleFolderClick(FolderInfo folderInfo) {
1387 if (!folderInfo.opened) {
1388 // Close any open folder
1389 closeFolder();
1390 // Open the requested folder
1391 openFolder(folderInfo);
1392 } else {
1393 // Find the open folder...
1394 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1395 int folderScreen;
1396 if (openFolder != null) {
1397 folderScreen = mWorkspace.getScreenForView(openFolder);
1398 // .. and close it
1399 closeFolder(openFolder);
1400 if (folderScreen != mWorkspace.getCurrentScreen()) {
1401 // Close any folder open on the current screen
1402 closeFolder();
1403 // Pull the folder onto this screen
1404 openFolder(folderInfo);
1405 }
1406 }
1407 }
1408 }
1409
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 /**
1411 * Opens the user fodler described by the specified tag. The opening of the folder
1412 * is animated relative to the specified View. If the View is null, no animation
1413 * is played.
1414 *
1415 * @param folderInfo The FolderInfo describing the folder to open.
1416 */
1417 private void openFolder(FolderInfo folderInfo) {
1418 Folder openFolder;
1419
1420 if (folderInfo instanceof UserFolderInfo) {
1421 openFolder = UserFolder.fromXml(this);
1422 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001423 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 } else {
1425 return;
1426 }
1427
Joe Onorato00acb122009-08-04 16:04:30 -04001428 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 openFolder.setLauncher(this);
1430
1431 openFolder.bind(folderInfo);
1432 folderInfo.opened = true;
1433
1434 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1435 openFolder.onOpen();
1436 }
1437
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001439 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 return false;
1441 }
1442
1443 if (!(v instanceof CellLayout)) {
1444 v = (View) v.getParent();
1445 }
1446
1447 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1448
1449 // This happens when long clicking an item with the dpad/trackball
1450 if (cellInfo == null) {
1451 return true;
1452 }
1453
1454 if (mWorkspace.allowLongPress()) {
1455 if (cellInfo.cell == null) {
1456 if (cellInfo.valid) {
1457 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001458 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 showAddDialog(cellInfo);
1460 }
1461 } else {
1462 if (!(cellInfo.cell instanceof Folder)) {
1463 // User long pressed on an item
1464 mWorkspace.startDrag(cellInfo);
1465 }
1466 }
1467 }
1468 return true;
1469 }
1470
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001471 View getDrawerHandle() {
1472 return mHandleView;
1473 }
1474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 Workspace getWorkspace() {
1476 return mWorkspace;
1477 }
1478
Joe Onorato7404ee42009-07-31 11:54:44 -07001479 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 GridView getApplicationsGrid() {
1481 return mAllAppsGrid;
1482 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001483 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484
1485 @Override
1486 protected Dialog onCreateDialog(int id) {
1487 switch (id) {
1488 case DIALOG_CREATE_SHORTCUT:
1489 return new CreateShortcut().createDialog();
1490 case DIALOG_RENAME_FOLDER:
1491 return new RenameFolder().createDialog();
1492 }
1493
1494 return super.onCreateDialog(id);
1495 }
1496
1497 @Override
1498 protected void onPrepareDialog(int id, Dialog dialog) {
1499 switch (id) {
1500 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 break;
1502 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001503 if (mFolderInfo != null) {
1504 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1505 final CharSequence text = mFolderInfo.title;
1506 input.setText(text);
1507 input.setSelection(0, text.length());
1508 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 break;
1510 }
1511 }
1512
1513 void showRenameDialog(FolderInfo info) {
1514 mFolderInfo = info;
1515 mWaitingForResult = true;
1516 showDialog(DIALOG_RENAME_FOLDER);
1517 }
1518
1519 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1520 mAddItemCellInfo = cellInfo;
1521 mWaitingForResult = true;
1522 showDialog(DIALOG_CREATE_SHORTCUT);
1523 }
1524
Romain Guy73b979d2009-06-09 12:57:21 -07001525 private void pickShortcut(int requestCode, int title) {
1526 Bundle bundle = new Bundle();
1527
1528 ArrayList<String> shortcutNames = new ArrayList<String>();
1529 shortcutNames.add(getString(R.string.group_applications));
1530 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1531
1532 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1533 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1534 R.drawable.ic_launcher_application));
1535 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1536
1537 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1538 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1539 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1540 pickIntent.putExtras(bundle);
1541
1542 startActivityForResult(pickIntent, requestCode);
1543 }
1544
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 private class RenameFolder {
1546 private EditText mInput;
1547
1548 Dialog createDialog() {
1549 mWaitingForResult = true;
1550 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1551 mInput = (EditText) layout.findViewById(R.id.folder_name);
1552
1553 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1554 builder.setIcon(0);
1555 builder.setTitle(getString(R.string.rename_folder_title));
1556 builder.setCancelable(true);
1557 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1558 public void onCancel(DialogInterface dialog) {
1559 cleanup();
1560 }
1561 });
1562 builder.setNegativeButton(getString(R.string.cancel_action),
1563 new Dialog.OnClickListener() {
1564 public void onClick(DialogInterface dialog, int which) {
1565 cleanup();
1566 }
1567 }
1568 );
1569 builder.setPositiveButton(getString(R.string.rename_action),
1570 new Dialog.OnClickListener() {
1571 public void onClick(DialogInterface dialog, int which) {
1572 changeFolderName();
1573 }
1574 }
1575 );
1576 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001577
1578 final AlertDialog dialog = builder.create();
1579 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1580 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001581 }
1582 });
1583
1584 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
1586
1587 private void changeFolderName() {
1588 final String name = mInput.getText().toString();
1589 if (!TextUtils.isEmpty(name)) {
1590 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001591 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 mFolderInfo.title = name;
1593 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1594
Joe Onorato9c1289c2009-08-17 11:03:03 -04001595 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001596 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001597 mModel.setWorkspaceDirty();
1598 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 } else {
1600 final FolderIcon folderIcon = (FolderIcon)
1601 mWorkspace.getViewForTag(mFolderInfo);
1602 if (folderIcon != null) {
1603 folderIcon.setText(name);
1604 getWorkspace().requestLayout();
1605 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001606 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001607 mModel.setWorkspaceDirty();
1608 mWorkspaceLoading = true;
1609 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 }
1611 }
1612 }
1613 cleanup();
1614 }
1615
1616 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 dismissDialog(DIALOG_RENAME_FOLDER);
1618 mWaitingForResult = false;
1619 mFolderInfo = null;
1620 }
1621 }
1622
Joe Onoratofb0ca672009-09-14 17:55:46 -04001623 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001624 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001625 }
1626
Jason Samsfd22dac2009-09-20 17:24:16 -07001627 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001628 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001629 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001630
Mike Cleronb6082fa02009-10-19 17:03:36 -07001631 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001632 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001633
Joe Onorato7c312c12009-08-13 21:36:53 -07001634 // TODO: fade these two too
1635 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001636 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001637 }
1638
Joe Onorato7bb17492009-09-24 17:51:01 -07001639 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001640 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001641 mAllAppsGrid.zoom(0.0f);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001642 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001643 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001644
Mike Cleronb6082fa02009-10-19 17:03:36 -07001645
Joe Onorato7c312c12009-08-13 21:36:53 -07001646 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001647 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001648 mDeleteZone.setVisibility(View.VISIBLE);
1649 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001650 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001651 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001652 }
1653
Joe Onorato7c312c12009-08-13 21:36:53 -07001654 void lockAllApps() {
1655 // TODO
1656 }
1657
1658 void unlockAllApps() {
1659 // TODO
1660 }
1661
Joe Onorato7404ee42009-07-31 11:54:44 -07001662 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 * Displays the shortcut creation dialog and launches, if necessary, the
1664 * appropriate activity.
1665 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001666 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001667 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1668 DialogInterface.OnShowListener {
1669
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001671
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 Dialog createDialog() {
1673 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001674
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001676
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1678 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001679 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001680
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 builder.setInverseBackgroundForced(true);
1682
1683 AlertDialog dialog = builder.create();
1684 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001685 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001686 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 return dialog;
1689 }
1690
1691 public void onCancel(DialogInterface dialog) {
1692 mWaitingForResult = false;
1693 cleanup();
1694 }
1695
Romain Guycbb89e42009-06-08 15:52:54 -07001696 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001697 }
1698
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 dismissDialog(DIALOG_CREATE_SHORTCUT);
1701 }
1702
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001703 /**
1704 * Handle the action clicked in the "Add to home" dialog.
1705 */
1706 public void onClick(DialogInterface dialog, int which) {
1707 Resources res = getResources();
1708 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001709
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001710 switch (which) {
1711 case AddAdapter.ITEM_SHORTCUT: {
1712 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001713 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001714 break;
1715 }
Romain Guycbb89e42009-06-08 15:52:54 -07001716
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001717 case AddAdapter.ITEM_APPWIDGET: {
1718 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001719
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001720 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1721 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1722 // add the search widget
1723 ArrayList<AppWidgetProviderInfo> customInfo =
1724 new ArrayList<AppWidgetProviderInfo>();
1725 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1726 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1727 info.label = getString(R.string.group_search);
1728 info.icon = R.drawable.ic_search_widget;
1729 customInfo.add(info);
1730 pickIntent.putParcelableArrayListExtra(
1731 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1732 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1733 Bundle b = new Bundle();
1734 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1735 customExtras.add(b);
1736 pickIntent.putParcelableArrayListExtra(
1737 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1738 // start the pick activity
1739 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1740 break;
1741 }
Romain Guycbb89e42009-06-08 15:52:54 -07001742
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001743 case AddAdapter.ITEM_LIVE_FOLDER: {
1744 // Insert extra item to handle inserting folder
1745 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001746
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001747 ArrayList<String> shortcutNames = new ArrayList<String>();
1748 shortcutNames.add(res.getString(R.string.group_folder));
1749 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001750
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001751 ArrayList<ShortcutIconResource> shortcutIcons =
1752 new ArrayList<ShortcutIconResource>();
1753 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1754 R.drawable.ic_launcher_folder));
1755 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1756
1757 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1758 pickIntent.putExtra(Intent.EXTRA_INTENT,
1759 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1760 pickIntent.putExtra(Intent.EXTRA_TITLE,
1761 getText(R.string.title_select_live_folder));
1762 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001763
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001764 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1765 break;
1766 }
1767
1768 case AddAdapter.ITEM_WALLPAPER: {
1769 startWallpaper();
1770 break;
1771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 }
1773 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001774
1775 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001776 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 }
1778
1779 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001780 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001782 public int getCurrentWorkspaceScreen() {
1783 return mWorkspace.getCurrentScreen();
1784 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001785
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786 /**
1787 * Refreshes the shortcuts shown on the workspace.
1788 *
1789 * Implementation of the method from LauncherModel.Callbacks.
1790 */
1791 public void startBinding() {
1792 final Workspace workspace = mWorkspace;
1793 int count = workspace.getChildCount();
1794 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001795 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001796 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1797 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001798
Joe Onorato9c1289c2009-08-17 11:03:03 -04001799 if (DEBUG_USER_INTERFACE) {
1800 android.widget.Button finishButton = new android.widget.Button(this);
1801 finishButton.setText("Finish");
1802 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1803
1804 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1805 public void onClick(View v) {
1806 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
1810 }
1811
1812 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001813 * Bind the items start-end from the list.
1814 *
1815 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001817 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1818
1819 final Workspace workspace = mWorkspace;
1820
1821 for (int i=start; i<end; i++) {
1822 final ItemInfo item = shortcuts.get(i);
1823 mDesktopItems.add(item);
1824 switch (item.itemType) {
1825 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1826 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1827 final View shortcut = createShortcut((ApplicationInfo) item);
1828 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1829 false);
1830 break;
1831 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1832 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1833 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1834 (UserFolderInfo) item);
1835 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1836 false);
1837 break;
1838 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1839 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1840 R.layout.live_folder_icon, this,
1841 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1842 (LiveFolderInfo) item);
1843 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1844 false);
1845 break;
1846 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1847 final int screen = workspace.getCurrentScreen();
1848 final View view = mInflater.inflate(R.layout.widget_search,
1849 (ViewGroup) workspace.getChildAt(screen), false);
1850
1851 Search search = (Search) view.findViewById(R.id.widget_search);
1852 search.setLauncher(this);
1853
1854 final Widget widget = (Widget) item;
1855 view.setTag(widget);
1856
1857 workspace.addWidget(view, widget, false);
1858 break;
1859 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 }
1861
Joe Onorato9c1289c2009-08-17 11:03:03 -04001862 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
1864
Joe Onorato9c1289c2009-08-17 11:03:03 -04001865 /**
1866 * Implementation of the method from LauncherModel.Callbacks.
1867 */
1868 void bindFolders(HashMap<Long, FolderInfo> folders) {
1869 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001871
1872 /**
1873 * Add the views for a widget to the workspace.
1874 *
1875 * Implementation of the method from LauncherModel.Callbacks.
1876 */
1877 public void bindAppWidget(LauncherAppWidgetInfo item) {
1878 final Workspace workspace = mWorkspace;
1879
1880 final int appWidgetId = item.appWidgetId;
1881 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1882 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1883
1884 if (true) {
1885 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1886 appWidgetId, appWidgetInfo));
1887 }
1888
1889 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1890 item.hostView.setTag(item);
1891
1892 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1893 item.cellY, item.spanX, item.spanY, false);
1894
1895 workspace.requestLayout();
1896
1897 mDesktopItems.add(item);
1898 }
1899
1900 /**
1901 * Callback saying that there aren't any more items to bind.
1902 *
1903 * Implementation of the method from LauncherModel.Callbacks.
1904 */
1905 public void finishBindingItems() {
1906 if (mSavedState != null) {
1907 if (!mWorkspace.hasFocus()) {
1908 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1909 }
1910
1911 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1912 if (userFolders != null) {
1913 for (long folderId : userFolders) {
1914 final FolderInfo info = mFolders.get(folderId);
1915 if (info != null) {
1916 openFolder(info);
1917 }
1918 }
1919 final Folder openFolder = mWorkspace.getOpenFolder();
1920 if (openFolder != null) {
1921 openFolder.requestFocus();
1922 }
1923 }
1924
1925 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1926 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001927 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001928 }
1929
1930 mSavedState = null;
1931 }
1932
1933 if (mSavedInstanceState != null) {
1934 super.onRestoreInstanceState(mSavedInstanceState);
1935 mSavedInstanceState = null;
1936 }
1937
Joe Onorato9c1289c2009-08-17 11:03:03 -04001938 Log.d(TAG, "finishBindingItems done");
1939 mWorkspaceLoading = false;
1940 }
1941
1942 /**
1943 * Add the icons for all apps.
1944 *
1945 * Implementation of the method from LauncherModel.Callbacks.
1946 */
1947 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001948 mAllAppsList = apps;
1949 mAllAppsGrid.setApps(mAllAppsList);
1950 }
1951
1952 /**
1953 * A package was installed.
1954 *
1955 * Implementation of the method from LauncherModel.Callbacks.
1956 */
1957 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1958 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001959 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001960 }
1961
1962 /**
1963 * A package was updated.
1964 *
1965 * Implementation of the method from LauncherModel.Callbacks.
1966 */
1967 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1968 removeDialog(DIALOG_CREATE_SHORTCUT);
1969 mWorkspace.updateShortcutsForPackage(packageName);
1970 }
1971
1972 /**
1973 * A package was uninstalled.
1974 *
1975 * Implementation of the method from LauncherModel.Callbacks.
1976 */
1977 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1978 removeDialog(DIALOG_CREATE_SHORTCUT);
1979 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001980 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982}