blob: d29d721878b0c093ff7b952a7b29b6df593f0fef [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;
64import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.widget.TextView;
67import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080068import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080069import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080070import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070071import android.appwidget.AppWidgetManager;
72import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040075import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070076import java.io.DataOutputStream;
77import java.io.FileNotFoundException;
78import java.io.IOException;
79import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
81/**
82 * Default launcher application.
83 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040084public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080085 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080086 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 static final boolean LOGD = false;
88
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 static final boolean PROFILE_STARTUP = false;
90 static final boolean PROFILE_ROTATE = false;
91 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070092
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 private static final int WALLPAPER_SCREENS_SPAN = 2;
94
95 private static final int MENU_GROUP_ADD = 1;
96 private static final int MENU_ADD = Menu.FIRST + 1;
97 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
98 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
99 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700100 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
102 private static final int REQUEST_CREATE_SHORTCUT = 1;
103 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700104 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 private static final int REQUEST_PICK_APPLICATION = 6;
106 private static final int REQUEST_PICK_SHORTCUT = 7;
107 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700108 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700109 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
111 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
112
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800113 static final int SCREEN_COUNT = 5;
114 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700116 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Joe Onorato7c312c12009-08-13 21:36:53 -0700118 static final int DIALOG_CREATE_SHORTCUT = 1;
119 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Romain Guy98d01652009-06-30 16:21:04 -0700121 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
123 // Type: int
124 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
125 // Type: boolean
126 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
127 // Type: long
128 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
129 // Type: int
130 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
131 // Type: int
132 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
133 // Type: int
134 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
143 // Type: int[]
144 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
145 // Type: boolean
146 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
147 // Type: long
148 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
149
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800153 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800155 private final BroadcastReceiver mCloseSystemDialogsReceiver
156 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800157 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 private LayoutInflater mInflater;
160
Joe Onorato00acb122009-08-04 16:04:30 -0400161 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700163
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700164 private AppWidgetManager mAppWidgetManager;
165 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private CellLayout.CellInfo mAddItemCellInfo;
168 private CellLayout.CellInfo mMenuAddInfo;
169 private final int[] mCellCoordinates = new int[2];
170 private FolderInfo mFolderInfo;
171
Joe Onorato7c312c12009-08-13 21:36:53 -0700172 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700173 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700174 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private Bundle mSavedState;
177
178 private SpannableStringBuilder mDefaultKeySsb = null;
179
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 private boolean mWorkspaceLoading = true;
181
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800182 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private boolean mRestoring;
184 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
186 private Bundle mSavedInstanceState;
187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 private LauncherModel mModel;
189
Romain Guy84f296c2009-11-04 15:00:44 -0800190 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
191 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700192
Romain Guy1fbc1c82009-11-09 20:43:08 -0800193 private ImageView mPreviousView;
194 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 @Override
197 protected void onCreate(Bundle savedInstanceState) {
198 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400201 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700205 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
206 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 if (PROFILE_STARTUP) {
209 android.os.Debug.startMethodTracing("/sdcard/launcher");
210 }
211
212 checkForLocaleChange();
213 setWallpaperDimension();
214
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 setContentView(R.layout.launcher);
216 setupViews();
217
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800218 registerContentObservers();
219
Joe Onorato7c312c12009-08-13 21:36:53 -0700220 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 mSavedState = savedInstanceState;
223 restoreState(mSavedState);
224
225 if (PROFILE_STARTUP) {
226 android.os.Debug.stopMethodTracing();
227 }
228
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700229 // We have a new AllAppsView, we need to re-bind everything, and it could have
230 // changed in our absence.
231 mModel.setAllAppsDirty();
232 mModel.setWorkspaceDirty();
233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 }
237
238 // For handling default keys
239 mDefaultKeySsb = new SpannableStringBuilder();
240 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800241
242 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
243 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 }
Romain Guycbb89e42009-06-08 15:52:54 -0700245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700247 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
248 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 final Configuration configuration = getResources().getConfiguration();
251
Romain Guy98d01652009-06-30 16:21:04 -0700252 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 final String locale = configuration.locale.toString();
254
Romain Guy98d01652009-06-30 16:21:04 -0700255 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 final int mcc = configuration.mcc;
257
Romain Guy98d01652009-06-30 16:21:04 -0700258 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 final int mnc = configuration.mnc;
260
Romain Guy84f296c2009-11-04 15:00:44 -0800261 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
Romain Guy84f296c2009-11-04 15:00:44 -0800263 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700264 localeConfiguration.locale = locale;
265 localeConfiguration.mcc = mcc;
266 localeConfiguration.mnc = mnc;
267
268 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700270 }
271 }
272
273 private static class LocaleConfiguration {
274 public String locale;
275 public int mcc = -1;
276 public int mnc = -1;
277 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700278
Romain Guy98d01652009-06-30 16:21:04 -0700279 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
280 DataInputStream in = null;
281 try {
282 in = new DataInputStream(context.openFileInput(PREFERENCES));
283 configuration.locale = in.readUTF();
284 configuration.mcc = in.readInt();
285 configuration.mnc = in.readInt();
286 } catch (FileNotFoundException e) {
287 // Ignore
288 } catch (IOException e) {
289 // Ignore
290 } finally {
291 if (in != null) {
292 try {
293 in.close();
294 } catch (IOException e) {
295 // Ignore
296 }
297 }
298 }
299 }
300
301 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
302 DataOutputStream out = null;
303 try {
304 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
305 out.writeUTF(configuration.locale);
306 out.writeInt(configuration.mcc);
307 out.writeInt(configuration.mnc);
308 out.flush();
309 } catch (FileNotFoundException e) {
310 // Ignore
311 } catch (IOException e) {
312 //noinspection ResultOfMethodCallIgnored
313 context.getFileStreamPath(PREFERENCES).delete();
314 } finally {
315 if (out != null) {
316 try {
317 out.close();
318 } catch (IOException e) {
319 // Ignore
320 }
321 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
323 }
324
325 static int getScreen() {
326 synchronized (sLock) {
327 return sScreen;
328 }
329 }
330
331 static void setScreen(int screen) {
332 synchronized (sLock) {
333 sScreen = screen;
334 }
335 }
336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700338 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339
340 Display display = getWindowManager().getDefaultDisplay();
341 boolean isPortrait = display.getWidth() < display.getHeight();
342
343 final int width = isPortrait ? display.getWidth() : display.getHeight();
344 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700345 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 }
347
348 @Override
349 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700350 mWaitingForResult = false;
351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 // The pattern used here is that a user PICKs a specific application,
353 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
356 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700357
Romain Guy94dabf12009-07-21 10:55:43 -0700358 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 switch (requestCode) {
360 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400361 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 break;
363 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700364 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 break;
366 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 break;
369 case REQUEST_PICK_LIVE_FOLDER:
370 addLiveFolder(data);
371 break;
372 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700375 case REQUEST_PICK_APPWIDGET:
376 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700378 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700381 case REQUEST_PICK_WALLPAPER:
382 // We just wanted the activity result here so we can clear mWaitingForResult
383 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 }
Romain Guy18042c82009-11-06 11:44:55 -0800385 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
386 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
387 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700388 // Clean up the appWidgetId if we canceled
389 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
390 if (appWidgetId != -1) {
391 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
393 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 }
395
396 @Override
397 protected void onResume() {
398 super.onResume();
399
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800400 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800401
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400403 mWorkspaceLoading = true;
404 mModel.startLoader(this, true);
405 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 }
407 }
408
409 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700410 protected void onPause() {
411 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800412 dismissPreview(mPreviousView);
413 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800414 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700415 }
Romain Guycbb89e42009-06-08 15:52:54 -0700416
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700417 @Override
418 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400419 // Flag the loader to stop early before switching
420 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700421
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700422 if (PROFILE_ROTATE) {
423 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
424 }
425 return null;
426 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 private boolean acceptFilter() {
429 final InputMethodManager inputManager = (InputMethodManager)
430 getSystemService(Context.INPUT_METHOD_SERVICE);
431 return !inputManager.isFullscreenMode();
432 }
433
434 @Override
435 public boolean onKeyDown(int keyCode, KeyEvent event) {
436 boolean handled = super.onKeyDown(keyCode, event);
437 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
438 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
439 keyCode, event);
440 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700441 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700442 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700443 // showSearchDialog()
444 // If there are multiple keystrokes before the search dialog takes focus,
445 // onSearchRequested() will be called for every keystroke,
446 // but it is idempotent, so it's fine.
447 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 }
449 }
450
451 return handled;
452 }
453
Karl Rosaen138a0412009-04-23 19:00:21 -0700454 private String getTypedText() {
455 return mDefaultKeySsb.toString();
456 }
457
458 private void clearTypedText() {
459 mDefaultKeySsb.clear();
460 mDefaultKeySsb.clearSpans();
461 Selection.setSelection(mDefaultKeySsb, 0);
462 }
463
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 /**
465 * Restores the previous state, if it exists.
466 *
467 * @param savedState The previous state.
468 */
469 private void restoreState(Bundle savedState) {
470 if (savedState == null) {
471 return;
472 }
473
Joe Onorato3a8820b2009-11-10 15:06:42 -0800474 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
475 if (allApps) {
476 showAllApps(false);
477 }
478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
480 if (currentScreen > -1) {
481 mWorkspace.setCurrentScreen(currentScreen);
482 }
483
484 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
485 if (addScreen > -1) {
486 mAddItemCellInfo = new CellLayout.CellInfo();
487 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
488 addItemCellInfo.valid = true;
489 addItemCellInfo.screen = addScreen;
490 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
491 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
492 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
493 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
494 addItemCellInfo.findVacantCellsFromOccupied(
495 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
496 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
497 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
498 mRestoring = true;
499 }
500
501 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
502 if (renameFolder) {
503 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400504 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 mRestoring = true;
506 }
507 }
508
509 /**
510 * Finds all the views we need and configure them properly.
511 */
512 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400513 DragController dragController = mDragController;
514
Romain Guyb1b69f52009-08-10 15:10:15 -0700515 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400516 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517
Joe Onorato7c312c12009-08-13 21:36:53 -0700518 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700519 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700520 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700521 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700522 // Manage focusability manually since this thing is always visible
523 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700524
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800525 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
526 final Workspace workspace = mWorkspace;
527
Joe Onorato7c312c12009-08-13 21:36:53 -0700528 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
529 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530
Joe Onorato7c312c12009-08-13 21:36:53 -0700531 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700532 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700533 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800534 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800535
536 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
537 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
538
539 Drawable previous = mPreviousView.getDrawable();
540 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800541 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542
Joe Onorato0d44e942009-11-16 18:20:51 -0800543 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800544 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800545 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800546 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800547
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400549 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551
552 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400553 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700554 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555
Joe Onorato00acb122009-08-04 16:04:30 -0400556 dragController.setDragScoller(workspace);
557 dragController.setDragListener(deleteZone);
558 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800559 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700560
Joe Onorato00acb122009-08-04 16:04:30 -0400561 // The order here is bottom to top.
562 dragController.addDropTarget(workspace);
563 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 }
565
Romain Guy8a73c512009-11-09 19:19:59 -0800566 @SuppressWarnings({"UnusedDeclaration"})
567 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800568 if (!isAllAppsVisible()) {
569 mWorkspace.scrollLeft();
570 }
Romain Guy8a73c512009-11-09 19:19:59 -0800571 }
572
573 @SuppressWarnings({"UnusedDeclaration"})
574 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800575 if (!isAllAppsVisible()) {
576 mWorkspace.scrollRight();
577 }
Romain Guy8a73c512009-11-09 19:19:59 -0800578 }
579
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 /**
581 * Creates a view representing a shortcut.
582 *
583 * @param info The data structure describing the shortcut.
584 *
585 * @return A View inflated from R.layout.application.
586 */
587 View createShortcut(ApplicationInfo info) {
588 return createShortcut(R.layout.application,
589 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
590 }
591
592 /**
593 * Creates a view representing a shortcut inflated from the specified resource.
594 *
595 * @param layoutResId The id of the XML layout used to create the shortcut.
596 * @param parent The group the shortcut belongs to.
597 * @param info The data structure describing the shortcut.
598 *
599 * @return A View inflated from layoutResId.
600 */
601 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
602 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
603
Joe Onorato5162ea92009-09-03 09:39:42 -0700604 if (info.icon == null) {
605 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
606 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700608 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 info.filtered = true;
610 }
611
612 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
613 favorite.setText(info.title);
614 favorite.setTag(info);
615 favorite.setOnClickListener(this);
616
617 return favorite;
618 }
619
620 /**
621 * Add an application shortcut to the workspace.
622 *
623 * @param data The intent describing the application.
624 * @param cellInfo The position on screen where to create the shortcut.
625 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400626 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 cellInfo.screen = mWorkspace.getCurrentScreen();
628 if (!findSingleSlot(cellInfo)) return;
629
Romain Guy73b979d2009-06-09 12:57:21 -0700630 final ApplicationInfo info = infoFromApplicationIntent(context, data);
631 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400632 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700633 }
634 }
635
636 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 ComponentName component = data.getComponent();
638 PackageManager packageManager = context.getPackageManager();
639 ActivityInfo activityInfo = null;
640 try {
641 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
642 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800643 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 }
Romain Guycbb89e42009-06-08 15:52:54 -0700645
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 if (activityInfo != null) {
647 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700648
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 itemInfo.title = activityInfo.loadLabel(packageManager);
650 if (itemInfo.title == null) {
651 itemInfo.title = activityInfo.name;
652 }
Romain Guycbb89e42009-06-08 15:52:54 -0700653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
655 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
656 itemInfo.icon = activityInfo.loadIcon(packageManager);
657 itemInfo.container = ItemInfo.NO_ID;
658
Romain Guy73b979d2009-06-09 12:57:21 -0700659 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Romain Guy73b979d2009-06-09 12:57:21 -0700661
662 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
Romain Guycbb89e42009-06-08 15:52:54 -0700664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 /**
666 * Add a shortcut to the workspace.
667 *
668 * @param data The intent describing the shortcut.
669 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400671 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 cellInfo.screen = mWorkspace.getCurrentScreen();
673 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
676
677 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400679 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
680 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
682 }
683
Romain Guycbb89e42009-06-08 15:52:54 -0700684
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688 * @param data The intent describing the appWidgetId.
689 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700693 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700694
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800695 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700696
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700698
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700699 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700701 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700702
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 // Try finding open space on Launcher screen
704 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800705 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
706 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
707 return;
708 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700710 // Build Launcher-specific widget info and save to database
711 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 launcherInfo.spanX = spans[0];
713 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 LauncherModel.addItemToDatabase(this, launcherInfo,
716 LauncherSettings.Favorites.CONTAINER_DESKTOP,
717 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
718
719 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700723 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700724
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700725 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400729 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 }
731 }
Romain Guycbb89e42009-06-08 15:52:54 -0700732
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
734 mDesktopItems.remove(launcherInfo);
735 launcherInfo.hostView = null;
736 }
737
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700738 public LauncherAppWidgetHost getAppWidgetHost() {
739 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
Romain Guycbb89e42009-06-08 15:52:54 -0700741
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 static ApplicationInfo addShortcut(Context context, Intent data,
743 CellLayout.CellInfo cellInfo, boolean notify) {
744
Romain Guy73b979d2009-06-09 12:57:21 -0700745 final ApplicationInfo info = infoFromShortcutIntent(context, data);
746 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
747 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
748
749 return info;
750 }
751
752 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
754 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guyb5de7752010-01-04 14:43:49 -0800755 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756
757 Drawable icon = null;
758 boolean filtered = false;
759 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700760 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761
Romain Guyb5de7752010-01-04 14:43:49 -0800762 if (bitmap != null && bitmap instanceof Bitmap) {
763 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail((Bitmap) bitmap, context));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 filtered = true;
765 customIcon = true;
766 } else {
767 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700768 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700770 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 final PackageManager packageManager = context.getPackageManager();
772 Resources resources = packageManager.getResourcesForApplication(
773 iconResource.packageName);
774 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
775 icon = resources.getDrawable(id);
776 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800777 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 }
779 }
780 }
781
782 if (icon == null) {
783 icon = context.getPackageManager().getDefaultActivityIcon();
784 }
785
786 final ApplicationInfo info = new ApplicationInfo();
787 info.icon = icon;
788 info.filtered = filtered;
789 info.title = name;
790 info.intent = intent;
791 info.customIcon = customIcon;
792 info.iconResource = iconResource;
793
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 return info;
795 }
796
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800797 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800798 getWindow().closeAllPanels();
799
800 try {
801 dismissDialog(DIALOG_CREATE_SHORTCUT);
802 // Unlock the workspace if the dialog was showing
803 } catch (Exception e) {
804 // An exception is thrown if the dialog is not visible, which is fine
805 }
806
807 try {
808 dismissDialog(DIALOG_RENAME_FOLDER);
809 // Unlock the workspace if the dialog was showing
810 } catch (Exception e) {
811 // An exception is thrown if the dialog is not visible, which is fine
812 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800813
814 // Whatever we were doing is hereby canceled.
815 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800816 }
817
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 @Override
819 protected void onNewIntent(Intent intent) {
820 super.onNewIntent(intent);
821
822 // Close the menu
823 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800824 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800825 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700826
Joe Onorato14f122b2009-11-19 14:06:36 -0800827 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
828 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
829 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800830 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800831 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800832 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800833 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700834
Joe Onorato3a8820b2009-11-10 15:06:42 -0800835 final View v = getWindow().peekDecorView();
836 if (v != null && v.getWindowToken() != null) {
837 InputMethodManager imm = (InputMethodManager)getSystemService(
838 INPUT_METHOD_SERVICE);
839 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 }
841 }
842 }
843
844 @Override
845 protected void onRestoreInstanceState(Bundle savedInstanceState) {
846 // Do not call super here
847 mSavedInstanceState = savedInstanceState;
848 }
849
850 @Override
851 protected void onSaveInstanceState(Bundle outState) {
852 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
853
854 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
855 if (folders.size() > 0) {
856 final int count = folders.size();
857 long[] ids = new long[count];
858 for (int i = 0; i < count; i++) {
859 final FolderInfo info = folders.get(i).getInfo();
860 ids[i] = info.id;
861 }
862 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
863 } else {
864 super.onSaveInstanceState(outState);
865 }
866
Joe Onoratofb0ca672009-09-14 17:55:46 -0400867 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800868 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871
872 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
873 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
874 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
875
876 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
877 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
878 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
879 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
880 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
881 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
882 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
883 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
884 layout.getOccupiedCells());
885 }
886
887 if (mFolderInfo != null && mWaitingForResult) {
888 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
889 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
890 }
891 }
892
893 @Override
894 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700896
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700898 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800900 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 }
902
903 TextKeyListener.getInstance().release();
904
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906
Joe Onorato9c1289c2009-08-17 11:03:03 -0400907 unbindDesktopItems();
908 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800909
910 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800911
912 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800913 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800914
915 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 }
917
918 @Override
919 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700920 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 super.startActivityForResult(intent, requestCode);
922 }
923
924 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700925 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700927
Joe Onorato7bb17492009-09-24 17:51:01 -0700928 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700929
Karl Rosaen138a0412009-04-23 19:00:21 -0700930 if (initialQuery == null) {
931 // Use any text typed in the launcher as the initial query
932 initialQuery = getTypedText();
933 clearTypedText();
934 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 if (appSearchData == null) {
936 appSearchData = new Bundle();
937 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
938 }
Romain Guycbb89e42009-06-08 15:52:54 -0700939
Karl Rosaen138a0412009-04-23 19:00:21 -0700940 final SearchManager searchManager =
941 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -0700942 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700943 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945
946 @Override
947 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 if (isWorkspaceLocked()) {
949 return false;
950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951
952 super.onCreateOptionsMenu(menu);
953 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
954 .setIcon(android.R.drawable.ic_menu_add)
955 .setAlphabeticShortcut('A');
956 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
957 .setIcon(android.R.drawable.ic_menu_gallery)
958 .setAlphabeticShortcut('W');
959 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
960 .setIcon(android.R.drawable.ic_search_category_default)
961 .setAlphabeticShortcut(SearchManager.MENU_KEY);
962 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
963 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
964 .setAlphabeticShortcut('N');
965
966 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700967 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
968 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969
970 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
971 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
972 .setIntent(settings);
973
974 return true;
975 }
976
977 @Override
978 public boolean onPrepareOptionsMenu(Menu menu) {
979 super.onPrepareOptionsMenu(menu);
980
981 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
982 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
983
984 return true;
985 }
986
987 @Override
988 public boolean onOptionsItemSelected(MenuItem item) {
989 switch (item.getItemId()) {
990 case MENU_ADD:
991 addItems();
992 return true;
993 case MENU_WALLPAPER_SETTINGS:
994 startWallpaper();
995 return true;
996 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700997 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 return true;
999 case MENU_NOTIFICATIONS:
1000 showNotifications();
1001 return true;
1002 }
1003
1004 return super.onOptionsItemSelected(item);
1005 }
Romain Guycbb89e42009-06-08 15:52:54 -07001006
Karl Rosaen138a0412009-04-23 19:00:21 -07001007 /**
1008 * Indicates that we want global search for this activity by setting the globalSearch
1009 * argument for {@link #startSearch} to true.
1010 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001012 @Override
1013 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001014 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001015 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001016 }
1017
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018 public boolean isWorkspaceLocked() {
1019 return mWorkspaceLoading || mWaitingForResult;
1020 }
1021
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001023 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 showAddDialog(mMenuAddInfo);
1025 }
1026
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001027 void addAppWidget(Intent data) {
1028 // TODO: catch bad widget exception when sent
1029 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001030 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031
Bjorn Bringert7984c942009-12-09 15:38:25 +00001032 if (appWidget.configure != null) {
1033 // Launch over to configure widget, if needed
1034 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1035 intent.setComponent(appWidget.configure);
1036 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1037
1038 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001040 // Otherwise just add it
1041 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 }
1043 }
Romain Guycbb89e42009-06-08 15:52:54 -07001044
Romain Guy73b979d2009-06-09 12:57:21 -07001045 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001046 // Handle case where user selected "Applications"
1047 String applicationName = getResources().getString(R.string.group_applications);
1048 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001049
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001050 if (applicationName != null && applicationName.equals(shortcutName)) {
1051 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1052 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001053
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001054 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1055 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001056 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001057 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001058 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001059 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 }
1061
1062 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001063 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001064 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001065 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001066
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001067 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001068 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001069 } else {
1070 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1071 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 }
1073
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 UserFolderInfo folderInfo = new UserFolderInfo();
1076 folderInfo.title = getText(R.string.folder_name);
1077
1078 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1079 cellInfo.screen = mWorkspace.getCurrentScreen();
1080 if (!findSingleSlot(cellInfo)) return;
1081
1082 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001083 LauncherModel.addItemToDatabase(this, folderInfo,
1084 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001086 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087
1088 // Create the view
1089 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1090 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1091 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001092 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
Romain Guycbb89e42009-06-08 15:52:54 -07001094
Joe Onorato9c1289c2009-08-17 11:03:03 -04001095 void removeFolder(FolderInfo folder) {
1096 mFolders.remove(folder.id);
1097 }
1098
1099 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 cellInfo.screen = mWorkspace.getCurrentScreen();
1101 if (!findSingleSlot(cellInfo)) return;
1102
1103 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1104
1105 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1107 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1109 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 }
1111 }
1112
1113 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1114 CellLayout.CellInfo cellInfo, boolean notify) {
1115
1116 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1117 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1118
1119 Drawable icon = null;
1120 boolean filtered = false;
1121 Intent.ShortcutIconResource iconResource = null;
1122
1123 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1124 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1125 try {
1126 iconResource = (Intent.ShortcutIconResource) extra;
1127 final PackageManager packageManager = context.getPackageManager();
1128 Resources resources = packageManager.getResourcesForApplication(
1129 iconResource.packageName);
1130 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1131 icon = resources.getDrawable(id);
1132 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001133 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 }
1135 }
1136
1137 if (icon == null) {
1138 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1139 }
1140
1141 final LiveFolderInfo info = new LiveFolderInfo();
1142 info.icon = icon;
1143 info.filtered = filtered;
1144 info.title = name;
1145 info.iconResource = iconResource;
1146 info.uri = data.getData();
1147 info.baseIntent = baseIntent;
1148 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1149 LiveFolders.DISPLAY_MODE_GRID);
1150
1151 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1152 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001153 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154
1155 return info;
1156 }
1157
1158 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1159 final int[] xy = new int[2];
1160 if (findSlot(cellInfo, xy, 1, 1)) {
1161 cellInfo.cellX = xy[0];
1162 cellInfo.cellY = xy[1];
1163 return true;
1164 }
1165 return false;
1166 }
1167
1168 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1169 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1170 boolean[] occupied = mSavedState != null ?
1171 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1172 cellInfo = mWorkspace.findAllVacantCells(occupied);
1173 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1174 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1175 return false;
1176 }
1177 }
1178 return true;
1179 }
1180
1181 private void showNotifications() {
1182 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1183 if (statusBar != null) {
1184 statusBar.expand();
1185 }
1186 }
1187
1188 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001189 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001191 Intent chooser = Intent.createChooser(pickWallpaper,
1192 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001193 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1194 // Removed in Eclair MR1
1195// WallpaperManager wm = (WallpaperManager)
1196// getSystemService(Context.WALLPAPER_SERVICE);
1197// WallpaperInfo wi = wm.getWallpaperInfo();
1198// if (wi != null && wi.getSettingsActivity() != null) {
1199// LabeledIntent li = new LabeledIntent(getPackageName(),
1200// R.string.configure_wallpaper, 0);
1201// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1202// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1203// }
Mike Clerona0618e42009-10-22 13:55:21 -07001204 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
1206
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001207 /**
1208 * Registers various content observers. The current implementation registers
1209 * only a favorites observer to keep track of the favorites applications.
1210 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001211 private void registerContentObservers() {
1212 ContentResolver resolver = getContentResolver();
1213 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1214 true, mWidgetObserver);
1215 }
1216
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 @Override
1218 public boolean dispatchKeyEvent(KeyEvent event) {
1219 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1220 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001222 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001223 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001224 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001225 dumpState();
1226 return true;
1227 }
1228 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001229 }
1230 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1231 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001232 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 return true;
1234 }
1235 }
1236
1237 return super.dispatchKeyEvent(event);
1238 }
1239
Joe Onorato88ec0992009-11-19 13:16:06 -08001240 @Override
1241 public void onBackPressed() {
1242 if (isAllAppsVisible()) {
1243 closeAllApps(true);
1244 } else {
1245 closeFolder();
1246 }
1247 dismissPreview(mPreviousView);
1248 dismissPreview(mNextView);
1249 }
1250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 private void closeFolder() {
1252 Folder folder = mWorkspace.getOpenFolder();
1253 if (folder != null) {
1254 closeFolder(folder);
1255 }
1256 }
1257
1258 void closeFolder(Folder folder) {
1259 folder.getInfo().opened = false;
1260 ViewGroup parent = (ViewGroup) folder.getParent();
1261 if (parent != null) {
1262 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001263 if (folder instanceof DropTarget) {
1264 // Live folders aren't DropTargets.
1265 mDragController.removeDropTarget((DropTarget)folder);
1266 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 }
1268 folder.onClose();
1269 }
1270
1271 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001272 * Re-listen when widgets are reset.
1273 */
1274 private void onAppWidgetReset() {
1275 mAppWidgetHost.startListening();
1276 }
1277
1278 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001279 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1280 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001282 private void unbindDesktopItems() {
1283 for (ItemInfo item: mDesktopItems) {
1284 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
1286 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 /**
1289 * Launches the intent referred by the clicked shortcut.
1290 *
1291 * @param v The view representing the clicked shortcut.
1292 */
1293 public void onClick(View v) {
1294 Object tag = v.getTag();
1295 if (tag instanceof ApplicationInfo) {
1296 // Open shortcut
1297 final Intent intent = ((ApplicationInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001298 int[] pos = new int[2];
1299 v.getLocationOnScreen(pos);
1300 intent.setSourceBounds(
1301 new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 startActivitySafely(intent);
1303 } else if (tag instanceof FolderInfo) {
1304 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001305 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001306 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001307 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001308 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001309 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
1312 }
1313
1314 void startActivitySafely(Intent intent) {
1315 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1316 try {
1317 startActivity(intent);
1318 } catch (ActivityNotFoundException e) {
1319 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1320 } catch (SecurityException e) {
1321 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001322 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1324 "or use the exported attribute for this activity.", e);
1325 }
1326 }
1327
1328 private void handleFolderClick(FolderInfo folderInfo) {
1329 if (!folderInfo.opened) {
1330 // Close any open folder
1331 closeFolder();
1332 // Open the requested folder
1333 openFolder(folderInfo);
1334 } else {
1335 // Find the open folder...
1336 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1337 int folderScreen;
1338 if (openFolder != null) {
1339 folderScreen = mWorkspace.getScreenForView(openFolder);
1340 // .. and close it
1341 closeFolder(openFolder);
1342 if (folderScreen != mWorkspace.getCurrentScreen()) {
1343 // Close any folder open on the current screen
1344 closeFolder();
1345 // Pull the folder onto this screen
1346 openFolder(folderInfo);
1347 }
1348 }
1349 }
1350 }
1351
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 /**
1353 * Opens the user fodler described by the specified tag. The opening of the folder
1354 * is animated relative to the specified View. If the View is null, no animation
1355 * is played.
1356 *
1357 * @param folderInfo The FolderInfo describing the folder to open.
1358 */
1359 private void openFolder(FolderInfo folderInfo) {
1360 Folder openFolder;
1361
1362 if (folderInfo instanceof UserFolderInfo) {
1363 openFolder = UserFolder.fromXml(this);
1364 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001365 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 } else {
1367 return;
1368 }
1369
Joe Onorato00acb122009-08-04 16:04:30 -04001370 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 openFolder.setLauncher(this);
1372
1373 openFolder.bind(folderInfo);
1374 folderInfo.opened = true;
1375
1376 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1377 openFolder.onOpen();
1378 }
1379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001381 switch (v.getId()) {
1382 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001383 if (!isAllAppsVisible()) {
1384 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1385 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001386 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001387 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001388 return true;
1389 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001390 if (!isAllAppsVisible()) {
1391 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1392 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001393 showPreviews(v);
1394 }
1395 return true;
1396 case R.id.all_apps_button:
1397 if (!isAllAppsVisible()) {
1398 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1399 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1400 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001401 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001402 return true;
1403 }
1404
Joe Onorato9c1289c2009-08-17 11:03:03 -04001405 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 return false;
1407 }
1408
1409 if (!(v instanceof CellLayout)) {
1410 v = (View) v.getParent();
1411 }
1412
1413 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1414
1415 // This happens when long clicking an item with the dpad/trackball
1416 if (cellInfo == null) {
1417 return true;
1418 }
1419
1420 if (mWorkspace.allowLongPress()) {
1421 if (cellInfo.cell == null) {
1422 if (cellInfo.valid) {
1423 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001424 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 showAddDialog(cellInfo);
1426 }
1427 } else {
1428 if (!(cellInfo.cell instanceof Folder)) {
1429 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001430 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1431 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 mWorkspace.startDrag(cellInfo);
1433 }
1434 }
1435 }
1436 return true;
1437 }
1438
Romain Guye6b8e2f2009-11-10 11:56:55 -08001439 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001440 private void dismissPreview(final View v) {
1441 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001442 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001443 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1444 public void onDismiss() {
1445 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1446 int count = group.getChildCount();
1447 for (int i = 0; i < count; i++) {
1448 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1449 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001450 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1451 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1452
1453 v.setTag(R.id.workspace, null);
1454 v.setTag(R.id.icon, null);
1455 window.setOnDismissListener(null);
1456 }
1457 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001458 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001459 }
1460 v.setTag(null);
1461 }
1462
Romain Guyf8e6a802009-12-07 17:48:02 -08001463 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001464 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001465 }
1466
Romain Guya6abce82009-11-10 02:54:41 -08001467 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001468 final Resources resources = getResources();
1469 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001470
Romain Guya6abce82009-11-10 02:54:41 -08001471 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001472
1473 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001474
Romain Guyf8e6a802009-12-07 17:48:02 -08001475 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001476 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001477 int extraW = (int) ((r.left + r.right) * max);
1478 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001479
1480 int aW = cell.getWidth() - extraW;
1481 float w = aW / max;
1482
1483 int width = cell.getWidth();
1484 int height = cell.getHeight();
1485 int x = cell.getLeftPadding();
1486 int y = cell.getTopPadding();
1487 width -= (x + cell.getRightPadding());
1488 height -= (y + cell.getBottomPadding());
1489
1490 float scale = w / width;
1491
1492 int count = end - start;
1493
1494 final float sWidth = width * scale;
1495 float sHeight = height * scale;
1496
Romain Guye6b8e2f2009-11-10 11:56:55 -08001497 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001498
Romain Guye6b8e2f2009-11-10 11:56:55 -08001499 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1500 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001501
1502 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001503 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001504 cell = (CellLayout) workspace.getChildAt(i);
1505
Romain Guyf8e6a802009-12-07 17:48:02 -08001506 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001507 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001508
1509 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001510 c.scale(scale, scale);
1511 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1512 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001513
Romain Guy9d31e9f2009-11-11 18:54:28 -08001514 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001515 image.setImageBitmap(bitmap);
1516 image.setTag(i);
1517 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001518 image.setOnFocusChangeListener(handler);
1519 image.setFocusable(true);
1520 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001521
1522 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001523 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1524
1525 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001526 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001527
1528 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001529 p.setContentView(preview);
1530 p.setWidth((int) (sWidth * count + extraW));
1531 p.setHeight((int) (sHeight + extraH));
1532 p.setAnimationStyle(R.style.AnimationPreview);
1533 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001534 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001535 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001536 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001537
Romain Guye6b8e2f2009-11-10 11:56:55 -08001538 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1539 public void onDismiss() {
1540 dismissPreview(anchor);
1541 }
1542 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001543
1544 anchor.setTag(p);
1545 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001546 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001547 }
1548
Romain Guy9d31e9f2009-11-11 18:54:28 -08001549 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001550 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001551
Romain Guye6b8e2f2009-11-10 11:56:55 -08001552 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001553 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001554 }
1555
1556 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001557 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001558 v.post(this);
1559 }
1560
1561 public void run() {
1562 dismissPreview(mAnchor);
1563 }
1564
1565 public void onFocusChange(View v, boolean hasFocus) {
1566 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001567 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001568 }
Romain Guya6abce82009-11-10 02:54:41 -08001569 }
1570 }
1571
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 Workspace getWorkspace() {
1573 return mWorkspace;
1574 }
1575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 @Override
1577 protected Dialog onCreateDialog(int id) {
1578 switch (id) {
1579 case DIALOG_CREATE_SHORTCUT:
1580 return new CreateShortcut().createDialog();
1581 case DIALOG_RENAME_FOLDER:
1582 return new RenameFolder().createDialog();
1583 }
1584
1585 return super.onCreateDialog(id);
1586 }
1587
1588 @Override
1589 protected void onPrepareDialog(int id, Dialog dialog) {
1590 switch (id) {
1591 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 break;
1593 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001594 if (mFolderInfo != null) {
1595 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1596 final CharSequence text = mFolderInfo.title;
1597 input.setText(text);
1598 input.setSelection(0, text.length());
1599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 break;
1601 }
1602 }
1603
1604 void showRenameDialog(FolderInfo info) {
1605 mFolderInfo = info;
1606 mWaitingForResult = true;
1607 showDialog(DIALOG_RENAME_FOLDER);
1608 }
1609
1610 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1611 mAddItemCellInfo = cellInfo;
1612 mWaitingForResult = true;
1613 showDialog(DIALOG_CREATE_SHORTCUT);
1614 }
1615
Romain Guy73b979d2009-06-09 12:57:21 -07001616 private void pickShortcut(int requestCode, int title) {
1617 Bundle bundle = new Bundle();
1618
1619 ArrayList<String> shortcutNames = new ArrayList<String>();
1620 shortcutNames.add(getString(R.string.group_applications));
1621 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1622
1623 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1624 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1625 R.drawable.ic_launcher_application));
1626 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1627
1628 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1629 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1630 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1631 pickIntent.putExtras(bundle);
1632
1633 startActivityForResult(pickIntent, requestCode);
1634 }
1635
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 private class RenameFolder {
1637 private EditText mInput;
1638
1639 Dialog createDialog() {
1640 mWaitingForResult = true;
1641 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1642 mInput = (EditText) layout.findViewById(R.id.folder_name);
1643
1644 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1645 builder.setIcon(0);
1646 builder.setTitle(getString(R.string.rename_folder_title));
1647 builder.setCancelable(true);
1648 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1649 public void onCancel(DialogInterface dialog) {
1650 cleanup();
1651 }
1652 });
1653 builder.setNegativeButton(getString(R.string.cancel_action),
1654 new Dialog.OnClickListener() {
1655 public void onClick(DialogInterface dialog, int which) {
1656 cleanup();
1657 }
1658 }
1659 );
1660 builder.setPositiveButton(getString(R.string.rename_action),
1661 new Dialog.OnClickListener() {
1662 public void onClick(DialogInterface dialog, int which) {
1663 changeFolderName();
1664 }
1665 }
1666 );
1667 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001668
1669 final AlertDialog dialog = builder.create();
1670 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1671 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001672 mInput.requestFocus();
1673 InputMethodManager inputManager = (InputMethodManager)
1674 getSystemService(Context.INPUT_METHOD_SERVICE);
1675 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001676 }
1677 });
1678
1679 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 }
1681
1682 private void changeFolderName() {
1683 final String name = mInput.getText().toString();
1684 if (!TextUtils.isEmpty(name)) {
1685 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001686 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 mFolderInfo.title = name;
1688 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1689
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001691 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001692 mModel.setWorkspaceDirty();
1693 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 } else {
1695 final FolderIcon folderIcon = (FolderIcon)
1696 mWorkspace.getViewForTag(mFolderInfo);
1697 if (folderIcon != null) {
1698 folderIcon.setText(name);
1699 getWorkspace().requestLayout();
1700 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001701 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001702 mModel.setWorkspaceDirty();
1703 mWorkspaceLoading = true;
1704 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 }
1706 }
1707 }
1708 cleanup();
1709 }
1710
1711 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 dismissDialog(DIALOG_RENAME_FOLDER);
1713 mWaitingForResult = false;
1714 mFolderInfo = null;
1715 }
1716 }
1717
Joe Onoratofb0ca672009-09-14 17:55:46 -04001718 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001719 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001720 }
1721
Joe Onorato3a8820b2009-11-10 15:06:42 -08001722 void showAllApps(boolean animated) {
1723 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001724
Mike Cleronb6082fa2009-10-19 17:03:36 -07001725 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001726 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001727
Joe Onorato7c312c12009-08-13 21:36:53 -07001728 // TODO: fade these two too
1729 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001730 }
1731
Joe Onoratob2061212009-11-24 16:13:54 -05001732 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001733 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001734 * - Home from workspace
1735 * - from center screen
1736 * - from other screens
1737 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001738 * - from center screen
1739 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001740 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001741 * - from center screen
1742 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001743 * - Launch app from workspace and quit
1744 * - with back
1745 * - with home
1746 * - Launch app from all apps and quit
1747 * - with back
1748 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001749 * - Go to a screen that's not the default, then all
1750 * apps, and launch and app, and go back
1751 * - with back
1752 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001753 * - On workspace, long press power and go back
1754 * - with back
1755 * - with home
1756 * - On all apps, long press power and go back
1757 * - with back
1758 * - with home
1759 * - On workspace, power off
1760 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001761 * - Launch an app and turn off the screen while in that app
1762 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001763 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001764 * - From all apps
1765 * - From workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001766 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001767 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001768 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001769 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001770 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001771 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001772 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001773 }
1774
Joe Onorato7c312c12009-08-13 21:36:53 -07001775 void lockAllApps() {
1776 // TODO
1777 }
1778
1779 void unlockAllApps() {
1780 // TODO
1781 }
1782
Joe Onorato7404ee42009-07-31 11:54:44 -07001783 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 * Displays the shortcut creation dialog and launches, if necessary, the
1785 * appropriate activity.
1786 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001787 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001788 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1789 DialogInterface.OnShowListener {
1790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001792
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 Dialog createDialog() {
1794 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001795
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001797
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1799 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001800 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001801
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 builder.setInverseBackgroundForced(true);
1803
1804 AlertDialog dialog = builder.create();
1805 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001806 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001807 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 return dialog;
1810 }
1811
1812 public void onCancel(DialogInterface dialog) {
1813 mWaitingForResult = false;
1814 cleanup();
1815 }
1816
Romain Guycbb89e42009-06-08 15:52:54 -07001817 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001818 }
1819
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001821 try {
1822 dismissDialog(DIALOG_CREATE_SHORTCUT);
1823 } catch (Exception e) {
1824 // An exception is thrown if the dialog is not visible, which is fine
1825 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 }
1827
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001828 /**
1829 * Handle the action clicked in the "Add to home" dialog.
1830 */
1831 public void onClick(DialogInterface dialog, int which) {
1832 Resources res = getResources();
1833 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001834
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001835 switch (which) {
1836 case AddAdapter.ITEM_SHORTCUT: {
1837 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001838 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001839 break;
1840 }
Romain Guycbb89e42009-06-08 15:52:54 -07001841
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001842 case AddAdapter.ITEM_APPWIDGET: {
1843 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001844
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001845 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1846 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001847 // start the pick activity
1848 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1849 break;
1850 }
Romain Guycbb89e42009-06-08 15:52:54 -07001851
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001852 case AddAdapter.ITEM_LIVE_FOLDER: {
1853 // Insert extra item to handle inserting folder
1854 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001855
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001856 ArrayList<String> shortcutNames = new ArrayList<String>();
1857 shortcutNames.add(res.getString(R.string.group_folder));
1858 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001859
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001860 ArrayList<ShortcutIconResource> shortcutIcons =
1861 new ArrayList<ShortcutIconResource>();
1862 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1863 R.drawable.ic_launcher_folder));
1864 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1865
1866 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1867 pickIntent.putExtra(Intent.EXTRA_INTENT,
1868 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1869 pickIntent.putExtra(Intent.EXTRA_TITLE,
1870 getText(R.string.title_select_live_folder));
1871 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001872
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001873 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1874 break;
1875 }
1876
1877 case AddAdapter.ITEM_WALLPAPER: {
1878 startWallpaper();
1879 break;
1880 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 }
1882 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001883
1884 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001885 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 }
1887
1888 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001889 * Receives notifications when applications are added/removed.
1890 */
1891 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1892 @Override
1893 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001894 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05001895 String reason = intent.getStringExtra("reason");
1896 if (!"homekey".equals(reason)) {
1897 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001898 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05001899 animate = false;
1900 }
Joe Onoratob2061212009-11-24 16:13:54 -05001901 closeAllApps(animate);
1902 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001903 }
1904 }
1905
1906 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001907 * Receives notifications whenever the appwidgets are reset.
1908 */
1909 private class AppWidgetResetObserver extends ContentObserver {
1910 public AppWidgetResetObserver() {
1911 super(new Handler());
1912 }
1913
1914 @Override
1915 public void onChange(boolean selfChange) {
1916 onAppWidgetReset();
1917 }
1918 }
1919
1920 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001921 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001923 public int getCurrentWorkspaceScreen() {
1924 return mWorkspace.getCurrentScreen();
1925 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001926
Joe Onorato9c1289c2009-08-17 11:03:03 -04001927 /**
1928 * Refreshes the shortcuts shown on the workspace.
1929 *
1930 * Implementation of the method from LauncherModel.Callbacks.
1931 */
1932 public void startBinding() {
1933 final Workspace workspace = mWorkspace;
1934 int count = workspace.getChildCount();
1935 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001936 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001937 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1938 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001939
Joe Onorato9c1289c2009-08-17 11:03:03 -04001940 if (DEBUG_USER_INTERFACE) {
1941 android.widget.Button finishButton = new android.widget.Button(this);
1942 finishButton.setText("Finish");
1943 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1944
1945 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1946 public void onClick(View v) {
1947 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001948 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001949 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 }
1951 }
1952
1953 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001954 * Bind the items start-end from the list.
1955 *
1956 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001958 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1959
1960 final Workspace workspace = mWorkspace;
1961
1962 for (int i=start; i<end; i++) {
1963 final ItemInfo item = shortcuts.get(i);
1964 mDesktopItems.add(item);
1965 switch (item.itemType) {
1966 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1967 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1968 final View shortcut = createShortcut((ApplicationInfo) item);
1969 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1970 false);
1971 break;
1972 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1973 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1974 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1975 (UserFolderInfo) item);
1976 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1977 false);
1978 break;
1979 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1980 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1981 R.layout.live_folder_icon, this,
1982 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1983 (LiveFolderInfo) item);
1984 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1985 false);
1986 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
1989
Joe Onorato9c1289c2009-08-17 11:03:03 -04001990 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 }
1992
Joe Onorato9c1289c2009-08-17 11:03:03 -04001993 /**
1994 * Implementation of the method from LauncherModel.Callbacks.
1995 */
Joe Onoratoad72e172009-11-06 16:25:04 -05001996 public void bindFolders(HashMap<Long, FolderInfo> folders) {
1997 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001998 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002000
2001 /**
2002 * Add the views for a widget to the workspace.
2003 *
2004 * Implementation of the method from LauncherModel.Callbacks.
2005 */
2006 public void bindAppWidget(LauncherAppWidgetInfo item) {
2007 final Workspace workspace = mWorkspace;
2008
2009 final int appWidgetId = item.appWidgetId;
2010 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2011 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2012
Joe Onorato9c1289c2009-08-17 11:03:03 -04002013 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2014 item.hostView.setTag(item);
2015
2016 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2017 item.cellY, item.spanX, item.spanY, false);
2018
2019 workspace.requestLayout();
2020
2021 mDesktopItems.add(item);
2022 }
2023
2024 /**
2025 * Callback saying that there aren't any more items to bind.
2026 *
2027 * Implementation of the method from LauncherModel.Callbacks.
2028 */
2029 public void finishBindingItems() {
2030 if (mSavedState != null) {
2031 if (!mWorkspace.hasFocus()) {
2032 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2033 }
2034
2035 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2036 if (userFolders != null) {
2037 for (long folderId : userFolders) {
2038 final FolderInfo info = mFolders.get(folderId);
2039 if (info != null) {
2040 openFolder(info);
2041 }
2042 }
2043 final Folder openFolder = mWorkspace.getOpenFolder();
2044 if (openFolder != null) {
2045 openFolder.requestFocus();
2046 }
2047 }
2048
Joe Onorato9c1289c2009-08-17 11:03:03 -04002049 mSavedState = null;
2050 }
2051
2052 if (mSavedInstanceState != null) {
2053 super.onRestoreInstanceState(mSavedInstanceState);
2054 mSavedInstanceState = null;
2055 }
2056
Joe Onorato9c1289c2009-08-17 11:03:03 -04002057 mWorkspaceLoading = false;
2058 }
2059
2060 /**
2061 * Add the icons for all apps.
2062 *
2063 * Implementation of the method from LauncherModel.Callbacks.
2064 */
2065 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002066 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002067 }
2068
2069 /**
2070 * A package was installed.
2071 *
2072 * Implementation of the method from LauncherModel.Callbacks.
2073 */
2074 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2075 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002076 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002077 }
2078
2079 /**
2080 * A package was updated.
2081 *
2082 * Implementation of the method from LauncherModel.Callbacks.
2083 */
2084 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2085 removeDialog(DIALOG_CREATE_SHORTCUT);
2086 mWorkspace.updateShortcutsForPackage(packageName);
Joe Onoratof0be2132009-11-24 19:34:29 -05002087 mAllAppsGrid.updateApps(packageName, apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002088 }
2089
2090 /**
2091 * A package was uninstalled.
2092 *
2093 * Implementation of the method from LauncherModel.Callbacks.
2094 */
2095 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2096 removeDialog(DIALOG_CREATE_SHORTCUT);
2097 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002098 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002099 }
Joe Onoratobe386092009-11-17 17:32:16 -08002100
2101 /**
2102 * Prints out out state for debugging.
2103 */
2104 public void dumpState() {
2105 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002106 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002107 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2108 Log.d(TAG, "mRestoring=" + mRestoring);
2109 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2110 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2111 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2112 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002113 mModel.dumpState();
2114 mAllAppsGrid.dumpState();
2115 Log.d(TAG, "END launcher2 dump state");
2116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117}