blob: 6f6183e4542552423997e213464e57b0b5c9ac21 [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;
22import android.app.SearchManager;
23import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070024import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080026import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080028import android.content.ContentResolver;
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;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080033import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ActivityInfo;
35import 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;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080041import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080042import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080044import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080046import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.os.Parcelable;
Joe Onoratobe386092009-11-17 17:32:16 -080048import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.text.Selection;
51import android.text.SpannableStringBuilder;
52import android.text.TextUtils;
53import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070054import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080056import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.KeyEvent;
58import android.view.LayoutInflater;
59import android.view.Menu;
60import android.view.MenuItem;
61import android.view.View;
62import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.View.OnLongClickListener;
64import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.widget.TextView;
67import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080068import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080069import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080070import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070071import android.appwidget.AppWidgetManager;
72import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040075import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070076import java.io.DataOutputStream;
77import java.io.FileNotFoundException;
78import java.io.IOException;
79import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
81/**
82 * Default launcher application.
83 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040084public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080085 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080086 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 static final boolean LOGD = false;
88
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 static final boolean PROFILE_STARTUP = false;
90 static final boolean PROFILE_ROTATE = false;
91 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070092
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 private static final int WALLPAPER_SCREENS_SPAN = 2;
94
95 private static final int MENU_GROUP_ADD = 1;
96 private static final int MENU_ADD = Menu.FIRST + 1;
97 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
98 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
99 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700100 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
102 private static final int REQUEST_CREATE_SHORTCUT = 1;
103 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700104 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 private static final int REQUEST_PICK_APPLICATION = 6;
106 private static final int REQUEST_PICK_SHORTCUT = 7;
107 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700108 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700109 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
111 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
112
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800113 static final int SCREEN_COUNT = 5;
114 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700116 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Joe Onorato7c312c12009-08-13 21:36:53 -0700118 static final int DIALOG_CREATE_SHORTCUT = 1;
119 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Romain Guy98d01652009-06-30 16:21:04 -0700121 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
123 // Type: int
124 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
125 // Type: boolean
126 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
127 // Type: long
128 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
129 // Type: int
130 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
131 // Type: int
132 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
133 // Type: int
134 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
143 // Type: int[]
144 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
145 // Type: boolean
146 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
147 // Type: long
148 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
149
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800153 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800155 private final BroadcastReceiver mCloseSystemDialogsReceiver
156 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800157 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 private LayoutInflater mInflater;
160
Joe Onorato00acb122009-08-04 16:04:30 -0400161 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700163
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700164 private AppWidgetManager mAppWidgetManager;
165 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private CellLayout.CellInfo mAddItemCellInfo;
168 private CellLayout.CellInfo mMenuAddInfo;
169 private final int[] mCellCoordinates = new int[2];
170 private FolderInfo mFolderInfo;
171
Joe Onorato7c312c12009-08-13 21:36:53 -0700172 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700173 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700174 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private Bundle mSavedState;
177
178 private SpannableStringBuilder mDefaultKeySsb = null;
179
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 private boolean mWorkspaceLoading = true;
181
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800182 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private boolean mRestoring;
184 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
186 private Bundle mSavedInstanceState;
187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 private LauncherModel mModel;
189
Romain Guy84f296c2009-11-04 15:00:44 -0800190 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
191 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700192
Romain Guy1fbc1c82009-11-09 20:43:08 -0800193 private ImageView mPreviousView;
194 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 @Override
197 protected void onCreate(Bundle savedInstanceState) {
198 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400201 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700205 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
206 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 if (PROFILE_STARTUP) {
209 android.os.Debug.startMethodTracing("/sdcard/launcher");
210 }
211
212 checkForLocaleChange();
213 setWallpaperDimension();
214
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 setContentView(R.layout.launcher);
216 setupViews();
217
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800218 registerContentObservers();
219
Joe Onorato7c312c12009-08-13 21:36:53 -0700220 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 mSavedState = savedInstanceState;
223 restoreState(mSavedState);
224
225 if (PROFILE_STARTUP) {
226 android.os.Debug.stopMethodTracing();
227 }
228
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700229 // We have a new AllAppsView, we need to re-bind everything, and it could have
230 // changed in our absence.
231 mModel.setAllAppsDirty();
232 mModel.setWorkspaceDirty();
233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 }
237
238 // For handling default keys
239 mDefaultKeySsb = new SpannableStringBuilder();
240 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800241
242 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
243 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 }
Romain Guycbb89e42009-06-08 15:52:54 -0700245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700247 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
248 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 final Configuration configuration = getResources().getConfiguration();
251
Romain Guy98d01652009-06-30 16:21:04 -0700252 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 final String locale = configuration.locale.toString();
254
Romain Guy98d01652009-06-30 16:21:04 -0700255 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 final int mcc = configuration.mcc;
257
Romain Guy98d01652009-06-30 16:21:04 -0700258 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 final int mnc = configuration.mnc;
260
Romain Guy84f296c2009-11-04 15:00:44 -0800261 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
Romain Guy84f296c2009-11-04 15:00:44 -0800263 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700264 localeConfiguration.locale = locale;
265 localeConfiguration.mcc = mcc;
266 localeConfiguration.mnc = mnc;
267
268 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700270 }
271 }
272
273 private static class LocaleConfiguration {
274 public String locale;
275 public int mcc = -1;
276 public int mnc = -1;
277 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700278
Romain Guy98d01652009-06-30 16:21:04 -0700279 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
280 DataInputStream in = null;
281 try {
282 in = new DataInputStream(context.openFileInput(PREFERENCES));
283 configuration.locale = in.readUTF();
284 configuration.mcc = in.readInt();
285 configuration.mnc = in.readInt();
286 } catch (FileNotFoundException e) {
287 // Ignore
288 } catch (IOException e) {
289 // Ignore
290 } finally {
291 if (in != null) {
292 try {
293 in.close();
294 } catch (IOException e) {
295 // Ignore
296 }
297 }
298 }
299 }
300
301 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
302 DataOutputStream out = null;
303 try {
304 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
305 out.writeUTF(configuration.locale);
306 out.writeInt(configuration.mcc);
307 out.writeInt(configuration.mnc);
308 out.flush();
309 } catch (FileNotFoundException e) {
310 // Ignore
311 } catch (IOException e) {
312 //noinspection ResultOfMethodCallIgnored
313 context.getFileStreamPath(PREFERENCES).delete();
314 } finally {
315 if (out != null) {
316 try {
317 out.close();
318 } catch (IOException e) {
319 // Ignore
320 }
321 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
323 }
324
325 static int getScreen() {
326 synchronized (sLock) {
327 return sScreen;
328 }
329 }
330
331 static void setScreen(int screen) {
332 synchronized (sLock) {
333 sScreen = screen;
334 }
335 }
336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700338 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339
340 Display display = getWindowManager().getDefaultDisplay();
341 boolean isPortrait = display.getWidth() < display.getHeight();
342
343 final int width = isPortrait ? display.getWidth() : display.getHeight();
344 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700345 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 }
347
348 @Override
349 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700350 mWaitingForResult = false;
351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 // The pattern used here is that a user PICKs a specific application,
353 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
356 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700357
Romain Guy94dabf12009-07-21 10:55:43 -0700358 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 switch (requestCode) {
360 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400361 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 break;
363 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700364 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 break;
366 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 break;
369 case REQUEST_PICK_LIVE_FOLDER:
370 addLiveFolder(data);
371 break;
372 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700375 case REQUEST_PICK_APPWIDGET:
376 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700378 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700381 case REQUEST_PICK_WALLPAPER:
382 // We just wanted the activity result here so we can clear mWaitingForResult
383 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 }
Romain Guy18042c82009-11-06 11:44:55 -0800385 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
386 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
387 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700388 // Clean up the appWidgetId if we canceled
389 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
390 if (appWidgetId != -1) {
391 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
393 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 }
395
396 @Override
397 protected void onResume() {
398 super.onResume();
399
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800400 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800401
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400403 mWorkspaceLoading = true;
404 mModel.startLoader(this, true);
405 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 }
407 }
408
409 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700410 protected void onPause() {
411 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800412 dismissPreview(mPreviousView);
413 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800414 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700415 }
Romain Guycbb89e42009-06-08 15:52:54 -0700416
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700417 @Override
418 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400419 // Flag the loader to stop early before switching
420 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700421
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700422 if (PROFILE_ROTATE) {
423 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
424 }
425 return null;
426 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 private boolean acceptFilter() {
429 final InputMethodManager inputManager = (InputMethodManager)
430 getSystemService(Context.INPUT_METHOD_SERVICE);
431 return !inputManager.isFullscreenMode();
432 }
433
434 @Override
435 public boolean onKeyDown(int keyCode, KeyEvent event) {
436 boolean handled = super.onKeyDown(keyCode, event);
437 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
438 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
439 keyCode, event);
440 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700441 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700442 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700443 // showSearchDialog()
444 // If there are multiple keystrokes before the search dialog takes focus,
445 // onSearchRequested() will be called for every keystroke,
446 // but it is idempotent, so it's fine.
447 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 }
449 }
450
451 return handled;
452 }
453
Karl Rosaen138a0412009-04-23 19:00:21 -0700454 private String getTypedText() {
455 return mDefaultKeySsb.toString();
456 }
457
458 private void clearTypedText() {
459 mDefaultKeySsb.clear();
460 mDefaultKeySsb.clearSpans();
461 Selection.setSelection(mDefaultKeySsb, 0);
462 }
463
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 /**
465 * Restores the previous state, if it exists.
466 *
467 * @param savedState The previous state.
468 */
469 private void restoreState(Bundle savedState) {
470 if (savedState == null) {
471 return;
472 }
473
Joe Onorato3a8820b2009-11-10 15:06:42 -0800474 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
475 if (allApps) {
476 showAllApps(false);
477 }
478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
480 if (currentScreen > -1) {
481 mWorkspace.setCurrentScreen(currentScreen);
482 }
483
484 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
485 if (addScreen > -1) {
486 mAddItemCellInfo = new CellLayout.CellInfo();
487 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
488 addItemCellInfo.valid = true;
489 addItemCellInfo.screen = addScreen;
490 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
491 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
492 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
493 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
494 addItemCellInfo.findVacantCellsFromOccupied(
495 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
496 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
497 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
498 mRestoring = true;
499 }
500
501 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
502 if (renameFolder) {
503 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400504 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 mRestoring = true;
506 }
507 }
508
509 /**
510 * Finds all the views we need and configure them properly.
511 */
512 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400513 DragController dragController = mDragController;
514
Romain Guyb1b69f52009-08-10 15:10:15 -0700515 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400516 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517
Joe Onorato7c312c12009-08-13 21:36:53 -0700518 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700519 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700520 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700521 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700522 // Manage focusability manually since this thing is always visible
523 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700524
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800525 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
526 final Workspace workspace = mWorkspace;
527
Joe Onorato7c312c12009-08-13 21:36:53 -0700528 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
529 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530
Joe Onorato7c312c12009-08-13 21:36:53 -0700531 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700532 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700533 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800534 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800535
536 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
537 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
538
539 Drawable previous = mPreviousView.getDrawable();
540 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800541 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542
Joe Onorato0d44e942009-11-16 18:20:51 -0800543 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800544 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800545 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800546 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800547
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400549 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551
552 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400553 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700554 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555
Joe Onorato00acb122009-08-04 16:04:30 -0400556 dragController.setDragScoller(workspace);
557 dragController.setDragListener(deleteZone);
558 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700559
Joe Onorato00acb122009-08-04 16:04:30 -0400560 // The order here is bottom to top.
561 dragController.addDropTarget(workspace);
562 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 }
564
Romain Guy8a73c512009-11-09 19:19:59 -0800565 @SuppressWarnings({"UnusedDeclaration"})
566 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800567 if (!isAllAppsVisible()) {
568 mWorkspace.scrollLeft();
569 }
Romain Guy8a73c512009-11-09 19:19:59 -0800570 }
571
572 @SuppressWarnings({"UnusedDeclaration"})
573 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800574 if (!isAllAppsVisible()) {
575 mWorkspace.scrollRight();
576 }
Romain Guy8a73c512009-11-09 19:19:59 -0800577 }
578
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 /**
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) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800642 Log.e(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
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800694 if (LOGD) Log.d(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;
Romain Guy18042c82009-11-06 11:44:55 -0800704 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
705 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
706 return;
707 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700709 // Build Launcher-specific widget info and save to database
710 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 launcherInfo.spanX = spans[0];
712 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 LauncherModel.addItemToDatabase(this, launcherInfo,
715 LauncherSettings.Favorites.CONTAINER_DESKTOP,
716 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
717
718 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700720
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700722 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700723
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700724 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700726
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400728 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 }
730 }
Romain Guycbb89e42009-06-08 15:52:54 -0700731
Joe Onorato9c1289c2009-08-17 11:03:03 -0400732 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
733 mDesktopItems.remove(launcherInfo);
734 launcherInfo.hostView = null;
735 }
736
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700737 public LauncherAppWidgetHost getAppWidgetHost() {
738 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 }
Romain Guycbb89e42009-06-08 15:52:54 -0700740
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 static ApplicationInfo addShortcut(Context context, Intent data,
742 CellLayout.CellInfo cellInfo, boolean notify) {
743
Romain Guy73b979d2009-06-09 12:57:21 -0700744 final ApplicationInfo info = infoFromShortcutIntent(context, data);
745 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
746 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
747
748 return info;
749 }
750
751 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
753 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guyb5de7752010-01-04 14:43:49 -0800754 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755
756 Drawable icon = null;
757 boolean filtered = false;
758 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700759 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760
Romain Guyb5de7752010-01-04 14:43:49 -0800761 if (bitmap != null && bitmap instanceof Bitmap) {
762 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail((Bitmap) bitmap, context));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 filtered = true;
764 customIcon = true;
765 } else {
766 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700767 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700769 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 final PackageManager packageManager = context.getPackageManager();
771 Resources resources = packageManager.getResourcesForApplication(
772 iconResource.packageName);
773 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
774 icon = resources.getDrawable(id);
775 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800776 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 }
778 }
779 }
780
781 if (icon == null) {
782 icon = context.getPackageManager().getDefaultActivityIcon();
783 }
784
785 final ApplicationInfo info = new ApplicationInfo();
786 info.icon = icon;
787 info.filtered = filtered;
788 info.title = name;
789 info.intent = intent;
790 info.customIcon = customIcon;
791 info.iconResource = iconResource;
792
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 return info;
794 }
795
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800796 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800797 getWindow().closeAllPanels();
798
799 try {
800 dismissDialog(DIALOG_CREATE_SHORTCUT);
801 // Unlock the workspace if the dialog was showing
802 } catch (Exception e) {
803 // An exception is thrown if the dialog is not visible, which is fine
804 }
805
806 try {
807 dismissDialog(DIALOG_RENAME_FOLDER);
808 // Unlock the workspace if the dialog was showing
809 } catch (Exception e) {
810 // An exception is thrown if the dialog is not visible, which is fine
811 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800812
813 // Whatever we were doing is hereby canceled.
814 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800815 }
816
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 @Override
818 protected void onNewIntent(Intent intent) {
819 super.onNewIntent(intent);
820
821 // Close the menu
822 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800823 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800824 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700825
Joe Onorato14f122b2009-11-19 14:06:36 -0800826 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
827 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
828 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800829 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800830 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800831 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800832 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700833
Joe Onorato3a8820b2009-11-10 15:06:42 -0800834 final View v = getWindow().peekDecorView();
835 if (v != null && v.getWindowToken() != null) {
836 InputMethodManager imm = (InputMethodManager)getSystemService(
837 INPUT_METHOD_SERVICE);
838 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
840 }
841 }
842
843 @Override
844 protected void onRestoreInstanceState(Bundle savedInstanceState) {
845 // Do not call super here
846 mSavedInstanceState = savedInstanceState;
847 }
848
849 @Override
850 protected void onSaveInstanceState(Bundle outState) {
851 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
852
853 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
854 if (folders.size() > 0) {
855 final int count = folders.size();
856 long[] ids = new long[count];
857 for (int i = 0; i < count; i++) {
858 final FolderInfo info = folders.get(i).getInfo();
859 ids[i] = info.id;
860 }
861 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
862 } else {
863 super.onSaveInstanceState(outState);
864 }
865
Joe Onoratofb0ca672009-09-14 17:55:46 -0400866 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800867 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870
871 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
872 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
873 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
874
875 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
876 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
877 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
878 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
879 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
880 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
881 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
882 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
883 layout.getOccupiedCells());
884 }
885
886 if (mFolderInfo != null && mWaitingForResult) {
887 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
888 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
889 }
890 }
891
892 @Override
893 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700895
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700897 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800899 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 }
901
902 TextKeyListener.getInstance().release();
903
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905
Joe Onorato9c1289c2009-08-17 11:03:03 -0400906 unbindDesktopItems();
907 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800908
909 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800910
911 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800912 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800913
914 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 }
916
917 @Override
918 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700919 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 super.startActivityForResult(intent, requestCode);
921 }
922
923 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700924 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700926
Joe Onorato7bb17492009-09-24 17:51:01 -0700927 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700928
Karl Rosaen138a0412009-04-23 19:00:21 -0700929 if (initialQuery == null) {
930 // Use any text typed in the launcher as the initial query
931 initialQuery = getTypedText();
932 clearTypedText();
933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 if (appSearchData == null) {
935 appSearchData = new Bundle();
936 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
937 }
Romain Guycbb89e42009-06-08 15:52:54 -0700938
Karl Rosaen138a0412009-04-23 19:00:21 -0700939 final SearchManager searchManager =
940 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -0700941 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700942 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
944
945 @Override
946 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 if (isWorkspaceLocked()) {
948 return false;
949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950
951 super.onCreateOptionsMenu(menu);
952 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
953 .setIcon(android.R.drawable.ic_menu_add)
954 .setAlphabeticShortcut('A');
955 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
956 .setIcon(android.R.drawable.ic_menu_gallery)
957 .setAlphabeticShortcut('W');
958 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
959 .setIcon(android.R.drawable.ic_search_category_default)
960 .setAlphabeticShortcut(SearchManager.MENU_KEY);
961 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
962 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
963 .setAlphabeticShortcut('N');
964
965 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700966 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
967 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968
969 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
970 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
971 .setIntent(settings);
972
973 return true;
974 }
975
976 @Override
977 public boolean onPrepareOptionsMenu(Menu menu) {
978 super.onPrepareOptionsMenu(menu);
979
980 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
981 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
982
983 return true;
984 }
985
986 @Override
987 public boolean onOptionsItemSelected(MenuItem item) {
988 switch (item.getItemId()) {
989 case MENU_ADD:
990 addItems();
991 return true;
992 case MENU_WALLPAPER_SETTINGS:
993 startWallpaper();
994 return true;
995 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700996 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 return true;
998 case MENU_NOTIFICATIONS:
999 showNotifications();
1000 return true;
1001 }
1002
1003 return super.onOptionsItemSelected(item);
1004 }
Romain Guycbb89e42009-06-08 15:52:54 -07001005
Karl Rosaen138a0412009-04-23 19:00:21 -07001006 /**
1007 * Indicates that we want global search for this activity by setting the globalSearch
1008 * argument for {@link #startSearch} to true.
1009 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001011 @Override
1012 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001013 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001014 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001015 }
1016
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017 public boolean isWorkspaceLocked() {
1018 return mWorkspaceLoading || mWaitingForResult;
1019 }
1020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001022 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 showAddDialog(mMenuAddInfo);
1024 }
1025
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001026 void addAppWidget(Intent data) {
1027 // TODO: catch bad widget exception when sent
1028 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001029 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030
Bjorn Bringert7984c942009-12-09 15:38:25 +00001031 if (appWidget.configure != null) {
1032 // Launch over to configure widget, if needed
1033 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1034 intent.setComponent(appWidget.configure);
1035 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1036
1037 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001039 // Otherwise just add it
1040 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 }
1042 }
Romain Guycbb89e42009-06-08 15:52:54 -07001043
Romain Guy73b979d2009-06-09 12:57:21 -07001044 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001045 // Handle case where user selected "Applications"
1046 String applicationName = getResources().getString(R.string.group_applications);
1047 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001048
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001049 if (applicationName != null && applicationName.equals(shortcutName)) {
1050 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1051 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001052
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001053 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1054 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001055 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001056 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001057 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 }
1060
1061 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001062 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001063 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001064 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001065
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001066 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001067 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001068 } else {
1069 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 }
1072
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 UserFolderInfo folderInfo = new UserFolderInfo();
1075 folderInfo.title = getText(R.string.folder_name);
1076
1077 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1078 cellInfo.screen = mWorkspace.getCurrentScreen();
1079 if (!findSingleSlot(cellInfo)) return;
1080
1081 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082 LauncherModel.addItemToDatabase(this, folderInfo,
1083 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086
1087 // Create the view
1088 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1089 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1090 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001091 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 }
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Joe Onorato9c1289c2009-08-17 11:03:03 -04001094 void removeFolder(FolderInfo folder) {
1095 mFolders.remove(folder.id);
1096 }
1097
1098 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 cellInfo.screen = mWorkspace.getCurrentScreen();
1100 if (!findSingleSlot(cellInfo)) return;
1101
1102 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1103
1104 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1106 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1108 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 }
1110 }
1111
1112 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1113 CellLayout.CellInfo cellInfo, boolean notify) {
1114
1115 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1116 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1117
1118 Drawable icon = null;
1119 boolean filtered = false;
1120 Intent.ShortcutIconResource iconResource = null;
1121
1122 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1123 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1124 try {
1125 iconResource = (Intent.ShortcutIconResource) extra;
1126 final PackageManager packageManager = context.getPackageManager();
1127 Resources resources = packageManager.getResourcesForApplication(
1128 iconResource.packageName);
1129 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1130 icon = resources.getDrawable(id);
1131 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001132 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 }
1134 }
1135
1136 if (icon == null) {
1137 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1138 }
1139
1140 final LiveFolderInfo info = new LiveFolderInfo();
1141 info.icon = icon;
1142 info.filtered = filtered;
1143 info.title = name;
1144 info.iconResource = iconResource;
1145 info.uri = data.getData();
1146 info.baseIntent = baseIntent;
1147 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1148 LiveFolders.DISPLAY_MODE_GRID);
1149
1150 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1151 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001152 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153
1154 return info;
1155 }
1156
1157 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1158 final int[] xy = new int[2];
1159 if (findSlot(cellInfo, xy, 1, 1)) {
1160 cellInfo.cellX = xy[0];
1161 cellInfo.cellY = xy[1];
1162 return true;
1163 }
1164 return false;
1165 }
1166
1167 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1168 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1169 boolean[] occupied = mSavedState != null ?
1170 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1171 cellInfo = mWorkspace.findAllVacantCells(occupied);
1172 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1173 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1174 return false;
1175 }
1176 }
1177 return true;
1178 }
1179
1180 private void showNotifications() {
1181 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1182 if (statusBar != null) {
1183 statusBar.expand();
1184 }
1185 }
1186
1187 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001188 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001190 Intent chooser = Intent.createChooser(pickWallpaper,
1191 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001192 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1193 // Removed in Eclair MR1
1194// WallpaperManager wm = (WallpaperManager)
1195// getSystemService(Context.WALLPAPER_SERVICE);
1196// WallpaperInfo wi = wm.getWallpaperInfo();
1197// if (wi != null && wi.getSettingsActivity() != null) {
1198// LabeledIntent li = new LabeledIntent(getPackageName(),
1199// R.string.configure_wallpaper, 0);
1200// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1201// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1202// }
Mike Clerona0618e42009-10-22 13:55:21 -07001203 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 }
1205
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001206 /**
1207 * Registers various content observers. The current implementation registers
1208 * only a favorites observer to keep track of the favorites applications.
1209 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001210 private void registerContentObservers() {
1211 ContentResolver resolver = getContentResolver();
1212 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1213 true, mWidgetObserver);
1214 }
1215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 @Override
1217 public boolean dispatchKeyEvent(KeyEvent event) {
1218 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1219 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001221 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001222 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001223 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001224 dumpState();
1225 return true;
1226 }
1227 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001228 }
1229 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1230 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001231 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 return true;
1233 }
1234 }
1235
1236 return super.dispatchKeyEvent(event);
1237 }
1238
Joe Onorato88ec0992009-11-19 13:16:06 -08001239 @Override
1240 public void onBackPressed() {
1241 if (isAllAppsVisible()) {
1242 closeAllApps(true);
1243 } else {
1244 closeFolder();
1245 }
1246 dismissPreview(mPreviousView);
1247 dismissPreview(mNextView);
1248 }
1249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 private void closeFolder() {
1251 Folder folder = mWorkspace.getOpenFolder();
1252 if (folder != null) {
1253 closeFolder(folder);
1254 }
1255 }
1256
1257 void closeFolder(Folder folder) {
1258 folder.getInfo().opened = false;
1259 ViewGroup parent = (ViewGroup) folder.getParent();
1260 if (parent != null) {
1261 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001262 if (folder instanceof DropTarget) {
1263 // Live folders aren't DropTargets.
1264 mDragController.removeDropTarget((DropTarget)folder);
1265 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 }
1267 folder.onClose();
1268 }
1269
1270 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001271 * Re-listen when widgets are reset.
1272 */
1273 private void onAppWidgetReset() {
1274 mAppWidgetHost.startListening();
1275 }
1276
1277 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001278 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1279 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001281 private void unbindDesktopItems() {
1282 for (ItemInfo item: mDesktopItems) {
1283 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 }
1285 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 /**
1288 * Launches the intent referred by the clicked shortcut.
1289 *
1290 * @param v The view representing the clicked shortcut.
1291 */
1292 public void onClick(View v) {
1293 Object tag = v.getTag();
1294 if (tag instanceof ApplicationInfo) {
1295 // Open shortcut
1296 final Intent intent = ((ApplicationInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001297 int[] pos = new int[2];
1298 v.getLocationOnScreen(pos);
1299 intent.setSourceBounds(
1300 new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 startActivitySafely(intent);
1302 } else if (tag instanceof FolderInfo) {
1303 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001304 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001305 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001306 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001307 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001308 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
1311 }
1312
1313 void startActivitySafely(Intent intent) {
1314 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1315 try {
1316 startActivity(intent);
1317 } catch (ActivityNotFoundException e) {
1318 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1319 } catch (SecurityException e) {
1320 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001321 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1323 "or use the exported attribute for this activity.", e);
1324 }
1325 }
1326
1327 private void handleFolderClick(FolderInfo folderInfo) {
1328 if (!folderInfo.opened) {
1329 // Close any open folder
1330 closeFolder();
1331 // Open the requested folder
1332 openFolder(folderInfo);
1333 } else {
1334 // Find the open folder...
1335 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1336 int folderScreen;
1337 if (openFolder != null) {
1338 folderScreen = mWorkspace.getScreenForView(openFolder);
1339 // .. and close it
1340 closeFolder(openFolder);
1341 if (folderScreen != mWorkspace.getCurrentScreen()) {
1342 // Close any folder open on the current screen
1343 closeFolder();
1344 // Pull the folder onto this screen
1345 openFolder(folderInfo);
1346 }
1347 }
1348 }
1349 }
1350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 /**
1352 * Opens the user fodler described by the specified tag. The opening of the folder
1353 * is animated relative to the specified View. If the View is null, no animation
1354 * is played.
1355 *
1356 * @param folderInfo The FolderInfo describing the folder to open.
1357 */
1358 private void openFolder(FolderInfo folderInfo) {
1359 Folder openFolder;
1360
1361 if (folderInfo instanceof UserFolderInfo) {
1362 openFolder = UserFolder.fromXml(this);
1363 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001364 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 } else {
1366 return;
1367 }
1368
Joe Onorato00acb122009-08-04 16:04:30 -04001369 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 openFolder.setLauncher(this);
1371
1372 openFolder.bind(folderInfo);
1373 folderInfo.opened = true;
1374
1375 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1376 openFolder.onOpen();
1377 }
1378
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001380 switch (v.getId()) {
1381 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001382 if (!isAllAppsVisible()) {
1383 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1384 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001385 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001386 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001387 return true;
1388 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001389 if (!isAllAppsVisible()) {
1390 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1391 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001392 showPreviews(v);
1393 }
1394 return true;
1395 case R.id.all_apps_button:
1396 if (!isAllAppsVisible()) {
1397 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1398 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1399 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001400 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001401 return true;
1402 }
1403
Joe Onorato9c1289c2009-08-17 11:03:03 -04001404 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 return false;
1406 }
1407
1408 if (!(v instanceof CellLayout)) {
1409 v = (View) v.getParent();
1410 }
1411
1412 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1413
1414 // This happens when long clicking an item with the dpad/trackball
1415 if (cellInfo == null) {
1416 return true;
1417 }
1418
1419 if (mWorkspace.allowLongPress()) {
1420 if (cellInfo.cell == null) {
1421 if (cellInfo.valid) {
1422 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001423 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 showAddDialog(cellInfo);
1425 }
1426 } else {
1427 if (!(cellInfo.cell instanceof Folder)) {
1428 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001429 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1430 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 mWorkspace.startDrag(cellInfo);
1432 }
1433 }
1434 }
1435 return true;
1436 }
1437
Romain Guye6b8e2f2009-11-10 11:56:55 -08001438 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001439 private void dismissPreview(final View v) {
1440 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001441 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001442 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1443 public void onDismiss() {
1444 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1445 int count = group.getChildCount();
1446 for (int i = 0; i < count; i++) {
1447 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1448 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001449 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1450 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1451
1452 v.setTag(R.id.workspace, null);
1453 v.setTag(R.id.icon, null);
1454 window.setOnDismissListener(null);
1455 }
1456 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001457 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001458 }
1459 v.setTag(null);
1460 }
1461
Romain Guyf8e6a802009-12-07 17:48:02 -08001462 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001463 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001464 }
1465
Romain Guya6abce82009-11-10 02:54:41 -08001466 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001467 final Resources resources = getResources();
1468 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001469
Romain Guya6abce82009-11-10 02:54:41 -08001470 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001471
1472 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001473
Romain Guyf8e6a802009-12-07 17:48:02 -08001474 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001475 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001476 int extraW = (int) ((r.left + r.right) * max);
1477 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001478
1479 int aW = cell.getWidth() - extraW;
1480 float w = aW / max;
1481
1482 int width = cell.getWidth();
1483 int height = cell.getHeight();
1484 int x = cell.getLeftPadding();
1485 int y = cell.getTopPadding();
1486 width -= (x + cell.getRightPadding());
1487 height -= (y + cell.getBottomPadding());
1488
1489 float scale = w / width;
1490
1491 int count = end - start;
1492
1493 final float sWidth = width * scale;
1494 float sHeight = height * scale;
1495
Romain Guye6b8e2f2009-11-10 11:56:55 -08001496 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001497
Romain Guye6b8e2f2009-11-10 11:56:55 -08001498 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1499 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001500
1501 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001502 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001503 cell = (CellLayout) workspace.getChildAt(i);
1504
Romain Guyf8e6a802009-12-07 17:48:02 -08001505 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001506 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001507
1508 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001509 c.scale(scale, scale);
1510 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1511 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001512
Romain Guy9d31e9f2009-11-11 18:54:28 -08001513 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001514 image.setImageBitmap(bitmap);
1515 image.setTag(i);
1516 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001517 image.setOnFocusChangeListener(handler);
1518 image.setFocusable(true);
1519 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001520
1521 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001522 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1523
1524 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001525 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001526
1527 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001528 p.setContentView(preview);
1529 p.setWidth((int) (sWidth * count + extraW));
1530 p.setHeight((int) (sHeight + extraH));
1531 p.setAnimationStyle(R.style.AnimationPreview);
1532 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001533 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001534 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001535 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001536
Romain Guye6b8e2f2009-11-10 11:56:55 -08001537 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1538 public void onDismiss() {
1539 dismissPreview(anchor);
1540 }
1541 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001542
1543 anchor.setTag(p);
1544 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001545 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001546 }
1547
Romain Guy9d31e9f2009-11-11 18:54:28 -08001548 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001549 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001550
Romain Guye6b8e2f2009-11-10 11:56:55 -08001551 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001552 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001553 }
1554
1555 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001556 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001557 v.post(this);
1558 }
1559
1560 public void run() {
1561 dismissPreview(mAnchor);
1562 }
1563
1564 public void onFocusChange(View v, boolean hasFocus) {
1565 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001566 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001567 }
Romain Guya6abce82009-11-10 02:54:41 -08001568 }
1569 }
1570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 Workspace getWorkspace() {
1572 return mWorkspace;
1573 }
1574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 @Override
1576 protected Dialog onCreateDialog(int id) {
1577 switch (id) {
1578 case DIALOG_CREATE_SHORTCUT:
1579 return new CreateShortcut().createDialog();
1580 case DIALOG_RENAME_FOLDER:
1581 return new RenameFolder().createDialog();
1582 }
1583
1584 return super.onCreateDialog(id);
1585 }
1586
1587 @Override
1588 protected void onPrepareDialog(int id, Dialog dialog) {
1589 switch (id) {
1590 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 break;
1592 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001593 if (mFolderInfo != null) {
1594 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1595 final CharSequence text = mFolderInfo.title;
1596 input.setText(text);
1597 input.setSelection(0, text.length());
1598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 break;
1600 }
1601 }
1602
1603 void showRenameDialog(FolderInfo info) {
1604 mFolderInfo = info;
1605 mWaitingForResult = true;
1606 showDialog(DIALOG_RENAME_FOLDER);
1607 }
1608
1609 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1610 mAddItemCellInfo = cellInfo;
1611 mWaitingForResult = true;
1612 showDialog(DIALOG_CREATE_SHORTCUT);
1613 }
1614
Romain Guy73b979d2009-06-09 12:57:21 -07001615 private void pickShortcut(int requestCode, int title) {
1616 Bundle bundle = new Bundle();
1617
1618 ArrayList<String> shortcutNames = new ArrayList<String>();
1619 shortcutNames.add(getString(R.string.group_applications));
1620 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1621
1622 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1623 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1624 R.drawable.ic_launcher_application));
1625 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1626
1627 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1628 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1629 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1630 pickIntent.putExtras(bundle);
1631
1632 startActivityForResult(pickIntent, requestCode);
1633 }
1634
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 private class RenameFolder {
1636 private EditText mInput;
1637
1638 Dialog createDialog() {
1639 mWaitingForResult = true;
1640 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1641 mInput = (EditText) layout.findViewById(R.id.folder_name);
1642
1643 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1644 builder.setIcon(0);
1645 builder.setTitle(getString(R.string.rename_folder_title));
1646 builder.setCancelable(true);
1647 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1648 public void onCancel(DialogInterface dialog) {
1649 cleanup();
1650 }
1651 });
1652 builder.setNegativeButton(getString(R.string.cancel_action),
1653 new Dialog.OnClickListener() {
1654 public void onClick(DialogInterface dialog, int which) {
1655 cleanup();
1656 }
1657 }
1658 );
1659 builder.setPositiveButton(getString(R.string.rename_action),
1660 new Dialog.OnClickListener() {
1661 public void onClick(DialogInterface dialog, int which) {
1662 changeFolderName();
1663 }
1664 }
1665 );
1666 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001667
1668 final AlertDialog dialog = builder.create();
1669 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1670 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001671 mInput.requestFocus();
1672 InputMethodManager inputManager = (InputMethodManager)
1673 getSystemService(Context.INPUT_METHOD_SERVICE);
1674 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001675 }
1676 });
1677
1678 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
1680
1681 private void changeFolderName() {
1682 final String name = mInput.getText().toString();
1683 if (!TextUtils.isEmpty(name)) {
1684 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001685 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 mFolderInfo.title = name;
1687 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1688
Joe Onorato9c1289c2009-08-17 11:03:03 -04001689 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001690 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001691 mModel.setWorkspaceDirty();
1692 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 } else {
1694 final FolderIcon folderIcon = (FolderIcon)
1695 mWorkspace.getViewForTag(mFolderInfo);
1696 if (folderIcon != null) {
1697 folderIcon.setText(name);
1698 getWorkspace().requestLayout();
1699 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001700 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001701 mModel.setWorkspaceDirty();
1702 mWorkspaceLoading = true;
1703 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 }
1705 }
1706 }
1707 cleanup();
1708 }
1709
1710 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 dismissDialog(DIALOG_RENAME_FOLDER);
1712 mWaitingForResult = false;
1713 mFolderInfo = null;
1714 }
1715 }
1716
Joe Onoratofb0ca672009-09-14 17:55:46 -04001717 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001718 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001719 }
1720
Joe Onorato3a8820b2009-11-10 15:06:42 -08001721 void showAllApps(boolean animated) {
1722 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001723
Mike Cleronb6082fa02009-10-19 17:03:36 -07001724 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001725 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001726
Joe Onorato7c312c12009-08-13 21:36:53 -07001727 // TODO: fade these two too
1728 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001729 }
1730
Joe Onoratob2061212009-11-24 16:13:54 -05001731 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001732 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001733 * - Home from workspace
1734 * - from center screen
1735 * - from other screens
1736 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001737 * - from center screen
1738 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001739 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001740 * - from center screen
1741 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001742 * - Launch app from workspace and quit
1743 * - with back
1744 * - with home
1745 * - Launch app from all apps and quit
1746 * - with back
1747 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001748 * - Go to a screen that's not the default, then all
1749 * apps, and launch and app, and go back
1750 * - with back
1751 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001752 * - On workspace, long press power and go back
1753 * - with back
1754 * - with home
1755 * - On all apps, long press power and go back
1756 * - with back
1757 * - with home
1758 * - On workspace, power off
1759 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001760 * - Launch an app and turn off the screen while in that app
1761 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001762 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001763 * - From all apps
1764 * - From workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001765 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001766 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001767 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001768 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001769 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001770 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001771 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001772 }
1773
Joe Onorato7c312c12009-08-13 21:36:53 -07001774 void lockAllApps() {
1775 // TODO
1776 }
1777
1778 void unlockAllApps() {
1779 // TODO
1780 }
1781
Joe Onorato7404ee42009-07-31 11:54:44 -07001782 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 * Displays the shortcut creation dialog and launches, if necessary, the
1784 * appropriate activity.
1785 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001786 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001787 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1788 DialogInterface.OnShowListener {
1789
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 Dialog createDialog() {
1793 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001794
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001796
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1798 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001799 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001800
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 builder.setInverseBackgroundForced(true);
1802
1803 AlertDialog dialog = builder.create();
1804 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001805 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001806 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 return dialog;
1809 }
1810
1811 public void onCancel(DialogInterface dialog) {
1812 mWaitingForResult = false;
1813 cleanup();
1814 }
1815
Romain Guycbb89e42009-06-08 15:52:54 -07001816 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001817 }
1818
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001820 try {
1821 dismissDialog(DIALOG_CREATE_SHORTCUT);
1822 } catch (Exception e) {
1823 // An exception is thrown if the dialog is not visible, which is fine
1824 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 }
1826
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001827 /**
1828 * Handle the action clicked in the "Add to home" dialog.
1829 */
1830 public void onClick(DialogInterface dialog, int which) {
1831 Resources res = getResources();
1832 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001833
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001834 switch (which) {
1835 case AddAdapter.ITEM_SHORTCUT: {
1836 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001837 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001838 break;
1839 }
Romain Guycbb89e42009-06-08 15:52:54 -07001840
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001841 case AddAdapter.ITEM_APPWIDGET: {
1842 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001843
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001844 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1845 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001846 // start the pick activity
1847 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1848 break;
1849 }
Romain Guycbb89e42009-06-08 15:52:54 -07001850
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001851 case AddAdapter.ITEM_LIVE_FOLDER: {
1852 // Insert extra item to handle inserting folder
1853 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001854
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001855 ArrayList<String> shortcutNames = new ArrayList<String>();
1856 shortcutNames.add(res.getString(R.string.group_folder));
1857 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001858
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001859 ArrayList<ShortcutIconResource> shortcutIcons =
1860 new ArrayList<ShortcutIconResource>();
1861 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1862 R.drawable.ic_launcher_folder));
1863 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1864
1865 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1866 pickIntent.putExtra(Intent.EXTRA_INTENT,
1867 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1868 pickIntent.putExtra(Intent.EXTRA_TITLE,
1869 getText(R.string.title_select_live_folder));
1870 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001871
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001872 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1873 break;
1874 }
1875
1876 case AddAdapter.ITEM_WALLPAPER: {
1877 startWallpaper();
1878 break;
1879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 }
1881 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001882
1883 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001884 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
1886
1887 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001888 * Receives notifications when applications are added/removed.
1889 */
1890 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1891 @Override
1892 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001893 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05001894 String reason = intent.getStringExtra("reason");
1895 if (!"homekey".equals(reason)) {
1896 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001897 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05001898 animate = false;
1899 }
Joe Onoratob2061212009-11-24 16:13:54 -05001900 closeAllApps(animate);
1901 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001902 }
1903 }
1904
1905 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001906 * Receives notifications whenever the appwidgets are reset.
1907 */
1908 private class AppWidgetResetObserver extends ContentObserver {
1909 public AppWidgetResetObserver() {
1910 super(new Handler());
1911 }
1912
1913 @Override
1914 public void onChange(boolean selfChange) {
1915 onAppWidgetReset();
1916 }
1917 }
1918
1919 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001920 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001922 public int getCurrentWorkspaceScreen() {
1923 return mWorkspace.getCurrentScreen();
1924 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001925
Joe Onorato9c1289c2009-08-17 11:03:03 -04001926 /**
1927 * Refreshes the shortcuts shown on the workspace.
1928 *
1929 * Implementation of the method from LauncherModel.Callbacks.
1930 */
1931 public void startBinding() {
1932 final Workspace workspace = mWorkspace;
1933 int count = workspace.getChildCount();
1934 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001935 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001936 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1937 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001938
Joe Onorato9c1289c2009-08-17 11:03:03 -04001939 if (DEBUG_USER_INTERFACE) {
1940 android.widget.Button finishButton = new android.widget.Button(this);
1941 finishButton.setText("Finish");
1942 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1943
1944 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1945 public void onClick(View v) {
1946 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001947 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001948 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 }
1950 }
1951
1952 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953 * Bind the items start-end from the list.
1954 *
1955 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001957 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1958
1959 final Workspace workspace = mWorkspace;
1960
1961 for (int i=start; i<end; i++) {
1962 final ItemInfo item = shortcuts.get(i);
1963 mDesktopItems.add(item);
1964 switch (item.itemType) {
1965 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1966 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1967 final View shortcut = createShortcut((ApplicationInfo) item);
1968 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1969 false);
1970 break;
1971 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1972 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1973 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1974 (UserFolderInfo) item);
1975 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1976 false);
1977 break;
1978 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1979 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1980 R.layout.live_folder_icon, this,
1981 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1982 (LiveFolderInfo) item);
1983 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1984 false);
1985 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 }
1988
Joe Onorato9c1289c2009-08-17 11:03:03 -04001989 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
1991
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 /**
1993 * Implementation of the method from LauncherModel.Callbacks.
1994 */
Joe Onoratoad72e172009-11-06 16:25:04 -05001995 public void bindFolders(HashMap<Long, FolderInfo> folders) {
1996 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001999
2000 /**
2001 * Add the views for a widget to the workspace.
2002 *
2003 * Implementation of the method from LauncherModel.Callbacks.
2004 */
2005 public void bindAppWidget(LauncherAppWidgetInfo item) {
2006 final Workspace workspace = mWorkspace;
2007
2008 final int appWidgetId = item.appWidgetId;
2009 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2010 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2011
Joe Onorato9c1289c2009-08-17 11:03:03 -04002012 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2013 item.hostView.setTag(item);
2014
2015 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2016 item.cellY, item.spanX, item.spanY, false);
2017
2018 workspace.requestLayout();
2019
2020 mDesktopItems.add(item);
2021 }
2022
2023 /**
2024 * Callback saying that there aren't any more items to bind.
2025 *
2026 * Implementation of the method from LauncherModel.Callbacks.
2027 */
2028 public void finishBindingItems() {
2029 if (mSavedState != null) {
2030 if (!mWorkspace.hasFocus()) {
2031 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2032 }
2033
2034 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2035 if (userFolders != null) {
2036 for (long folderId : userFolders) {
2037 final FolderInfo info = mFolders.get(folderId);
2038 if (info != null) {
2039 openFolder(info);
2040 }
2041 }
2042 final Folder openFolder = mWorkspace.getOpenFolder();
2043 if (openFolder != null) {
2044 openFolder.requestFocus();
2045 }
2046 }
2047
Joe Onorato9c1289c2009-08-17 11:03:03 -04002048 mSavedState = null;
2049 }
2050
2051 if (mSavedInstanceState != null) {
2052 super.onRestoreInstanceState(mSavedInstanceState);
2053 mSavedInstanceState = null;
2054 }
2055
Joe Onorato9c1289c2009-08-17 11:03:03 -04002056 mWorkspaceLoading = false;
2057 }
2058
2059 /**
2060 * Add the icons for all apps.
2061 *
2062 * Implementation of the method from LauncherModel.Callbacks.
2063 */
2064 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002065 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002066 }
2067
2068 /**
2069 * A package was installed.
2070 *
2071 * Implementation of the method from LauncherModel.Callbacks.
2072 */
2073 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2074 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002075 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002076 }
2077
2078 /**
2079 * A package was updated.
2080 *
2081 * Implementation of the method from LauncherModel.Callbacks.
2082 */
2083 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2084 removeDialog(DIALOG_CREATE_SHORTCUT);
2085 mWorkspace.updateShortcutsForPackage(packageName);
Joe Onoratof0be2132009-11-24 19:34:29 -05002086 mAllAppsGrid.updateApps(packageName, apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002087 }
2088
2089 /**
2090 * A package was uninstalled.
2091 *
2092 * Implementation of the method from LauncherModel.Callbacks.
2093 */
2094 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2095 removeDialog(DIALOG_CREATE_SHORTCUT);
2096 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002097 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002098 }
Joe Onoratobe386092009-11-17 17:32:16 -08002099
2100 /**
2101 * Prints out out state for debugging.
2102 */
2103 public void dumpState() {
2104 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002105 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002106 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2107 Log.d(TAG, "mRestoring=" + mRestoring);
2108 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2109 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2110 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2111 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002112 mModel.dumpState();
2113 mAllAppsGrid.dumpState();
2114 Log.d(TAG, "END launcher2 dump state");
2115 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116}