blob: 8e61f1175015ee00dd87ebc36980f8d297ed5a8c [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
20import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Dialog;
22import android.app.SearchManager;
23import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070024import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080026import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080028import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
30import android.content.DialogInterface;
31import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070032import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080033import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ActivityInfo;
35import android.content.pm.PackageManager;
36import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070038import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080041import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080042import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080044import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080046import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.os.Parcelable;
Joe Onoratobe386092009-11-17 17:32:16 -080048import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.text.Selection;
51import android.text.SpannableStringBuilder;
52import android.text.TextUtils;
53import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070054import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080056import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.KeyEvent;
58import android.view.LayoutInflater;
59import android.view.Menu;
60import android.view.MenuItem;
61import android.view.View;
62import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.View.OnLongClickListener;
Joe Onorato8a9625e2010-01-28 15:55:35 -080064import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.widget.TextView;
68import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080069import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080070import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080071import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070072import android.appwidget.AppWidgetManager;
73import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040076import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070077import java.io.DataOutputStream;
78import java.io.FileNotFoundException;
79import java.io.IOException;
80import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081
82/**
83 * Default launcher application.
84 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040085public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080086 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080087 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 static final boolean LOGD = false;
89
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 static final boolean PROFILE_STARTUP = false;
91 static final boolean PROFILE_ROTATE = false;
92 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 private static final int WALLPAPER_SCREENS_SPAN = 2;
95
96 private static final int MENU_GROUP_ADD = 1;
97 private static final int MENU_ADD = Menu.FIRST + 1;
98 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
99 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
100 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700101 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
103 private static final int REQUEST_CREATE_SHORTCUT = 1;
104 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700105 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 private static final int REQUEST_PICK_APPLICATION = 6;
107 private static final int REQUEST_PICK_SHORTCUT = 7;
108 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700109 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700110 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
112 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
113
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800114 static final int SCREEN_COUNT = 5;
115 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700117 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato7c312c12009-08-13 21:36:53 -0700119 static final int DIALOG_CREATE_SHORTCUT = 1;
120 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Romain Guy98d01652009-06-30 16:21:04 -0700122 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
124 // Type: int
125 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
126 // Type: boolean
127 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
128 // Type: long
129 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
130 // Type: int
131 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
144 // Type: int[]
145 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
146 // Type: boolean
147 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
148 // Type: long
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
150
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800154 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800156 private final BroadcastReceiver mCloseSystemDialogsReceiver
157 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800158 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 private LayoutInflater mInflater;
161
Joe Onorato00acb122009-08-04 16:04:30 -0400162 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700164
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700165 private AppWidgetManager mAppWidgetManager;
166 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private CellLayout.CellInfo mAddItemCellInfo;
169 private CellLayout.CellInfo mMenuAddInfo;
170 private final int[] mCellCoordinates = new int[2];
171 private FolderInfo mFolderInfo;
172
Joe Onorato7c312c12009-08-13 21:36:53 -0700173 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700174 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700175 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private Bundle mSavedState;
178
179 private SpannableStringBuilder mDefaultKeySsb = null;
180
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 private boolean mWorkspaceLoading = true;
182
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800183 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private boolean mRestoring;
185 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
187 private Bundle mSavedInstanceState;
188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 private LauncherModel mModel;
190
Romain Guy84f296c2009-11-04 15:00:44 -0800191 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
192 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700193
Romain Guy1fbc1c82009-11-09 20:43:08 -0800194 private ImageView mPreviousView;
195 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 @Override
198 protected void onCreate(Bundle savedInstanceState) {
199 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700200
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400202 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700204
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700205 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700206 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
207 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 if (PROFILE_STARTUP) {
210 android.os.Debug.startMethodTracing("/sdcard/launcher");
211 }
212
213 checkForLocaleChange();
214 setWallpaperDimension();
215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 setContentView(R.layout.launcher);
217 setupViews();
218
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800219 registerContentObservers();
220
Joe Onorato7c312c12009-08-13 21:36:53 -0700221 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 mSavedState = savedInstanceState;
224 restoreState(mSavedState);
225
226 if (PROFILE_STARTUP) {
227 android.os.Debug.stopMethodTracing();
228 }
229
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700230 // We have a new AllAppsView, we need to re-bind everything, and it could have
231 // changed in our absence.
232 mModel.setAllAppsDirty();
233 mModel.setWorkspaceDirty();
234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 }
238
239 // For handling default keys
240 mDefaultKeySsb = new SpannableStringBuilder();
241 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800242
243 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
244 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 }
Romain Guycbb89e42009-06-08 15:52:54 -0700246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700248 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
249 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 final Configuration configuration = getResources().getConfiguration();
252
Romain Guy98d01652009-06-30 16:21:04 -0700253 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 final String locale = configuration.locale.toString();
255
Romain Guy98d01652009-06-30 16:21:04 -0700256 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 final int mcc = configuration.mcc;
258
Romain Guy98d01652009-06-30 16:21:04 -0700259 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 final int mnc = configuration.mnc;
261
Romain Guy84f296c2009-11-04 15:00:44 -0800262 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
Romain Guy84f296c2009-11-04 15:00:44 -0800264 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700265 localeConfiguration.locale = locale;
266 localeConfiguration.mcc = mcc;
267 localeConfiguration.mnc = mnc;
268
269 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700271 }
272 }
273
274 private static class LocaleConfiguration {
275 public String locale;
276 public int mcc = -1;
277 public int mnc = -1;
278 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700279
Romain Guy98d01652009-06-30 16:21:04 -0700280 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
281 DataInputStream in = null;
282 try {
283 in = new DataInputStream(context.openFileInput(PREFERENCES));
284 configuration.locale = in.readUTF();
285 configuration.mcc = in.readInt();
286 configuration.mnc = in.readInt();
287 } catch (FileNotFoundException e) {
288 // Ignore
289 } catch (IOException e) {
290 // Ignore
291 } finally {
292 if (in != null) {
293 try {
294 in.close();
295 } catch (IOException e) {
296 // Ignore
297 }
298 }
299 }
300 }
301
302 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
303 DataOutputStream out = null;
304 try {
305 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
306 out.writeUTF(configuration.locale);
307 out.writeInt(configuration.mcc);
308 out.writeInt(configuration.mnc);
309 out.flush();
310 } catch (FileNotFoundException e) {
311 // Ignore
312 } catch (IOException e) {
313 //noinspection ResultOfMethodCallIgnored
314 context.getFileStreamPath(PREFERENCES).delete();
315 } finally {
316 if (out != null) {
317 try {
318 out.close();
319 } catch (IOException e) {
320 // Ignore
321 }
322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 }
324 }
325
326 static int getScreen() {
327 synchronized (sLock) {
328 return sScreen;
329 }
330 }
331
332 static void setScreen(int screen) {
333 synchronized (sLock) {
334 sScreen = screen;
335 }
336 }
337
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700339 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340
341 Display display = getWindowManager().getDefaultDisplay();
342 boolean isPortrait = display.getWidth() < display.getHeight();
343
344 final int width = isPortrait ? display.getWidth() : display.getHeight();
345 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700346 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 }
348
349 @Override
350 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700351 mWaitingForResult = false;
352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 // The pattern used here is that a user PICKs a specific application,
354 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
357 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700358
Romain Guy94dabf12009-07-21 10:55:43 -0700359 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 switch (requestCode) {
361 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400362 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 break;
364 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700365 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 break;
367 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 break;
370 case REQUEST_PICK_LIVE_FOLDER:
371 addLiveFolder(data);
372 break;
373 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400374 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700376 case REQUEST_PICK_APPWIDGET:
377 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700379 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700382 case REQUEST_PICK_WALLPAPER:
383 // We just wanted the activity result here so we can clear mWaitingForResult
384 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
Romain Guy18042c82009-11-06 11:44:55 -0800386 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
387 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
388 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700389 // Clean up the appWidgetId if we canceled
390 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
391 if (appWidgetId != -1) {
392 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 }
396
397 @Override
398 protected void onResume() {
399 super.onResume();
400
Joe Onorato8a9625e2010-01-28 15:55:35 -0800401 /*
402 // We can't hide the IME if it was forced open. So don't bother.
403 final InputMethodManager inputManager = (InputMethodManager)
404 getSystemService(Context.INPUT_METHOD_SERVICE);
405 WindowManager.LayoutParams lp = getWindow().getAttributes();
406 inputManager.hideSoftInputFromWindow(lp.token, 0);
407 Log.d(TAG, "called hideSoftInputFromWindow");
408 */
409
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800410 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800411
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400413 mWorkspaceLoading = true;
414 mModel.startLoader(this, true);
415 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 }
417 }
418
419 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700420 protected void onPause() {
421 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800422 dismissPreview(mPreviousView);
423 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800424 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700425 }
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700427 @Override
428 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400429 // Flag the loader to stop early before switching
430 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700432 if (PROFILE_ROTATE) {
433 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
434 }
435 return null;
436 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 private boolean acceptFilter() {
439 final InputMethodManager inputManager = (InputMethodManager)
440 getSystemService(Context.INPUT_METHOD_SERVICE);
441 return !inputManager.isFullscreenMode();
442 }
443
444 @Override
445 public boolean onKeyDown(int keyCode, KeyEvent event) {
446 boolean handled = super.onKeyDown(keyCode, event);
447 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
448 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
449 keyCode, event);
450 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700451 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700452 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700453 // showSearchDialog()
454 // If there are multiple keystrokes before the search dialog takes focus,
455 // onSearchRequested() will be called for every keystroke,
456 // but it is idempotent, so it's fine.
457 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
459 }
460
Joe Onorato8a9625e2010-01-28 15:55:35 -0800461 // Eat the long press event so the keyboard doesn't come up.
462 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
463 return true;
464 }
465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 return handled;
467 }
468
Karl Rosaen138a0412009-04-23 19:00:21 -0700469 private String getTypedText() {
470 return mDefaultKeySsb.toString();
471 }
472
473 private void clearTypedText() {
474 mDefaultKeySsb.clear();
475 mDefaultKeySsb.clearSpans();
476 Selection.setSelection(mDefaultKeySsb, 0);
477 }
478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 /**
480 * Restores the previous state, if it exists.
481 *
482 * @param savedState The previous state.
483 */
484 private void restoreState(Bundle savedState) {
485 if (savedState == null) {
486 return;
487 }
488
Joe Onorato3a8820b2009-11-10 15:06:42 -0800489 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
490 if (allApps) {
491 showAllApps(false);
492 }
493
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
495 if (currentScreen > -1) {
496 mWorkspace.setCurrentScreen(currentScreen);
497 }
498
499 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
500 if (addScreen > -1) {
501 mAddItemCellInfo = new CellLayout.CellInfo();
502 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
503 addItemCellInfo.valid = true;
504 addItemCellInfo.screen = addScreen;
505 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
506 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
507 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
508 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
509 addItemCellInfo.findVacantCellsFromOccupied(
510 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
511 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
512 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
513 mRestoring = true;
514 }
515
516 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
517 if (renameFolder) {
518 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400519 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 mRestoring = true;
521 }
522 }
523
524 /**
525 * Finds all the views we need and configure them properly.
526 */
527 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400528 DragController dragController = mDragController;
529
Romain Guyb1b69f52009-08-10 15:10:15 -0700530 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400531 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532
Joe Onorato7c312c12009-08-13 21:36:53 -0700533 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700534 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700535 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700536 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700537 // Manage focusability manually since this thing is always visible
538 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700539
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
541 final Workspace workspace = mWorkspace;
542
Joe Onorato7c312c12009-08-13 21:36:53 -0700543 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
544 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545
Joe Onorato7c312c12009-08-13 21:36:53 -0700546 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700547 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700548 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800549 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800550
551 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
552 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
553
554 Drawable previous = mPreviousView.getDrawable();
555 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800556 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557
Joe Onorato0d44e942009-11-16 18:20:51 -0800558 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800559 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800560 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800561 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800562
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400564 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566
567 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400568 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700569 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570
Joe Onorato00acb122009-08-04 16:04:30 -0400571 dragController.setDragScoller(workspace);
572 dragController.setDragListener(deleteZone);
573 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800574 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700575
Joe Onorato00acb122009-08-04 16:04:30 -0400576 // The order here is bottom to top.
577 dragController.addDropTarget(workspace);
578 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 }
580
Romain Guy8a73c512009-11-09 19:19:59 -0800581 @SuppressWarnings({"UnusedDeclaration"})
582 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800583 if (!isAllAppsVisible()) {
584 mWorkspace.scrollLeft();
585 }
Romain Guy8a73c512009-11-09 19:19:59 -0800586 }
587
588 @SuppressWarnings({"UnusedDeclaration"})
589 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800590 if (!isAllAppsVisible()) {
591 mWorkspace.scrollRight();
592 }
Romain Guy8a73c512009-11-09 19:19:59 -0800593 }
594
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 /**
596 * Creates a view representing a shortcut.
597 *
598 * @param info The data structure describing the shortcut.
599 *
600 * @return A View inflated from R.layout.application.
601 */
602 View createShortcut(ApplicationInfo info) {
603 return createShortcut(R.layout.application,
604 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
605 }
606
607 /**
608 * Creates a view representing a shortcut inflated from the specified resource.
609 *
610 * @param layoutResId The id of the XML layout used to create the shortcut.
611 * @param parent The group the shortcut belongs to.
612 * @param info The data structure describing the shortcut.
613 *
614 * @return A View inflated from layoutResId.
615 */
616 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
617 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
618
Joe Onorato5162ea92009-09-03 09:39:42 -0700619 if (info.icon == null) {
620 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
621 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700623 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 info.filtered = true;
625 }
626
627 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
628 favorite.setText(info.title);
629 favorite.setTag(info);
630 favorite.setOnClickListener(this);
631
632 return favorite;
633 }
634
635 /**
636 * Add an application shortcut to the workspace.
637 *
638 * @param data The intent describing the application.
639 * @param cellInfo The position on screen where to create the shortcut.
640 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 cellInfo.screen = mWorkspace.getCurrentScreen();
643 if (!findSingleSlot(cellInfo)) return;
644
Romain Guy73b979d2009-06-09 12:57:21 -0700645 final ApplicationInfo info = infoFromApplicationIntent(context, data);
646 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400647 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700648 }
649 }
650
651 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 ComponentName component = data.getComponent();
653 PackageManager packageManager = context.getPackageManager();
654 ActivityInfo activityInfo = null;
655 try {
656 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
657 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800658 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
Romain Guycbb89e42009-06-08 15:52:54 -0700660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 if (activityInfo != null) {
662 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 itemInfo.title = activityInfo.loadLabel(packageManager);
665 if (itemInfo.title == null) {
666 itemInfo.title = activityInfo.name;
667 }
Romain Guycbb89e42009-06-08 15:52:54 -0700668
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
670 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
671 itemInfo.icon = activityInfo.loadIcon(packageManager);
672 itemInfo.container = ItemInfo.NO_ID;
673
Romain Guy73b979d2009-06-09 12:57:21 -0700674 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
Romain Guy73b979d2009-06-09 12:57:21 -0700676
677 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
Romain Guycbb89e42009-06-08 15:52:54 -0700679
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 /**
681 * Add a shortcut to the workspace.
682 *
683 * @param data The intent describing the shortcut.
684 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400686 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 cellInfo.screen = mWorkspace.getCurrentScreen();
688 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
691
692 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400694 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
695 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 }
697 }
698
Romain Guycbb89e42009-06-08 15:52:54 -0700699
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700701 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700703 * @param data The intent describing the appWidgetId.
704 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400706 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700709
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800710 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700711
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700714 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700717
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 // Try finding open space on Launcher screen
719 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800720 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
721 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
722 return;
723 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700725 // Build Launcher-specific widget info and save to database
726 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 launcherInfo.spanX = spans[0];
728 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700729
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 LauncherModel.addItemToDatabase(this, launcherInfo,
731 LauncherSettings.Favorites.CONTAINER_DESKTOP,
732 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
733
734 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700736
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700738 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700739
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700740 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700742
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 }
746 }
Romain Guycbb89e42009-06-08 15:52:54 -0700747
Joe Onorato9c1289c2009-08-17 11:03:03 -0400748 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
749 mDesktopItems.remove(launcherInfo);
750 launcherInfo.hostView = null;
751 }
752
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700753 public LauncherAppWidgetHost getAppWidgetHost() {
754 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 }
Romain Guycbb89e42009-06-08 15:52:54 -0700756
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 static ApplicationInfo addShortcut(Context context, Intent data,
758 CellLayout.CellInfo cellInfo, boolean notify) {
759
Romain Guy73b979d2009-06-09 12:57:21 -0700760 final ApplicationInfo info = infoFromShortcutIntent(context, data);
761 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
762 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
763
764 return info;
765 }
766
767 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
769 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guyb5de7752010-01-04 14:43:49 -0800770 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771
772 Drawable icon = null;
773 boolean filtered = false;
774 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700775 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776
Romain Guyb5de7752010-01-04 14:43:49 -0800777 if (bitmap != null && bitmap instanceof Bitmap) {
778 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail((Bitmap) bitmap, context));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 filtered = true;
780 customIcon = true;
781 } else {
782 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700783 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700785 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 final PackageManager packageManager = context.getPackageManager();
787 Resources resources = packageManager.getResourcesForApplication(
788 iconResource.packageName);
789 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
790 icon = resources.getDrawable(id);
791 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800792 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 }
794 }
795 }
796
797 if (icon == null) {
798 icon = context.getPackageManager().getDefaultActivityIcon();
799 }
800
801 final ApplicationInfo info = new ApplicationInfo();
802 info.icon = icon;
803 info.filtered = filtered;
804 info.title = name;
805 info.intent = intent;
806 info.customIcon = customIcon;
807 info.iconResource = iconResource;
808
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 return info;
810 }
811
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800812 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800813 getWindow().closeAllPanels();
814
815 try {
816 dismissDialog(DIALOG_CREATE_SHORTCUT);
817 // Unlock the workspace if the dialog was showing
818 } catch (Exception e) {
819 // An exception is thrown if the dialog is not visible, which is fine
820 }
821
822 try {
823 dismissDialog(DIALOG_RENAME_FOLDER);
824 // Unlock the workspace if the dialog was showing
825 } catch (Exception e) {
826 // An exception is thrown if the dialog is not visible, which is fine
827 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800828
829 // Whatever we were doing is hereby canceled.
830 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800831 }
832
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 @Override
834 protected void onNewIntent(Intent intent) {
835 super.onNewIntent(intent);
836
837 // Close the menu
838 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800839 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800840 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700841
Joe Onorato14f122b2009-11-19 14:06:36 -0800842 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
843 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
844 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800845 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800846 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800847 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800848 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700849
Joe Onorato3a8820b2009-11-10 15:06:42 -0800850 final View v = getWindow().peekDecorView();
851 if (v != null && v.getWindowToken() != null) {
852 InputMethodManager imm = (InputMethodManager)getSystemService(
853 INPUT_METHOD_SERVICE);
854 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 }
856 }
857 }
858
859 @Override
860 protected void onRestoreInstanceState(Bundle savedInstanceState) {
861 // Do not call super here
862 mSavedInstanceState = savedInstanceState;
863 }
864
865 @Override
866 protected void onSaveInstanceState(Bundle outState) {
867 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
868
869 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
870 if (folders.size() > 0) {
871 final int count = folders.size();
872 long[] ids = new long[count];
873 for (int i = 0; i < count; i++) {
874 final FolderInfo info = folders.get(i).getInfo();
875 ids[i] = info.id;
876 }
877 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
878 } else {
879 super.onSaveInstanceState(outState);
880 }
881
Joe Onoratofb0ca672009-09-14 17:55:46 -0400882 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800883 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700885 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886
887 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
888 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
889 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
890
891 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
892 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
893 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
894 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
895 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
896 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
897 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
898 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
899 layout.getOccupiedCells());
900 }
901
902 if (mFolderInfo != null && mWaitingForResult) {
903 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
904 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
905 }
906 }
907
908 @Override
909 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700911
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700913 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800915 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 }
917
918 TextKeyListener.getInstance().release();
919
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921
Joe Onorato9c1289c2009-08-17 11:03:03 -0400922 unbindDesktopItems();
923 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800924
925 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800926
927 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800928 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800929
930 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 }
932
933 @Override
934 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700935 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 super.startActivityForResult(intent, requestCode);
937 }
938
939 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700940 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700942
Joe Onorato7bb17492009-09-24 17:51:01 -0700943 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700944
Karl Rosaen138a0412009-04-23 19:00:21 -0700945 if (initialQuery == null) {
946 // Use any text typed in the launcher as the initial query
947 initialQuery = getTypedText();
948 clearTypedText();
949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 if (appSearchData == null) {
951 appSearchData = new Bundle();
952 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
953 }
Romain Guycbb89e42009-06-08 15:52:54 -0700954
Karl Rosaen138a0412009-04-23 19:00:21 -0700955 final SearchManager searchManager =
956 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -0700957 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700958 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 }
960
961 @Override
962 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963 if (isWorkspaceLocked()) {
964 return false;
965 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966
967 super.onCreateOptionsMenu(menu);
968 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
969 .setIcon(android.R.drawable.ic_menu_add)
970 .setAlphabeticShortcut('A');
971 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
972 .setIcon(android.R.drawable.ic_menu_gallery)
973 .setAlphabeticShortcut('W');
974 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
975 .setIcon(android.R.drawable.ic_search_category_default)
976 .setAlphabeticShortcut(SearchManager.MENU_KEY);
977 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
978 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
979 .setAlphabeticShortcut('N');
980
981 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700982 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
983 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984
985 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
986 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
987 .setIntent(settings);
988
989 return true;
990 }
991
992 @Override
993 public boolean onPrepareOptionsMenu(Menu menu) {
994 super.onPrepareOptionsMenu(menu);
995
996 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
997 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
998
999 return true;
1000 }
1001
1002 @Override
1003 public boolean onOptionsItemSelected(MenuItem item) {
1004 switch (item.getItemId()) {
1005 case MENU_ADD:
1006 addItems();
1007 return true;
1008 case MENU_WALLPAPER_SETTINGS:
1009 startWallpaper();
1010 return true;
1011 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001012 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 return true;
1014 case MENU_NOTIFICATIONS:
1015 showNotifications();
1016 return true;
1017 }
1018
1019 return super.onOptionsItemSelected(item);
1020 }
Romain Guycbb89e42009-06-08 15:52:54 -07001021
Karl Rosaen138a0412009-04-23 19:00:21 -07001022 /**
1023 * Indicates that we want global search for this activity by setting the globalSearch
1024 * argument for {@link #startSearch} to true.
1025 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001027 @Override
1028 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001029 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001030 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001031 }
1032
Joe Onorato9c1289c2009-08-17 11:03:03 -04001033 public boolean isWorkspaceLocked() {
1034 return mWorkspaceLoading || mWaitingForResult;
1035 }
1036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001038 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 showAddDialog(mMenuAddInfo);
1040 }
1041
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001042 void addAppWidget(Intent data) {
1043 // TODO: catch bad widget exception when sent
1044 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001045 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046
Bjorn Bringert7984c942009-12-09 15:38:25 +00001047 if (appWidget.configure != null) {
1048 // Launch over to configure widget, if needed
1049 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1050 intent.setComponent(appWidget.configure);
1051 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1052
1053 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001055 // Otherwise just add it
1056 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 }
1058 }
Romain Guycbb89e42009-06-08 15:52:54 -07001059
Romain Guy73b979d2009-06-09 12:57:21 -07001060 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001061 // Handle case where user selected "Applications"
1062 String applicationName = getResources().getString(R.string.group_applications);
1063 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001064
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001065 if (applicationName != null && applicationName.equals(shortcutName)) {
1066 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1067 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001068
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001069 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1070 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001071 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001072 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001073 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 }
1076
1077 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001078 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001079 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001080 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001081
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001082 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001083 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001084 } else {
1085 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1086 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 }
1088
Joe Onorato9c1289c2009-08-17 11:03:03 -04001089 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 UserFolderInfo folderInfo = new UserFolderInfo();
1091 folderInfo.title = getText(R.string.folder_name);
1092
1093 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1094 cellInfo.screen = mWorkspace.getCurrentScreen();
1095 if (!findSingleSlot(cellInfo)) return;
1096
1097 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001098 LauncherModel.addItemToDatabase(this, folderInfo,
1099 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001101 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102
1103 // Create the view
1104 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1105 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1106 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 }
Romain Guycbb89e42009-06-08 15:52:54 -07001109
Joe Onorato9c1289c2009-08-17 11:03:03 -04001110 void removeFolder(FolderInfo folder) {
1111 mFolders.remove(folder.id);
1112 }
1113
1114 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 cellInfo.screen = mWorkspace.getCurrentScreen();
1116 if (!findSingleSlot(cellInfo)) return;
1117
1118 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1119
1120 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1122 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001123 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1124 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
1126 }
1127
1128 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1129 CellLayout.CellInfo cellInfo, boolean notify) {
1130
1131 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1132 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1133
1134 Drawable icon = null;
1135 boolean filtered = false;
1136 Intent.ShortcutIconResource iconResource = null;
1137
1138 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1139 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1140 try {
1141 iconResource = (Intent.ShortcutIconResource) extra;
1142 final PackageManager packageManager = context.getPackageManager();
1143 Resources resources = packageManager.getResourcesForApplication(
1144 iconResource.packageName);
1145 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1146 icon = resources.getDrawable(id);
1147 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001148 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 }
1150 }
1151
1152 if (icon == null) {
1153 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1154 }
1155
1156 final LiveFolderInfo info = new LiveFolderInfo();
1157 info.icon = icon;
1158 info.filtered = filtered;
1159 info.title = name;
1160 info.iconResource = iconResource;
1161 info.uri = data.getData();
1162 info.baseIntent = baseIntent;
1163 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1164 LiveFolders.DISPLAY_MODE_GRID);
1165
1166 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1167 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001168 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169
1170 return info;
1171 }
1172
1173 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1174 final int[] xy = new int[2];
1175 if (findSlot(cellInfo, xy, 1, 1)) {
1176 cellInfo.cellX = xy[0];
1177 cellInfo.cellY = xy[1];
1178 return true;
1179 }
1180 return false;
1181 }
1182
1183 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1184 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1185 boolean[] occupied = mSavedState != null ?
1186 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1187 cellInfo = mWorkspace.findAllVacantCells(occupied);
1188 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1189 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1190 return false;
1191 }
1192 }
1193 return true;
1194 }
1195
1196 private void showNotifications() {
1197 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1198 if (statusBar != null) {
1199 statusBar.expand();
1200 }
1201 }
1202
1203 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001204 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001206 Intent chooser = Intent.createChooser(pickWallpaper,
1207 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001208 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1209 // Removed in Eclair MR1
1210// WallpaperManager wm = (WallpaperManager)
1211// getSystemService(Context.WALLPAPER_SERVICE);
1212// WallpaperInfo wi = wm.getWallpaperInfo();
1213// if (wi != null && wi.getSettingsActivity() != null) {
1214// LabeledIntent li = new LabeledIntent(getPackageName(),
1215// R.string.configure_wallpaper, 0);
1216// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1217// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1218// }
Mike Clerona0618e42009-10-22 13:55:21 -07001219 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
1221
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001222 /**
1223 * Registers various content observers. The current implementation registers
1224 * only a favorites observer to keep track of the favorites applications.
1225 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001226 private void registerContentObservers() {
1227 ContentResolver resolver = getContentResolver();
1228 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1229 true, mWidgetObserver);
1230 }
1231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 @Override
1233 public boolean dispatchKeyEvent(KeyEvent event) {
1234 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1235 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001237 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001238 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001239 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001240 dumpState();
1241 return true;
1242 }
1243 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001244 }
1245 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1246 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001247 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 return true;
1249 }
1250 }
1251
1252 return super.dispatchKeyEvent(event);
1253 }
1254
Joe Onorato88ec0992009-11-19 13:16:06 -08001255 @Override
1256 public void onBackPressed() {
1257 if (isAllAppsVisible()) {
1258 closeAllApps(true);
1259 } else {
1260 closeFolder();
1261 }
1262 dismissPreview(mPreviousView);
1263 dismissPreview(mNextView);
1264 }
1265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 private void closeFolder() {
1267 Folder folder = mWorkspace.getOpenFolder();
1268 if (folder != null) {
1269 closeFolder(folder);
1270 }
1271 }
1272
1273 void closeFolder(Folder folder) {
1274 folder.getInfo().opened = false;
1275 ViewGroup parent = (ViewGroup) folder.getParent();
1276 if (parent != null) {
1277 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001278 if (folder instanceof DropTarget) {
1279 // Live folders aren't DropTargets.
1280 mDragController.removeDropTarget((DropTarget)folder);
1281 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 }
1283 folder.onClose();
1284 }
1285
1286 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001287 * Re-listen when widgets are reset.
1288 */
1289 private void onAppWidgetReset() {
1290 mAppWidgetHost.startListening();
1291 }
1292
1293 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001294 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1295 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001297 private void unbindDesktopItems() {
1298 for (ItemInfo item: mDesktopItems) {
1299 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 /**
1304 * Launches the intent referred by the clicked shortcut.
1305 *
1306 * @param v The view representing the clicked shortcut.
1307 */
1308 public void onClick(View v) {
1309 Object tag = v.getTag();
1310 if (tag instanceof ApplicationInfo) {
1311 // Open shortcut
1312 final Intent intent = ((ApplicationInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001313 int[] pos = new int[2];
1314 v.getLocationOnScreen(pos);
1315 intent.setSourceBounds(
1316 new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 startActivitySafely(intent);
1318 } else if (tag instanceof FolderInfo) {
1319 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001320 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001321 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001322 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001323 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001324 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
1327 }
1328
1329 void startActivitySafely(Intent intent) {
1330 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1331 try {
1332 startActivity(intent);
1333 } catch (ActivityNotFoundException e) {
1334 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1335 } catch (SecurityException e) {
1336 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001337 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1339 "or use the exported attribute for this activity.", e);
1340 }
1341 }
1342
1343 private void handleFolderClick(FolderInfo folderInfo) {
1344 if (!folderInfo.opened) {
1345 // Close any open folder
1346 closeFolder();
1347 // Open the requested folder
1348 openFolder(folderInfo);
1349 } else {
1350 // Find the open folder...
1351 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1352 int folderScreen;
1353 if (openFolder != null) {
1354 folderScreen = mWorkspace.getScreenForView(openFolder);
1355 // .. and close it
1356 closeFolder(openFolder);
1357 if (folderScreen != mWorkspace.getCurrentScreen()) {
1358 // Close any folder open on the current screen
1359 closeFolder();
1360 // Pull the folder onto this screen
1361 openFolder(folderInfo);
1362 }
1363 }
1364 }
1365 }
1366
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 /**
1368 * Opens the user fodler described by the specified tag. The opening of the folder
1369 * is animated relative to the specified View. If the View is null, no animation
1370 * is played.
1371 *
1372 * @param folderInfo The FolderInfo describing the folder to open.
1373 */
1374 private void openFolder(FolderInfo folderInfo) {
1375 Folder openFolder;
1376
1377 if (folderInfo instanceof UserFolderInfo) {
1378 openFolder = UserFolder.fromXml(this);
1379 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001380 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 } else {
1382 return;
1383 }
1384
Joe Onorato00acb122009-08-04 16:04:30 -04001385 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 openFolder.setLauncher(this);
1387
1388 openFolder.bind(folderInfo);
1389 folderInfo.opened = true;
1390
1391 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1392 openFolder.onOpen();
1393 }
1394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001396 switch (v.getId()) {
1397 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001398 if (!isAllAppsVisible()) {
1399 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1400 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001401 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001402 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001403 return true;
1404 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001405 if (!isAllAppsVisible()) {
1406 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1407 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001408 showPreviews(v);
1409 }
1410 return true;
1411 case R.id.all_apps_button:
1412 if (!isAllAppsVisible()) {
1413 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1414 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1415 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001416 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001417 return true;
1418 }
1419
Joe Onorato9c1289c2009-08-17 11:03:03 -04001420 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 return false;
1422 }
1423
1424 if (!(v instanceof CellLayout)) {
1425 v = (View) v.getParent();
1426 }
1427
1428 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1429
1430 // This happens when long clicking an item with the dpad/trackball
1431 if (cellInfo == null) {
1432 return true;
1433 }
1434
1435 if (mWorkspace.allowLongPress()) {
1436 if (cellInfo.cell == null) {
1437 if (cellInfo.valid) {
1438 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001439 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 showAddDialog(cellInfo);
1441 }
1442 } else {
1443 if (!(cellInfo.cell instanceof Folder)) {
1444 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001445 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1446 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 mWorkspace.startDrag(cellInfo);
1448 }
1449 }
1450 }
1451 return true;
1452 }
1453
Romain Guye6b8e2f2009-11-10 11:56:55 -08001454 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001455 private void dismissPreview(final View v) {
1456 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001457 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001458 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1459 public void onDismiss() {
1460 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1461 int count = group.getChildCount();
1462 for (int i = 0; i < count; i++) {
1463 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1464 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001465 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1466 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1467
1468 v.setTag(R.id.workspace, null);
1469 v.setTag(R.id.icon, null);
1470 window.setOnDismissListener(null);
1471 }
1472 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001473 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001474 }
1475 v.setTag(null);
1476 }
1477
Romain Guyf8e6a802009-12-07 17:48:02 -08001478 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001479 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001480 }
1481
Romain Guya6abce82009-11-10 02:54:41 -08001482 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001483 final Resources resources = getResources();
1484 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001485
Romain Guya6abce82009-11-10 02:54:41 -08001486 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001487
1488 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001489
Romain Guyf8e6a802009-12-07 17:48:02 -08001490 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001491 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001492 int extraW = (int) ((r.left + r.right) * max);
1493 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001494
1495 int aW = cell.getWidth() - extraW;
1496 float w = aW / max;
1497
1498 int width = cell.getWidth();
1499 int height = cell.getHeight();
1500 int x = cell.getLeftPadding();
1501 int y = cell.getTopPadding();
1502 width -= (x + cell.getRightPadding());
1503 height -= (y + cell.getBottomPadding());
1504
1505 float scale = w / width;
1506
1507 int count = end - start;
1508
1509 final float sWidth = width * scale;
1510 float sHeight = height * scale;
1511
Romain Guye6b8e2f2009-11-10 11:56:55 -08001512 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001513
Romain Guye6b8e2f2009-11-10 11:56:55 -08001514 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1515 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001516
1517 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001518 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001519 cell = (CellLayout) workspace.getChildAt(i);
1520
Romain Guyf8e6a802009-12-07 17:48:02 -08001521 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001522 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001523
1524 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001525 c.scale(scale, scale);
1526 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1527 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001528
Romain Guy9d31e9f2009-11-11 18:54:28 -08001529 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001530 image.setImageBitmap(bitmap);
1531 image.setTag(i);
1532 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001533 image.setOnFocusChangeListener(handler);
1534 image.setFocusable(true);
1535 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001536
1537 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001538 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1539
1540 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001541 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001542
1543 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001544 p.setContentView(preview);
1545 p.setWidth((int) (sWidth * count + extraW));
1546 p.setHeight((int) (sHeight + extraH));
1547 p.setAnimationStyle(R.style.AnimationPreview);
1548 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001549 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001550 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001551 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001552
Romain Guye6b8e2f2009-11-10 11:56:55 -08001553 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1554 public void onDismiss() {
1555 dismissPreview(anchor);
1556 }
1557 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001558
1559 anchor.setTag(p);
1560 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001561 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001562 }
1563
Romain Guy9d31e9f2009-11-11 18:54:28 -08001564 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001565 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001566
Romain Guye6b8e2f2009-11-10 11:56:55 -08001567 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001568 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001569 }
1570
1571 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001572 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001573 v.post(this);
1574 }
1575
1576 public void run() {
1577 dismissPreview(mAnchor);
1578 }
1579
1580 public void onFocusChange(View v, boolean hasFocus) {
1581 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001582 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001583 }
Romain Guya6abce82009-11-10 02:54:41 -08001584 }
1585 }
1586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 Workspace getWorkspace() {
1588 return mWorkspace;
1589 }
1590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 @Override
1592 protected Dialog onCreateDialog(int id) {
1593 switch (id) {
1594 case DIALOG_CREATE_SHORTCUT:
1595 return new CreateShortcut().createDialog();
1596 case DIALOG_RENAME_FOLDER:
1597 return new RenameFolder().createDialog();
1598 }
1599
1600 return super.onCreateDialog(id);
1601 }
1602
1603 @Override
1604 protected void onPrepareDialog(int id, Dialog dialog) {
1605 switch (id) {
1606 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 break;
1608 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001609 if (mFolderInfo != null) {
1610 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1611 final CharSequence text = mFolderInfo.title;
1612 input.setText(text);
1613 input.setSelection(0, text.length());
1614 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 break;
1616 }
1617 }
1618
1619 void showRenameDialog(FolderInfo info) {
1620 mFolderInfo = info;
1621 mWaitingForResult = true;
1622 showDialog(DIALOG_RENAME_FOLDER);
1623 }
1624
1625 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1626 mAddItemCellInfo = cellInfo;
1627 mWaitingForResult = true;
1628 showDialog(DIALOG_CREATE_SHORTCUT);
1629 }
1630
Romain Guy73b979d2009-06-09 12:57:21 -07001631 private void pickShortcut(int requestCode, int title) {
1632 Bundle bundle = new Bundle();
1633
1634 ArrayList<String> shortcutNames = new ArrayList<String>();
1635 shortcutNames.add(getString(R.string.group_applications));
1636 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1637
1638 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1639 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1640 R.drawable.ic_launcher_application));
1641 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1642
1643 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1644 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1645 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1646 pickIntent.putExtras(bundle);
1647
1648 startActivityForResult(pickIntent, requestCode);
1649 }
1650
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 private class RenameFolder {
1652 private EditText mInput;
1653
1654 Dialog createDialog() {
1655 mWaitingForResult = true;
1656 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1657 mInput = (EditText) layout.findViewById(R.id.folder_name);
1658
1659 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1660 builder.setIcon(0);
1661 builder.setTitle(getString(R.string.rename_folder_title));
1662 builder.setCancelable(true);
1663 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1664 public void onCancel(DialogInterface dialog) {
1665 cleanup();
1666 }
1667 });
1668 builder.setNegativeButton(getString(R.string.cancel_action),
1669 new Dialog.OnClickListener() {
1670 public void onClick(DialogInterface dialog, int which) {
1671 cleanup();
1672 }
1673 }
1674 );
1675 builder.setPositiveButton(getString(R.string.rename_action),
1676 new Dialog.OnClickListener() {
1677 public void onClick(DialogInterface dialog, int which) {
1678 changeFolderName();
1679 }
1680 }
1681 );
1682 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001683
1684 final AlertDialog dialog = builder.create();
1685 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1686 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001687 mInput.requestFocus();
1688 InputMethodManager inputManager = (InputMethodManager)
1689 getSystemService(Context.INPUT_METHOD_SERVICE);
1690 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001691 }
1692 });
1693
1694 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 }
1696
1697 private void changeFolderName() {
1698 final String name = mInput.getText().toString();
1699 if (!TextUtils.isEmpty(name)) {
1700 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001701 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 mFolderInfo.title = name;
1703 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1704
Joe Onorato9c1289c2009-08-17 11:03:03 -04001705 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001706 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001707 mModel.setWorkspaceDirty();
1708 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 } else {
1710 final FolderIcon folderIcon = (FolderIcon)
1711 mWorkspace.getViewForTag(mFolderInfo);
1712 if (folderIcon != null) {
1713 folderIcon.setText(name);
1714 getWorkspace().requestLayout();
1715 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001716 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001717 mModel.setWorkspaceDirty();
1718 mWorkspaceLoading = true;
1719 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 }
1721 }
1722 }
1723 cleanup();
1724 }
1725
1726 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 dismissDialog(DIALOG_RENAME_FOLDER);
1728 mWaitingForResult = false;
1729 mFolderInfo = null;
1730 }
1731 }
1732
Joe Onoratofb0ca672009-09-14 17:55:46 -04001733 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001734 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001735 }
1736
Joe Onorato3a8820b2009-11-10 15:06:42 -08001737 void showAllApps(boolean animated) {
1738 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001739
Mike Cleronb6082fa2009-10-19 17:03:36 -07001740 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001741 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001742
Joe Onorato7c312c12009-08-13 21:36:53 -07001743 // TODO: fade these two too
1744 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001745 }
1746
Joe Onoratob2061212009-11-24 16:13:54 -05001747 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001748 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001749 * - Home from workspace
1750 * - from center screen
1751 * - from other screens
1752 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001753 * - from center screen
1754 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001755 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001756 * - from center screen
1757 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001758 * - Launch app from workspace and quit
1759 * - with back
1760 * - with home
1761 * - Launch app from all apps and quit
1762 * - with back
1763 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001764 * - Go to a screen that's not the default, then all
1765 * apps, and launch and app, and go back
1766 * - with back
1767 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001768 * - On workspace, long press power and go back
1769 * - with back
1770 * - with home
1771 * - On all apps, long press power and go back
1772 * - with back
1773 * - with home
1774 * - On workspace, power off
1775 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001776 * - Launch an app and turn off the screen while in that app
1777 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001778 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001779 * - From all apps
1780 * - From workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001781 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001782 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001783 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001784 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001785 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001786 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001787 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001788 }
1789
Joe Onorato7c312c12009-08-13 21:36:53 -07001790 void lockAllApps() {
1791 // TODO
1792 }
1793
1794 void unlockAllApps() {
1795 // TODO
1796 }
1797
Joe Onorato7404ee42009-07-31 11:54:44 -07001798 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 * Displays the shortcut creation dialog and launches, if necessary, the
1800 * appropriate activity.
1801 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001802 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001803 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1804 DialogInterface.OnShowListener {
1805
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001807
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 Dialog createDialog() {
1809 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001810
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001812
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1814 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001815 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001816
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 builder.setInverseBackgroundForced(true);
1818
1819 AlertDialog dialog = builder.create();
1820 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001821 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001822 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 return dialog;
1825 }
1826
1827 public void onCancel(DialogInterface dialog) {
1828 mWaitingForResult = false;
1829 cleanup();
1830 }
1831
Romain Guycbb89e42009-06-08 15:52:54 -07001832 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001833 }
1834
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001836 try {
1837 dismissDialog(DIALOG_CREATE_SHORTCUT);
1838 } catch (Exception e) {
1839 // An exception is thrown if the dialog is not visible, which is fine
1840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 }
1842
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001843 /**
1844 * Handle the action clicked in the "Add to home" dialog.
1845 */
1846 public void onClick(DialogInterface dialog, int which) {
1847 Resources res = getResources();
1848 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001849
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001850 switch (which) {
1851 case AddAdapter.ITEM_SHORTCUT: {
1852 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001853 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001854 break;
1855 }
Romain Guycbb89e42009-06-08 15:52:54 -07001856
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001857 case AddAdapter.ITEM_APPWIDGET: {
1858 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001859
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001860 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1861 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001862 // start the pick activity
1863 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1864 break;
1865 }
Romain Guycbb89e42009-06-08 15:52:54 -07001866
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001867 case AddAdapter.ITEM_LIVE_FOLDER: {
1868 // Insert extra item to handle inserting folder
1869 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001870
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001871 ArrayList<String> shortcutNames = new ArrayList<String>();
1872 shortcutNames.add(res.getString(R.string.group_folder));
1873 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001874
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001875 ArrayList<ShortcutIconResource> shortcutIcons =
1876 new ArrayList<ShortcutIconResource>();
1877 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1878 R.drawable.ic_launcher_folder));
1879 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1880
1881 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1882 pickIntent.putExtra(Intent.EXTRA_INTENT,
1883 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1884 pickIntent.putExtra(Intent.EXTRA_TITLE,
1885 getText(R.string.title_select_live_folder));
1886 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001887
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001888 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1889 break;
1890 }
1891
1892 case AddAdapter.ITEM_WALLPAPER: {
1893 startWallpaper();
1894 break;
1895 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 }
1897 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001898
1899 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 }
1902
1903 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001904 * Receives notifications when applications are added/removed.
1905 */
1906 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1907 @Override
1908 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001909 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05001910 String reason = intent.getStringExtra("reason");
1911 if (!"homekey".equals(reason)) {
1912 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001913 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05001914 animate = false;
1915 }
Joe Onoratob2061212009-11-24 16:13:54 -05001916 closeAllApps(animate);
1917 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001918 }
1919 }
1920
1921 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001922 * Receives notifications whenever the appwidgets are reset.
1923 */
1924 private class AppWidgetResetObserver extends ContentObserver {
1925 public AppWidgetResetObserver() {
1926 super(new Handler());
1927 }
1928
1929 @Override
1930 public void onChange(boolean selfChange) {
1931 onAppWidgetReset();
1932 }
1933 }
1934
1935 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001936 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001938 public int getCurrentWorkspaceScreen() {
1939 return mWorkspace.getCurrentScreen();
1940 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001941
Joe Onorato9c1289c2009-08-17 11:03:03 -04001942 /**
1943 * Refreshes the shortcuts shown on the workspace.
1944 *
1945 * Implementation of the method from LauncherModel.Callbacks.
1946 */
1947 public void startBinding() {
1948 final Workspace workspace = mWorkspace;
1949 int count = workspace.getChildCount();
1950 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001951 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001952 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1953 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001954
Joe Onorato9c1289c2009-08-17 11:03:03 -04001955 if (DEBUG_USER_INTERFACE) {
1956 android.widget.Button finishButton = new android.widget.Button(this);
1957 finishButton.setText("Finish");
1958 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1959
1960 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1961 public void onClick(View v) {
1962 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001963 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001964 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
1966 }
1967
1968 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001969 * Bind the items start-end from the list.
1970 *
1971 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001973 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1974
1975 final Workspace workspace = mWorkspace;
1976
1977 for (int i=start; i<end; i++) {
1978 final ItemInfo item = shortcuts.get(i);
1979 mDesktopItems.add(item);
1980 switch (item.itemType) {
1981 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1982 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1983 final View shortcut = createShortcut((ApplicationInfo) item);
1984 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1985 false);
1986 break;
1987 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1988 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1989 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1990 (UserFolderInfo) item);
1991 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1992 false);
1993 break;
1994 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1995 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1996 R.layout.live_folder_icon, this,
1997 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1998 (LiveFolderInfo) item);
1999 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2000 false);
2001 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 }
2004
Joe Onorato9c1289c2009-08-17 11:03:03 -04002005 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 }
2007
Joe Onorato9c1289c2009-08-17 11:03:03 -04002008 /**
2009 * Implementation of the method from LauncherModel.Callbacks.
2010 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002011 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2012 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002013 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002015
2016 /**
2017 * Add the views for a widget to the workspace.
2018 *
2019 * Implementation of the method from LauncherModel.Callbacks.
2020 */
2021 public void bindAppWidget(LauncherAppWidgetInfo item) {
2022 final Workspace workspace = mWorkspace;
2023
2024 final int appWidgetId = item.appWidgetId;
2025 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2026 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2027
Joe Onorato9c1289c2009-08-17 11:03:03 -04002028 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2029 item.hostView.setTag(item);
2030
2031 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2032 item.cellY, item.spanX, item.spanY, false);
2033
2034 workspace.requestLayout();
2035
2036 mDesktopItems.add(item);
2037 }
2038
2039 /**
2040 * Callback saying that there aren't any more items to bind.
2041 *
2042 * Implementation of the method from LauncherModel.Callbacks.
2043 */
2044 public void finishBindingItems() {
2045 if (mSavedState != null) {
2046 if (!mWorkspace.hasFocus()) {
2047 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2048 }
2049
2050 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2051 if (userFolders != null) {
2052 for (long folderId : userFolders) {
2053 final FolderInfo info = mFolders.get(folderId);
2054 if (info != null) {
2055 openFolder(info);
2056 }
2057 }
2058 final Folder openFolder = mWorkspace.getOpenFolder();
2059 if (openFolder != null) {
2060 openFolder.requestFocus();
2061 }
2062 }
2063
Joe Onorato9c1289c2009-08-17 11:03:03 -04002064 mSavedState = null;
2065 }
2066
2067 if (mSavedInstanceState != null) {
2068 super.onRestoreInstanceState(mSavedInstanceState);
2069 mSavedInstanceState = null;
2070 }
2071
Joe Onorato9c1289c2009-08-17 11:03:03 -04002072 mWorkspaceLoading = false;
2073 }
2074
2075 /**
2076 * Add the icons for all apps.
2077 *
2078 * Implementation of the method from LauncherModel.Callbacks.
2079 */
2080 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002081 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002082 }
2083
2084 /**
2085 * A package was installed.
2086 *
2087 * Implementation of the method from LauncherModel.Callbacks.
2088 */
2089 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2090 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002091 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002092 }
2093
2094 /**
2095 * A package was updated.
2096 *
2097 * Implementation of the method from LauncherModel.Callbacks.
2098 */
2099 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2100 removeDialog(DIALOG_CREATE_SHORTCUT);
2101 mWorkspace.updateShortcutsForPackage(packageName);
Joe Onoratof0be2132009-11-24 19:34:29 -05002102 mAllAppsGrid.updateApps(packageName, apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002103 }
2104
2105 /**
2106 * A package was uninstalled.
2107 *
2108 * Implementation of the method from LauncherModel.Callbacks.
2109 */
2110 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2111 removeDialog(DIALOG_CREATE_SHORTCUT);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002112 mWorkspace.removeItemsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002113 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002114 }
Joe Onoratobe386092009-11-17 17:32:16 -08002115
2116 /**
2117 * Prints out out state for debugging.
2118 */
2119 public void dumpState() {
2120 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002121 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002122 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2123 Log.d(TAG, "mRestoring=" + mRestoring);
2124 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2125 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2126 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2127 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002128 mModel.dumpState();
2129 mAllAppsGrid.dumpState();
2130 Log.d(TAG, "END launcher2 dump state");
2131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132}