blob: 99b1b6c841ab9d7a6fa4550e01a7ae510eee00dc [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;
Joe Onorato8a9625e2010-01-28 15:55:35 -080064import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.widget.TextView;
68import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080069import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080070import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080071import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070072import android.appwidget.AppWidgetManager;
73import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040076import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070077import java.io.DataOutputStream;
78import java.io.FileNotFoundException;
79import java.io.IOException;
80import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081
82/**
83 * Default launcher application.
84 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040085public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080086 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080087 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 static final boolean LOGD = false;
89
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 static final boolean PROFILE_STARTUP = false;
91 static final boolean PROFILE_ROTATE = false;
92 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 private static final int WALLPAPER_SCREENS_SPAN = 2;
95
96 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -080097 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
98
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 private static final int MENU_ADD = Menu.FIRST + 1;
100 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
101 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
102 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700103 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104
105 private static final int REQUEST_CREATE_SHORTCUT = 1;
106 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700107 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 private static final int REQUEST_PICK_APPLICATION = 6;
109 private static final int REQUEST_PICK_SHORTCUT = 7;
110 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700111 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700112 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
114 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
115
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800116 static final int SCREEN_COUNT = 5;
117 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700119 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Joe Onorato7c312c12009-08-13 21:36:53 -0700121 static final int DIALOG_CREATE_SHORTCUT = 1;
122 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Romain Guy98d01652009-06-30 16:21:04 -0700124 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 // Type: int
127 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
128 // Type: boolean
129 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
130 // Type: long
131 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
146 // Type: int[]
147 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
148 // Type: boolean
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
150 // Type: long
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
152
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800156 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800158 private final BroadcastReceiver mCloseSystemDialogsReceiver
159 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800160 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private LayoutInflater mInflater;
163
Joe Onorato00acb122009-08-04 16:04:30 -0400164 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700166
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private AppWidgetManager mAppWidgetManager;
168 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private CellLayout.CellInfo mAddItemCellInfo;
171 private CellLayout.CellInfo mMenuAddInfo;
172 private final int[] mCellCoordinates = new int[2];
173 private FolderInfo mFolderInfo;
174
Joe Onorato7c312c12009-08-13 21:36:53 -0700175 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700176 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700177 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private Bundle mSavedState;
180
181 private SpannableStringBuilder mDefaultKeySsb = null;
182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 private boolean mWorkspaceLoading = true;
184
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800185 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private boolean mRestoring;
187 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
189 private Bundle mSavedInstanceState;
190
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 private LauncherModel mModel;
192
Romain Guy84f296c2009-11-04 15:00:44 -0800193 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
194 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700195
Romain Guy1fbc1c82009-11-09 20:43:08 -0800196 private ImageView mPreviousView;
197 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 @Override
200 protected void onCreate(Bundle savedInstanceState) {
201 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400204 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700206
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700207 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
209 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 if (PROFILE_STARTUP) {
212 android.os.Debug.startMethodTracing("/sdcard/launcher");
213 }
214
215 checkForLocaleChange();
216 setWallpaperDimension();
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 setContentView(R.layout.launcher);
219 setupViews();
220
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800221 registerContentObservers();
222
Joe Onorato7c312c12009-08-13 21:36:53 -0700223 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 mSavedState = savedInstanceState;
226 restoreState(mSavedState);
227
228 if (PROFILE_STARTUP) {
229 android.os.Debug.stopMethodTracing();
230 }
231
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700232 // We have a new AllAppsView, we need to re-bind everything, and it could have
233 // changed in our absence.
234 mModel.setAllAppsDirty();
235 mModel.setWorkspaceDirty();
236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400238 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 }
240
241 // For handling default keys
242 mDefaultKeySsb = new SpannableStringBuilder();
243 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800244
245 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
246 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 }
Romain Guycbb89e42009-06-08 15:52:54 -0700248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700250 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
251 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 final Configuration configuration = getResources().getConfiguration();
254
Romain Guy98d01652009-06-30 16:21:04 -0700255 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 final String locale = configuration.locale.toString();
257
Romain Guy98d01652009-06-30 16:21:04 -0700258 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 final int mcc = configuration.mcc;
260
Romain Guy98d01652009-06-30 16:21:04 -0700261 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 final int mnc = configuration.mnc;
263
Romain Guy84f296c2009-11-04 15:00:44 -0800264 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265
Romain Guy84f296c2009-11-04 15:00:44 -0800266 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700267 localeConfiguration.locale = locale;
268 localeConfiguration.mcc = mcc;
269 localeConfiguration.mnc = mnc;
270
271 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700273 }
274 }
275
276 private static class LocaleConfiguration {
277 public String locale;
278 public int mcc = -1;
279 public int mnc = -1;
280 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700281
Romain Guy98d01652009-06-30 16:21:04 -0700282 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
283 DataInputStream in = null;
284 try {
285 in = new DataInputStream(context.openFileInput(PREFERENCES));
286 configuration.locale = in.readUTF();
287 configuration.mcc = in.readInt();
288 configuration.mnc = in.readInt();
289 } catch (FileNotFoundException e) {
290 // Ignore
291 } catch (IOException e) {
292 // Ignore
293 } finally {
294 if (in != null) {
295 try {
296 in.close();
297 } catch (IOException e) {
298 // Ignore
299 }
300 }
301 }
302 }
303
304 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
305 DataOutputStream out = null;
306 try {
307 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
308 out.writeUTF(configuration.locale);
309 out.writeInt(configuration.mcc);
310 out.writeInt(configuration.mnc);
311 out.flush();
312 } catch (FileNotFoundException e) {
313 // Ignore
314 } catch (IOException e) {
315 //noinspection ResultOfMethodCallIgnored
316 context.getFileStreamPath(PREFERENCES).delete();
317 } finally {
318 if (out != null) {
319 try {
320 out.close();
321 } catch (IOException e) {
322 // Ignore
323 }
324 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 }
326 }
327
328 static int getScreen() {
329 synchronized (sLock) {
330 return sScreen;
331 }
332 }
333
334 static void setScreen(int screen) {
335 synchronized (sLock) {
336 sScreen = screen;
337 }
338 }
339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700341 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342
343 Display display = getWindowManager().getDefaultDisplay();
344 boolean isPortrait = display.getWidth() < display.getHeight();
345
346 final int width = isPortrait ? display.getWidth() : display.getHeight();
347 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700348 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 }
350
351 @Override
352 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700353 mWaitingForResult = false;
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 // The pattern used here is that a user PICKs a specific application,
356 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
359 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700360
Romain Guy94dabf12009-07-21 10:55:43 -0700361 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 switch (requestCode) {
363 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400364 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 break;
366 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700367 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 break;
369 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400370 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 break;
372 case REQUEST_PICK_LIVE_FOLDER:
373 addLiveFolder(data);
374 break;
375 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400376 completeAddLiveFolder(data, mAddItemCellInfo);
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_PICK_APPWIDGET:
379 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700381 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400382 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700384 case REQUEST_PICK_WALLPAPER:
385 // We just wanted the activity result here so we can clear mWaitingForResult
386 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 }
Romain Guy18042c82009-11-06 11:44:55 -0800388 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
389 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
390 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700391 // Clean up the appWidgetId if we canceled
392 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
393 if (appWidgetId != -1) {
394 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 }
396 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 }
398
399 @Override
400 protected void onResume() {
401 super.onResume();
402
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800403 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800404
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400406 mWorkspaceLoading = true;
407 mModel.startLoader(this, true);
408 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
410 }
411
412 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700413 protected void onPause() {
414 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800415 dismissPreview(mPreviousView);
416 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800417 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700418 }
Romain Guycbb89e42009-06-08 15:52:54 -0700419
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700420 @Override
421 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400422 // Flag the loader to stop early before switching
423 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700425 if (PROFILE_ROTATE) {
426 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
427 }
428 return null;
429 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800431 // We can't hide the IME if it was forced open. So don't bother
432 /*
433 @Override
434 public void onWindowFocusChanged(boolean hasFocus) {
435 super.onWindowFocusChanged(hasFocus);
436
437 if (hasFocus) {
438 final InputMethodManager inputManager = (InputMethodManager)
439 getSystemService(Context.INPUT_METHOD_SERVICE);
440 WindowManager.LayoutParams lp = getWindow().getAttributes();
441 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
442 android.os.Handler()) {
443 protected void onReceiveResult(int resultCode, Bundle resultData) {
444 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
445 }
446 });
447 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
448 }
449 }
450 */
451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 private boolean acceptFilter() {
453 final InputMethodManager inputManager = (InputMethodManager)
454 getSystemService(Context.INPUT_METHOD_SERVICE);
455 return !inputManager.isFullscreenMode();
456 }
457
458 @Override
459 public boolean onKeyDown(int keyCode, KeyEvent event) {
460 boolean handled = super.onKeyDown(keyCode, event);
461 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
462 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
463 keyCode, event);
464 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700465 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700466 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700467 // showSearchDialog()
468 // If there are multiple keystrokes before the search dialog takes focus,
469 // onSearchRequested() will be called for every keystroke,
470 // but it is idempotent, so it's fine.
471 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
473 }
474
Joe Onorato8a9625e2010-01-28 15:55:35 -0800475 // Eat the long press event so the keyboard doesn't come up.
476 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
477 return true;
478 }
479
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 return handled;
481 }
482
Karl Rosaen138a0412009-04-23 19:00:21 -0700483 private String getTypedText() {
484 return mDefaultKeySsb.toString();
485 }
486
487 private void clearTypedText() {
488 mDefaultKeySsb.clear();
489 mDefaultKeySsb.clearSpans();
490 Selection.setSelection(mDefaultKeySsb, 0);
491 }
492
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 /**
494 * Restores the previous state, if it exists.
495 *
496 * @param savedState The previous state.
497 */
498 private void restoreState(Bundle savedState) {
499 if (savedState == null) {
500 return;
501 }
502
Joe Onorato3a8820b2009-11-10 15:06:42 -0800503 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
504 if (allApps) {
505 showAllApps(false);
506 }
507
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800508 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
509 if (currentScreen > -1) {
510 mWorkspace.setCurrentScreen(currentScreen);
511 }
512
513 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
514 if (addScreen > -1) {
515 mAddItemCellInfo = new CellLayout.CellInfo();
516 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
517 addItemCellInfo.valid = true;
518 addItemCellInfo.screen = addScreen;
519 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
520 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
521 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
522 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
523 addItemCellInfo.findVacantCellsFromOccupied(
524 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
525 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
526 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
527 mRestoring = true;
528 }
529
530 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
531 if (renameFolder) {
532 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400533 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 mRestoring = true;
535 }
536 }
537
538 /**
539 * Finds all the views we need and configure them properly.
540 */
541 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400542 DragController dragController = mDragController;
543
Romain Guyb1b69f52009-08-10 15:10:15 -0700544 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400545 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546
Joe Onorato7c312c12009-08-13 21:36:53 -0700547 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700548 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700549 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700550 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700551 // Manage focusability manually since this thing is always visible
552 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700553
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
555 final Workspace workspace = mWorkspace;
556
Joe Onorato7c312c12009-08-13 21:36:53 -0700557 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
558 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559
Joe Onorato7c312c12009-08-13 21:36:53 -0700560 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700561 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700562 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800563 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800564
565 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
566 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
567
568 Drawable previous = mPreviousView.getDrawable();
569 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800570 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571
Joe Onorato0d44e942009-11-16 18:20:51 -0800572 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800573 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800574 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800575 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800576
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400578 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580
581 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400582 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700583 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584
Joe Onorato00acb122009-08-04 16:04:30 -0400585 dragController.setDragScoller(workspace);
586 dragController.setDragListener(deleteZone);
587 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800588 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700589
Joe Onorato00acb122009-08-04 16:04:30 -0400590 // The order here is bottom to top.
591 dragController.addDropTarget(workspace);
592 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 }
594
Romain Guy8a73c512009-11-09 19:19:59 -0800595 @SuppressWarnings({"UnusedDeclaration"})
596 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800597 if (!isAllAppsVisible()) {
598 mWorkspace.scrollLeft();
599 }
Romain Guy8a73c512009-11-09 19:19:59 -0800600 }
601
602 @SuppressWarnings({"UnusedDeclaration"})
603 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800604 if (!isAllAppsVisible()) {
605 mWorkspace.scrollRight();
606 }
Romain Guy8a73c512009-11-09 19:19:59 -0800607 }
608
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 /**
610 * Creates a view representing a shortcut.
611 *
612 * @param info The data structure describing the shortcut.
613 *
614 * @return A View inflated from R.layout.application.
615 */
616 View createShortcut(ApplicationInfo info) {
617 return createShortcut(R.layout.application,
618 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
619 }
620
621 /**
622 * Creates a view representing a shortcut inflated from the specified resource.
623 *
624 * @param layoutResId The id of the XML layout used to create the shortcut.
625 * @param parent The group the shortcut belongs to.
626 * @param info The data structure describing the shortcut.
627 *
628 * @return A View inflated from layoutResId.
629 */
630 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
631 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
632
Joe Onorato5162ea92009-09-03 09:39:42 -0700633 if (info.icon == null) {
634 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
635 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700637 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 info.filtered = true;
639 }
640
641 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
642 favorite.setText(info.title);
643 favorite.setTag(info);
644 favorite.setOnClickListener(this);
645
646 return favorite;
647 }
648
649 /**
650 * Add an application shortcut to the workspace.
651 *
652 * @param data The intent describing the application.
653 * @param cellInfo The position on screen where to create the shortcut.
654 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 cellInfo.screen = mWorkspace.getCurrentScreen();
657 if (!findSingleSlot(cellInfo)) return;
658
Romain Guy73b979d2009-06-09 12:57:21 -0700659 final ApplicationInfo info = infoFromApplicationIntent(context, data);
660 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700662 }
663 }
664
665 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 ComponentName component = data.getComponent();
667 PackageManager packageManager = context.getPackageManager();
668 ActivityInfo activityInfo = null;
669 try {
670 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
671 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800672 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
Romain Guycbb89e42009-06-08 15:52:54 -0700674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 if (activityInfo != null) {
676 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700677
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 itemInfo.title = activityInfo.loadLabel(packageManager);
679 if (itemInfo.title == null) {
680 itemInfo.title = activityInfo.name;
681 }
Romain Guycbb89e42009-06-08 15:52:54 -0700682
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
684 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
685 itemInfo.icon = activityInfo.loadIcon(packageManager);
686 itemInfo.container = ItemInfo.NO_ID;
687
Romain Guy73b979d2009-06-09 12:57:21 -0700688 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 }
Romain Guy73b979d2009-06-09 12:57:21 -0700690
691 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 }
Romain Guycbb89e42009-06-08 15:52:54 -0700693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 /**
695 * Add a shortcut to the workspace.
696 *
697 * @param data The intent describing the shortcut.
698 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 cellInfo.screen = mWorkspace.getCurrentScreen();
702 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
705
706 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
709 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 }
711 }
712
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700715 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 * @param data The intent describing the appWidgetId.
718 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700722 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700723
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800724 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700725
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700726 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700727
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700730 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700731
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 // Try finding open space on Launcher screen
733 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800734 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
735 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
736 return;
737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700739 // Build Launcher-specific widget info and save to database
740 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 launcherInfo.spanX = spans[0];
742 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700743
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 LauncherModel.addItemToDatabase(this, launcherInfo,
745 LauncherSettings.Favorites.CONTAINER_DESKTOP,
746 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
747
748 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700750
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700752 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700753
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700754 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700756
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
760 }
Romain Guycbb89e42009-06-08 15:52:54 -0700761
Joe Onorato9c1289c2009-08-17 11:03:03 -0400762 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
763 mDesktopItems.remove(launcherInfo);
764 launcherInfo.hostView = null;
765 }
766
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700767 public LauncherAppWidgetHost getAppWidgetHost() {
768 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 }
Romain Guycbb89e42009-06-08 15:52:54 -0700770
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 static ApplicationInfo addShortcut(Context context, Intent data,
772 CellLayout.CellInfo cellInfo, boolean notify) {
773
Romain Guy73b979d2009-06-09 12:57:21 -0700774 final ApplicationInfo info = infoFromShortcutIntent(context, data);
775 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
776 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
777
778 return info;
779 }
780
781 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
783 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guyb5de7752010-01-04 14:43:49 -0800784 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785
786 Drawable icon = null;
787 boolean filtered = false;
788 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700789 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790
Romain Guyb5de7752010-01-04 14:43:49 -0800791 if (bitmap != null && bitmap instanceof Bitmap) {
792 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail((Bitmap) bitmap, context));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 filtered = true;
794 customIcon = true;
795 } else {
796 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700797 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700799 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 final PackageManager packageManager = context.getPackageManager();
801 Resources resources = packageManager.getResourcesForApplication(
802 iconResource.packageName);
803 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
804 icon = resources.getDrawable(id);
805 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800806 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 }
808 }
809 }
810
811 if (icon == null) {
812 icon = context.getPackageManager().getDefaultActivityIcon();
813 }
814
815 final ApplicationInfo info = new ApplicationInfo();
816 info.icon = icon;
817 info.filtered = filtered;
818 info.title = name;
819 info.intent = intent;
820 info.customIcon = customIcon;
821 info.iconResource = iconResource;
822
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 return info;
824 }
825
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800826 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800827 getWindow().closeAllPanels();
828
829 try {
830 dismissDialog(DIALOG_CREATE_SHORTCUT);
831 // Unlock the workspace if the dialog was showing
832 } catch (Exception e) {
833 // An exception is thrown if the dialog is not visible, which is fine
834 }
835
836 try {
837 dismissDialog(DIALOG_RENAME_FOLDER);
838 // Unlock the workspace if the dialog was showing
839 } catch (Exception e) {
840 // An exception is thrown if the dialog is not visible, which is fine
841 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800842
843 // Whatever we were doing is hereby canceled.
844 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800845 }
846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 @Override
848 protected void onNewIntent(Intent intent) {
849 super.onNewIntent(intent);
850
851 // Close the menu
852 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800853 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800854 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700855
Joe Onorato14f122b2009-11-19 14:06:36 -0800856 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
857 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
858 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800859 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800860 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800861 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800862 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700863
Joe Onorato3a8820b2009-11-10 15:06:42 -0800864 final View v = getWindow().peekDecorView();
865 if (v != null && v.getWindowToken() != null) {
866 InputMethodManager imm = (InputMethodManager)getSystemService(
867 INPUT_METHOD_SERVICE);
868 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 }
870 }
871 }
872
873 @Override
874 protected void onRestoreInstanceState(Bundle savedInstanceState) {
875 // Do not call super here
876 mSavedInstanceState = savedInstanceState;
877 }
878
879 @Override
880 protected void onSaveInstanceState(Bundle outState) {
881 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
882
883 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
884 if (folders.size() > 0) {
885 final int count = folders.size();
886 long[] ids = new long[count];
887 for (int i = 0; i < count; i++) {
888 final FolderInfo info = folders.get(i).getInfo();
889 ids[i] = info.id;
890 }
891 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
892 } else {
893 super.onSaveInstanceState(outState);
894 }
895
Joe Onoratofb0ca672009-09-14 17:55:46 -0400896 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800897 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900
901 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
902 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
903 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
904
905 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
906 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
907 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
908 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
909 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
910 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
911 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
912 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
913 layout.getOccupiedCells());
914 }
915
916 if (mFolderInfo != null && mWaitingForResult) {
917 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
918 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
919 }
920 }
921
922 @Override
923 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700925
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700927 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800929 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 }
931
932 TextKeyListener.getInstance().release();
933
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935
Joe Onorato9c1289c2009-08-17 11:03:03 -0400936 unbindDesktopItems();
937 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800938
939 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800940
941 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800942 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800943
944 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 }
946
947 @Override
948 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700949 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 super.startActivityForResult(intent, requestCode);
951 }
952
953 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700954 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700956
Joe Onorato7bb17492009-09-24 17:51:01 -0700957 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700958
Karl Rosaen138a0412009-04-23 19:00:21 -0700959 if (initialQuery == null) {
960 // Use any text typed in the launcher as the initial query
961 initialQuery = getTypedText();
962 clearTypedText();
963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 if (appSearchData == null) {
965 appSearchData = new Bundle();
966 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
967 }
Romain Guycbb89e42009-06-08 15:52:54 -0700968
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 final SearchManager searchManager =
970 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700972 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 }
974
975 @Override
976 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400977 if (isWorkspaceLocked()) {
978 return false;
979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980
981 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
984 .setIcon(android.R.drawable.ic_menu_add)
985 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800986 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 .setIcon(android.R.drawable.ic_menu_gallery)
988 .setAlphabeticShortcut('W');
989 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
990 .setIcon(android.R.drawable.ic_search_category_default)
991 .setAlphabeticShortcut(SearchManager.MENU_KEY);
992 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
993 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
994 .setAlphabeticShortcut('N');
995
996 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700997 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
998 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999
1000 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1001 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1002 .setIntent(settings);
1003
1004 return true;
1005 }
1006
1007 @Override
1008 public boolean onPrepareOptionsMenu(Menu menu) {
1009 super.onPrepareOptionsMenu(menu);
1010
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001011 // If all apps is animating, don't show the menu, because we don't know
1012 // which one to show.
1013 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1014 return false;
1015 }
1016
1017 // Only show the add and wallpaper options when we're not in all apps.
1018 boolean visible = !mAllAppsGrid.isOpaque();
1019 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1020 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1021
1022 // Disable add if the workspace is full.
1023 if (visible) {
1024 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1025 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027
1028 return true;
1029 }
1030
1031 @Override
1032 public boolean onOptionsItemSelected(MenuItem item) {
1033 switch (item.getItemId()) {
1034 case MENU_ADD:
1035 addItems();
1036 return true;
1037 case MENU_WALLPAPER_SETTINGS:
1038 startWallpaper();
1039 return true;
1040 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001041 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 return true;
1043 case MENU_NOTIFICATIONS:
1044 showNotifications();
1045 return true;
1046 }
1047
1048 return super.onOptionsItemSelected(item);
1049 }
Romain Guycbb89e42009-06-08 15:52:54 -07001050
Karl Rosaen138a0412009-04-23 19:00:21 -07001051 /**
1052 * Indicates that we want global search for this activity by setting the globalSearch
1053 * argument for {@link #startSearch} to true.
1054 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001056 @Override
1057 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001058 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001059 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001060 }
1061
Joe Onorato9c1289c2009-08-17 11:03:03 -04001062 public boolean isWorkspaceLocked() {
1063 return mWorkspaceLoading || mWaitingForResult;
1064 }
1065
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001067 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 showAddDialog(mMenuAddInfo);
1069 }
1070
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001071 void addAppWidget(Intent data) {
1072 // TODO: catch bad widget exception when sent
1073 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001074 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075
Bjorn Bringert7984c942009-12-09 15:38:25 +00001076 if (appWidget.configure != null) {
1077 // Launch over to configure widget, if needed
1078 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1079 intent.setComponent(appWidget.configure);
1080 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1081
1082 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001084 // Otherwise just add it
1085 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 }
1087 }
Romain Guycbb89e42009-06-08 15:52:54 -07001088
Romain Guy73b979d2009-06-09 12:57:21 -07001089 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001090 // Handle case where user selected "Applications"
1091 String applicationName = getResources().getString(R.string.group_applications);
1092 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001094 if (applicationName != null && applicationName.equals(shortcutName)) {
1095 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1096 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001097
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001098 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1099 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001100 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001101 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001102 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 }
1105
1106 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001107 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001108 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001109 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001110
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001111 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001112 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001113 } else {
1114 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1115 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 }
1117
Joe Onorato9c1289c2009-08-17 11:03:03 -04001118 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 UserFolderInfo folderInfo = new UserFolderInfo();
1120 folderInfo.title = getText(R.string.folder_name);
1121
1122 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1123 cellInfo.screen = mWorkspace.getCurrentScreen();
1124 if (!findSingleSlot(cellInfo)) return;
1125
1126 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 LauncherModel.addItemToDatabase(this, folderInfo,
1128 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131
1132 // Create the view
1133 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1134 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1135 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 }
Romain Guycbb89e42009-06-08 15:52:54 -07001138
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 void removeFolder(FolderInfo folder) {
1140 mFolders.remove(folder.id);
1141 }
1142
1143 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 cellInfo.screen = mWorkspace.getCurrentScreen();
1145 if (!findSingleSlot(cellInfo)) return;
1146
1147 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1148
1149 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1151 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001152 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1153 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
1155 }
1156
1157 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1158 CellLayout.CellInfo cellInfo, boolean notify) {
1159
1160 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1161 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1162
1163 Drawable icon = null;
1164 boolean filtered = false;
1165 Intent.ShortcutIconResource iconResource = null;
1166
1167 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1168 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1169 try {
1170 iconResource = (Intent.ShortcutIconResource) extra;
1171 final PackageManager packageManager = context.getPackageManager();
1172 Resources resources = packageManager.getResourcesForApplication(
1173 iconResource.packageName);
1174 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1175 icon = resources.getDrawable(id);
1176 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001177 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 }
1179 }
1180
1181 if (icon == null) {
1182 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1183 }
1184
1185 final LiveFolderInfo info = new LiveFolderInfo();
1186 info.icon = icon;
1187 info.filtered = filtered;
1188 info.title = name;
1189 info.iconResource = iconResource;
1190 info.uri = data.getData();
1191 info.baseIntent = baseIntent;
1192 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1193 LiveFolders.DISPLAY_MODE_GRID);
1194
1195 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1196 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001197 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198
1199 return info;
1200 }
1201
1202 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1203 final int[] xy = new int[2];
1204 if (findSlot(cellInfo, xy, 1, 1)) {
1205 cellInfo.cellX = xy[0];
1206 cellInfo.cellY = xy[1];
1207 return true;
1208 }
1209 return false;
1210 }
1211
1212 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1213 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1214 boolean[] occupied = mSavedState != null ?
1215 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1216 cellInfo = mWorkspace.findAllVacantCells(occupied);
1217 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1218 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1219 return false;
1220 }
1221 }
1222 return true;
1223 }
1224
1225 private void showNotifications() {
1226 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1227 if (statusBar != null) {
1228 statusBar.expand();
1229 }
1230 }
1231
1232 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001233 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001235 Intent chooser = Intent.createChooser(pickWallpaper,
1236 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001237 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1238 // Removed in Eclair MR1
1239// WallpaperManager wm = (WallpaperManager)
1240// getSystemService(Context.WALLPAPER_SERVICE);
1241// WallpaperInfo wi = wm.getWallpaperInfo();
1242// if (wi != null && wi.getSettingsActivity() != null) {
1243// LabeledIntent li = new LabeledIntent(getPackageName(),
1244// R.string.configure_wallpaper, 0);
1245// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1246// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1247// }
Mike Clerona0618e42009-10-22 13:55:21 -07001248 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
1250
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001251 /**
1252 * Registers various content observers. The current implementation registers
1253 * only a favorites observer to keep track of the favorites applications.
1254 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001255 private void registerContentObservers() {
1256 ContentResolver resolver = getContentResolver();
1257 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1258 true, mWidgetObserver);
1259 }
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 @Override
1262 public boolean dispatchKeyEvent(KeyEvent event) {
1263 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1264 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001266 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001267 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001268 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001269 dumpState();
1270 return true;
1271 }
1272 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001273 }
1274 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1275 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001276 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 return true;
1278 }
1279 }
1280
1281 return super.dispatchKeyEvent(event);
1282 }
1283
Joe Onorato88ec0992009-11-19 13:16:06 -08001284 @Override
1285 public void onBackPressed() {
1286 if (isAllAppsVisible()) {
1287 closeAllApps(true);
1288 } else {
1289 closeFolder();
1290 }
1291 dismissPreview(mPreviousView);
1292 dismissPreview(mNextView);
1293 }
1294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 private void closeFolder() {
1296 Folder folder = mWorkspace.getOpenFolder();
1297 if (folder != null) {
1298 closeFolder(folder);
1299 }
1300 }
1301
1302 void closeFolder(Folder folder) {
1303 folder.getInfo().opened = false;
1304 ViewGroup parent = (ViewGroup) folder.getParent();
1305 if (parent != null) {
1306 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001307 if (folder instanceof DropTarget) {
1308 // Live folders aren't DropTargets.
1309 mDragController.removeDropTarget((DropTarget)folder);
1310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
1312 folder.onClose();
1313 }
1314
1315 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001316 * Re-listen when widgets are reset.
1317 */
1318 private void onAppWidgetReset() {
1319 mAppWidgetHost.startListening();
1320 }
1321
1322 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001323 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1324 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001326 private void unbindDesktopItems() {
1327 for (ItemInfo item: mDesktopItems) {
1328 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 }
1330 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 /**
1333 * Launches the intent referred by the clicked shortcut.
1334 *
1335 * @param v The view representing the clicked shortcut.
1336 */
1337 public void onClick(View v) {
1338 Object tag = v.getTag();
1339 if (tag instanceof ApplicationInfo) {
1340 // Open shortcut
1341 final Intent intent = ((ApplicationInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001342 int[] pos = new int[2];
1343 v.getLocationOnScreen(pos);
1344 intent.setSourceBounds(
1345 new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 startActivitySafely(intent);
1347 } else if (tag instanceof FolderInfo) {
1348 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001349 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001350 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001351 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001352 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001353 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
1356 }
1357
1358 void startActivitySafely(Intent intent) {
1359 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1360 try {
1361 startActivity(intent);
1362 } catch (ActivityNotFoundException e) {
1363 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1364 } catch (SecurityException e) {
1365 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001366 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1368 "or use the exported attribute for this activity.", e);
1369 }
1370 }
1371
1372 private void handleFolderClick(FolderInfo folderInfo) {
1373 if (!folderInfo.opened) {
1374 // Close any open folder
1375 closeFolder();
1376 // Open the requested folder
1377 openFolder(folderInfo);
1378 } else {
1379 // Find the open folder...
1380 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1381 int folderScreen;
1382 if (openFolder != null) {
1383 folderScreen = mWorkspace.getScreenForView(openFolder);
1384 // .. and close it
1385 closeFolder(openFolder);
1386 if (folderScreen != mWorkspace.getCurrentScreen()) {
1387 // Close any folder open on the current screen
1388 closeFolder();
1389 // Pull the folder onto this screen
1390 openFolder(folderInfo);
1391 }
1392 }
1393 }
1394 }
1395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 /**
1397 * Opens the user fodler described by the specified tag. The opening of the folder
1398 * is animated relative to the specified View. If the View is null, no animation
1399 * is played.
1400 *
1401 * @param folderInfo The FolderInfo describing the folder to open.
1402 */
1403 private void openFolder(FolderInfo folderInfo) {
1404 Folder openFolder;
1405
1406 if (folderInfo instanceof UserFolderInfo) {
1407 openFolder = UserFolder.fromXml(this);
1408 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001409 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 } else {
1411 return;
1412 }
1413
Joe Onorato00acb122009-08-04 16:04:30 -04001414 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 openFolder.setLauncher(this);
1416
1417 openFolder.bind(folderInfo);
1418 folderInfo.opened = true;
1419
1420 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1421 openFolder.onOpen();
1422 }
1423
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001425 switch (v.getId()) {
1426 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001427 if (!isAllAppsVisible()) {
1428 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1429 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001430 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001431 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001432 return true;
1433 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001434 if (!isAllAppsVisible()) {
1435 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1436 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001437 showPreviews(v);
1438 }
1439 return true;
1440 case R.id.all_apps_button:
1441 if (!isAllAppsVisible()) {
1442 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1443 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1444 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001445 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001446 return true;
1447 }
1448
Joe Onorato9c1289c2009-08-17 11:03:03 -04001449 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 return false;
1451 }
1452
1453 if (!(v instanceof CellLayout)) {
1454 v = (View) v.getParent();
1455 }
1456
1457 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1458
1459 // This happens when long clicking an item with the dpad/trackball
1460 if (cellInfo == null) {
1461 return true;
1462 }
1463
1464 if (mWorkspace.allowLongPress()) {
1465 if (cellInfo.cell == null) {
1466 if (cellInfo.valid) {
1467 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001468 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 showAddDialog(cellInfo);
1470 }
1471 } else {
1472 if (!(cellInfo.cell instanceof Folder)) {
1473 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001474 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1475 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 mWorkspace.startDrag(cellInfo);
1477 }
1478 }
1479 }
1480 return true;
1481 }
1482
Romain Guye6b8e2f2009-11-10 11:56:55 -08001483 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001484 private void dismissPreview(final View v) {
1485 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001486 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001487 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1488 public void onDismiss() {
1489 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1490 int count = group.getChildCount();
1491 for (int i = 0; i < count; i++) {
1492 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1493 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001494 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1495 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1496
1497 v.setTag(R.id.workspace, null);
1498 v.setTag(R.id.icon, null);
1499 window.setOnDismissListener(null);
1500 }
1501 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001502 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001503 }
1504 v.setTag(null);
1505 }
1506
Romain Guyf8e6a802009-12-07 17:48:02 -08001507 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001508 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001509 }
1510
Romain Guya6abce82009-11-10 02:54:41 -08001511 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001512 final Resources resources = getResources();
1513 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001514
Romain Guya6abce82009-11-10 02:54:41 -08001515 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001516
1517 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001518
Romain Guyf8e6a802009-12-07 17:48:02 -08001519 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001520 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001521 int extraW = (int) ((r.left + r.right) * max);
1522 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001523
1524 int aW = cell.getWidth() - extraW;
1525 float w = aW / max;
1526
1527 int width = cell.getWidth();
1528 int height = cell.getHeight();
1529 int x = cell.getLeftPadding();
1530 int y = cell.getTopPadding();
1531 width -= (x + cell.getRightPadding());
1532 height -= (y + cell.getBottomPadding());
1533
1534 float scale = w / width;
1535
1536 int count = end - start;
1537
1538 final float sWidth = width * scale;
1539 float sHeight = height * scale;
1540
Romain Guye6b8e2f2009-11-10 11:56:55 -08001541 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001542
Romain Guye6b8e2f2009-11-10 11:56:55 -08001543 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1544 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001545
1546 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001547 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001548 cell = (CellLayout) workspace.getChildAt(i);
1549
Romain Guyf8e6a802009-12-07 17:48:02 -08001550 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001551 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001552
1553 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001554 c.scale(scale, scale);
1555 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1556 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001557
Romain Guy9d31e9f2009-11-11 18:54:28 -08001558 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001559 image.setImageBitmap(bitmap);
1560 image.setTag(i);
1561 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001562 image.setOnFocusChangeListener(handler);
1563 image.setFocusable(true);
1564 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001565
1566 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001567 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1568
1569 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001570 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001571
1572 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001573 p.setContentView(preview);
1574 p.setWidth((int) (sWidth * count + extraW));
1575 p.setHeight((int) (sHeight + extraH));
1576 p.setAnimationStyle(R.style.AnimationPreview);
1577 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001578 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001579 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001580 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001581
Romain Guye6b8e2f2009-11-10 11:56:55 -08001582 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1583 public void onDismiss() {
1584 dismissPreview(anchor);
1585 }
1586 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001587
1588 anchor.setTag(p);
1589 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001590 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001591 }
1592
Romain Guy9d31e9f2009-11-11 18:54:28 -08001593 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001594 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001595
Romain Guye6b8e2f2009-11-10 11:56:55 -08001596 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001597 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001598 }
1599
1600 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001601 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001602 v.post(this);
1603 }
1604
1605 public void run() {
1606 dismissPreview(mAnchor);
1607 }
1608
1609 public void onFocusChange(View v, boolean hasFocus) {
1610 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001611 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001612 }
Romain Guya6abce82009-11-10 02:54:41 -08001613 }
1614 }
1615
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 Workspace getWorkspace() {
1617 return mWorkspace;
1618 }
1619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 @Override
1621 protected Dialog onCreateDialog(int id) {
1622 switch (id) {
1623 case DIALOG_CREATE_SHORTCUT:
1624 return new CreateShortcut().createDialog();
1625 case DIALOG_RENAME_FOLDER:
1626 return new RenameFolder().createDialog();
1627 }
1628
1629 return super.onCreateDialog(id);
1630 }
1631
1632 @Override
1633 protected void onPrepareDialog(int id, Dialog dialog) {
1634 switch (id) {
1635 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 break;
1637 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001638 if (mFolderInfo != null) {
1639 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1640 final CharSequence text = mFolderInfo.title;
1641 input.setText(text);
1642 input.setSelection(0, text.length());
1643 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 break;
1645 }
1646 }
1647
1648 void showRenameDialog(FolderInfo info) {
1649 mFolderInfo = info;
1650 mWaitingForResult = true;
1651 showDialog(DIALOG_RENAME_FOLDER);
1652 }
1653
1654 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1655 mAddItemCellInfo = cellInfo;
1656 mWaitingForResult = true;
1657 showDialog(DIALOG_CREATE_SHORTCUT);
1658 }
1659
Romain Guy73b979d2009-06-09 12:57:21 -07001660 private void pickShortcut(int requestCode, int title) {
1661 Bundle bundle = new Bundle();
1662
1663 ArrayList<String> shortcutNames = new ArrayList<String>();
1664 shortcutNames.add(getString(R.string.group_applications));
1665 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1666
1667 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1668 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1669 R.drawable.ic_launcher_application));
1670 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1671
1672 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1673 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1674 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1675 pickIntent.putExtras(bundle);
1676
1677 startActivityForResult(pickIntent, requestCode);
1678 }
1679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 private class RenameFolder {
1681 private EditText mInput;
1682
1683 Dialog createDialog() {
1684 mWaitingForResult = true;
1685 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1686 mInput = (EditText) layout.findViewById(R.id.folder_name);
1687
1688 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1689 builder.setIcon(0);
1690 builder.setTitle(getString(R.string.rename_folder_title));
1691 builder.setCancelable(true);
1692 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1693 public void onCancel(DialogInterface dialog) {
1694 cleanup();
1695 }
1696 });
1697 builder.setNegativeButton(getString(R.string.cancel_action),
1698 new Dialog.OnClickListener() {
1699 public void onClick(DialogInterface dialog, int which) {
1700 cleanup();
1701 }
1702 }
1703 );
1704 builder.setPositiveButton(getString(R.string.rename_action),
1705 new Dialog.OnClickListener() {
1706 public void onClick(DialogInterface dialog, int which) {
1707 changeFolderName();
1708 }
1709 }
1710 );
1711 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001712
1713 final AlertDialog dialog = builder.create();
1714 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1715 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001716 mInput.requestFocus();
1717 InputMethodManager inputManager = (InputMethodManager)
1718 getSystemService(Context.INPUT_METHOD_SERVICE);
1719 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001720 }
1721 });
1722
1723 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 }
1725
1726 private void changeFolderName() {
1727 final String name = mInput.getText().toString();
1728 if (!TextUtils.isEmpty(name)) {
1729 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001730 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 mFolderInfo.title = name;
1732 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1733
Joe Onorato9c1289c2009-08-17 11:03:03 -04001734 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001735 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001736 mModel.setWorkspaceDirty();
1737 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 } else {
1739 final FolderIcon folderIcon = (FolderIcon)
1740 mWorkspace.getViewForTag(mFolderInfo);
1741 if (folderIcon != null) {
1742 folderIcon.setText(name);
1743 getWorkspace().requestLayout();
1744 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001745 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001746 mModel.setWorkspaceDirty();
1747 mWorkspaceLoading = true;
1748 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 }
1750 }
1751 }
1752 cleanup();
1753 }
1754
1755 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 dismissDialog(DIALOG_RENAME_FOLDER);
1757 mWaitingForResult = false;
1758 mFolderInfo = null;
1759 }
1760 }
1761
Joe Onoratofb0ca672009-09-14 17:55:46 -04001762 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001763 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001764 }
1765
Joe Onorato3a8820b2009-11-10 15:06:42 -08001766 void showAllApps(boolean animated) {
1767 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001768
Mike Cleronb6082fa2009-10-19 17:03:36 -07001769 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001770 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001771
Joe Onorato7c312c12009-08-13 21:36:53 -07001772 // TODO: fade these two too
1773 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001774 }
1775
Joe Onoratob2061212009-11-24 16:13:54 -05001776 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001777 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001778 * - Home from workspace
1779 * - from center screen
1780 * - from other screens
1781 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001782 * - from center screen
1783 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001784 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001785 * - from center screen
1786 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001787 * - Launch app from workspace and quit
1788 * - with back
1789 * - with home
1790 * - Launch app from all apps and quit
1791 * - with back
1792 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001793 * - Go to a screen that's not the default, then all
1794 * apps, and launch and app, and go back
1795 * - with back
1796 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001797 * - On workspace, long press power and go back
1798 * - with back
1799 * - with home
1800 * - On all apps, long press power and go back
1801 * - with back
1802 * - with home
1803 * - On workspace, power off
1804 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001805 * - Launch an app and turn off the screen while in that app
1806 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001807 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001808 * - From all apps
1809 * - From workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001810 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001811 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001812 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001813 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001814 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001815 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001816 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001817 }
1818
Joe Onorato7c312c12009-08-13 21:36:53 -07001819 void lockAllApps() {
1820 // TODO
1821 }
1822
1823 void unlockAllApps() {
1824 // TODO
1825 }
1826
Joe Onorato7404ee42009-07-31 11:54:44 -07001827 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 * Displays the shortcut creation dialog and launches, if necessary, the
1829 * appropriate activity.
1830 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001831 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001832 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1833 DialogInterface.OnShowListener {
1834
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001836
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 Dialog createDialog() {
1838 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001839
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001841
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1843 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001844 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001845
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 builder.setInverseBackgroundForced(true);
1847
1848 AlertDialog dialog = builder.create();
1849 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001850 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001851 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 return dialog;
1854 }
1855
1856 public void onCancel(DialogInterface dialog) {
1857 mWaitingForResult = false;
1858 cleanup();
1859 }
1860
Romain Guycbb89e42009-06-08 15:52:54 -07001861 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001862 }
1863
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001865 try {
1866 dismissDialog(DIALOG_CREATE_SHORTCUT);
1867 } catch (Exception e) {
1868 // An exception is thrown if the dialog is not visible, which is fine
1869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 }
1871
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001872 /**
1873 * Handle the action clicked in the "Add to home" dialog.
1874 */
1875 public void onClick(DialogInterface dialog, int which) {
1876 Resources res = getResources();
1877 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001878
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001879 switch (which) {
1880 case AddAdapter.ITEM_SHORTCUT: {
1881 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001882 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001883 break;
1884 }
Romain Guycbb89e42009-06-08 15:52:54 -07001885
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001886 case AddAdapter.ITEM_APPWIDGET: {
1887 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001888
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001889 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1890 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001891 // start the pick activity
1892 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1893 break;
1894 }
Romain Guycbb89e42009-06-08 15:52:54 -07001895
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001896 case AddAdapter.ITEM_LIVE_FOLDER: {
1897 // Insert extra item to handle inserting folder
1898 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001899
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001900 ArrayList<String> shortcutNames = new ArrayList<String>();
1901 shortcutNames.add(res.getString(R.string.group_folder));
1902 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001903
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001904 ArrayList<ShortcutIconResource> shortcutIcons =
1905 new ArrayList<ShortcutIconResource>();
1906 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1907 R.drawable.ic_launcher_folder));
1908 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1909
1910 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1911 pickIntent.putExtra(Intent.EXTRA_INTENT,
1912 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1913 pickIntent.putExtra(Intent.EXTRA_TITLE,
1914 getText(R.string.title_select_live_folder));
1915 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001916
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001917 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1918 break;
1919 }
1920
1921 case AddAdapter.ITEM_WALLPAPER: {
1922 startWallpaper();
1923 break;
1924 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 }
1926 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001927
1928 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001929 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 }
1931
1932 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001933 * Receives notifications when applications are added/removed.
1934 */
1935 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1936 @Override
1937 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001938 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05001939 String reason = intent.getStringExtra("reason");
1940 if (!"homekey".equals(reason)) {
1941 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001942 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05001943 animate = false;
1944 }
Joe Onoratob2061212009-11-24 16:13:54 -05001945 closeAllApps(animate);
1946 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001947 }
1948 }
1949
1950 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001951 * Receives notifications whenever the appwidgets are reset.
1952 */
1953 private class AppWidgetResetObserver extends ContentObserver {
1954 public AppWidgetResetObserver() {
1955 super(new Handler());
1956 }
1957
1958 @Override
1959 public void onChange(boolean selfChange) {
1960 onAppWidgetReset();
1961 }
1962 }
1963
1964 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001965 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001967 public int getCurrentWorkspaceScreen() {
1968 return mWorkspace.getCurrentScreen();
1969 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001970
Joe Onorato9c1289c2009-08-17 11:03:03 -04001971 /**
1972 * Refreshes the shortcuts shown on the workspace.
1973 *
1974 * Implementation of the method from LauncherModel.Callbacks.
1975 */
1976 public void startBinding() {
1977 final Workspace workspace = mWorkspace;
1978 int count = workspace.getChildCount();
1979 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001980 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001981 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1982 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001983
Joe Onorato9c1289c2009-08-17 11:03:03 -04001984 if (DEBUG_USER_INTERFACE) {
1985 android.widget.Button finishButton = new android.widget.Button(this);
1986 finishButton.setText("Finish");
1987 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1988
1989 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1990 public void onClick(View v) {
1991 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001992 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001993 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
1995 }
1996
1997 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001998 * Bind the items start-end from the list.
1999 *
2000 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002002 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2003
2004 final Workspace workspace = mWorkspace;
2005
2006 for (int i=start; i<end; i++) {
2007 final ItemInfo item = shortcuts.get(i);
2008 mDesktopItems.add(item);
2009 switch (item.itemType) {
2010 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2011 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2012 final View shortcut = createShortcut((ApplicationInfo) item);
2013 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2014 false);
2015 break;
2016 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2017 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2018 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2019 (UserFolderInfo) item);
2020 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2021 false);
2022 break;
2023 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2024 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2025 R.layout.live_folder_icon, this,
2026 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2027 (LiveFolderInfo) item);
2028 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2029 false);
2030 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
2033
Joe Onorato9c1289c2009-08-17 11:03:03 -04002034 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 }
2036
Joe Onorato9c1289c2009-08-17 11:03:03 -04002037 /**
2038 * Implementation of the method from LauncherModel.Callbacks.
2039 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002040 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2041 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002042 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002044
2045 /**
2046 * Add the views for a widget to the workspace.
2047 *
2048 * Implementation of the method from LauncherModel.Callbacks.
2049 */
2050 public void bindAppWidget(LauncherAppWidgetInfo item) {
2051 final Workspace workspace = mWorkspace;
2052
2053 final int appWidgetId = item.appWidgetId;
2054 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2055 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2056
Joe Onorato9c1289c2009-08-17 11:03:03 -04002057 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2058 item.hostView.setTag(item);
2059
2060 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2061 item.cellY, item.spanX, item.spanY, false);
2062
2063 workspace.requestLayout();
2064
2065 mDesktopItems.add(item);
2066 }
2067
2068 /**
2069 * Callback saying that there aren't any more items to bind.
2070 *
2071 * Implementation of the method from LauncherModel.Callbacks.
2072 */
2073 public void finishBindingItems() {
2074 if (mSavedState != null) {
2075 if (!mWorkspace.hasFocus()) {
2076 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2077 }
2078
2079 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2080 if (userFolders != null) {
2081 for (long folderId : userFolders) {
2082 final FolderInfo info = mFolders.get(folderId);
2083 if (info != null) {
2084 openFolder(info);
2085 }
2086 }
2087 final Folder openFolder = mWorkspace.getOpenFolder();
2088 if (openFolder != null) {
2089 openFolder.requestFocus();
2090 }
2091 }
2092
Joe Onorato9c1289c2009-08-17 11:03:03 -04002093 mSavedState = null;
2094 }
2095
2096 if (mSavedInstanceState != null) {
2097 super.onRestoreInstanceState(mSavedInstanceState);
2098 mSavedInstanceState = null;
2099 }
2100
Joe Onorato9c1289c2009-08-17 11:03:03 -04002101 mWorkspaceLoading = false;
2102 }
2103
2104 /**
2105 * Add the icons for all apps.
2106 *
2107 * Implementation of the method from LauncherModel.Callbacks.
2108 */
2109 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002110 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002111 }
2112
2113 /**
2114 * A package was installed.
2115 *
2116 * Implementation of the method from LauncherModel.Callbacks.
2117 */
2118 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2119 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002120 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002121 }
2122
2123 /**
2124 * A package was updated.
2125 *
2126 * Implementation of the method from LauncherModel.Callbacks.
2127 */
2128 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2129 removeDialog(DIALOG_CREATE_SHORTCUT);
2130 mWorkspace.updateShortcutsForPackage(packageName);
Joe Onoratof0be2132009-11-24 19:34:29 -05002131 mAllAppsGrid.updateApps(packageName, apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002132 }
2133
2134 /**
2135 * A package was uninstalled.
2136 *
2137 * Implementation of the method from LauncherModel.Callbacks.
2138 */
2139 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2140 removeDialog(DIALOG_CREATE_SHORTCUT);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002141 mWorkspace.removeItemsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002142 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 }
Joe Onoratobe386092009-11-17 17:32:16 -08002144
2145 /**
2146 * Prints out out state for debugging.
2147 */
2148 public void dumpState() {
2149 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002150 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002151 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2152 Log.d(TAG, "mRestoring=" + mRestoring);
2153 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2154 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2155 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2156 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002157 mModel.dumpState();
2158 mAllAppsGrid.dumpState();
2159 Log.d(TAG, "END launcher2 dump state");
2160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161}