blob: 1f48f5f306e94839110135e89a5a3a7292302b45 [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
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000019import com.android.common.Search;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Activity;
22import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.Dialog;
24import android.app.SearchManager;
25import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080028import android.content.BroadcastReceiver;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080029import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070033import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070037import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080040import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080041import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080043import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080045import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Parcelable;
Joe Onoratobe386092009-11-17 17:32:16 -080047import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.text.Selection;
50import android.text.SpannableStringBuilder;
51import android.text.TextUtils;
52import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070053import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080055import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.view.KeyEvent;
57import android.view.LayoutInflater;
58import android.view.Menu;
59import android.view.MenuItem;
60import android.view.View;
61import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.view.View.OnLongClickListener;
63import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.widget.TextView;
66import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080067import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080068import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080069import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070070import android.appwidget.AppWidgetManager;
71import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070075import java.io.DataOutputStream;
76import java.io.FileNotFoundException;
77import java.io.IOException;
78import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
Romain Guyedcce092010-03-04 13:03:17 -080080import com.android.launcher.R;
81
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082/**
83 * Default launcher application.
84 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040085public final class Launcher extends Activity
Daniel Sandlerc351eb82010-03-03 15:05:19 -050086 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
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;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800192 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193
Romain Guy84f296c2009-11-04 15:00:44 -0800194 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
195 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700196
Romain Guy1fbc1c82009-11-09 20:43:08 -0800197 private ImageView mPreviousView;
198 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 @Override
201 protected void onCreate(Bundle savedInstanceState) {
202 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700203
Joe Onorato0589f0f2010-02-08 13:44:00 -0800204 LauncherApplication app = ((LauncherApplication)getApplication());
205 mModel = app.setLauncher(this);
206 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400207 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700209
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700211 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
212 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 if (PROFILE_STARTUP) {
215 android.os.Debug.startMethodTracing("/sdcard/launcher");
216 }
217
218 checkForLocaleChange();
219 setWallpaperDimension();
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 setContentView(R.layout.launcher);
222 setupViews();
223
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800224 registerContentObservers();
225
Joe Onorato7c312c12009-08-13 21:36:53 -0700226 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 mSavedState = savedInstanceState;
229 restoreState(mSavedState);
230
231 if (PROFILE_STARTUP) {
232 android.os.Debug.stopMethodTracing();
233 }
234
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700235 // We have a new AllAppsView, we need to re-bind everything, and it could have
236 // changed in our absence.
237 mModel.setAllAppsDirty();
238 mModel.setWorkspaceDirty();
239
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 }
243
244 // For handling default keys
245 mDefaultKeySsb = new SpannableStringBuilder();
246 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800247
248 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
249 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 }
Romain Guycbb89e42009-06-08 15:52:54 -0700251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700253 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
254 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700255
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 final Configuration configuration = getResources().getConfiguration();
257
Romain Guy98d01652009-06-30 16:21:04 -0700258 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 final String locale = configuration.locale.toString();
260
Romain Guy98d01652009-06-30 16:21:04 -0700261 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 final int mcc = configuration.mcc;
263
Romain Guy98d01652009-06-30 16:21:04 -0700264 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 final int mnc = configuration.mnc;
266
Romain Guy84f296c2009-11-04 15:00:44 -0800267 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
Romain Guy84f296c2009-11-04 15:00:44 -0800269 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700270 localeConfiguration.locale = locale;
271 localeConfiguration.mcc = mcc;
272 localeConfiguration.mnc = mnc;
273
274 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800275 mIconCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700276 }
277 }
278
279 private static class LocaleConfiguration {
280 public String locale;
281 public int mcc = -1;
282 public int mnc = -1;
283 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700284
Romain Guy98d01652009-06-30 16:21:04 -0700285 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
286 DataInputStream in = null;
287 try {
288 in = new DataInputStream(context.openFileInput(PREFERENCES));
289 configuration.locale = in.readUTF();
290 configuration.mcc = in.readInt();
291 configuration.mnc = in.readInt();
292 } catch (FileNotFoundException e) {
293 // Ignore
294 } catch (IOException e) {
295 // Ignore
296 } finally {
297 if (in != null) {
298 try {
299 in.close();
300 } catch (IOException e) {
301 // Ignore
302 }
303 }
304 }
305 }
306
307 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
308 DataOutputStream out = null;
309 try {
310 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
311 out.writeUTF(configuration.locale);
312 out.writeInt(configuration.mcc);
313 out.writeInt(configuration.mnc);
314 out.flush();
315 } catch (FileNotFoundException e) {
316 // Ignore
317 } catch (IOException e) {
318 //noinspection ResultOfMethodCallIgnored
319 context.getFileStreamPath(PREFERENCES).delete();
320 } finally {
321 if (out != null) {
322 try {
323 out.close();
324 } catch (IOException e) {
325 // Ignore
326 }
327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 }
329 }
330
331 static int getScreen() {
332 synchronized (sLock) {
333 return sScreen;
334 }
335 }
336
337 static void setScreen(int screen) {
338 synchronized (sLock) {
339 sScreen = screen;
340 }
341 }
342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700344 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345
346 Display display = getWindowManager().getDefaultDisplay();
347 boolean isPortrait = display.getWidth() < display.getHeight();
348
349 final int width = isPortrait ? display.getWidth() : display.getHeight();
350 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700351 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 }
353
354 @Override
355 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700356 mWaitingForResult = false;
357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 // The pattern used here is that a user PICKs a specific application,
359 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700360
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
362 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700363
Romain Guy94dabf12009-07-21 10:55:43 -0700364 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 switch (requestCode) {
366 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 break;
369 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800370 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 break;
372 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 break;
375 case REQUEST_PICK_LIVE_FOLDER:
376 addLiveFolder(data);
377 break;
378 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 completeAddLiveFolder(data, mAddItemCellInfo);
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_PICK_APPWIDGET:
382 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700384 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400385 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700387 case REQUEST_PICK_WALLPAPER:
388 // We just wanted the activity result here so we can clear mWaitingForResult
389 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
Romain Guy18042c82009-11-06 11:44:55 -0800391 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
392 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
393 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700394 // Clean up the appWidgetId if we canceled
395 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
396 if (appWidgetId != -1) {
397 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 }
401
402 @Override
403 protected void onResume() {
404 super.onResume();
405
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800406 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400409 mWorkspaceLoading = true;
410 mModel.startLoader(this, true);
411 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 }
413 }
414
415 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700416 protected void onPause() {
417 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800418 dismissPreview(mPreviousView);
419 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800420 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700421 }
Romain Guycbb89e42009-06-08 15:52:54 -0700422
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700423 @Override
424 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425 // Flag the loader to stop early before switching
426 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800427 mAllAppsGrid.surrender();
Romain Guycbb89e42009-06-08 15:52:54 -0700428
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700429 if (PROFILE_ROTATE) {
430 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
431 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800432 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700433 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700434
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800435 // We can't hide the IME if it was forced open. So don't bother
436 /*
437 @Override
438 public void onWindowFocusChanged(boolean hasFocus) {
439 super.onWindowFocusChanged(hasFocus);
440
441 if (hasFocus) {
442 final InputMethodManager inputManager = (InputMethodManager)
443 getSystemService(Context.INPUT_METHOD_SERVICE);
444 WindowManager.LayoutParams lp = getWindow().getAttributes();
445 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
446 android.os.Handler()) {
447 protected void onReceiveResult(int resultCode, Bundle resultData) {
448 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
449 }
450 });
451 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
452 }
453 }
454 */
455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 private boolean acceptFilter() {
457 final InputMethodManager inputManager = (InputMethodManager)
458 getSystemService(Context.INPUT_METHOD_SERVICE);
459 return !inputManager.isFullscreenMode();
460 }
461
462 @Override
463 public boolean onKeyDown(int keyCode, KeyEvent event) {
464 boolean handled = super.onKeyDown(keyCode, event);
465 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
466 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
467 keyCode, event);
468 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700469 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700470 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700471 // showSearchDialog()
472 // If there are multiple keystrokes before the search dialog takes focus,
473 // onSearchRequested() will be called for every keystroke,
474 // but it is idempotent, so it's fine.
475 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 }
477 }
478
Joe Onorato8a9625e2010-01-28 15:55:35 -0800479 // Eat the long press event so the keyboard doesn't come up.
480 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
481 return true;
482 }
483
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 return handled;
485 }
486
Karl Rosaen138a0412009-04-23 19:00:21 -0700487 private String getTypedText() {
488 return mDefaultKeySsb.toString();
489 }
490
491 private void clearTypedText() {
492 mDefaultKeySsb.clear();
493 mDefaultKeySsb.clearSpans();
494 Selection.setSelection(mDefaultKeySsb, 0);
495 }
496
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497 /**
498 * Restores the previous state, if it exists.
499 *
500 * @param savedState The previous state.
501 */
502 private void restoreState(Bundle savedState) {
503 if (savedState == null) {
504 return;
505 }
506
Joe Onorato3a8820b2009-11-10 15:06:42 -0800507 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
508 if (allApps) {
509 showAllApps(false);
510 }
511
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
513 if (currentScreen > -1) {
514 mWorkspace.setCurrentScreen(currentScreen);
515 }
516
517 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
518 if (addScreen > -1) {
519 mAddItemCellInfo = new CellLayout.CellInfo();
520 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
521 addItemCellInfo.valid = true;
522 addItemCellInfo.screen = addScreen;
523 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
524 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
525 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
526 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
527 addItemCellInfo.findVacantCellsFromOccupied(
528 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
529 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
530 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
531 mRestoring = true;
532 }
533
534 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
535 if (renameFolder) {
536 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400537 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 mRestoring = true;
539 }
540 }
541
542 /**
543 * Finds all the views we need and configure them properly.
544 */
545 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400546 DragController dragController = mDragController;
547
Romain Guyb1b69f52009-08-10 15:10:15 -0700548 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400549 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550
Joe Onorato7c312c12009-08-13 21:36:53 -0700551 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700552 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700553 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800554 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700555 // Manage focusability manually since this thing is always visible
Romain Guyc16fea72010-03-12 17:17:56 -0800556 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700557
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
559 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500560 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561
Joe Onorato7c312c12009-08-13 21:36:53 -0700562 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
563 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564
Joe Onorato7c312c12009-08-13 21:36:53 -0700565 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700566 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700567 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800568 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800569
570 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
571 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
572
573 Drawable previous = mPreviousView.getDrawable();
574 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800575 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576
Joe Onorato0d44e942009-11-16 18:20:51 -0800577 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800578 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800579 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800580 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800581
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400583 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585
586 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400587 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700588 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589
Joe Onorato00acb122009-08-04 16:04:30 -0400590 dragController.setDragScoller(workspace);
591 dragController.setDragListener(deleteZone);
592 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800593 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700594
Joe Onorato00acb122009-08-04 16:04:30 -0400595 // The order here is bottom to top.
596 dragController.addDropTarget(workspace);
597 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599
Romain Guy8a73c512009-11-09 19:19:59 -0800600 @SuppressWarnings({"UnusedDeclaration"})
601 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800602 if (!isAllAppsVisible()) {
603 mWorkspace.scrollLeft();
604 }
Romain Guy8a73c512009-11-09 19:19:59 -0800605 }
606
607 @SuppressWarnings({"UnusedDeclaration"})
608 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800609 if (!isAllAppsVisible()) {
610 mWorkspace.scrollRight();
611 }
Romain Guy8a73c512009-11-09 19:19:59 -0800612 }
613
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 /**
615 * Creates a view representing a shortcut.
616 *
617 * @param info The data structure describing the shortcut.
618 *
619 * @return A View inflated from R.layout.application.
620 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800621 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 return createShortcut(R.layout.application,
623 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
624 }
625
626 /**
627 * Creates a view representing a shortcut inflated from the specified resource.
628 *
629 * @param layoutResId The id of the XML layout used to create the shortcut.
630 * @param parent The group the shortcut belongs to.
631 * @param info The data structure describing the shortcut.
632 *
633 * @return A View inflated from layoutResId.
634 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800635 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
637
Joe Onorato0589f0f2010-02-08 13:44:00 -0800638 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
639 new FastBitmapDrawable(info.getIcon(mIconCache)),
640 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 favorite.setText(info.title);
642 favorite.setTag(info);
643 favorite.setOnClickListener(this);
644
645 return favorite;
646 }
647
648 /**
649 * Add an application shortcut to the workspace.
650 *
651 * @param data The intent describing the application.
652 * @param cellInfo The position on screen where to create the shortcut.
653 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 cellInfo.screen = mWorkspace.getCurrentScreen();
656 if (!findSingleSlot(cellInfo)) return;
657
Joe Onorato0589f0f2010-02-08 13:44:00 -0800658 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
659 data, context);
660
Romain Guy73b979d2009-06-09 12:57:21 -0700661 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800662 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800664 info.container = ItemInfo.NO_ID;
665 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
666 } else {
667 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
669 }
Romain Guycbb89e42009-06-08 15:52:54 -0700670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 /**
672 * Add a shortcut to the workspace.
673 *
674 * @param data The intent describing the shortcut.
675 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400677 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 cellInfo.screen = mWorkspace.getCurrentScreen();
679 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700680
Joe Onorato0589f0f2010-02-08 13:44:00 -0800681 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682
683 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400685 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
686 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 }
688 }
689
Romain Guycbb89e42009-06-08 15:52:54 -0700690
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700692 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700694 * @param data The intent describing the appWidgetId.
695 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700699 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700700
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800701 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700702
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700703 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700704
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700705 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 // Try finding open space on Launcher screen
710 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800711 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
712 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
713 return;
714 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716 // Build Launcher-specific widget info and save to database
717 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 launcherInfo.spanX = spans[0];
719 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700720
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 LauncherModel.addItemToDatabase(this, launcherInfo,
722 LauncherSettings.Favorites.CONTAINER_DESKTOP,
723 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
724
725 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400726 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700729 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700730
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700731 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 }
737 }
Romain Guycbb89e42009-06-08 15:52:54 -0700738
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
740 mDesktopItems.remove(launcherInfo);
741 launcherInfo.hostView = null;
742 }
743
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700744 public LauncherAppWidgetHost getAppWidgetHost() {
745 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 }
Romain Guycbb89e42009-06-08 15:52:54 -0700747
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800748 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800749 getWindow().closeAllPanels();
750
751 try {
752 dismissDialog(DIALOG_CREATE_SHORTCUT);
753 // Unlock the workspace if the dialog was showing
754 } catch (Exception e) {
755 // An exception is thrown if the dialog is not visible, which is fine
756 }
757
758 try {
759 dismissDialog(DIALOG_RENAME_FOLDER);
760 // Unlock the workspace if the dialog was showing
761 } catch (Exception e) {
762 // An exception is thrown if the dialog is not visible, which is fine
763 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800764
765 // Whatever we were doing is hereby canceled.
766 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800767 }
768
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 @Override
770 protected void onNewIntent(Intent intent) {
771 super.onNewIntent(intent);
772
773 // Close the menu
774 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800775 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800776 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700777
Joe Onorato14f122b2009-11-19 14:06:36 -0800778 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
779 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
780 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800781 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800782 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800783 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800784 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700785
Joe Onorato3a8820b2009-11-10 15:06:42 -0800786 final View v = getWindow().peekDecorView();
787 if (v != null && v.getWindowToken() != null) {
788 InputMethodManager imm = (InputMethodManager)getSystemService(
789 INPUT_METHOD_SERVICE);
790 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 }
792 }
793 }
794
795 @Override
796 protected void onRestoreInstanceState(Bundle savedInstanceState) {
797 // Do not call super here
798 mSavedInstanceState = savedInstanceState;
799 }
800
801 @Override
802 protected void onSaveInstanceState(Bundle outState) {
803 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
804
805 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
806 if (folders.size() > 0) {
807 final int count = folders.size();
808 long[] ids = new long[count];
809 for (int i = 0; i < count; i++) {
810 final FolderInfo info = folders.get(i).getInfo();
811 ids[i] = info.id;
812 }
813 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
814 } else {
815 super.onSaveInstanceState(outState);
816 }
817
Joe Onoratofb0ca672009-09-14 17:55:46 -0400818 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800819 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822
823 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
824 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
825 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
826
827 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
828 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
829 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
830 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
831 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
832 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
833 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
834 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
835 layout.getOccupiedCells());
836 }
837
838 if (mFolderInfo != null && mWaitingForResult) {
839 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
840 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
841 }
842 }
843
844 @Override
845 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700847
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700849 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800851 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 }
853
854 TextKeyListener.getInstance().release();
855
Joe Onorato9c1289c2009-08-17 11:03:03 -0400856 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857
Joe Onorato9c1289c2009-08-17 11:03:03 -0400858 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800859
860 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800861
862 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800863 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800864
865 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 }
867
868 @Override
869 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700870 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 super.startActivityForResult(intent, requestCode);
872 }
873
874 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700875 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700877
Joe Onorato7bb17492009-09-24 17:51:01 -0700878 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700879
Karl Rosaen138a0412009-04-23 19:00:21 -0700880 if (initialQuery == null) {
881 // Use any text typed in the launcher as the initial query
882 initialQuery = getTypedText();
883 clearTypedText();
884 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 if (appSearchData == null) {
886 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +0000887 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 }
Romain Guycbb89e42009-06-08 15:52:54 -0700889
Karl Rosaen138a0412009-04-23 19:00:21 -0700890 final SearchManager searchManager =
891 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -0700892 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700893 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 }
895
896 @Override
897 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400898 if (isWorkspaceLocked()) {
899 return false;
900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901
902 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800903
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
905 .setIcon(android.R.drawable.ic_menu_add)
906 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800907 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 .setIcon(android.R.drawable.ic_menu_gallery)
909 .setAlphabeticShortcut('W');
910 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
911 .setIcon(android.R.drawable.ic_search_category_default)
912 .setAlphabeticShortcut(SearchManager.MENU_KEY);
913 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
914 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
915 .setAlphabeticShortcut('N');
916
917 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700918 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
919 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920
921 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
922 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
923 .setIntent(settings);
924
925 return true;
926 }
927
928 @Override
929 public boolean onPrepareOptionsMenu(Menu menu) {
930 super.onPrepareOptionsMenu(menu);
931
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800932 // If all apps is animating, don't show the menu, because we don't know
933 // which one to show.
934 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
935 return false;
936 }
937
938 // Only show the add and wallpaper options when we're not in all apps.
939 boolean visible = !mAllAppsGrid.isOpaque();
940 menu.setGroupVisible(MENU_GROUP_ADD, visible);
941 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
942
943 // Disable add if the workspace is full.
944 if (visible) {
945 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
946 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948
949 return true;
950 }
951
952 @Override
953 public boolean onOptionsItemSelected(MenuItem item) {
954 switch (item.getItemId()) {
955 case MENU_ADD:
956 addItems();
957 return true;
958 case MENU_WALLPAPER_SETTINGS:
959 startWallpaper();
960 return true;
961 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700962 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 return true;
964 case MENU_NOTIFICATIONS:
965 showNotifications();
966 return true;
967 }
968
969 return super.onOptionsItemSelected(item);
970 }
Romain Guycbb89e42009-06-08 15:52:54 -0700971
Karl Rosaen138a0412009-04-23 19:00:21 -0700972 /**
973 * Indicates that we want global search for this activity by setting the globalSearch
974 * argument for {@link #startSearch} to true.
975 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700977 @Override
978 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -0700979 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -0700980 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700981 }
982
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 public boolean isWorkspaceLocked() {
984 return mWorkspaceLoading || mWaitingForResult;
985 }
986
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -0500988 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 showAddDialog(mMenuAddInfo);
990 }
991
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700992 void addAppWidget(Intent data) {
993 // TODO: catch bad widget exception when sent
994 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000995 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996
Bjorn Bringert7984c942009-12-09 15:38:25 +0000997 if (appWidget.configure != null) {
998 // Launch over to configure widget, if needed
999 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1000 intent.setComponent(appWidget.configure);
1001 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1002
Romain Guy8e633c52010-03-04 12:51:36 -08001003 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001005 // Otherwise just add it
1006 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 }
1008 }
Romain Guycbb89e42009-06-08 15:52:54 -07001009
Joe Onoratodeb98af2010-02-19 14:59:39 -08001010 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001011 // Handle case where user selected "Applications"
1012 String applicationName = getResources().getString(R.string.group_applications);
1013 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001014
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001015 if (applicationName != null && applicationName.equals(shortcutName)) {
1016 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1017 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001018
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001019 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1020 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001021 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001022 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001023 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 }
1026
1027 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001028 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001029 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001030 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001031
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001032 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001033 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001034 } else {
1035 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 }
1038
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 UserFolderInfo folderInfo = new UserFolderInfo();
1041 folderInfo.title = getText(R.string.folder_name);
1042
1043 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1044 cellInfo.screen = mWorkspace.getCurrentScreen();
1045 if (!findSingleSlot(cellInfo)) return;
1046
1047 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 LauncherModel.addItemToDatabase(this, folderInfo,
1049 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052
1053 // Create the view
1054 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1055 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1056 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001057 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 }
Romain Guycbb89e42009-06-08 15:52:54 -07001059
Joe Onorato9c1289c2009-08-17 11:03:03 -04001060 void removeFolder(FolderInfo folder) {
1061 mFolders.remove(folder.id);
1062 }
1063
1064 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 cellInfo.screen = mWorkspace.getCurrentScreen();
1066 if (!findSingleSlot(cellInfo)) return;
1067
1068 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1069
1070 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1072 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1074 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 }
1076 }
1077
1078 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1079 CellLayout.CellInfo cellInfo, boolean notify) {
1080
1081 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1082 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1083
1084 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 Intent.ShortcutIconResource iconResource = null;
1086
1087 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1088 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1089 try {
1090 iconResource = (Intent.ShortcutIconResource) extra;
1091 final PackageManager packageManager = context.getPackageManager();
1092 Resources resources = packageManager.getResourcesForApplication(
1093 iconResource.packageName);
1094 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1095 icon = resources.getDrawable(id);
1096 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001097 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 }
1099 }
1100
1101 if (icon == null) {
1102 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1103 }
1104
1105 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001106 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 info.title = name;
1108 info.iconResource = iconResource;
1109 info.uri = data.getData();
1110 info.baseIntent = baseIntent;
1111 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1112 LiveFolders.DISPLAY_MODE_GRID);
1113
1114 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1115 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001116 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117
1118 return info;
1119 }
1120
1121 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1122 final int[] xy = new int[2];
1123 if (findSlot(cellInfo, xy, 1, 1)) {
1124 cellInfo.cellX = xy[0];
1125 cellInfo.cellY = xy[1];
1126 return true;
1127 }
1128 return false;
1129 }
1130
1131 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1132 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1133 boolean[] occupied = mSavedState != null ?
1134 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1135 cellInfo = mWorkspace.findAllVacantCells(occupied);
1136 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1137 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1138 return false;
1139 }
1140 }
1141 return true;
1142 }
1143
1144 private void showNotifications() {
1145 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1146 if (statusBar != null) {
1147 statusBar.expand();
1148 }
1149 }
1150
1151 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001152 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001154 Intent chooser = Intent.createChooser(pickWallpaper,
1155 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001156 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1157 // Removed in Eclair MR1
1158// WallpaperManager wm = (WallpaperManager)
1159// getSystemService(Context.WALLPAPER_SERVICE);
1160// WallpaperInfo wi = wm.getWallpaperInfo();
1161// if (wi != null && wi.getSettingsActivity() != null) {
1162// LabeledIntent li = new LabeledIntent(getPackageName(),
1163// R.string.configure_wallpaper, 0);
1164// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1165// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1166// }
Mike Clerona0618e42009-10-22 13:55:21 -07001167 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 }
1169
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001170 /**
1171 * Registers various content observers. The current implementation registers
1172 * only a favorites observer to keep track of the favorites applications.
1173 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001174 private void registerContentObservers() {
1175 ContentResolver resolver = getContentResolver();
1176 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1177 true, mWidgetObserver);
1178 }
1179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 @Override
1181 public boolean dispatchKeyEvent(KeyEvent event) {
1182 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1183 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001185 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001186 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001187 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001188 dumpState();
1189 return true;
1190 }
1191 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001192 }
1193 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1194 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001195 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 return true;
1197 }
1198 }
1199
1200 return super.dispatchKeyEvent(event);
1201 }
1202
Joe Onorato88ec0992009-11-19 13:16:06 -08001203 @Override
1204 public void onBackPressed() {
1205 if (isAllAppsVisible()) {
1206 closeAllApps(true);
1207 } else {
1208 closeFolder();
1209 }
1210 dismissPreview(mPreviousView);
1211 dismissPreview(mNextView);
1212 }
1213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 private void closeFolder() {
1215 Folder folder = mWorkspace.getOpenFolder();
1216 if (folder != null) {
1217 closeFolder(folder);
1218 }
1219 }
1220
1221 void closeFolder(Folder folder) {
1222 folder.getInfo().opened = false;
1223 ViewGroup parent = (ViewGroup) folder.getParent();
1224 if (parent != null) {
1225 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001226 if (folder instanceof DropTarget) {
1227 // Live folders aren't DropTargets.
1228 mDragController.removeDropTarget((DropTarget)folder);
1229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
1231 folder.onClose();
1232 }
1233
1234 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001235 * Re-listen when widgets are reset.
1236 */
1237 private void onAppWidgetReset() {
1238 mAppWidgetHost.startListening();
1239 }
1240
1241 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001242 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1243 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001245 private void unbindDesktopItems() {
1246 for (ItemInfo item: mDesktopItems) {
1247 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
1249 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 /**
1252 * Launches the intent referred by the clicked shortcut.
1253 *
1254 * @param v The view representing the clicked shortcut.
1255 */
1256 public void onClick(View v) {
1257 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001258 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001260 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001261 int[] pos = new int[2];
1262 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001263 intent.setSourceBounds(new Rect(pos[0], pos[1],
1264 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 startActivitySafely(intent);
1266 } else if (tag instanceof FolderInfo) {
1267 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001268 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001269 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001270 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001271 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001272 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
1275 }
1276
1277 void startActivitySafely(Intent intent) {
1278 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1279 try {
1280 startActivity(intent);
1281 } catch (ActivityNotFoundException e) {
1282 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1283 } catch (SecurityException e) {
1284 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001285 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1287 "or use the exported attribute for this activity.", e);
1288 }
1289 }
Romain Guy8e633c52010-03-04 12:51:36 -08001290
1291 void startActivityForResultSafely(Intent intent, int requestCode) {
1292 try {
1293 startActivityForResult(intent, requestCode);
1294 } catch (ActivityNotFoundException e) {
1295 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1296 } catch (SecurityException e) {
1297 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1298 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1299 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1300 "or use the exported attribute for this activity.", e);
1301 }
1302 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303
1304 private void handleFolderClick(FolderInfo folderInfo) {
1305 if (!folderInfo.opened) {
1306 // Close any open folder
1307 closeFolder();
1308 // Open the requested folder
1309 openFolder(folderInfo);
1310 } else {
1311 // Find the open folder...
1312 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1313 int folderScreen;
1314 if (openFolder != null) {
1315 folderScreen = mWorkspace.getScreenForView(openFolder);
1316 // .. and close it
1317 closeFolder(openFolder);
1318 if (folderScreen != mWorkspace.getCurrentScreen()) {
1319 // Close any folder open on the current screen
1320 closeFolder();
1321 // Pull the folder onto this screen
1322 openFolder(folderInfo);
1323 }
1324 }
1325 }
1326 }
1327
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 /**
1329 * Opens the user fodler described by the specified tag. The opening of the folder
1330 * is animated relative to the specified View. If the View is null, no animation
1331 * is played.
1332 *
1333 * @param folderInfo The FolderInfo describing the folder to open.
1334 */
1335 private void openFolder(FolderInfo folderInfo) {
1336 Folder openFolder;
1337
1338 if (folderInfo instanceof UserFolderInfo) {
1339 openFolder = UserFolder.fromXml(this);
1340 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001341 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 } else {
1343 return;
1344 }
1345
Joe Onorato00acb122009-08-04 16:04:30 -04001346 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 openFolder.setLauncher(this);
1348
1349 openFolder.bind(folderInfo);
1350 folderInfo.opened = true;
1351
1352 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1353 openFolder.onOpen();
1354 }
1355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001357 switch (v.getId()) {
1358 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001359 if (!isAllAppsVisible()) {
1360 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1361 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001362 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001363 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001364 return true;
1365 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001366 if (!isAllAppsVisible()) {
1367 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1368 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001369 showPreviews(v);
1370 }
1371 return true;
1372 case R.id.all_apps_button:
1373 if (!isAllAppsVisible()) {
1374 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1375 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1376 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001377 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001378 return true;
1379 }
1380
Joe Onorato9c1289c2009-08-17 11:03:03 -04001381 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 return false;
1383 }
1384
1385 if (!(v instanceof CellLayout)) {
1386 v = (View) v.getParent();
1387 }
1388
1389 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1390
1391 // This happens when long clicking an item with the dpad/trackball
1392 if (cellInfo == null) {
1393 return true;
1394 }
1395
1396 if (mWorkspace.allowLongPress()) {
1397 if (cellInfo.cell == null) {
1398 if (cellInfo.valid) {
1399 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001400 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001401 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1402 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 showAddDialog(cellInfo);
1404 }
1405 } else {
1406 if (!(cellInfo.cell instanceof Folder)) {
1407 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001408 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1409 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 mWorkspace.startDrag(cellInfo);
1411 }
1412 }
1413 }
1414 return true;
1415 }
1416
Romain Guye6b8e2f2009-11-10 11:56:55 -08001417 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001418 private void dismissPreview(final View v) {
1419 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001420 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001421 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1422 public void onDismiss() {
1423 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1424 int count = group.getChildCount();
1425 for (int i = 0; i < count; i++) {
1426 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1427 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001428 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1429 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1430
1431 v.setTag(R.id.workspace, null);
1432 v.setTag(R.id.icon, null);
1433 window.setOnDismissListener(null);
1434 }
1435 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001436 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001437 }
1438 v.setTag(null);
1439 }
1440
Romain Guyf8e6a802009-12-07 17:48:02 -08001441 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001442 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001443 }
1444
Romain Guya6abce82009-11-10 02:54:41 -08001445 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001446 final Resources resources = getResources();
1447 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001448
Romain Guya6abce82009-11-10 02:54:41 -08001449 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001450
1451 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001452
Romain Guyf8e6a802009-12-07 17:48:02 -08001453 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001454 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001455 int extraW = (int) ((r.left + r.right) * max);
1456 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001457
1458 int aW = cell.getWidth() - extraW;
1459 float w = aW / max;
1460
1461 int width = cell.getWidth();
1462 int height = cell.getHeight();
1463 int x = cell.getLeftPadding();
1464 int y = cell.getTopPadding();
1465 width -= (x + cell.getRightPadding());
1466 height -= (y + cell.getBottomPadding());
1467
1468 float scale = w / width;
1469
1470 int count = end - start;
1471
1472 final float sWidth = width * scale;
1473 float sHeight = height * scale;
1474
Romain Guye6b8e2f2009-11-10 11:56:55 -08001475 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001476
Romain Guye6b8e2f2009-11-10 11:56:55 -08001477 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1478 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001479
1480 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001481 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001482 cell = (CellLayout) workspace.getChildAt(i);
1483
Romain Guyf8e6a802009-12-07 17:48:02 -08001484 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001485 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001486
1487 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001488 c.scale(scale, scale);
1489 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1490 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001491
Romain Guy9d31e9f2009-11-11 18:54:28 -08001492 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001493 image.setImageBitmap(bitmap);
1494 image.setTag(i);
1495 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001496 image.setOnFocusChangeListener(handler);
1497 image.setFocusable(true);
1498 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001499
1500 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001501 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1502
1503 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001504 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001505
1506 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001507 p.setContentView(preview);
1508 p.setWidth((int) (sWidth * count + extraW));
1509 p.setHeight((int) (sHeight + extraH));
1510 p.setAnimationStyle(R.style.AnimationPreview);
1511 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001512 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001513 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001514 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001515
Romain Guye6b8e2f2009-11-10 11:56:55 -08001516 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1517 public void onDismiss() {
1518 dismissPreview(anchor);
1519 }
1520 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001521
1522 anchor.setTag(p);
1523 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001524 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001525 }
1526
Romain Guy9d31e9f2009-11-11 18:54:28 -08001527 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001528 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001529
Romain Guye6b8e2f2009-11-10 11:56:55 -08001530 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001531 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001532 }
1533
1534 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001535 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001536 v.post(this);
1537 }
1538
1539 public void run() {
1540 dismissPreview(mAnchor);
1541 }
1542
1543 public void onFocusChange(View v, boolean hasFocus) {
1544 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001545 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001546 }
Romain Guya6abce82009-11-10 02:54:41 -08001547 }
1548 }
1549
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 Workspace getWorkspace() {
1551 return mWorkspace;
1552 }
1553
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 @Override
1555 protected Dialog onCreateDialog(int id) {
1556 switch (id) {
1557 case DIALOG_CREATE_SHORTCUT:
1558 return new CreateShortcut().createDialog();
1559 case DIALOG_RENAME_FOLDER:
1560 return new RenameFolder().createDialog();
1561 }
1562
1563 return super.onCreateDialog(id);
1564 }
1565
1566 @Override
1567 protected void onPrepareDialog(int id, Dialog dialog) {
1568 switch (id) {
1569 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 break;
1571 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001572 if (mFolderInfo != null) {
1573 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1574 final CharSequence text = mFolderInfo.title;
1575 input.setText(text);
1576 input.setSelection(0, text.length());
1577 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 break;
1579 }
1580 }
1581
1582 void showRenameDialog(FolderInfo info) {
1583 mFolderInfo = info;
1584 mWaitingForResult = true;
1585 showDialog(DIALOG_RENAME_FOLDER);
1586 }
1587
1588 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1589 mAddItemCellInfo = cellInfo;
1590 mWaitingForResult = true;
1591 showDialog(DIALOG_CREATE_SHORTCUT);
1592 }
1593
Joe Onoratodeb98af2010-02-19 14:59:39 -08001594 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001595 Bundle bundle = new Bundle();
1596
1597 ArrayList<String> shortcutNames = new ArrayList<String>();
1598 shortcutNames.add(getString(R.string.group_applications));
1599 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1600
1601 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1602 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1603 R.drawable.ic_launcher_application));
1604 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1605
1606 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1607 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001608 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001609 pickIntent.putExtras(bundle);
1610
Joe Onoratodeb98af2010-02-19 14:59:39 -08001611 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001612 }
1613
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 private class RenameFolder {
1615 private EditText mInput;
1616
1617 Dialog createDialog() {
1618 mWaitingForResult = true;
1619 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1620 mInput = (EditText) layout.findViewById(R.id.folder_name);
1621
1622 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1623 builder.setIcon(0);
1624 builder.setTitle(getString(R.string.rename_folder_title));
1625 builder.setCancelable(true);
1626 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1627 public void onCancel(DialogInterface dialog) {
1628 cleanup();
1629 }
1630 });
1631 builder.setNegativeButton(getString(R.string.cancel_action),
1632 new Dialog.OnClickListener() {
1633 public void onClick(DialogInterface dialog, int which) {
1634 cleanup();
1635 }
1636 }
1637 );
1638 builder.setPositiveButton(getString(R.string.rename_action),
1639 new Dialog.OnClickListener() {
1640 public void onClick(DialogInterface dialog, int which) {
1641 changeFolderName();
1642 }
1643 }
1644 );
1645 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001646
1647 final AlertDialog dialog = builder.create();
1648 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1649 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001650 mInput.requestFocus();
1651 InputMethodManager inputManager = (InputMethodManager)
1652 getSystemService(Context.INPUT_METHOD_SERVICE);
1653 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001654 }
1655 });
1656
1657 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 }
1659
1660 private void changeFolderName() {
1661 final String name = mInput.getText().toString();
1662 if (!TextUtils.isEmpty(name)) {
1663 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001664 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 mFolderInfo.title = name;
1666 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1667
Joe Onorato9c1289c2009-08-17 11:03:03 -04001668 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001669 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001670 mModel.setWorkspaceDirty();
1671 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 } else {
1673 final FolderIcon folderIcon = (FolderIcon)
1674 mWorkspace.getViewForTag(mFolderInfo);
1675 if (folderIcon != null) {
1676 folderIcon.setText(name);
1677 getWorkspace().requestLayout();
1678 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001679 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001680 mModel.setWorkspaceDirty();
1681 mWorkspaceLoading = true;
1682 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 }
1684 }
1685 }
1686 cleanup();
1687 }
1688
1689 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 dismissDialog(DIALOG_RENAME_FOLDER);
1691 mWaitingForResult = false;
1692 mFolderInfo = null;
1693 }
1694 }
1695
Joe Onoratofb0ca672009-09-14 17:55:46 -04001696 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001697 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001698 }
1699
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001700 // AllAppsView.Watcher
1701 public void zoomed(float zoom) {
1702 if (zoom == 1.0f) {
1703 mWorkspace.setVisibility(View.GONE);
1704 }
1705 }
1706
Joe Onorato3a8820b2009-11-10 15:06:42 -08001707 void showAllApps(boolean animated) {
1708 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001709
Romain Guyc16fea72010-03-12 17:17:56 -08001710 ((View) mAllAppsGrid).setFocusable(true);
1711 ((View) mAllAppsGrid).requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001712
Joe Onorato7c312c12009-08-13 21:36:53 -07001713 // TODO: fade these two too
1714 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001715 }
1716
Joe Onoratob2061212009-11-24 16:13:54 -05001717 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001718 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001719 * - Home from workspace
1720 * - from center screen
1721 * - from other screens
1722 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001723 * - from center screen
1724 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001725 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001726 * - from center screen
1727 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001728 * - Launch app from workspace and quit
1729 * - with back
1730 * - with home
1731 * - Launch app from all apps and quit
1732 * - with back
1733 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001734 * - Go to a screen that's not the default, then all
1735 * apps, and launch and app, and go back
1736 * - with back
1737 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001738 * - On workspace, long press power and go back
1739 * - with back
1740 * - with home
1741 * - On all apps, long press power and go back
1742 * - with back
1743 * - with home
1744 * - On workspace, power off
1745 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001746 * - Launch an app and turn off the screen while in that app
1747 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001748 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001749 * - From all apps
1750 * - From workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001751 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001752 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001753 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001754 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001755 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05001756 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001757 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001758 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001759 }
1760
Joe Onorato7c312c12009-08-13 21:36:53 -07001761 void lockAllApps() {
1762 // TODO
1763 }
1764
1765 void unlockAllApps() {
1766 // TODO
1767 }
1768
Joe Onorato7404ee42009-07-31 11:54:44 -07001769 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 * Displays the shortcut creation dialog and launches, if necessary, the
1771 * appropriate activity.
1772 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001773 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001774 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1775 DialogInterface.OnShowListener {
1776
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001781
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1783 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001784 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001785
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 builder.setInverseBackgroundForced(true);
1787
1788 AlertDialog dialog = builder.create();
1789 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001790 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001791 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 return dialog;
1794 }
1795
1796 public void onCancel(DialogInterface dialog) {
1797 mWaitingForResult = false;
1798 cleanup();
1799 }
1800
Romain Guycbb89e42009-06-08 15:52:54 -07001801 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001802 }
1803
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001805 try {
1806 dismissDialog(DIALOG_CREATE_SHORTCUT);
1807 } catch (Exception e) {
1808 // An exception is thrown if the dialog is not visible, which is fine
1809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 }
1811
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001812 /**
1813 * Handle the action clicked in the "Add to home" dialog.
1814 */
1815 public void onClick(DialogInterface dialog, int which) {
1816 Resources res = getResources();
1817 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001818
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001819 switch (which) {
1820 case AddAdapter.ITEM_SHORTCUT: {
1821 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08001822 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001823 break;
1824 }
Romain Guycbb89e42009-06-08 15:52:54 -07001825
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001826 case AddAdapter.ITEM_APPWIDGET: {
1827 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001828
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001829 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1830 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001831 // start the pick activity
1832 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1833 break;
1834 }
Romain Guycbb89e42009-06-08 15:52:54 -07001835
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001836 case AddAdapter.ITEM_LIVE_FOLDER: {
1837 // Insert extra item to handle inserting folder
1838 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001839
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001840 ArrayList<String> shortcutNames = new ArrayList<String>();
1841 shortcutNames.add(res.getString(R.string.group_folder));
1842 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001843
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001844 ArrayList<ShortcutIconResource> shortcutIcons =
1845 new ArrayList<ShortcutIconResource>();
1846 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1847 R.drawable.ic_launcher_folder));
1848 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1849
1850 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1851 pickIntent.putExtra(Intent.EXTRA_INTENT,
1852 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1853 pickIntent.putExtra(Intent.EXTRA_TITLE,
1854 getText(R.string.title_select_live_folder));
1855 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001856
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001857 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1858 break;
1859 }
1860
1861 case AddAdapter.ITEM_WALLPAPER: {
1862 startWallpaper();
1863 break;
1864 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 }
1866 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001867
1868 public void onShow(DialogInterface dialog) {
Romain Guy406d7032010-03-04 18:59:31 -08001869 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07001870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 }
1872
1873 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001874 * Receives notifications when applications are added/removed.
1875 */
1876 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1877 @Override
1878 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001879 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05001880 String reason = intent.getStringExtra("reason");
1881 if (!"homekey".equals(reason)) {
1882 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001883 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05001884 animate = false;
1885 }
Joe Onoratob2061212009-11-24 16:13:54 -05001886 closeAllApps(animate);
1887 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001888 }
1889 }
1890
1891 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001892 * Receives notifications whenever the appwidgets are reset.
1893 */
1894 private class AppWidgetResetObserver extends ContentObserver {
1895 public AppWidgetResetObserver() {
1896 super(new Handler());
1897 }
1898
1899 @Override
1900 public void onChange(boolean selfChange) {
1901 onAppWidgetReset();
1902 }
1903 }
1904
1905 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001906 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001908 public int getCurrentWorkspaceScreen() {
1909 return mWorkspace.getCurrentScreen();
1910 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001911
Joe Onorato9c1289c2009-08-17 11:03:03 -04001912 /**
1913 * Refreshes the shortcuts shown on the workspace.
1914 *
1915 * Implementation of the method from LauncherModel.Callbacks.
1916 */
1917 public void startBinding() {
1918 final Workspace workspace = mWorkspace;
1919 int count = workspace.getChildCount();
1920 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001921 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001922 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1923 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001924
Joe Onorato9c1289c2009-08-17 11:03:03 -04001925 if (DEBUG_USER_INTERFACE) {
1926 android.widget.Button finishButton = new android.widget.Button(this);
1927 finishButton.setText("Finish");
1928 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1929
1930 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1931 public void onClick(View v) {
1932 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001933 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001934 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 }
1936 }
1937
1938 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001939 * Bind the items start-end from the list.
1940 *
1941 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001943 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1944
1945 final Workspace workspace = mWorkspace;
1946
1947 for (int i=start; i<end; i++) {
1948 final ItemInfo item = shortcuts.get(i);
1949 mDesktopItems.add(item);
1950 switch (item.itemType) {
1951 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1952 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001953 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001954 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1955 false);
1956 break;
1957 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1958 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1959 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1960 (UserFolderInfo) item);
1961 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1962 false);
1963 break;
1964 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1965 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1966 R.layout.live_folder_icon, this,
1967 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1968 (LiveFolderInfo) item);
1969 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1970 false);
1971 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
1974
Joe Onorato9c1289c2009-08-17 11:03:03 -04001975 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 }
1977
Joe Onorato9c1289c2009-08-17 11:03:03 -04001978 /**
1979 * Implementation of the method from LauncherModel.Callbacks.
1980 */
Joe Onoratoad72e172009-11-06 16:25:04 -05001981 public void bindFolders(HashMap<Long, FolderInfo> folders) {
1982 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001983 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001985
1986 /**
1987 * Add the views for a widget to the workspace.
1988 *
1989 * Implementation of the method from LauncherModel.Callbacks.
1990 */
1991 public void bindAppWidget(LauncherAppWidgetInfo item) {
1992 final Workspace workspace = mWorkspace;
1993
1994 final int appWidgetId = item.appWidgetId;
1995 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1996 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1997
Joe Onorato9c1289c2009-08-17 11:03:03 -04001998 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1999 item.hostView.setTag(item);
2000
2001 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2002 item.cellY, item.spanX, item.spanY, false);
2003
2004 workspace.requestLayout();
2005
2006 mDesktopItems.add(item);
2007 }
2008
2009 /**
2010 * Callback saying that there aren't any more items to bind.
2011 *
2012 * Implementation of the method from LauncherModel.Callbacks.
2013 */
2014 public void finishBindingItems() {
2015 if (mSavedState != null) {
2016 if (!mWorkspace.hasFocus()) {
2017 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2018 }
2019
2020 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2021 if (userFolders != null) {
2022 for (long folderId : userFolders) {
2023 final FolderInfo info = mFolders.get(folderId);
2024 if (info != null) {
2025 openFolder(info);
2026 }
2027 }
2028 final Folder openFolder = mWorkspace.getOpenFolder();
2029 if (openFolder != null) {
2030 openFolder.requestFocus();
2031 }
2032 }
2033
Joe Onorato9c1289c2009-08-17 11:03:03 -04002034 mSavedState = null;
2035 }
2036
2037 if (mSavedInstanceState != null) {
2038 super.onRestoreInstanceState(mSavedInstanceState);
2039 mSavedInstanceState = null;
2040 }
2041
Joe Onorato9c1289c2009-08-17 11:03:03 -04002042 mWorkspaceLoading = false;
2043 }
2044
2045 /**
2046 * Add the icons for all apps.
2047 *
2048 * Implementation of the method from LauncherModel.Callbacks.
2049 */
2050 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002051 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002052 }
2053
2054 /**
2055 * A package was installed.
2056 *
2057 * Implementation of the method from LauncherModel.Callbacks.
2058 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002059 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002060 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002061 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002062 }
2063
2064 /**
2065 * A package was updated.
2066 *
2067 * Implementation of the method from LauncherModel.Callbacks.
2068 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002069 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002070 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002071 mWorkspace.updateShortcuts(apps);
2072 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002073 }
2074
2075 /**
2076 * A package was uninstalled.
2077 *
2078 * Implementation of the method from LauncherModel.Callbacks.
2079 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002080 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002081 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002082 mWorkspace.removeItems(apps);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002083 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002084 }
Joe Onoratobe386092009-11-17 17:32:16 -08002085
2086 /**
2087 * Prints out out state for debugging.
2088 */
2089 public void dumpState() {
2090 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002091 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002092 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2093 Log.d(TAG, "mRestoring=" + mRestoring);
2094 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2095 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2096 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2097 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002098 mModel.dumpState();
2099 mAllAppsGrid.dumpState();
2100 Log.d(TAG, "END launcher2 dump state");
2101 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102}