blob: ee92fc72503d1c828075adb451ea2f88d85e4025 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080030import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070034import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.pm.ActivityInfo;
37import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070040import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080041import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080043import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080044import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080046import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080048import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.os.Parcelable;
Joe Onoratobe386092009-11-17 17:32:16 -080050import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.text.Selection;
53import android.text.SpannableStringBuilder;
54import android.text.TextUtils;
55import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070056import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080058import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.view.KeyEvent;
60import android.view.LayoutInflater;
61import android.view.Menu;
62import android.view.MenuItem;
63import android.view.View;
64import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.View.OnLongClickListener;
Joe Onorato8a9625e2010-01-28 15:55:35 -080066import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.widget.TextView;
70import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080071import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080072import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080073import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070074import android.appwidget.AppWidgetManager;
75import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040078import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070079import java.io.DataOutputStream;
80import java.io.FileNotFoundException;
81import java.io.IOException;
82import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
84/**
85 * Default launcher application.
86 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040087public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080088 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080089 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 static final boolean LOGD = false;
91
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 static final boolean PROFILE_STARTUP = false;
93 static final boolean PROFILE_ROTATE = false;
94 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070095
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 private static final int WALLPAPER_SCREENS_SPAN = 2;
97
98 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -080099 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 private static final int MENU_ADD = Menu.FIRST + 1;
102 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
103 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
104 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700105 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106
107 private static final int REQUEST_CREATE_SHORTCUT = 1;
108 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700109 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 private static final int REQUEST_PICK_APPLICATION = 6;
111 private static final int REQUEST_PICK_SHORTCUT = 7;
112 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700113 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700114 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
116 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
117
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800118 static final int SCREEN_COUNT = 5;
119 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700121 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato7c312c12009-08-13 21:36:53 -0700123 static final int DIALOG_CREATE_SHORTCUT = 1;
124 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Romain Guy98d01652009-06-30 16:21:04 -0700126 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
128 // Type: int
129 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
130 // Type: boolean
131 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
132 // Type: long
133 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
148 // Type: int[]
149 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
150 // Type: boolean
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
152 // Type: long
153 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
154
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800158 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800160 private final BroadcastReceiver mCloseSystemDialogsReceiver
161 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800162 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private LayoutInflater mInflater;
165
Joe Onorato00acb122009-08-04 16:04:30 -0400166 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private AppWidgetManager mAppWidgetManager;
170 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private CellLayout.CellInfo mAddItemCellInfo;
173 private CellLayout.CellInfo mMenuAddInfo;
174 private final int[] mCellCoordinates = new int[2];
175 private FolderInfo mFolderInfo;
176
Joe Onorato7c312c12009-08-13 21:36:53 -0700177 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700179 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private Bundle mSavedState;
182
183 private SpannableStringBuilder mDefaultKeySsb = null;
184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 private boolean mWorkspaceLoading = true;
186
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800187 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private boolean mRestoring;
189 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
191 private Bundle mSavedInstanceState;
192
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800194 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195
Romain Guy84f296c2009-11-04 15:00:44 -0800196 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
197 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700198
Romain Guy1fbc1c82009-11-09 20:43:08 -0800199 private ImageView mPreviousView;
200 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 @Override
203 protected void onCreate(Bundle savedInstanceState) {
204 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700205
Joe Onorato0589f0f2010-02-08 13:44:00 -0800206 LauncherApplication app = ((LauncherApplication)getApplication());
207 mModel = app.setLauncher(this);
208 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400209 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700211
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
214 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 if (PROFILE_STARTUP) {
217 android.os.Debug.startMethodTracing("/sdcard/launcher");
218 }
219
220 checkForLocaleChange();
221 setWallpaperDimension();
222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 setContentView(R.layout.launcher);
224 setupViews();
225
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800226 registerContentObservers();
227
Joe Onorato7c312c12009-08-13 21:36:53 -0700228 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 mSavedState = savedInstanceState;
231 restoreState(mSavedState);
232
233 if (PROFILE_STARTUP) {
234 android.os.Debug.stopMethodTracing();
235 }
236
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700237 // We have a new AllAppsView, we need to re-bind everything, and it could have
238 // changed in our absence.
239 mModel.setAllAppsDirty();
240 mModel.setWorkspaceDirty();
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400243 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 }
245
246 // For handling default keys
247 mDefaultKeySsb = new SpannableStringBuilder();
248 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800249
250 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
251 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 }
Romain Guycbb89e42009-06-08 15:52:54 -0700253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700255 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
256 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 final Configuration configuration = getResources().getConfiguration();
259
Romain Guy98d01652009-06-30 16:21:04 -0700260 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 final String locale = configuration.locale.toString();
262
Romain Guy98d01652009-06-30 16:21:04 -0700263 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 final int mcc = configuration.mcc;
265
Romain Guy98d01652009-06-30 16:21:04 -0700266 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 final int mnc = configuration.mnc;
268
Romain Guy84f296c2009-11-04 15:00:44 -0800269 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270
Romain Guy84f296c2009-11-04 15:00:44 -0800271 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700272 localeConfiguration.locale = locale;
273 localeConfiguration.mcc = mcc;
274 localeConfiguration.mnc = mnc;
275
276 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800277 mIconCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700278 }
279 }
280
281 private static class LocaleConfiguration {
282 public String locale;
283 public int mcc = -1;
284 public int mnc = -1;
285 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700286
Romain Guy98d01652009-06-30 16:21:04 -0700287 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
288 DataInputStream in = null;
289 try {
290 in = new DataInputStream(context.openFileInput(PREFERENCES));
291 configuration.locale = in.readUTF();
292 configuration.mcc = in.readInt();
293 configuration.mnc = in.readInt();
294 } catch (FileNotFoundException e) {
295 // Ignore
296 } catch (IOException e) {
297 // Ignore
298 } finally {
299 if (in != null) {
300 try {
301 in.close();
302 } catch (IOException e) {
303 // Ignore
304 }
305 }
306 }
307 }
308
309 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
310 DataOutputStream out = null;
311 try {
312 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
313 out.writeUTF(configuration.locale);
314 out.writeInt(configuration.mcc);
315 out.writeInt(configuration.mnc);
316 out.flush();
317 } catch (FileNotFoundException e) {
318 // Ignore
319 } catch (IOException e) {
320 //noinspection ResultOfMethodCallIgnored
321 context.getFileStreamPath(PREFERENCES).delete();
322 } finally {
323 if (out != null) {
324 try {
325 out.close();
326 } catch (IOException e) {
327 // Ignore
328 }
329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 }
331 }
332
333 static int getScreen() {
334 synchronized (sLock) {
335 return sScreen;
336 }
337 }
338
339 static void setScreen(int screen) {
340 synchronized (sLock) {
341 sScreen = screen;
342 }
343 }
344
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700346 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347
348 Display display = getWindowManager().getDefaultDisplay();
349 boolean isPortrait = display.getWidth() < display.getHeight();
350
351 final int width = isPortrait ? display.getWidth() : display.getHeight();
352 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700353 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 }
355
356 @Override
357 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700358 mWaitingForResult = false;
359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 // The pattern used here is that a user PICKs a specific application,
361 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700362
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
364 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700365
Romain Guy94dabf12009-07-21 10:55:43 -0700366 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 switch (requestCode) {
368 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400369 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 break;
371 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700372 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 break;
374 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400375 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 break;
377 case REQUEST_PICK_LIVE_FOLDER:
378 addLiveFolder(data);
379 break;
380 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700383 case REQUEST_PICK_APPWIDGET:
384 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700386 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400387 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700389 case REQUEST_PICK_WALLPAPER:
390 // We just wanted the activity result here so we can clear mWaitingForResult
391 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
Romain Guy18042c82009-11-06 11:44:55 -0800393 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
394 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
395 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700396 // Clean up the appWidgetId if we canceled
397 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
398 if (appWidgetId != -1) {
399 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 }
401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 }
403
404 @Override
405 protected void onResume() {
406 super.onResume();
407
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800408 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400411 mWorkspaceLoading = true;
412 mModel.startLoader(this, true);
413 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 }
415 }
416
417 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700418 protected void onPause() {
419 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800420 dismissPreview(mPreviousView);
421 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800422 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700423 }
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700425 @Override
426 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400427 // Flag the loader to stop early before switching
428 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700430 if (PROFILE_ROTATE) {
431 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
432 }
433 return null;
434 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800436 // We can't hide the IME if it was forced open. So don't bother
437 /*
438 @Override
439 public void onWindowFocusChanged(boolean hasFocus) {
440 super.onWindowFocusChanged(hasFocus);
441
442 if (hasFocus) {
443 final InputMethodManager inputManager = (InputMethodManager)
444 getSystemService(Context.INPUT_METHOD_SERVICE);
445 WindowManager.LayoutParams lp = getWindow().getAttributes();
446 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
447 android.os.Handler()) {
448 protected void onReceiveResult(int resultCode, Bundle resultData) {
449 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
450 }
451 });
452 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
453 }
454 }
455 */
456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 private boolean acceptFilter() {
458 final InputMethodManager inputManager = (InputMethodManager)
459 getSystemService(Context.INPUT_METHOD_SERVICE);
460 return !inputManager.isFullscreenMode();
461 }
462
463 @Override
464 public boolean onKeyDown(int keyCode, KeyEvent event) {
465 boolean handled = super.onKeyDown(keyCode, event);
466 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
467 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
468 keyCode, event);
469 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700470 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700471 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700472 // showSearchDialog()
473 // If there are multiple keystrokes before the search dialog takes focus,
474 // onSearchRequested() will be called for every keystroke,
475 // but it is idempotent, so it's fine.
476 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 }
478 }
479
Joe Onorato8a9625e2010-01-28 15:55:35 -0800480 // Eat the long press event so the keyboard doesn't come up.
481 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
482 return true;
483 }
484
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 return handled;
486 }
487
Karl Rosaen138a0412009-04-23 19:00:21 -0700488 private String getTypedText() {
489 return mDefaultKeySsb.toString();
490 }
491
492 private void clearTypedText() {
493 mDefaultKeySsb.clear();
494 mDefaultKeySsb.clearSpans();
495 Selection.setSelection(mDefaultKeySsb, 0);
496 }
497
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 /**
499 * Restores the previous state, if it exists.
500 *
501 * @param savedState The previous state.
502 */
503 private void restoreState(Bundle savedState) {
504 if (savedState == null) {
505 return;
506 }
507
Joe Onorato3a8820b2009-11-10 15:06:42 -0800508 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
509 if (allApps) {
510 showAllApps(false);
511 }
512
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800513 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
514 if (currentScreen > -1) {
515 mWorkspace.setCurrentScreen(currentScreen);
516 }
517
518 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
519 if (addScreen > -1) {
520 mAddItemCellInfo = new CellLayout.CellInfo();
521 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
522 addItemCellInfo.valid = true;
523 addItemCellInfo.screen = addScreen;
524 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
525 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
526 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
527 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
528 addItemCellInfo.findVacantCellsFromOccupied(
529 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
530 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
531 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
532 mRestoring = true;
533 }
534
535 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
536 if (renameFolder) {
537 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 mRestoring = true;
540 }
541 }
542
543 /**
544 * Finds all the views we need and configure them properly.
545 */
546 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400547 DragController dragController = mDragController;
548
Romain Guyb1b69f52009-08-10 15:10:15 -0700549 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400550 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551
Joe Onorato7c312c12009-08-13 21:36:53 -0700552 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700553 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700554 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700555 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700556 // Manage focusability manually since this thing is always visible
557 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700558
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
560 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500561 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562
Joe Onorato7c312c12009-08-13 21:36:53 -0700563 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
564 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565
Joe Onorato7c312c12009-08-13 21:36:53 -0700566 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700567 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700568 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800569 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800570
571 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
572 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
573
574 Drawable previous = mPreviousView.getDrawable();
575 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800576 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577
Joe Onorato0d44e942009-11-16 18:20:51 -0800578 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800579 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800580 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800581 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800582
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400584 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586
587 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400588 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700589 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590
Joe Onorato00acb122009-08-04 16:04:30 -0400591 dragController.setDragScoller(workspace);
592 dragController.setDragListener(deleteZone);
593 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800594 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700595
Joe Onorato00acb122009-08-04 16:04:30 -0400596 // The order here is bottom to top.
597 dragController.addDropTarget(workspace);
598 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
600
Romain Guy8a73c512009-11-09 19:19:59 -0800601 @SuppressWarnings({"UnusedDeclaration"})
602 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800603 if (!isAllAppsVisible()) {
604 mWorkspace.scrollLeft();
605 }
Romain Guy8a73c512009-11-09 19:19:59 -0800606 }
607
608 @SuppressWarnings({"UnusedDeclaration"})
609 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800610 if (!isAllAppsVisible()) {
611 mWorkspace.scrollRight();
612 }
Romain Guy8a73c512009-11-09 19:19:59 -0800613 }
614
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 /**
616 * Creates a view representing a shortcut.
617 *
618 * @param info The data structure describing the shortcut.
619 *
620 * @return A View inflated from R.layout.application.
621 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800622 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 return createShortcut(R.layout.application,
624 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
625 }
626
627 /**
628 * Creates a view representing a shortcut inflated from the specified resource.
629 *
630 * @param layoutResId The id of the XML layout used to create the shortcut.
631 * @param parent The group the shortcut belongs to.
632 * @param info The data structure describing the shortcut.
633 *
634 * @return A View inflated from layoutResId.
635 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800636 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
638
Joe Onorato0589f0f2010-02-08 13:44:00 -0800639 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
640 new FastBitmapDrawable(info.getIcon(mIconCache)),
641 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 favorite.setText(info.title);
643 favorite.setTag(info);
644 favorite.setOnClickListener(this);
645
646 return favorite;
647 }
648
649 /**
650 * Add an application shortcut to the workspace.
651 *
652 * @param data The intent describing the application.
653 * @param cellInfo The position on screen where to create the shortcut.
654 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 cellInfo.screen = mWorkspace.getCurrentScreen();
657 if (!findSingleSlot(cellInfo)) return;
658
Joe Onorato0589f0f2010-02-08 13:44:00 -0800659 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
660 data, context);
661
Romain Guy73b979d2009-06-09 12:57:21 -0700662 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800663 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800665 info.container = ItemInfo.NO_ID;
666 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
667 } else {
668 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 }
670 }
Romain Guycbb89e42009-06-08 15:52:54 -0700671
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 /**
673 * Add a shortcut to the workspace.
674 *
675 * @param data The intent describing the shortcut.
676 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400678 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 cellInfo.screen = mWorkspace.getCurrentScreen();
680 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700681
Joe Onorato0589f0f2010-02-08 13:44:00 -0800682 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683
684 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400686 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
687 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 }
689 }
690
Romain Guycbb89e42009-06-08 15:52:54 -0700691
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700693 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700695 * @param data The intent describing the appWidgetId.
696 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700700 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700701
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800702 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700703
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700704 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700705
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 // Try finding open space on Launcher screen
711 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800712 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
713 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
714 return;
715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 // Build Launcher-specific widget info and save to database
718 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 launcherInfo.spanX = spans[0];
720 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 LauncherModel.addItemToDatabase(this, launcherInfo,
723 LauncherSettings.Favorites.CONTAINER_DESKTOP,
724 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
725
726 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400727 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700728
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700730 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700731
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700734
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 }
738 }
Romain Guycbb89e42009-06-08 15:52:54 -0700739
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
741 mDesktopItems.remove(launcherInfo);
742 launcherInfo.hostView = null;
743 }
744
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700745 public LauncherAppWidgetHost getAppWidgetHost() {
746 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 }
Romain Guycbb89e42009-06-08 15:52:54 -0700748
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800749 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800750 getWindow().closeAllPanels();
751
752 try {
753 dismissDialog(DIALOG_CREATE_SHORTCUT);
754 // Unlock the workspace if the dialog was showing
755 } catch (Exception e) {
756 // An exception is thrown if the dialog is not visible, which is fine
757 }
758
759 try {
760 dismissDialog(DIALOG_RENAME_FOLDER);
761 // Unlock the workspace if the dialog was showing
762 } catch (Exception e) {
763 // An exception is thrown if the dialog is not visible, which is fine
764 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800765
766 // Whatever we were doing is hereby canceled.
767 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800768 }
769
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 @Override
771 protected void onNewIntent(Intent intent) {
772 super.onNewIntent(intent);
773
774 // Close the menu
775 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800776 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800777 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700778
Joe Onorato14f122b2009-11-19 14:06:36 -0800779 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
780 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
781 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800782 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800783 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800784 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800785 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700786
Joe Onorato3a8820b2009-11-10 15:06:42 -0800787 final View v = getWindow().peekDecorView();
788 if (v != null && v.getWindowToken() != null) {
789 InputMethodManager imm = (InputMethodManager)getSystemService(
790 INPUT_METHOD_SERVICE);
791 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 }
793 }
794 }
795
796 @Override
797 protected void onRestoreInstanceState(Bundle savedInstanceState) {
798 // Do not call super here
799 mSavedInstanceState = savedInstanceState;
800 }
801
802 @Override
803 protected void onSaveInstanceState(Bundle outState) {
804 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
805
806 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
807 if (folders.size() > 0) {
808 final int count = folders.size();
809 long[] ids = new long[count];
810 for (int i = 0; i < count; i++) {
811 final FolderInfo info = folders.get(i).getInfo();
812 ids[i] = info.id;
813 }
814 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
815 } else {
816 super.onSaveInstanceState(outState);
817 }
818
Joe Onoratofb0ca672009-09-14 17:55:46 -0400819 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800820 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823
824 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
825 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
826 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
827
828 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
829 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
830 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
831 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
832 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
833 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
834 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
835 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
836 layout.getOccupiedCells());
837 }
838
839 if (mFolderInfo != null && mWaitingForResult) {
840 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
841 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
842 }
843 }
844
845 @Override
846 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700850 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800852 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 }
854
855 TextKeyListener.getInstance().release();
856
Joe Onorato9c1289c2009-08-17 11:03:03 -0400857 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800860
861 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800862
863 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800864 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800865
866 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 }
868
869 @Override
870 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700871 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 super.startActivityForResult(intent, requestCode);
873 }
874
875 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700876 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700878
Joe Onorato7bb17492009-09-24 17:51:01 -0700879 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700880
Karl Rosaen138a0412009-04-23 19:00:21 -0700881 if (initialQuery == null) {
882 // Use any text typed in the launcher as the initial query
883 initialQuery = getTypedText();
884 clearTypedText();
885 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 if (appSearchData == null) {
887 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +0000888 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
Romain Guycbb89e42009-06-08 15:52:54 -0700890
Karl Rosaen138a0412009-04-23 19:00:21 -0700891 final SearchManager searchManager =
892 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -0700893 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700894 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
896
897 @Override
898 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899 if (isWorkspaceLocked()) {
900 return false;
901 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902
903 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800904
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
906 .setIcon(android.R.drawable.ic_menu_add)
907 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800908 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 .setIcon(android.R.drawable.ic_menu_gallery)
910 .setAlphabeticShortcut('W');
911 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
912 .setIcon(android.R.drawable.ic_search_category_default)
913 .setAlphabeticShortcut(SearchManager.MENU_KEY);
914 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
915 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
916 .setAlphabeticShortcut('N');
917
918 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700919 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
920 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921
922 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
923 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
924 .setIntent(settings);
925
926 return true;
927 }
928
929 @Override
930 public boolean onPrepareOptionsMenu(Menu menu) {
931 super.onPrepareOptionsMenu(menu);
932
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800933 // If all apps is animating, don't show the menu, because we don't know
934 // which one to show.
935 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
936 return false;
937 }
938
939 // Only show the add and wallpaper options when we're not in all apps.
940 boolean visible = !mAllAppsGrid.isOpaque();
941 menu.setGroupVisible(MENU_GROUP_ADD, visible);
942 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
943
944 // Disable add if the workspace is full.
945 if (visible) {
946 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
947 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
948 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949
950 return true;
951 }
952
953 @Override
954 public boolean onOptionsItemSelected(MenuItem item) {
955 switch (item.getItemId()) {
956 case MENU_ADD:
957 addItems();
958 return true;
959 case MENU_WALLPAPER_SETTINGS:
960 startWallpaper();
961 return true;
962 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700963 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 return true;
965 case MENU_NOTIFICATIONS:
966 showNotifications();
967 return true;
968 }
969
970 return super.onOptionsItemSelected(item);
971 }
Romain Guycbb89e42009-06-08 15:52:54 -0700972
Karl Rosaen138a0412009-04-23 19:00:21 -0700973 /**
974 * Indicates that we want global search for this activity by setting the globalSearch
975 * argument for {@link #startSearch} to true.
976 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700978 @Override
979 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -0700980 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -0700981 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700982 }
983
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984 public boolean isWorkspaceLocked() {
985 return mWorkspaceLoading || mWaitingForResult;
986 }
987
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -0500989 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 showAddDialog(mMenuAddInfo);
991 }
992
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700993 void addAppWidget(Intent data) {
994 // TODO: catch bad widget exception when sent
995 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000996 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997
Bjorn Bringert7984c942009-12-09 15:38:25 +0000998 if (appWidget.configure != null) {
999 // Launch over to configure widget, if needed
1000 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1001 intent.setComponent(appWidget.configure);
1002 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1003
1004 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001006 // Otherwise just add it
1007 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 }
1009 }
Romain Guycbb89e42009-06-08 15:52:54 -07001010
Romain Guy73b979d2009-06-09 12:57:21 -07001011 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001012 // Handle case where user selected "Applications"
1013 String applicationName = getResources().getString(R.string.group_applications);
1014 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001015
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001016 if (applicationName != null && applicationName.equals(shortcutName)) {
1017 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1018 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001019
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001020 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1021 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001022 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001023 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001024 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
1027
1028 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001029 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001030 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001031 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001032
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001033 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001034 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001035 } else {
1036 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 }
1039
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 UserFolderInfo folderInfo = new UserFolderInfo();
1042 folderInfo.title = getText(R.string.folder_name);
1043
1044 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1045 cellInfo.screen = mWorkspace.getCurrentScreen();
1046 if (!findSingleSlot(cellInfo)) return;
1047
1048 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001049 LauncherModel.addItemToDatabase(this, folderInfo,
1050 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001052 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053
1054 // Create the view
1055 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1056 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1057 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001058 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 }
Romain Guycbb89e42009-06-08 15:52:54 -07001060
Joe Onorato9c1289c2009-08-17 11:03:03 -04001061 void removeFolder(FolderInfo folder) {
1062 mFolders.remove(folder.id);
1063 }
1064
1065 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 cellInfo.screen = mWorkspace.getCurrentScreen();
1067 if (!findSingleSlot(cellInfo)) return;
1068
1069 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1070
1071 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1073 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1075 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 }
1077 }
1078
1079 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1080 CellLayout.CellInfo cellInfo, boolean notify) {
1081
1082 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1083 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1084
1085 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 Intent.ShortcutIconResource iconResource = null;
1087
1088 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1089 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1090 try {
1091 iconResource = (Intent.ShortcutIconResource) extra;
1092 final PackageManager packageManager = context.getPackageManager();
1093 Resources resources = packageManager.getResourcesForApplication(
1094 iconResource.packageName);
1095 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1096 icon = resources.getDrawable(id);
1097 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001098 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 }
1100 }
1101
1102 if (icon == null) {
1103 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1104 }
1105
1106 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001107 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 info.title = name;
1109 info.iconResource = iconResource;
1110 info.uri = data.getData();
1111 info.baseIntent = baseIntent;
1112 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1113 LiveFolders.DISPLAY_MODE_GRID);
1114
1115 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1116 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001117 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118
1119 return info;
1120 }
1121
1122 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1123 final int[] xy = new int[2];
1124 if (findSlot(cellInfo, xy, 1, 1)) {
1125 cellInfo.cellX = xy[0];
1126 cellInfo.cellY = xy[1];
1127 return true;
1128 }
1129 return false;
1130 }
1131
1132 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1133 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1134 boolean[] occupied = mSavedState != null ?
1135 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1136 cellInfo = mWorkspace.findAllVacantCells(occupied);
1137 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1138 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1139 return false;
1140 }
1141 }
1142 return true;
1143 }
1144
1145 private void showNotifications() {
1146 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1147 if (statusBar != null) {
1148 statusBar.expand();
1149 }
1150 }
1151
1152 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001153 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001155 Intent chooser = Intent.createChooser(pickWallpaper,
1156 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001157 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1158 // Removed in Eclair MR1
1159// WallpaperManager wm = (WallpaperManager)
1160// getSystemService(Context.WALLPAPER_SERVICE);
1161// WallpaperInfo wi = wm.getWallpaperInfo();
1162// if (wi != null && wi.getSettingsActivity() != null) {
1163// LabeledIntent li = new LabeledIntent(getPackageName(),
1164// R.string.configure_wallpaper, 0);
1165// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1166// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1167// }
Mike Clerona0618e42009-10-22 13:55:21 -07001168 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001171 /**
1172 * Registers various content observers. The current implementation registers
1173 * only a favorites observer to keep track of the favorites applications.
1174 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001175 private void registerContentObservers() {
1176 ContentResolver resolver = getContentResolver();
1177 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1178 true, mWidgetObserver);
1179 }
1180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 @Override
1182 public boolean dispatchKeyEvent(KeyEvent event) {
1183 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1184 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001186 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001187 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001188 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001189 dumpState();
1190 return true;
1191 }
1192 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001193 }
1194 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1195 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001196 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 return true;
1198 }
1199 }
1200
1201 return super.dispatchKeyEvent(event);
1202 }
1203
Joe Onorato88ec0992009-11-19 13:16:06 -08001204 @Override
1205 public void onBackPressed() {
1206 if (isAllAppsVisible()) {
1207 closeAllApps(true);
1208 } else {
1209 closeFolder();
1210 }
1211 dismissPreview(mPreviousView);
1212 dismissPreview(mNextView);
1213 }
1214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 private void closeFolder() {
1216 Folder folder = mWorkspace.getOpenFolder();
1217 if (folder != null) {
1218 closeFolder(folder);
1219 }
1220 }
1221
1222 void closeFolder(Folder folder) {
1223 folder.getInfo().opened = false;
1224 ViewGroup parent = (ViewGroup) folder.getParent();
1225 if (parent != null) {
1226 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001227 if (folder instanceof DropTarget) {
1228 // Live folders aren't DropTargets.
1229 mDragController.removeDropTarget((DropTarget)folder);
1230 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 }
1232 folder.onClose();
1233 }
1234
1235 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001236 * Re-listen when widgets are reset.
1237 */
1238 private void onAppWidgetReset() {
1239 mAppWidgetHost.startListening();
1240 }
1241
1242 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001243 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1244 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001246 private void unbindDesktopItems() {
1247 for (ItemInfo item: mDesktopItems) {
1248 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
1250 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 /**
1253 * Launches the intent referred by the clicked shortcut.
1254 *
1255 * @param v The view representing the clicked shortcut.
1256 */
1257 public void onClick(View v) {
1258 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001259 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 // Open shortcut
Joe Onorato0589f0f2010-02-08 13:44:00 -08001261 final Intent intent = ((ShortcutInfo)tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001262 int[] pos = new int[2];
1263 v.getLocationOnScreen(pos);
1264 intent.setSourceBounds(
1265 new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 startActivitySafely(intent);
1267 } else if (tag instanceof FolderInfo) {
1268 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001269 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001270 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001271 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001272 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001273 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 }
1276 }
1277
1278 void startActivitySafely(Intent intent) {
1279 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1280 try {
1281 startActivity(intent);
1282 } catch (ActivityNotFoundException e) {
1283 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1284 } catch (SecurityException e) {
1285 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001286 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1288 "or use the exported attribute for this activity.", e);
1289 }
1290 }
1291
1292 private void handleFolderClick(FolderInfo folderInfo) {
1293 if (!folderInfo.opened) {
1294 // Close any open folder
1295 closeFolder();
1296 // Open the requested folder
1297 openFolder(folderInfo);
1298 } else {
1299 // Find the open folder...
1300 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1301 int folderScreen;
1302 if (openFolder != null) {
1303 folderScreen = mWorkspace.getScreenForView(openFolder);
1304 // .. and close it
1305 closeFolder(openFolder);
1306 if (folderScreen != mWorkspace.getCurrentScreen()) {
1307 // Close any folder open on the current screen
1308 closeFolder();
1309 // Pull the folder onto this screen
1310 openFolder(folderInfo);
1311 }
1312 }
1313 }
1314 }
1315
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 /**
1317 * Opens the user fodler described by the specified tag. The opening of the folder
1318 * is animated relative to the specified View. If the View is null, no animation
1319 * is played.
1320 *
1321 * @param folderInfo The FolderInfo describing the folder to open.
1322 */
1323 private void openFolder(FolderInfo folderInfo) {
1324 Folder openFolder;
1325
1326 if (folderInfo instanceof UserFolderInfo) {
1327 openFolder = UserFolder.fromXml(this);
1328 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001329 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 } else {
1331 return;
1332 }
1333
Joe Onorato00acb122009-08-04 16:04:30 -04001334 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 openFolder.setLauncher(this);
1336
1337 openFolder.bind(folderInfo);
1338 folderInfo.opened = true;
1339
1340 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1341 openFolder.onOpen();
1342 }
1343
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001345 switch (v.getId()) {
1346 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001347 if (!isAllAppsVisible()) {
1348 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1349 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001350 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001351 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001352 return true;
1353 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001354 if (!isAllAppsVisible()) {
1355 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1356 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001357 showPreviews(v);
1358 }
1359 return true;
1360 case R.id.all_apps_button:
1361 if (!isAllAppsVisible()) {
1362 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1363 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1364 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001365 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001366 return true;
1367 }
1368
Joe Onorato9c1289c2009-08-17 11:03:03 -04001369 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 return false;
1371 }
1372
1373 if (!(v instanceof CellLayout)) {
1374 v = (View) v.getParent();
1375 }
1376
1377 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1378
1379 // This happens when long clicking an item with the dpad/trackball
1380 if (cellInfo == null) {
1381 return true;
1382 }
1383
1384 if (mWorkspace.allowLongPress()) {
1385 if (cellInfo.cell == null) {
1386 if (cellInfo.valid) {
1387 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001388 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001389 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1390 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 showAddDialog(cellInfo);
1392 }
1393 } else {
1394 if (!(cellInfo.cell instanceof Folder)) {
1395 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001396 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1397 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 mWorkspace.startDrag(cellInfo);
1399 }
1400 }
1401 }
1402 return true;
1403 }
1404
Romain Guye6b8e2f2009-11-10 11:56:55 -08001405 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001406 private void dismissPreview(final View v) {
1407 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001408 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001409 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1410 public void onDismiss() {
1411 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1412 int count = group.getChildCount();
1413 for (int i = 0; i < count; i++) {
1414 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1415 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001416 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1417 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1418
1419 v.setTag(R.id.workspace, null);
1420 v.setTag(R.id.icon, null);
1421 window.setOnDismissListener(null);
1422 }
1423 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001424 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001425 }
1426 v.setTag(null);
1427 }
1428
Romain Guyf8e6a802009-12-07 17:48:02 -08001429 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001430 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001431 }
1432
Romain Guya6abce82009-11-10 02:54:41 -08001433 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001434 final Resources resources = getResources();
1435 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001436
Romain Guya6abce82009-11-10 02:54:41 -08001437 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001438
1439 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001440
Romain Guyf8e6a802009-12-07 17:48:02 -08001441 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001442 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001443 int extraW = (int) ((r.left + r.right) * max);
1444 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001445
1446 int aW = cell.getWidth() - extraW;
1447 float w = aW / max;
1448
1449 int width = cell.getWidth();
1450 int height = cell.getHeight();
1451 int x = cell.getLeftPadding();
1452 int y = cell.getTopPadding();
1453 width -= (x + cell.getRightPadding());
1454 height -= (y + cell.getBottomPadding());
1455
1456 float scale = w / width;
1457
1458 int count = end - start;
1459
1460 final float sWidth = width * scale;
1461 float sHeight = height * scale;
1462
Romain Guye6b8e2f2009-11-10 11:56:55 -08001463 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001464
Romain Guye6b8e2f2009-11-10 11:56:55 -08001465 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1466 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001467
1468 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001469 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001470 cell = (CellLayout) workspace.getChildAt(i);
1471
Romain Guyf8e6a802009-12-07 17:48:02 -08001472 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001473 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001474
1475 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001476 c.scale(scale, scale);
1477 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1478 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001479
Romain Guy9d31e9f2009-11-11 18:54:28 -08001480 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001481 image.setImageBitmap(bitmap);
1482 image.setTag(i);
1483 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001484 image.setOnFocusChangeListener(handler);
1485 image.setFocusable(true);
1486 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001487
1488 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001489 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1490
1491 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001492 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001493
1494 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001495 p.setContentView(preview);
1496 p.setWidth((int) (sWidth * count + extraW));
1497 p.setHeight((int) (sHeight + extraH));
1498 p.setAnimationStyle(R.style.AnimationPreview);
1499 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001500 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001501 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001502 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001503
Romain Guye6b8e2f2009-11-10 11:56:55 -08001504 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1505 public void onDismiss() {
1506 dismissPreview(anchor);
1507 }
1508 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001509
1510 anchor.setTag(p);
1511 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001512 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001513 }
1514
Romain Guy9d31e9f2009-11-11 18:54:28 -08001515 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001516 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001517
Romain Guye6b8e2f2009-11-10 11:56:55 -08001518 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001519 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001520 }
1521
1522 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001523 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001524 v.post(this);
1525 }
1526
1527 public void run() {
1528 dismissPreview(mAnchor);
1529 }
1530
1531 public void onFocusChange(View v, boolean hasFocus) {
1532 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001533 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001534 }
Romain Guya6abce82009-11-10 02:54:41 -08001535 }
1536 }
1537
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 Workspace getWorkspace() {
1539 return mWorkspace;
1540 }
1541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 @Override
1543 protected Dialog onCreateDialog(int id) {
1544 switch (id) {
1545 case DIALOG_CREATE_SHORTCUT:
1546 return new CreateShortcut().createDialog();
1547 case DIALOG_RENAME_FOLDER:
1548 return new RenameFolder().createDialog();
1549 }
1550
1551 return super.onCreateDialog(id);
1552 }
1553
1554 @Override
1555 protected void onPrepareDialog(int id, Dialog dialog) {
1556 switch (id) {
1557 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 break;
1559 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001560 if (mFolderInfo != null) {
1561 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1562 final CharSequence text = mFolderInfo.title;
1563 input.setText(text);
1564 input.setSelection(0, text.length());
1565 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 break;
1567 }
1568 }
1569
1570 void showRenameDialog(FolderInfo info) {
1571 mFolderInfo = info;
1572 mWaitingForResult = true;
1573 showDialog(DIALOG_RENAME_FOLDER);
1574 }
1575
1576 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1577 mAddItemCellInfo = cellInfo;
1578 mWaitingForResult = true;
1579 showDialog(DIALOG_CREATE_SHORTCUT);
1580 }
1581
Romain Guy73b979d2009-06-09 12:57:21 -07001582 private void pickShortcut(int requestCode, int title) {
1583 Bundle bundle = new Bundle();
1584
1585 ArrayList<String> shortcutNames = new ArrayList<String>();
1586 shortcutNames.add(getString(R.string.group_applications));
1587 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1588
1589 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1590 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1591 R.drawable.ic_launcher_application));
1592 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1593
1594 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1595 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1596 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1597 pickIntent.putExtras(bundle);
1598
1599 startActivityForResult(pickIntent, requestCode);
1600 }
1601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 private class RenameFolder {
1603 private EditText mInput;
1604
1605 Dialog createDialog() {
1606 mWaitingForResult = true;
1607 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1608 mInput = (EditText) layout.findViewById(R.id.folder_name);
1609
1610 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1611 builder.setIcon(0);
1612 builder.setTitle(getString(R.string.rename_folder_title));
1613 builder.setCancelable(true);
1614 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1615 public void onCancel(DialogInterface dialog) {
1616 cleanup();
1617 }
1618 });
1619 builder.setNegativeButton(getString(R.string.cancel_action),
1620 new Dialog.OnClickListener() {
1621 public void onClick(DialogInterface dialog, int which) {
1622 cleanup();
1623 }
1624 }
1625 );
1626 builder.setPositiveButton(getString(R.string.rename_action),
1627 new Dialog.OnClickListener() {
1628 public void onClick(DialogInterface dialog, int which) {
1629 changeFolderName();
1630 }
1631 }
1632 );
1633 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001634
1635 final AlertDialog dialog = builder.create();
1636 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1637 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001638 mInput.requestFocus();
1639 InputMethodManager inputManager = (InputMethodManager)
1640 getSystemService(Context.INPUT_METHOD_SERVICE);
1641 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001642 }
1643 });
1644
1645 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 }
1647
1648 private void changeFolderName() {
1649 final String name = mInput.getText().toString();
1650 if (!TextUtils.isEmpty(name)) {
1651 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001652 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 mFolderInfo.title = name;
1654 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1655
Joe Onorato9c1289c2009-08-17 11:03:03 -04001656 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001657 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658 mModel.setWorkspaceDirty();
1659 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 } else {
1661 final FolderIcon folderIcon = (FolderIcon)
1662 mWorkspace.getViewForTag(mFolderInfo);
1663 if (folderIcon != null) {
1664 folderIcon.setText(name);
1665 getWorkspace().requestLayout();
1666 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001667 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001668 mModel.setWorkspaceDirty();
1669 mWorkspaceLoading = true;
1670 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 }
1672 }
1673 }
1674 cleanup();
1675 }
1676
1677 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 dismissDialog(DIALOG_RENAME_FOLDER);
1679 mWaitingForResult = false;
1680 mFolderInfo = null;
1681 }
1682 }
1683
Joe Onoratofb0ca672009-09-14 17:55:46 -04001684 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001685 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001686 }
1687
Joe Onorato3a8820b2009-11-10 15:06:42 -08001688 void showAllApps(boolean animated) {
1689 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001690
Mike Cleronb6082fa02009-10-19 17:03:36 -07001691 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001692 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001693
Joe Onorato7c312c12009-08-13 21:36:53 -07001694 // TODO: fade these two too
1695 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001696 }
1697
Joe Onoratob2061212009-11-24 16:13:54 -05001698 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001699 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001700 * - Home from workspace
1701 * - from center screen
1702 * - from other screens
1703 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001704 * - from center screen
1705 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001706 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001707 * - from center screen
1708 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001709 * - Launch app from workspace and quit
1710 * - with back
1711 * - with home
1712 * - Launch app from all apps and quit
1713 * - with back
1714 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001715 * - Go to a screen that's not the default, then all
1716 * apps, and launch and app, and go back
1717 * - with back
1718 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001719 * - On workspace, long press power and go back
1720 * - with back
1721 * - with home
1722 * - On all apps, long press power and go back
1723 * - with back
1724 * - with home
1725 * - On workspace, power off
1726 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001727 * - Launch an app and turn off the screen while in that app
1728 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001729 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001730 * - From all apps
1731 * - From workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001732 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001733 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001734 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001735 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001736 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001737 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001738 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001739 }
1740
Joe Onorato7c312c12009-08-13 21:36:53 -07001741 void lockAllApps() {
1742 // TODO
1743 }
1744
1745 void unlockAllApps() {
1746 // TODO
1747 }
1748
Joe Onorato7404ee42009-07-31 11:54:44 -07001749 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 * Displays the shortcut creation dialog and launches, if necessary, the
1751 * appropriate activity.
1752 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001753 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001754 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1755 DialogInterface.OnShowListener {
1756
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001758
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 Dialog createDialog() {
1760 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001761
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001763
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1765 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001766 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001767
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 builder.setInverseBackgroundForced(true);
1769
1770 AlertDialog dialog = builder.create();
1771 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001772 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001773 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 return dialog;
1776 }
1777
1778 public void onCancel(DialogInterface dialog) {
1779 mWaitingForResult = false;
1780 cleanup();
1781 }
1782
Romain Guycbb89e42009-06-08 15:52:54 -07001783 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001784 }
1785
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001787 try {
1788 dismissDialog(DIALOG_CREATE_SHORTCUT);
1789 } catch (Exception e) {
1790 // An exception is thrown if the dialog is not visible, which is fine
1791 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 }
1793
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001794 /**
1795 * Handle the action clicked in the "Add to home" dialog.
1796 */
1797 public void onClick(DialogInterface dialog, int which) {
1798 Resources res = getResources();
1799 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001800
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001801 switch (which) {
1802 case AddAdapter.ITEM_SHORTCUT: {
1803 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001804 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001805 break;
1806 }
Romain Guycbb89e42009-06-08 15:52:54 -07001807
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001808 case AddAdapter.ITEM_APPWIDGET: {
1809 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001810
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001811 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1812 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001813 // start the pick activity
1814 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1815 break;
1816 }
Romain Guycbb89e42009-06-08 15:52:54 -07001817
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001818 case AddAdapter.ITEM_LIVE_FOLDER: {
1819 // Insert extra item to handle inserting folder
1820 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001821
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001822 ArrayList<String> shortcutNames = new ArrayList<String>();
1823 shortcutNames.add(res.getString(R.string.group_folder));
1824 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001825
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001826 ArrayList<ShortcutIconResource> shortcutIcons =
1827 new ArrayList<ShortcutIconResource>();
1828 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1829 R.drawable.ic_launcher_folder));
1830 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1831
1832 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1833 pickIntent.putExtra(Intent.EXTRA_INTENT,
1834 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1835 pickIntent.putExtra(Intent.EXTRA_TITLE,
1836 getText(R.string.title_select_live_folder));
1837 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001838
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001839 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1840 break;
1841 }
1842
1843 case AddAdapter.ITEM_WALLPAPER: {
1844 startWallpaper();
1845 break;
1846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 }
1848 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001849
1850 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001851 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 }
1853
1854 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001855 * Receives notifications when applications are added/removed.
1856 */
1857 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1858 @Override
1859 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001860 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05001861 String reason = intent.getStringExtra("reason");
1862 if (!"homekey".equals(reason)) {
1863 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001864 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05001865 animate = false;
1866 }
Joe Onoratob2061212009-11-24 16:13:54 -05001867 closeAllApps(animate);
1868 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001869 }
1870 }
1871
1872 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001873 * Receives notifications whenever the appwidgets are reset.
1874 */
1875 private class AppWidgetResetObserver extends ContentObserver {
1876 public AppWidgetResetObserver() {
1877 super(new Handler());
1878 }
1879
1880 @Override
1881 public void onChange(boolean selfChange) {
1882 onAppWidgetReset();
1883 }
1884 }
1885
1886 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001889 public int getCurrentWorkspaceScreen() {
1890 return mWorkspace.getCurrentScreen();
1891 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001892
Joe Onorato9c1289c2009-08-17 11:03:03 -04001893 /**
1894 * Refreshes the shortcuts shown on the workspace.
1895 *
1896 * Implementation of the method from LauncherModel.Callbacks.
1897 */
1898 public void startBinding() {
1899 final Workspace workspace = mWorkspace;
1900 int count = workspace.getChildCount();
1901 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001902 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001903 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1904 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001905
Joe Onorato9c1289c2009-08-17 11:03:03 -04001906 if (DEBUG_USER_INTERFACE) {
1907 android.widget.Button finishButton = new android.widget.Button(this);
1908 finishButton.setText("Finish");
1909 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1910
1911 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1912 public void onClick(View v) {
1913 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001914 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001915 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917 }
1918
1919 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001920 * Bind the items start-end from the list.
1921 *
1922 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1925
1926 final Workspace workspace = mWorkspace;
1927
1928 for (int i=start; i<end; i++) {
1929 final ItemInfo item = shortcuts.get(i);
1930 mDesktopItems.add(item);
1931 switch (item.itemType) {
1932 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1933 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001934 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001935 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1936 false);
1937 break;
1938 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1939 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1940 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1941 (UserFolderInfo) item);
1942 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1943 false);
1944 break;
1945 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1946 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1947 R.layout.live_folder_icon, this,
1948 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1949 (LiveFolderInfo) item);
1950 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1951 false);
1952 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955
Joe Onorato9c1289c2009-08-17 11:03:03 -04001956 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
1958
Joe Onorato9c1289c2009-08-17 11:03:03 -04001959 /**
1960 * Implementation of the method from LauncherModel.Callbacks.
1961 */
Joe Onoratoad72e172009-11-06 16:25:04 -05001962 public void bindFolders(HashMap<Long, FolderInfo> folders) {
1963 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001964 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001966
1967 /**
1968 * Add the views for a widget to the workspace.
1969 *
1970 * Implementation of the method from LauncherModel.Callbacks.
1971 */
1972 public void bindAppWidget(LauncherAppWidgetInfo item) {
1973 final Workspace workspace = mWorkspace;
1974
1975 final int appWidgetId = item.appWidgetId;
1976 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1977 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1978
Joe Onorato9c1289c2009-08-17 11:03:03 -04001979 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1980 item.hostView.setTag(item);
1981
1982 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1983 item.cellY, item.spanX, item.spanY, false);
1984
1985 workspace.requestLayout();
1986
1987 mDesktopItems.add(item);
1988 }
1989
1990 /**
1991 * Callback saying that there aren't any more items to bind.
1992 *
1993 * Implementation of the method from LauncherModel.Callbacks.
1994 */
1995 public void finishBindingItems() {
1996 if (mSavedState != null) {
1997 if (!mWorkspace.hasFocus()) {
1998 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1999 }
2000
2001 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2002 if (userFolders != null) {
2003 for (long folderId : userFolders) {
2004 final FolderInfo info = mFolders.get(folderId);
2005 if (info != null) {
2006 openFolder(info);
2007 }
2008 }
2009 final Folder openFolder = mWorkspace.getOpenFolder();
2010 if (openFolder != null) {
2011 openFolder.requestFocus();
2012 }
2013 }
2014
Joe Onorato9c1289c2009-08-17 11:03:03 -04002015 mSavedState = null;
2016 }
2017
2018 if (mSavedInstanceState != null) {
2019 super.onRestoreInstanceState(mSavedInstanceState);
2020 mSavedInstanceState = null;
2021 }
2022
Joe Onorato9c1289c2009-08-17 11:03:03 -04002023 mWorkspaceLoading = false;
2024 }
2025
2026 /**
2027 * Add the icons for all apps.
2028 *
2029 * Implementation of the method from LauncherModel.Callbacks.
2030 */
2031 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002032 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002033 }
2034
2035 /**
2036 * A package was installed.
2037 *
2038 * Implementation of the method from LauncherModel.Callbacks.
2039 */
2040 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2041 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002042 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002043 }
2044
2045 /**
2046 * A package was updated.
2047 *
2048 * Implementation of the method from LauncherModel.Callbacks.
2049 */
2050 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2051 removeDialog(DIALOG_CREATE_SHORTCUT);
2052 mWorkspace.updateShortcutsForPackage(packageName);
Joe Onoratof0be2132009-11-24 19:34:29 -05002053 mAllAppsGrid.updateApps(packageName, apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002054 }
2055
2056 /**
2057 * A package was uninstalled.
2058 *
2059 * Implementation of the method from LauncherModel.Callbacks.
2060 */
2061 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2062 removeDialog(DIALOG_CREATE_SHORTCUT);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002063 mWorkspace.removeItemsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002064 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002065 }
Joe Onoratobe386092009-11-17 17:32:16 -08002066
2067 /**
2068 * Prints out out state for debugging.
2069 */
2070 public void dumpState() {
2071 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002072 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002073 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2074 Log.d(TAG, "mRestoring=" + mRestoring);
2075 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2076 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2077 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2078 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002079 mModel.dumpState();
2080 mAllAppsGrid.dumpState();
2081 Log.d(TAG, "END launcher2 dump state");
2082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083}