blob: df2dd6655548419938033adddfc0362eceb74b01 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000019import com.android.common.Search;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Activity;
22import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.Dialog;
24import android.app.SearchManager;
25import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080028import android.content.BroadcastReceiver;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080029import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070033import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070037import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080040import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080041import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080043import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080045import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Parcelable;
Joe Onoratobe386092009-11-17 17:32:16 -080047import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.text.Selection;
50import android.text.SpannableStringBuilder;
51import android.text.TextUtils;
52import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070053import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080055import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.view.KeyEvent;
57import android.view.LayoutInflater;
58import android.view.Menu;
59import android.view.MenuItem;
60import android.view.View;
61import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.view.View.OnLongClickListener;
63import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.widget.TextView;
66import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080067import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080068import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080069import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070070import android.appwidget.AppWidgetManager;
71import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070075import java.io.DataOutputStream;
76import java.io.FileNotFoundException;
77import java.io.IOException;
78import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
Romain Guyedcce092010-03-04 13:03:17 -080080import com.android.launcher.R;
81
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082/**
83 * Default launcher application.
84 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040085public final class Launcher extends Activity
Daniel Sandlerc351eb82010-03-03 15:05:19 -050086 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080087 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 static final boolean LOGD = false;
89
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 static final boolean PROFILE_STARTUP = false;
91 static final boolean PROFILE_ROTATE = false;
92 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 private static final int WALLPAPER_SCREENS_SPAN = 2;
95
96 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -080097 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
98
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 private static final int MENU_ADD = Menu.FIRST + 1;
100 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
101 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
102 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700103 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104
105 private static final int REQUEST_CREATE_SHORTCUT = 1;
106 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700107 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 private static final int REQUEST_PICK_APPLICATION = 6;
109 private static final int REQUEST_PICK_SHORTCUT = 7;
110 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700111 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700112 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
114 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
115
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800116 static final int SCREEN_COUNT = 5;
117 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700119 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Joe Onorato7c312c12009-08-13 21:36:53 -0700121 static final int DIALOG_CREATE_SHORTCUT = 1;
122 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Romain Guy98d01652009-06-30 16:21:04 -0700124 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 // Type: int
127 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
128 // Type: boolean
129 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
130 // Type: long
131 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
146 // Type: int[]
147 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
148 // Type: boolean
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
150 // Type: long
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
152
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800156 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800158 private final BroadcastReceiver mCloseSystemDialogsReceiver
159 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800160 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private LayoutInflater mInflater;
163
Joe Onorato00acb122009-08-04 16:04:30 -0400164 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700166
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private AppWidgetManager mAppWidgetManager;
168 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private CellLayout.CellInfo mAddItemCellInfo;
171 private CellLayout.CellInfo mMenuAddInfo;
172 private final int[] mCellCoordinates = new int[2];
173 private FolderInfo mFolderInfo;
174
Joe Onorato7c312c12009-08-13 21:36:53 -0700175 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700176 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700177 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private Bundle mSavedState;
180
181 private SpannableStringBuilder mDefaultKeySsb = null;
182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 private boolean mWorkspaceLoading = true;
184
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800185 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private boolean mRestoring;
187 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
189 private Bundle mSavedInstanceState;
190
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800192 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193
Romain Guy84f296c2009-11-04 15:00:44 -0800194 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
195 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700196
Romain Guy1fbc1c82009-11-09 20:43:08 -0800197 private ImageView mPreviousView;
198 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 @Override
201 protected void onCreate(Bundle savedInstanceState) {
202 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700203
Joe Onorato0589f0f2010-02-08 13:44:00 -0800204 LauncherApplication app = ((LauncherApplication)getApplication());
205 mModel = app.setLauncher(this);
206 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400207 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700209
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700211 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
212 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 if (PROFILE_STARTUP) {
215 android.os.Debug.startMethodTracing("/sdcard/launcher");
216 }
217
218 checkForLocaleChange();
219 setWallpaperDimension();
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 setContentView(R.layout.launcher);
222 setupViews();
223
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800224 registerContentObservers();
225
Joe Onorato7c312c12009-08-13 21:36:53 -0700226 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 mSavedState = savedInstanceState;
229 restoreState(mSavedState);
230
231 if (PROFILE_STARTUP) {
232 android.os.Debug.stopMethodTracing();
233 }
234
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700235 // We have a new AllAppsView, we need to re-bind everything, and it could have
236 // changed in our absence.
237 mModel.setAllAppsDirty();
238 mModel.setWorkspaceDirty();
239
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 }
243
244 // For handling default keys
245 mDefaultKeySsb = new SpannableStringBuilder();
246 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800247
248 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
249 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 }
Romain Guycbb89e42009-06-08 15:52:54 -0700251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700253 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
254 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700255
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 final Configuration configuration = getResources().getConfiguration();
257
Romain Guy98d01652009-06-30 16:21:04 -0700258 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 final String locale = configuration.locale.toString();
260
Romain Guy98d01652009-06-30 16:21:04 -0700261 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 final int mcc = configuration.mcc;
263
Romain Guy98d01652009-06-30 16:21:04 -0700264 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 final int mnc = configuration.mnc;
266
Romain Guy84f296c2009-11-04 15:00:44 -0800267 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
Romain Guy84f296c2009-11-04 15:00:44 -0800269 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700270 localeConfiguration.locale = locale;
271 localeConfiguration.mcc = mcc;
272 localeConfiguration.mnc = mnc;
273
274 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800275 mIconCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700276 }
277 }
278
279 private static class LocaleConfiguration {
280 public String locale;
281 public int mcc = -1;
282 public int mnc = -1;
283 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700284
Romain Guy98d01652009-06-30 16:21:04 -0700285 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
286 DataInputStream in = null;
287 try {
288 in = new DataInputStream(context.openFileInput(PREFERENCES));
289 configuration.locale = in.readUTF();
290 configuration.mcc = in.readInt();
291 configuration.mnc = in.readInt();
292 } catch (FileNotFoundException e) {
293 // Ignore
294 } catch (IOException e) {
295 // Ignore
296 } finally {
297 if (in != null) {
298 try {
299 in.close();
300 } catch (IOException e) {
301 // Ignore
302 }
303 }
304 }
305 }
306
307 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
308 DataOutputStream out = null;
309 try {
310 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
311 out.writeUTF(configuration.locale);
312 out.writeInt(configuration.mcc);
313 out.writeInt(configuration.mnc);
314 out.flush();
315 } catch (FileNotFoundException e) {
316 // Ignore
317 } catch (IOException e) {
318 //noinspection ResultOfMethodCallIgnored
319 context.getFileStreamPath(PREFERENCES).delete();
320 } finally {
321 if (out != null) {
322 try {
323 out.close();
324 } catch (IOException e) {
325 // Ignore
326 }
327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 }
329 }
330
331 static int getScreen() {
332 synchronized (sLock) {
333 return sScreen;
334 }
335 }
336
337 static void setScreen(int screen) {
338 synchronized (sLock) {
339 sScreen = screen;
340 }
341 }
342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700344 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345
346 Display display = getWindowManager().getDefaultDisplay();
347 boolean isPortrait = display.getWidth() < display.getHeight();
348
349 final int width = isPortrait ? display.getWidth() : display.getHeight();
350 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700351 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 }
353
354 @Override
355 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700356 mWaitingForResult = false;
357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 // The pattern used here is that a user PICKs a specific application,
359 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700360
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
362 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700363
Romain Guy94dabf12009-07-21 10:55:43 -0700364 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 switch (requestCode) {
366 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 break;
369 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800370 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 break;
372 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 break;
375 case REQUEST_PICK_LIVE_FOLDER:
376 addLiveFolder(data);
377 break;
378 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700381 case REQUEST_PICK_APPWIDGET:
382 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700384 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400385 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700387 case REQUEST_PICK_WALLPAPER:
388 // We just wanted the activity result here so we can clear mWaitingForResult
389 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
Romain Guy18042c82009-11-06 11:44:55 -0800391 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
392 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
393 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700394 // Clean up the appWidgetId if we canceled
395 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
396 if (appWidgetId != -1) {
397 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 }
401
402 @Override
403 protected void onResume() {
404 super.onResume();
405
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800406 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400409 mWorkspaceLoading = true;
410 mModel.startLoader(this, true);
411 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 }
413 }
414
415 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700416 protected void onPause() {
417 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800418 dismissPreview(mPreviousView);
419 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800420 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700421 }
Romain Guycbb89e42009-06-08 15:52:54 -0700422
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700423 @Override
424 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425 // Flag the loader to stop early before switching
426 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700428 if (PROFILE_ROTATE) {
429 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
430 }
431 return null;
432 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800434 // We can't hide the IME if it was forced open. So don't bother
435 /*
436 @Override
437 public void onWindowFocusChanged(boolean hasFocus) {
438 super.onWindowFocusChanged(hasFocus);
439
440 if (hasFocus) {
441 final InputMethodManager inputManager = (InputMethodManager)
442 getSystemService(Context.INPUT_METHOD_SERVICE);
443 WindowManager.LayoutParams lp = getWindow().getAttributes();
444 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
445 android.os.Handler()) {
446 protected void onReceiveResult(int resultCode, Bundle resultData) {
447 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
448 }
449 });
450 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
451 }
452 }
453 */
454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 private boolean acceptFilter() {
456 final InputMethodManager inputManager = (InputMethodManager)
457 getSystemService(Context.INPUT_METHOD_SERVICE);
458 return !inputManager.isFullscreenMode();
459 }
460
461 @Override
462 public boolean onKeyDown(int keyCode, KeyEvent event) {
463 boolean handled = super.onKeyDown(keyCode, event);
464 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
465 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
466 keyCode, event);
467 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700468 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700469 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700470 // showSearchDialog()
471 // If there are multiple keystrokes before the search dialog takes focus,
472 // onSearchRequested() will be called for every keystroke,
473 // but it is idempotent, so it's fine.
474 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 }
476 }
477
Joe Onorato8a9625e2010-01-28 15:55:35 -0800478 // Eat the long press event so the keyboard doesn't come up.
479 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
480 return true;
481 }
482
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 return handled;
484 }
485
Karl Rosaen138a0412009-04-23 19:00:21 -0700486 private String getTypedText() {
487 return mDefaultKeySsb.toString();
488 }
489
490 private void clearTypedText() {
491 mDefaultKeySsb.clear();
492 mDefaultKeySsb.clearSpans();
493 Selection.setSelection(mDefaultKeySsb, 0);
494 }
495
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 /**
497 * Restores the previous state, if it exists.
498 *
499 * @param savedState The previous state.
500 */
501 private void restoreState(Bundle savedState) {
502 if (savedState == null) {
503 return;
504 }
505
Joe Onorato3a8820b2009-11-10 15:06:42 -0800506 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
507 if (allApps) {
508 showAllApps(false);
509 }
510
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
512 if (currentScreen > -1) {
513 mWorkspace.setCurrentScreen(currentScreen);
514 }
515
516 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
517 if (addScreen > -1) {
518 mAddItemCellInfo = new CellLayout.CellInfo();
519 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
520 addItemCellInfo.valid = true;
521 addItemCellInfo.screen = addScreen;
522 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
523 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
524 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
525 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
526 addItemCellInfo.findVacantCellsFromOccupied(
527 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
528 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
529 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
530 mRestoring = true;
531 }
532
533 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
534 if (renameFolder) {
535 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400536 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 mRestoring = true;
538 }
539 }
540
541 /**
542 * Finds all the views we need and configure them properly.
543 */
544 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400545 DragController dragController = mDragController;
546
Romain Guyb1b69f52009-08-10 15:10:15 -0700547 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400548 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549
Joe Onorato7c312c12009-08-13 21:36:53 -0700550 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700551 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700552 mAllAppsGrid.setDragController(dragController);
Daniel Sandler388f6792010-03-02 14:08:08 -0500553 ((View)mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700554 // Manage focusability manually since this thing is always visible
Daniel Sandler388f6792010-03-02 14:08:08 -0500555 ((View)mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700556
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
558 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500559 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560
Joe Onorato7c312c12009-08-13 21:36:53 -0700561 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
562 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563
Joe Onorato7c312c12009-08-13 21:36:53 -0700564 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700565 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700566 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800567 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800568
569 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
570 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
571
572 Drawable previous = mPreviousView.getDrawable();
573 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800574 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575
Joe Onorato0d44e942009-11-16 18:20:51 -0800576 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800577 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800578 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800579 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400582 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584
585 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400586 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700587 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588
Joe Onorato00acb122009-08-04 16:04:30 -0400589 dragController.setDragScoller(workspace);
590 dragController.setDragListener(deleteZone);
591 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800592 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700593
Joe Onorato00acb122009-08-04 16:04:30 -0400594 // The order here is bottom to top.
595 dragController.addDropTarget(workspace);
596 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 }
598
Romain Guy8a73c512009-11-09 19:19:59 -0800599 @SuppressWarnings({"UnusedDeclaration"})
600 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800601 if (!isAllAppsVisible()) {
602 mWorkspace.scrollLeft();
603 }
Romain Guy8a73c512009-11-09 19:19:59 -0800604 }
605
606 @SuppressWarnings({"UnusedDeclaration"})
607 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800608 if (!isAllAppsVisible()) {
609 mWorkspace.scrollRight();
610 }
Romain Guy8a73c512009-11-09 19:19:59 -0800611 }
612
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 /**
614 * Creates a view representing a shortcut.
615 *
616 * @param info The data structure describing the shortcut.
617 *
618 * @return A View inflated from R.layout.application.
619 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800620 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 return createShortcut(R.layout.application,
622 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
623 }
624
625 /**
626 * Creates a view representing a shortcut inflated from the specified resource.
627 *
628 * @param layoutResId The id of the XML layout used to create the shortcut.
629 * @param parent The group the shortcut belongs to.
630 * @param info The data structure describing the shortcut.
631 *
632 * @return A View inflated from layoutResId.
633 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800634 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
636
Joe Onorato0589f0f2010-02-08 13:44:00 -0800637 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
638 new FastBitmapDrawable(info.getIcon(mIconCache)),
639 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 favorite.setText(info.title);
641 favorite.setTag(info);
642 favorite.setOnClickListener(this);
643
644 return favorite;
645 }
646
647 /**
648 * Add an application shortcut to the workspace.
649 *
650 * @param data The intent describing the application.
651 * @param cellInfo The position on screen where to create the shortcut.
652 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 cellInfo.screen = mWorkspace.getCurrentScreen();
655 if (!findSingleSlot(cellInfo)) return;
656
Joe Onorato0589f0f2010-02-08 13:44:00 -0800657 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
658 data, context);
659
Romain Guy73b979d2009-06-09 12:57:21 -0700660 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800661 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800663 info.container = ItemInfo.NO_ID;
664 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
665 } else {
666 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 }
668 }
Romain Guycbb89e42009-06-08 15:52:54 -0700669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 /**
671 * Add a shortcut to the workspace.
672 *
673 * @param data The intent describing the shortcut.
674 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400676 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 cellInfo.screen = mWorkspace.getCurrentScreen();
678 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700679
Joe Onorato0589f0f2010-02-08 13:44:00 -0800680 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681
682 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
685 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 }
687 }
688
Romain Guycbb89e42009-06-08 15:52:54 -0700689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700693 * @param data The intent describing the appWidgetId.
694 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700698 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700699
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800700 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700702 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700703
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700704 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 // Try finding open space on Launcher screen
709 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800710 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
711 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
712 return;
713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700715 // Build Launcher-specific widget info and save to database
716 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 launcherInfo.spanX = spans[0];
718 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 LauncherModel.addItemToDatabase(this, launcherInfo,
721 LauncherSettings.Favorites.CONTAINER_DESKTOP,
722 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
723
724 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400725 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700726
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700729
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700730 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 }
736 }
Romain Guycbb89e42009-06-08 15:52:54 -0700737
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
739 mDesktopItems.remove(launcherInfo);
740 launcherInfo.hostView = null;
741 }
742
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700743 public LauncherAppWidgetHost getAppWidgetHost() {
744 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 }
Romain Guycbb89e42009-06-08 15:52:54 -0700746
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800747 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800748 getWindow().closeAllPanels();
749
750 try {
751 dismissDialog(DIALOG_CREATE_SHORTCUT);
752 // Unlock the workspace if the dialog was showing
753 } catch (Exception e) {
754 // An exception is thrown if the dialog is not visible, which is fine
755 }
756
757 try {
758 dismissDialog(DIALOG_RENAME_FOLDER);
759 // Unlock the workspace if the dialog was showing
760 } catch (Exception e) {
761 // An exception is thrown if the dialog is not visible, which is fine
762 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800763
764 // Whatever we were doing is hereby canceled.
765 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800766 }
767
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 @Override
769 protected void onNewIntent(Intent intent) {
770 super.onNewIntent(intent);
771
772 // Close the menu
773 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800774 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800775 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700776
Joe Onorato14f122b2009-11-19 14:06:36 -0800777 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
778 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
779 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800780 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800781 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800782 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800783 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700784
Joe Onorato3a8820b2009-11-10 15:06:42 -0800785 final View v = getWindow().peekDecorView();
786 if (v != null && v.getWindowToken() != null) {
787 InputMethodManager imm = (InputMethodManager)getSystemService(
788 INPUT_METHOD_SERVICE);
789 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 }
791 }
792 }
793
794 @Override
795 protected void onRestoreInstanceState(Bundle savedInstanceState) {
796 // Do not call super here
797 mSavedInstanceState = savedInstanceState;
798 }
799
800 @Override
801 protected void onSaveInstanceState(Bundle outState) {
802 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
803
804 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
805 if (folders.size() > 0) {
806 final int count = folders.size();
807 long[] ids = new long[count];
808 for (int i = 0; i < count; i++) {
809 final FolderInfo info = folders.get(i).getInfo();
810 ids[i] = info.id;
811 }
812 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
813 } else {
814 super.onSaveInstanceState(outState);
815 }
816
Joe Onoratofb0ca672009-09-14 17:55:46 -0400817 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800818 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821
822 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
823 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
824 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
825
826 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
827 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
828 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
829 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
830 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
831 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
832 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
833 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
834 layout.getOccupiedCells());
835 }
836
837 if (mFolderInfo != null && mWaitingForResult) {
838 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
839 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
840 }
841 }
842
843 @Override
844 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700848 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800850 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 }
852
853 TextKeyListener.getInstance().release();
854
Joe Onorato9c1289c2009-08-17 11:03:03 -0400855 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856
Joe Onorato9c1289c2009-08-17 11:03:03 -0400857 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800858
859 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800860
861 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800862 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800863
864 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 }
866
867 @Override
868 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700869 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 super.startActivityForResult(intent, requestCode);
871 }
872
873 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700874 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700876
Joe Onorato7bb17492009-09-24 17:51:01 -0700877 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700878
Karl Rosaen138a0412009-04-23 19:00:21 -0700879 if (initialQuery == null) {
880 // Use any text typed in the launcher as the initial query
881 initialQuery = getTypedText();
882 clearTypedText();
883 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 if (appSearchData == null) {
885 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +0000886 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 }
Romain Guycbb89e42009-06-08 15:52:54 -0700888
Karl Rosaen138a0412009-04-23 19:00:21 -0700889 final SearchManager searchManager =
890 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -0700891 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700892 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 }
894
895 @Override
896 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400897 if (isWorkspaceLocked()) {
898 return false;
899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900
901 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800902
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
904 .setIcon(android.R.drawable.ic_menu_add)
905 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800906 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 .setIcon(android.R.drawable.ic_menu_gallery)
908 .setAlphabeticShortcut('W');
909 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
910 .setIcon(android.R.drawable.ic_search_category_default)
911 .setAlphabeticShortcut(SearchManager.MENU_KEY);
912 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
913 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
914 .setAlphabeticShortcut('N');
915
916 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700917 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
918 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919
920 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
921 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
922 .setIntent(settings);
923
924 return true;
925 }
926
927 @Override
928 public boolean onPrepareOptionsMenu(Menu menu) {
929 super.onPrepareOptionsMenu(menu);
930
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800931 // If all apps is animating, don't show the menu, because we don't know
932 // which one to show.
933 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
934 return false;
935 }
936
937 // Only show the add and wallpaper options when we're not in all apps.
938 boolean visible = !mAllAppsGrid.isOpaque();
939 menu.setGroupVisible(MENU_GROUP_ADD, visible);
940 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
941
942 // Disable add if the workspace is full.
943 if (visible) {
944 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
945 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947
948 return true;
949 }
950
951 @Override
952 public boolean onOptionsItemSelected(MenuItem item) {
953 switch (item.getItemId()) {
954 case MENU_ADD:
955 addItems();
956 return true;
957 case MENU_WALLPAPER_SETTINGS:
958 startWallpaper();
959 return true;
960 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700961 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 return true;
963 case MENU_NOTIFICATIONS:
964 showNotifications();
965 return true;
966 }
967
968 return super.onOptionsItemSelected(item);
969 }
Romain Guycbb89e42009-06-08 15:52:54 -0700970
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 /**
972 * Indicates that we want global search for this activity by setting the globalSearch
973 * argument for {@link #startSearch} to true.
974 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700976 @Override
977 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -0700978 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -0700979 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700980 }
981
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 public boolean isWorkspaceLocked() {
983 return mWorkspaceLoading || mWaitingForResult;
984 }
985
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -0500987 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 showAddDialog(mMenuAddInfo);
989 }
990
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700991 void addAppWidget(Intent data) {
992 // TODO: catch bad widget exception when sent
993 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000994 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995
Bjorn Bringert7984c942009-12-09 15:38:25 +0000996 if (appWidget.configure != null) {
997 // Launch over to configure widget, if needed
998 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
999 intent.setComponent(appWidget.configure);
1000 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1001
Romain Guy8e633c52010-03-04 12:51:36 -08001002 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001004 // Otherwise just add it
1005 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
1007 }
Romain Guycbb89e42009-06-08 15:52:54 -07001008
Joe Onoratodeb98af2010-02-19 14:59:39 -08001009 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001010 // Handle case where user selected "Applications"
1011 String applicationName = getResources().getString(R.string.group_applications);
1012 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001013
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001014 if (applicationName != null && applicationName.equals(shortcutName)) {
1015 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1016 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001017
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001018 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1019 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001020 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001021 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001022 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001023 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
1025
1026 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001027 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001028 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001029 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001030
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001031 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001033 } else {
1034 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037
Joe Onorato9c1289c2009-08-17 11:03:03 -04001038 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 UserFolderInfo folderInfo = new UserFolderInfo();
1040 folderInfo.title = getText(R.string.folder_name);
1041
1042 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1043 cellInfo.screen = mWorkspace.getCurrentScreen();
1044 if (!findSingleSlot(cellInfo)) return;
1045
1046 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047 LauncherModel.addItemToDatabase(this, folderInfo,
1048 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051
1052 // Create the view
1053 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1054 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1055 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001056 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 }
Romain Guycbb89e42009-06-08 15:52:54 -07001058
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059 void removeFolder(FolderInfo folder) {
1060 mFolders.remove(folder.id);
1061 }
1062
1063 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 cellInfo.screen = mWorkspace.getCurrentScreen();
1065 if (!findSingleSlot(cellInfo)) return;
1066
1067 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1068
1069 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1071 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001072 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1073 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 }
1075 }
1076
1077 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1078 CellLayout.CellInfo cellInfo, boolean notify) {
1079
1080 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1081 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1082
1083 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 Intent.ShortcutIconResource iconResource = null;
1085
1086 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1087 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1088 try {
1089 iconResource = (Intent.ShortcutIconResource) extra;
1090 final PackageManager packageManager = context.getPackageManager();
1091 Resources resources = packageManager.getResourcesForApplication(
1092 iconResource.packageName);
1093 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1094 icon = resources.getDrawable(id);
1095 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001096 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
1098 }
1099
1100 if (icon == null) {
1101 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1102 }
1103
1104 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001105 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 info.title = name;
1107 info.iconResource = iconResource;
1108 info.uri = data.getData();
1109 info.baseIntent = baseIntent;
1110 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1111 LiveFolders.DISPLAY_MODE_GRID);
1112
1113 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1114 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001115 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116
1117 return info;
1118 }
1119
1120 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1121 final int[] xy = new int[2];
1122 if (findSlot(cellInfo, xy, 1, 1)) {
1123 cellInfo.cellX = xy[0];
1124 cellInfo.cellY = xy[1];
1125 return true;
1126 }
1127 return false;
1128 }
1129
1130 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1131 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1132 boolean[] occupied = mSavedState != null ?
1133 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1134 cellInfo = mWorkspace.findAllVacantCells(occupied);
1135 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1136 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1137 return false;
1138 }
1139 }
1140 return true;
1141 }
1142
1143 private void showNotifications() {
1144 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1145 if (statusBar != null) {
1146 statusBar.expand();
1147 }
1148 }
1149
1150 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001151 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001153 Intent chooser = Intent.createChooser(pickWallpaper,
1154 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001155 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1156 // Removed in Eclair MR1
1157// WallpaperManager wm = (WallpaperManager)
1158// getSystemService(Context.WALLPAPER_SERVICE);
1159// WallpaperInfo wi = wm.getWallpaperInfo();
1160// if (wi != null && wi.getSettingsActivity() != null) {
1161// LabeledIntent li = new LabeledIntent(getPackageName(),
1162// R.string.configure_wallpaper, 0);
1163// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1164// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1165// }
Mike Clerona0618e42009-10-22 13:55:21 -07001166 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 }
1168
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001169 /**
1170 * Registers various content observers. The current implementation registers
1171 * only a favorites observer to keep track of the favorites applications.
1172 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001173 private void registerContentObservers() {
1174 ContentResolver resolver = getContentResolver();
1175 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1176 true, mWidgetObserver);
1177 }
1178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 @Override
1180 public boolean dispatchKeyEvent(KeyEvent event) {
1181 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1182 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001184 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001185 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001186 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001187 dumpState();
1188 return true;
1189 }
1190 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001191 }
1192 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1193 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001194 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 return true;
1196 }
1197 }
1198
1199 return super.dispatchKeyEvent(event);
1200 }
1201
Joe Onorato88ec0992009-11-19 13:16:06 -08001202 @Override
1203 public void onBackPressed() {
1204 if (isAllAppsVisible()) {
1205 closeAllApps(true);
1206 } else {
1207 closeFolder();
1208 }
1209 dismissPreview(mPreviousView);
1210 dismissPreview(mNextView);
1211 }
1212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 private void closeFolder() {
1214 Folder folder = mWorkspace.getOpenFolder();
1215 if (folder != null) {
1216 closeFolder(folder);
1217 }
1218 }
1219
1220 void closeFolder(Folder folder) {
1221 folder.getInfo().opened = false;
1222 ViewGroup parent = (ViewGroup) folder.getParent();
1223 if (parent != null) {
1224 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001225 if (folder instanceof DropTarget) {
1226 // Live folders aren't DropTargets.
1227 mDragController.removeDropTarget((DropTarget)folder);
1228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 }
1230 folder.onClose();
1231 }
1232
1233 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001234 * Re-listen when widgets are reset.
1235 */
1236 private void onAppWidgetReset() {
1237 mAppWidgetHost.startListening();
1238 }
1239
1240 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1242 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001244 private void unbindDesktopItems() {
1245 for (ItemInfo item: mDesktopItems) {
1246 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
1248 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 /**
1251 * Launches the intent referred by the clicked shortcut.
1252 *
1253 * @param v The view representing the clicked shortcut.
1254 */
1255 public void onClick(View v) {
1256 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001257 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001259 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001260 int[] pos = new int[2];
1261 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001262 intent.setSourceBounds(new Rect(pos[0], pos[1],
1263 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 startActivitySafely(intent);
1265 } else if (tag instanceof FolderInfo) {
1266 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001267 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001268 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001269 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001270 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001271 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001272 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
1274 }
1275
1276 void startActivitySafely(Intent intent) {
1277 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1278 try {
1279 startActivity(intent);
1280 } catch (ActivityNotFoundException e) {
1281 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1282 } catch (SecurityException e) {
1283 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001284 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1286 "or use the exported attribute for this activity.", e);
1287 }
1288 }
Romain Guy8e633c52010-03-04 12:51:36 -08001289
1290 void startActivityForResultSafely(Intent intent, int requestCode) {
1291 try {
1292 startActivityForResult(intent, requestCode);
1293 } catch (ActivityNotFoundException e) {
1294 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1295 } catch (SecurityException e) {
1296 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1297 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1298 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1299 "or use the exported attribute for this activity.", e);
1300 }
1301 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302
1303 private void handleFolderClick(FolderInfo folderInfo) {
1304 if (!folderInfo.opened) {
1305 // Close any open folder
1306 closeFolder();
1307 // Open the requested folder
1308 openFolder(folderInfo);
1309 } else {
1310 // Find the open folder...
1311 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1312 int folderScreen;
1313 if (openFolder != null) {
1314 folderScreen = mWorkspace.getScreenForView(openFolder);
1315 // .. and close it
1316 closeFolder(openFolder);
1317 if (folderScreen != mWorkspace.getCurrentScreen()) {
1318 // Close any folder open on the current screen
1319 closeFolder();
1320 // Pull the folder onto this screen
1321 openFolder(folderInfo);
1322 }
1323 }
1324 }
1325 }
1326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 /**
1328 * Opens the user fodler described by the specified tag. The opening of the folder
1329 * is animated relative to the specified View. If the View is null, no animation
1330 * is played.
1331 *
1332 * @param folderInfo The FolderInfo describing the folder to open.
1333 */
1334 private void openFolder(FolderInfo folderInfo) {
1335 Folder openFolder;
1336
1337 if (folderInfo instanceof UserFolderInfo) {
1338 openFolder = UserFolder.fromXml(this);
1339 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001340 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 } else {
1342 return;
1343 }
1344
Joe Onorato00acb122009-08-04 16:04:30 -04001345 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 openFolder.setLauncher(this);
1347
1348 openFolder.bind(folderInfo);
1349 folderInfo.opened = true;
1350
1351 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1352 openFolder.onOpen();
1353 }
1354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001356 switch (v.getId()) {
1357 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001358 if (!isAllAppsVisible()) {
1359 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1360 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001361 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001362 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001363 return true;
1364 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001365 if (!isAllAppsVisible()) {
1366 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1367 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001368 showPreviews(v);
1369 }
1370 return true;
1371 case R.id.all_apps_button:
1372 if (!isAllAppsVisible()) {
1373 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1374 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1375 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001376 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001377 return true;
1378 }
1379
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 return false;
1382 }
1383
1384 if (!(v instanceof CellLayout)) {
1385 v = (View) v.getParent();
1386 }
1387
1388 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1389
1390 // This happens when long clicking an item with the dpad/trackball
1391 if (cellInfo == null) {
1392 return true;
1393 }
1394
1395 if (mWorkspace.allowLongPress()) {
1396 if (cellInfo.cell == null) {
1397 if (cellInfo.valid) {
1398 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001399 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001400 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1401 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 showAddDialog(cellInfo);
1403 }
1404 } else {
1405 if (!(cellInfo.cell instanceof Folder)) {
1406 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001407 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1408 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 mWorkspace.startDrag(cellInfo);
1410 }
1411 }
1412 }
1413 return true;
1414 }
1415
Romain Guye6b8e2f2009-11-10 11:56:55 -08001416 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001417 private void dismissPreview(final View v) {
1418 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001419 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001420 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1421 public void onDismiss() {
1422 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1423 int count = group.getChildCount();
1424 for (int i = 0; i < count; i++) {
1425 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1426 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001427 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1428 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1429
1430 v.setTag(R.id.workspace, null);
1431 v.setTag(R.id.icon, null);
1432 window.setOnDismissListener(null);
1433 }
1434 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001435 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001436 }
1437 v.setTag(null);
1438 }
1439
Romain Guyf8e6a802009-12-07 17:48:02 -08001440 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001441 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001442 }
1443
Romain Guya6abce82009-11-10 02:54:41 -08001444 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001445 final Resources resources = getResources();
1446 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001447
Romain Guya6abce82009-11-10 02:54:41 -08001448 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001449
1450 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001451
Romain Guyf8e6a802009-12-07 17:48:02 -08001452 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001453 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001454 int extraW = (int) ((r.left + r.right) * max);
1455 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001456
1457 int aW = cell.getWidth() - extraW;
1458 float w = aW / max;
1459
1460 int width = cell.getWidth();
1461 int height = cell.getHeight();
1462 int x = cell.getLeftPadding();
1463 int y = cell.getTopPadding();
1464 width -= (x + cell.getRightPadding());
1465 height -= (y + cell.getBottomPadding());
1466
1467 float scale = w / width;
1468
1469 int count = end - start;
1470
1471 final float sWidth = width * scale;
1472 float sHeight = height * scale;
1473
Romain Guye6b8e2f2009-11-10 11:56:55 -08001474 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001475
Romain Guye6b8e2f2009-11-10 11:56:55 -08001476 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1477 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001478
1479 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001480 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001481 cell = (CellLayout) workspace.getChildAt(i);
1482
Romain Guyf8e6a802009-12-07 17:48:02 -08001483 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001484 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001485
1486 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001487 c.scale(scale, scale);
1488 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1489 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001490
Romain Guy9d31e9f2009-11-11 18:54:28 -08001491 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001492 image.setImageBitmap(bitmap);
1493 image.setTag(i);
1494 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001495 image.setOnFocusChangeListener(handler);
1496 image.setFocusable(true);
1497 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001498
1499 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001500 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1501
1502 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001503 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001504
1505 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001506 p.setContentView(preview);
1507 p.setWidth((int) (sWidth * count + extraW));
1508 p.setHeight((int) (sHeight + extraH));
1509 p.setAnimationStyle(R.style.AnimationPreview);
1510 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001511 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001512 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001513 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001514
Romain Guye6b8e2f2009-11-10 11:56:55 -08001515 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1516 public void onDismiss() {
1517 dismissPreview(anchor);
1518 }
1519 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001520
1521 anchor.setTag(p);
1522 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001523 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001524 }
1525
Romain Guy9d31e9f2009-11-11 18:54:28 -08001526 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001527 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001528
Romain Guye6b8e2f2009-11-10 11:56:55 -08001529 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001530 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001531 }
1532
1533 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001534 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001535 v.post(this);
1536 }
1537
1538 public void run() {
1539 dismissPreview(mAnchor);
1540 }
1541
1542 public void onFocusChange(View v, boolean hasFocus) {
1543 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001544 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001545 }
Romain Guya6abce82009-11-10 02:54:41 -08001546 }
1547 }
1548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 Workspace getWorkspace() {
1550 return mWorkspace;
1551 }
1552
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 @Override
1554 protected Dialog onCreateDialog(int id) {
1555 switch (id) {
1556 case DIALOG_CREATE_SHORTCUT:
1557 return new CreateShortcut().createDialog();
1558 case DIALOG_RENAME_FOLDER:
1559 return new RenameFolder().createDialog();
1560 }
1561
1562 return super.onCreateDialog(id);
1563 }
1564
1565 @Override
1566 protected void onPrepareDialog(int id, Dialog dialog) {
1567 switch (id) {
1568 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 break;
1570 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001571 if (mFolderInfo != null) {
1572 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1573 final CharSequence text = mFolderInfo.title;
1574 input.setText(text);
1575 input.setSelection(0, text.length());
1576 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 break;
1578 }
1579 }
1580
1581 void showRenameDialog(FolderInfo info) {
1582 mFolderInfo = info;
1583 mWaitingForResult = true;
1584 showDialog(DIALOG_RENAME_FOLDER);
1585 }
1586
1587 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1588 mAddItemCellInfo = cellInfo;
1589 mWaitingForResult = true;
1590 showDialog(DIALOG_CREATE_SHORTCUT);
1591 }
1592
Joe Onoratodeb98af2010-02-19 14:59:39 -08001593 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001594 Bundle bundle = new Bundle();
1595
1596 ArrayList<String> shortcutNames = new ArrayList<String>();
1597 shortcutNames.add(getString(R.string.group_applications));
1598 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1599
1600 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1601 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1602 R.drawable.ic_launcher_application));
1603 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1604
1605 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1606 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001607 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001608 pickIntent.putExtras(bundle);
1609
Joe Onoratodeb98af2010-02-19 14:59:39 -08001610 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001611 }
1612
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 private class RenameFolder {
1614 private EditText mInput;
1615
1616 Dialog createDialog() {
1617 mWaitingForResult = true;
1618 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1619 mInput = (EditText) layout.findViewById(R.id.folder_name);
1620
1621 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1622 builder.setIcon(0);
1623 builder.setTitle(getString(R.string.rename_folder_title));
1624 builder.setCancelable(true);
1625 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1626 public void onCancel(DialogInterface dialog) {
1627 cleanup();
1628 }
1629 });
1630 builder.setNegativeButton(getString(R.string.cancel_action),
1631 new Dialog.OnClickListener() {
1632 public void onClick(DialogInterface dialog, int which) {
1633 cleanup();
1634 }
1635 }
1636 );
1637 builder.setPositiveButton(getString(R.string.rename_action),
1638 new Dialog.OnClickListener() {
1639 public void onClick(DialogInterface dialog, int which) {
1640 changeFolderName();
1641 }
1642 }
1643 );
1644 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001645
1646 final AlertDialog dialog = builder.create();
1647 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1648 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001649 mInput.requestFocus();
1650 InputMethodManager inputManager = (InputMethodManager)
1651 getSystemService(Context.INPUT_METHOD_SERVICE);
1652 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001653 }
1654 });
1655
1656 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 }
1658
1659 private void changeFolderName() {
1660 final String name = mInput.getText().toString();
1661 if (!TextUtils.isEmpty(name)) {
1662 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001663 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 mFolderInfo.title = name;
1665 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1666
Joe Onorato9c1289c2009-08-17 11:03:03 -04001667 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001668 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001669 mModel.setWorkspaceDirty();
1670 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 } else {
1672 final FolderIcon folderIcon = (FolderIcon)
1673 mWorkspace.getViewForTag(mFolderInfo);
1674 if (folderIcon != null) {
1675 folderIcon.setText(name);
1676 getWorkspace().requestLayout();
1677 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001678 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001679 mModel.setWorkspaceDirty();
1680 mWorkspaceLoading = true;
1681 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 }
1683 }
1684 }
1685 cleanup();
1686 }
1687
1688 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 dismissDialog(DIALOG_RENAME_FOLDER);
1690 mWaitingForResult = false;
1691 mFolderInfo = null;
1692 }
1693 }
1694
Joe Onoratofb0ca672009-09-14 17:55:46 -04001695 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001696 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001697 }
1698
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001699 // AllAppsView.Watcher
1700 public void zoomed(float zoom) {
1701 if (zoom == 1.0f) {
1702 mWorkspace.setVisibility(View.GONE);
1703 }
1704 }
1705
Joe Onorato3a8820b2009-11-10 15:06:42 -08001706 void showAllApps(boolean animated) {
1707 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001708
Daniel Sandler388f6792010-03-02 14:08:08 -05001709 ((View)mAllAppsGrid).setFocusable(true);
1710 ((View)mAllAppsGrid).requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001711
Joe Onorato7c312c12009-08-13 21:36:53 -07001712 // TODO: fade these two too
1713 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001714 }
1715
Joe Onoratob2061212009-11-24 16:13:54 -05001716 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001717 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001718 * - Home from workspace
1719 * - from center screen
1720 * - from other screens
1721 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001722 * - from center screen
1723 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001724 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001725 * - from center screen
1726 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001727 * - Launch app from workspace and quit
1728 * - with back
1729 * - with home
1730 * - Launch app from all apps and quit
1731 * - with back
1732 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001733 * - Go to a screen that's not the default, then all
1734 * apps, and launch and app, and go back
1735 * - with back
1736 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001737 * - On workspace, long press power and go back
1738 * - with back
1739 * - with home
1740 * - On all apps, long press power and go back
1741 * - with back
1742 * - with home
1743 * - On workspace, power off
1744 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001745 * - Launch an app and turn off the screen while in that app
1746 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001747 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001748 * - From all apps
1749 * - From workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001750 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001751 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001752 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001753 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001754 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05001755 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001756 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001757 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001758 }
1759
Joe Onorato7c312c12009-08-13 21:36:53 -07001760 void lockAllApps() {
1761 // TODO
1762 }
1763
1764 void unlockAllApps() {
1765 // TODO
1766 }
1767
Joe Onorato7404ee42009-07-31 11:54:44 -07001768 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 * Displays the shortcut creation dialog and launches, if necessary, the
1770 * appropriate activity.
1771 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001772 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001773 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1774 DialogInterface.OnShowListener {
1775
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001777
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001780
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1782 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001783 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001784
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 builder.setInverseBackgroundForced(true);
1786
1787 AlertDialog dialog = builder.create();
1788 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001789 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001790 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 return dialog;
1793 }
1794
1795 public void onCancel(DialogInterface dialog) {
1796 mWaitingForResult = false;
1797 cleanup();
1798 }
1799
Romain Guycbb89e42009-06-08 15:52:54 -07001800 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001801 }
1802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001804 try {
1805 dismissDialog(DIALOG_CREATE_SHORTCUT);
1806 } catch (Exception e) {
1807 // An exception is thrown if the dialog is not visible, which is fine
1808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
1810
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001811 /**
1812 * Handle the action clicked in the "Add to home" dialog.
1813 */
1814 public void onClick(DialogInterface dialog, int which) {
1815 Resources res = getResources();
1816 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001817
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001818 switch (which) {
1819 case AddAdapter.ITEM_SHORTCUT: {
1820 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08001821 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001822 break;
1823 }
Romain Guycbb89e42009-06-08 15:52:54 -07001824
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001825 case AddAdapter.ITEM_APPWIDGET: {
1826 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001827
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001828 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1829 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001830 // start the pick activity
1831 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1832 break;
1833 }
Romain Guycbb89e42009-06-08 15:52:54 -07001834
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001835 case AddAdapter.ITEM_LIVE_FOLDER: {
1836 // Insert extra item to handle inserting folder
1837 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001838
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001839 ArrayList<String> shortcutNames = new ArrayList<String>();
1840 shortcutNames.add(res.getString(R.string.group_folder));
1841 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001842
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001843 ArrayList<ShortcutIconResource> shortcutIcons =
1844 new ArrayList<ShortcutIconResource>();
1845 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1846 R.drawable.ic_launcher_folder));
1847 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1848
1849 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1850 pickIntent.putExtra(Intent.EXTRA_INTENT,
1851 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1852 pickIntent.putExtra(Intent.EXTRA_TITLE,
1853 getText(R.string.title_select_live_folder));
1854 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001855
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001856 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1857 break;
1858 }
1859
1860 case AddAdapter.ITEM_WALLPAPER: {
1861 startWallpaper();
1862 break;
1863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 }
1865 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001866
1867 public void onShow(DialogInterface dialog) {
Romain Guy406d7032010-03-04 18:59:31 -08001868 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07001869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 }
1871
1872 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001873 * Receives notifications when applications are added/removed.
1874 */
1875 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1876 @Override
1877 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001878 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05001879 String reason = intent.getStringExtra("reason");
1880 if (!"homekey".equals(reason)) {
1881 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001882 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05001883 animate = false;
1884 }
Joe Onoratob2061212009-11-24 16:13:54 -05001885 closeAllApps(animate);
1886 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001887 }
1888 }
1889
1890 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001891 * Receives notifications whenever the appwidgets are reset.
1892 */
1893 private class AppWidgetResetObserver extends ContentObserver {
1894 public AppWidgetResetObserver() {
1895 super(new Handler());
1896 }
1897
1898 @Override
1899 public void onChange(boolean selfChange) {
1900 onAppWidgetReset();
1901 }
1902 }
1903
1904 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001905 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001907 public int getCurrentWorkspaceScreen() {
1908 return mWorkspace.getCurrentScreen();
1909 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001910
Joe Onorato9c1289c2009-08-17 11:03:03 -04001911 /**
1912 * Refreshes the shortcuts shown on the workspace.
1913 *
1914 * Implementation of the method from LauncherModel.Callbacks.
1915 */
1916 public void startBinding() {
1917 final Workspace workspace = mWorkspace;
1918 int count = workspace.getChildCount();
1919 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001920 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001921 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1922 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001923
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 if (DEBUG_USER_INTERFACE) {
1925 android.widget.Button finishButton = new android.widget.Button(this);
1926 finishButton.setText("Finish");
1927 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1928
1929 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1930 public void onClick(View v) {
1931 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001932 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001933 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 }
1935 }
1936
1937 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001938 * Bind the items start-end from the list.
1939 *
1940 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001942 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1943
1944 final Workspace workspace = mWorkspace;
1945
1946 for (int i=start; i<end; i++) {
1947 final ItemInfo item = shortcuts.get(i);
1948 mDesktopItems.add(item);
1949 switch (item.itemType) {
1950 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1951 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001952 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1954 false);
1955 break;
1956 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1957 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1958 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1959 (UserFolderInfo) item);
1960 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1961 false);
1962 break;
1963 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1964 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1965 R.layout.live_folder_icon, this,
1966 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1967 (LiveFolderInfo) item);
1968 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1969 false);
1970 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
Joe Onorato9c1289c2009-08-17 11:03:03 -04001974 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 }
1976
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977 /**
1978 * Implementation of the method from LauncherModel.Callbacks.
1979 */
Joe Onoratoad72e172009-11-06 16:25:04 -05001980 public void bindFolders(HashMap<Long, FolderInfo> folders) {
1981 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001982 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001984
1985 /**
1986 * Add the views for a widget to the workspace.
1987 *
1988 * Implementation of the method from LauncherModel.Callbacks.
1989 */
1990 public void bindAppWidget(LauncherAppWidgetInfo item) {
1991 final Workspace workspace = mWorkspace;
1992
1993 final int appWidgetId = item.appWidgetId;
1994 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1995 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1996
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1998 item.hostView.setTag(item);
1999
2000 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2001 item.cellY, item.spanX, item.spanY, false);
2002
2003 workspace.requestLayout();
2004
2005 mDesktopItems.add(item);
2006 }
2007
2008 /**
2009 * Callback saying that there aren't any more items to bind.
2010 *
2011 * Implementation of the method from LauncherModel.Callbacks.
2012 */
2013 public void finishBindingItems() {
2014 if (mSavedState != null) {
2015 if (!mWorkspace.hasFocus()) {
2016 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2017 }
2018
2019 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2020 if (userFolders != null) {
2021 for (long folderId : userFolders) {
2022 final FolderInfo info = mFolders.get(folderId);
2023 if (info != null) {
2024 openFolder(info);
2025 }
2026 }
2027 final Folder openFolder = mWorkspace.getOpenFolder();
2028 if (openFolder != null) {
2029 openFolder.requestFocus();
2030 }
2031 }
2032
Joe Onorato9c1289c2009-08-17 11:03:03 -04002033 mSavedState = null;
2034 }
2035
2036 if (mSavedInstanceState != null) {
2037 super.onRestoreInstanceState(mSavedInstanceState);
2038 mSavedInstanceState = null;
2039 }
2040
Joe Onorato9c1289c2009-08-17 11:03:03 -04002041 mWorkspaceLoading = false;
2042 }
2043
2044 /**
2045 * Add the icons for all apps.
2046 *
2047 * Implementation of the method from LauncherModel.Callbacks.
2048 */
2049 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002050 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002051 }
2052
2053 /**
2054 * A package was installed.
2055 *
2056 * Implementation of the method from LauncherModel.Callbacks.
2057 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002058 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002059 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002060 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002061 }
2062
2063 /**
2064 * A package was updated.
2065 *
2066 * Implementation of the method from LauncherModel.Callbacks.
2067 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002068 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002069 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002070 mWorkspace.updateShortcuts(apps);
2071 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002072 }
2073
2074 /**
2075 * A package was uninstalled.
2076 *
2077 * Implementation of the method from LauncherModel.Callbacks.
2078 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002079 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002080 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002081 mWorkspace.removeItems(apps);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002082 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002083 }
Joe Onoratobe386092009-11-17 17:32:16 -08002084
2085 /**
2086 * Prints out out state for debugging.
2087 */
2088 public void dumpState() {
2089 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002090 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002091 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2092 Log.d(TAG, "mRestoring=" + mRestoring);
2093 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2094 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2095 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2096 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002097 mModel.dumpState();
2098 mAllAppsGrid.dumpState();
2099 Log.d(TAG, "END launcher2 dump state");
2100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101}