blob: 45545db55b2e0199fecc390577afe71592d0a43a [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;
Mike LeBeau736cf282009-07-02 17:46:59 -070022import android.app.ISearchManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.SearchManager;
24import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070025import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080027import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080029import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070033import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.ActivityInfo;
36import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070039import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080042import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080043import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080045import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080047import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.os.Parcelable;
49import android.os.RemoteException;
50import android.os.ServiceManager;
Joe Onoratobe386092009-11-17 17:32:16 -080051import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070052import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.text.Selection;
54import android.text.SpannableStringBuilder;
55import android.text.TextUtils;
56import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070057import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080059import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.view.KeyEvent;
61import android.view.LayoutInflater;
62import android.view.Menu;
63import android.view.MenuItem;
64import android.view.View;
65import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.view.View.OnLongClickListener;
67import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.widget.TextView;
70import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080071import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080072import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080073import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070074import android.appwidget.AppWidgetManager;
75import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040078import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070079import java.io.DataOutputStream;
80import java.io.FileNotFoundException;
81import java.io.IOException;
82import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
84/**
85 * Default launcher application.
86 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040087public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080088 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080089 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 static final boolean LOGD = false;
91
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 static final boolean PROFILE_STARTUP = false;
93 static final boolean PROFILE_ROTATE = false;
94 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070095
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 private static final int WALLPAPER_SCREENS_SPAN = 2;
97
98 private static final int MENU_GROUP_ADD = 1;
99 private static final int MENU_ADD = Menu.FIRST + 1;
100 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
101 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
102 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700103 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104
105 private static final int REQUEST_CREATE_SHORTCUT = 1;
106 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700107 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 private static final int REQUEST_PICK_APPLICATION = 6;
109 private static final int REQUEST_PICK_SHORTCUT = 7;
110 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700111 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700112 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
114 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
115
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700116 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
117 static final String SEARCH_WIDGET = "search_widget";
118
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800119 static final int SCREEN_COUNT = 5;
120 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700122 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato7c312c12009-08-13 21:36:53 -0700124 static final int DIALOG_CREATE_SHORTCUT = 1;
125 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Romain Guy98d01652009-06-30 16:21:04 -0700127 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 // Type: int
130 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
131 // Type: boolean
132 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
133 // Type: long
134 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
149 // Type: int[]
150 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
151 // Type: boolean
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
153 // Type: long
154 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
155
Joe Onorato9c1289c2009-08-17 11:03:03 -0400156 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800159 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800161 private final BroadcastReceiver mCloseSystemDialogsReceiver
162 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800163 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private LayoutInflater mInflater;
166
Joe Onorato00acb122009-08-04 16:04:30 -0400167 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700169
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private AppWidgetManager mAppWidgetManager;
171 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private CellLayout.CellInfo mAddItemCellInfo;
174 private CellLayout.CellInfo mMenuAddInfo;
175 private final int[] mCellCoordinates = new int[2];
176 private FolderInfo mFolderInfo;
177
Joe Onorato7c312c12009-08-13 21:36:53 -0700178 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700179 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700180 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private Bundle mSavedState;
183
184 private SpannableStringBuilder mDefaultKeySsb = null;
185
Mike LeBeau736cf282009-07-02 17:46:59 -0700186 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 private boolean mWorkspaceLoading = true;
189
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800190 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private boolean mRestoring;
192 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
194 private Bundle mSavedInstanceState;
195
Joe Onorato9c1289c2009-08-17 11:03:03 -0400196 private LauncherModel mModel;
197
Romain Guy84f296c2009-11-04 15:00:44 -0800198 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
199 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700200
Romain Guy1fbc1c82009-11-09 20:43:08 -0800201 private ImageView mPreviousView;
202 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 @Override
205 protected void onCreate(Bundle savedInstanceState) {
206 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700207
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400209 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700211
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
214 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 if (PROFILE_STARTUP) {
217 android.os.Debug.startMethodTracing("/sdcard/launcher");
218 }
219
220 checkForLocaleChange();
221 setWallpaperDimension();
222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 setContentView(R.layout.launcher);
224 setupViews();
225
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800226 registerContentObservers();
227
Joe Onorato7c312c12009-08-13 21:36:53 -0700228 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 mSavedState = savedInstanceState;
231 restoreState(mSavedState);
232
233 if (PROFILE_STARTUP) {
234 android.os.Debug.stopMethodTracing();
235 }
236
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700237 // We have a new AllAppsView, we need to re-bind everything, and it could have
238 // changed in our absence.
239 mModel.setAllAppsDirty();
240 mModel.setWorkspaceDirty();
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400243 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 }
245
246 // For handling default keys
247 mDefaultKeySsb = new SpannableStringBuilder();
248 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800249
250 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
251 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 }
Romain Guycbb89e42009-06-08 15:52:54 -0700253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700255 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
256 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 final Configuration configuration = getResources().getConfiguration();
259
Romain Guy98d01652009-06-30 16:21:04 -0700260 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 final String locale = configuration.locale.toString();
262
Romain Guy98d01652009-06-30 16:21:04 -0700263 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 final int mcc = configuration.mcc;
265
Romain Guy98d01652009-06-30 16:21:04 -0700266 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 final int mnc = configuration.mnc;
268
Romain Guy84f296c2009-11-04 15:00:44 -0800269 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270
Romain Guy84f296c2009-11-04 15:00:44 -0800271 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700272 localeConfiguration.locale = locale;
273 localeConfiguration.mcc = mcc;
274 localeConfiguration.mnc = mnc;
275
276 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700278 }
279 }
280
281 private static class LocaleConfiguration {
282 public String locale;
283 public int mcc = -1;
284 public int mnc = -1;
285 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700286
Romain Guy98d01652009-06-30 16:21:04 -0700287 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
288 DataInputStream in = null;
289 try {
290 in = new DataInputStream(context.openFileInput(PREFERENCES));
291 configuration.locale = in.readUTF();
292 configuration.mcc = in.readInt();
293 configuration.mnc = in.readInt();
294 } catch (FileNotFoundException e) {
295 // Ignore
296 } catch (IOException e) {
297 // Ignore
298 } finally {
299 if (in != null) {
300 try {
301 in.close();
302 } catch (IOException e) {
303 // Ignore
304 }
305 }
306 }
307 }
308
309 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
310 DataOutputStream out = null;
311 try {
312 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
313 out.writeUTF(configuration.locale);
314 out.writeInt(configuration.mcc);
315 out.writeInt(configuration.mnc);
316 out.flush();
317 } catch (FileNotFoundException e) {
318 // Ignore
319 } catch (IOException e) {
320 //noinspection ResultOfMethodCallIgnored
321 context.getFileStreamPath(PREFERENCES).delete();
322 } finally {
323 if (out != null) {
324 try {
325 out.close();
326 } catch (IOException e) {
327 // Ignore
328 }
329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 }
331 }
332
333 static int getScreen() {
334 synchronized (sLock) {
335 return sScreen;
336 }
337 }
338
339 static void setScreen(int screen) {
340 synchronized (sLock) {
341 sScreen = screen;
342 }
343 }
344
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700346 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347
348 Display display = getWindowManager().getDefaultDisplay();
349 boolean isPortrait = display.getWidth() < display.getHeight();
350
351 final int width = isPortrait ? display.getWidth() : display.getHeight();
352 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700353 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 }
355
356 @Override
357 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700358 mWaitingForResult = false;
359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 // The pattern used here is that a user PICKs a specific application,
361 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700362
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
364 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700365
Romain Guy94dabf12009-07-21 10:55:43 -0700366 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 switch (requestCode) {
368 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400369 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 break;
371 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700372 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 break;
374 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400375 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 break;
377 case REQUEST_PICK_LIVE_FOLDER:
378 addLiveFolder(data);
379 break;
380 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700383 case REQUEST_PICK_APPWIDGET:
384 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700386 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400387 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700389 case REQUEST_PICK_WALLPAPER:
390 // We just wanted the activity result here so we can clear mWaitingForResult
391 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
Romain Guy18042c82009-11-06 11:44:55 -0800393 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
394 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
395 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700396 // Clean up the appWidgetId if we canceled
397 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
398 if (appWidgetId != -1) {
399 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 }
401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 }
403
404 @Override
405 protected void onResume() {
406 super.onResume();
407
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800408 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400411 mWorkspaceLoading = true;
412 mModel.startLoader(this, true);
413 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700415
Mike LeBeau736cf282009-07-02 17:46:59 -0700416 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
417 // onNewIntent), then close the search dialog if needed, because it probably
418 // came from the user pressing 'home' (rather than, for example, pressing 'back').
419 if (mIsNewIntent) {
420 // Post to a handler so that this happens after the search dialog tries to open
421 // itself again.
422 mWorkspace.post(new Runnable() {
423 public void run() {
Bjorn Bringert98047dc2009-11-21 22:08:04 +0000424 stopSearch();
Mike LeBeau736cf282009-07-02 17:46:59 -0700425 }
426 });
427 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700428
Mike LeBeau736cf282009-07-02 17:46:59 -0700429 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 }
431
432 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 protected void onPause() {
434 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800435 dismissPreview(mPreviousView);
436 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800437 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700438 }
Romain Guycbb89e42009-06-08 15:52:54 -0700439
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700440 @Override
441 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400442 // Flag the loader to stop early before switching
443 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700444
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700445 if (PROFILE_ROTATE) {
446 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
447 }
448 return null;
449 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 private boolean acceptFilter() {
452 final InputMethodManager inputManager = (InputMethodManager)
453 getSystemService(Context.INPUT_METHOD_SERVICE);
454 return !inputManager.isFullscreenMode();
455 }
456
457 @Override
458 public boolean onKeyDown(int keyCode, KeyEvent event) {
459 boolean handled = super.onKeyDown(keyCode, event);
460 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
461 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
462 keyCode, event);
463 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700464 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700465 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700466 // showSearchDialog()
467 // If there are multiple keystrokes before the search dialog takes focus,
468 // onSearchRequested() will be called for every keystroke,
469 // but it is idempotent, so it's fine.
470 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 }
472 }
473
474 return handled;
475 }
476
Karl Rosaen138a0412009-04-23 19:00:21 -0700477 private String getTypedText() {
478 return mDefaultKeySsb.toString();
479 }
480
481 private void clearTypedText() {
482 mDefaultKeySsb.clear();
483 mDefaultKeySsb.clearSpans();
484 Selection.setSelection(mDefaultKeySsb, 0);
485 }
486
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 /**
488 * Restores the previous state, if it exists.
489 *
490 * @param savedState The previous state.
491 */
492 private void restoreState(Bundle savedState) {
493 if (savedState == null) {
494 return;
495 }
496
Joe Onorato3a8820b2009-11-10 15:06:42 -0800497 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
498 if (allApps) {
499 showAllApps(false);
500 }
501
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
503 if (currentScreen > -1) {
504 mWorkspace.setCurrentScreen(currentScreen);
505 }
506
507 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
508 if (addScreen > -1) {
509 mAddItemCellInfo = new CellLayout.CellInfo();
510 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
511 addItemCellInfo.valid = true;
512 addItemCellInfo.screen = addScreen;
513 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
514 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
515 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
516 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
517 addItemCellInfo.findVacantCellsFromOccupied(
518 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
519 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
520 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
521 mRestoring = true;
522 }
523
524 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
525 if (renameFolder) {
526 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400527 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800528 mRestoring = true;
529 }
530 }
531
532 /**
533 * Finds all the views we need and configure them properly.
534 */
535 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400536 DragController dragController = mDragController;
537
Romain Guyb1b69f52009-08-10 15:10:15 -0700538 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400539 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540
Joe Onorato7c312c12009-08-13 21:36:53 -0700541 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700542 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700543 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700544 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700545 // Manage focusability manually since this thing is always visible
546 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700547
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
549 final Workspace workspace = mWorkspace;
550
Joe Onorato7c312c12009-08-13 21:36:53 -0700551 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
552 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553
Joe Onorato7c312c12009-08-13 21:36:53 -0700554 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700555 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700556 mHandleView.setOnClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800557
558 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
559 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
560
561 Drawable previous = mPreviousView.getDrawable();
562 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800563 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564
Joe Onorato0d44e942009-11-16 18:20:51 -0800565 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800566 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800567 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800568 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800569
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400571 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573
574 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400575 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700576 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577
Joe Onorato00acb122009-08-04 16:04:30 -0400578 dragController.setDragScoller(workspace);
579 dragController.setDragListener(deleteZone);
580 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700581
Joe Onorato00acb122009-08-04 16:04:30 -0400582 // The order here is bottom to top.
583 dragController.addDropTarget(workspace);
584 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 }
586
Romain Guy8a73c512009-11-09 19:19:59 -0800587 @SuppressWarnings({"UnusedDeclaration"})
588 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800589 if (!isAllAppsVisible()) {
590 mWorkspace.scrollLeft();
591 }
Romain Guy8a73c512009-11-09 19:19:59 -0800592 }
593
594 @SuppressWarnings({"UnusedDeclaration"})
595 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800596 if (!isAllAppsVisible()) {
597 mWorkspace.scrollRight();
598 }
Romain Guy8a73c512009-11-09 19:19:59 -0800599 }
600
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 /**
602 * Creates a view representing a shortcut.
603 *
604 * @param info The data structure describing the shortcut.
605 *
606 * @return A View inflated from R.layout.application.
607 */
608 View createShortcut(ApplicationInfo info) {
609 return createShortcut(R.layout.application,
610 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
611 }
612
613 /**
614 * Creates a view representing a shortcut inflated from the specified resource.
615 *
616 * @param layoutResId The id of the XML layout used to create the shortcut.
617 * @param parent The group the shortcut belongs to.
618 * @param info The data structure describing the shortcut.
619 *
620 * @return A View inflated from layoutResId.
621 */
622 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
623 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
624
Joe Onorato5162ea92009-09-03 09:39:42 -0700625 if (info.icon == null) {
626 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
627 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700629 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 info.filtered = true;
631 }
632
633 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
634 favorite.setText(info.title);
635 favorite.setTag(info);
636 favorite.setOnClickListener(this);
637
638 return favorite;
639 }
640
641 /**
642 * Add an application shortcut to the workspace.
643 *
644 * @param data The intent describing the application.
645 * @param cellInfo The position on screen where to create the shortcut.
646 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400647 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 cellInfo.screen = mWorkspace.getCurrentScreen();
649 if (!findSingleSlot(cellInfo)) return;
650
Romain Guy73b979d2009-06-09 12:57:21 -0700651 final ApplicationInfo info = infoFromApplicationIntent(context, data);
652 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700654 }
655 }
656
657 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 ComponentName component = data.getComponent();
659 PackageManager packageManager = context.getPackageManager();
660 ActivityInfo activityInfo = null;
661 try {
662 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
663 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800664 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
Romain Guycbb89e42009-06-08 15:52:54 -0700666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 if (activityInfo != null) {
668 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 itemInfo.title = activityInfo.loadLabel(packageManager);
671 if (itemInfo.title == null) {
672 itemInfo.title = activityInfo.name;
673 }
Romain Guycbb89e42009-06-08 15:52:54 -0700674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
676 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
677 itemInfo.icon = activityInfo.loadIcon(packageManager);
678 itemInfo.container = ItemInfo.NO_ID;
679
Romain Guy73b979d2009-06-09 12:57:21 -0700680 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
Romain Guy73b979d2009-06-09 12:57:21 -0700682
683 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
Romain Guycbb89e42009-06-08 15:52:54 -0700685
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 /**
687 * Add a shortcut to the workspace.
688 *
689 * @param data The intent describing the shortcut.
690 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400692 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 cellInfo.screen = mWorkspace.getCurrentScreen();
694 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700695
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
697
698 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
701 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 }
703 }
704
Romain Guycbb89e42009-06-08 15:52:54 -0700705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700709 * @param data The intent describing the appWidgetId.
710 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400712 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700714 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700715
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800716 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700717
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700719
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700720 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700722 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700723
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 // Try finding open space on Launcher screen
725 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800726 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
727 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
728 return;
729 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700731 // Build Launcher-specific widget info and save to database
732 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 launcherInfo.spanX = spans[0];
734 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700735
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 LauncherModel.addItemToDatabase(this, launcherInfo,
737 LauncherSettings.Favorites.CONTAINER_DESKTOP,
738 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
739
740 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700742
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700744 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700745
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700746 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700748
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 }
752 }
Romain Guycbb89e42009-06-08 15:52:54 -0700753
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
755 mDesktopItems.remove(launcherInfo);
756 launcherInfo.hostView = null;
757 }
758
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700759 public LauncherAppWidgetHost getAppWidgetHost() {
760 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 }
Romain Guycbb89e42009-06-08 15:52:54 -0700762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 static ApplicationInfo addShortcut(Context context, Intent data,
764 CellLayout.CellInfo cellInfo, boolean notify) {
765
Romain Guy73b979d2009-06-09 12:57:21 -0700766 final ApplicationInfo info = infoFromShortcutIntent(context, data);
767 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
768 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
769
770 return info;
771 }
772
773 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
775 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
776 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
777
778 Drawable icon = null;
779 boolean filtered = false;
780 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700781 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782
783 if (bitmap != null) {
784 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
785 filtered = true;
786 customIcon = true;
787 } else {
788 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700789 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700791 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 final PackageManager packageManager = context.getPackageManager();
793 Resources resources = packageManager.getResourcesForApplication(
794 iconResource.packageName);
795 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
796 icon = resources.getDrawable(id);
797 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800798 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 }
800 }
801 }
802
803 if (icon == null) {
804 icon = context.getPackageManager().getDefaultActivityIcon();
805 }
806
807 final ApplicationInfo info = new ApplicationInfo();
808 info.icon = icon;
809 info.filtered = filtered;
810 info.title = name;
811 info.intent = intent;
812 info.customIcon = customIcon;
813 info.iconResource = iconResource;
814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 return info;
816 }
817
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800818 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800819 getWindow().closeAllPanels();
820
821 try {
822 dismissDialog(DIALOG_CREATE_SHORTCUT);
823 // Unlock the workspace if the dialog was showing
824 } catch (Exception e) {
825 // An exception is thrown if the dialog is not visible, which is fine
826 }
827
828 try {
829 dismissDialog(DIALOG_RENAME_FOLDER);
830 // Unlock the workspace if the dialog was showing
831 } catch (Exception e) {
832 // An exception is thrown if the dialog is not visible, which is fine
833 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800834
835 // Whatever we were doing is hereby canceled.
836 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800837 }
838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 @Override
840 protected void onNewIntent(Intent intent) {
841 super.onNewIntent(intent);
842
843 // Close the menu
844 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800845 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800846 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700847
Mike LeBeau736cf282009-07-02 17:46:59 -0700848 // Set this flag so that onResume knows to close the search dialog if it's open,
849 // because this was a new intent (thus a press of 'home' or some such) rather than
850 // for example onResume being called when the user pressed the 'back' button.
851 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852
Joe Onorato14f122b2009-11-19 14:06:36 -0800853 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
854 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
855 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800856 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800857 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800858 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800859 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700860
Joe Onorato3a8820b2009-11-10 15:06:42 -0800861 final View v = getWindow().peekDecorView();
862 if (v != null && v.getWindowToken() != null) {
863 InputMethodManager imm = (InputMethodManager)getSystemService(
864 INPUT_METHOD_SERVICE);
865 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 }
867 }
868 }
869
870 @Override
871 protected void onRestoreInstanceState(Bundle savedInstanceState) {
872 // Do not call super here
873 mSavedInstanceState = savedInstanceState;
874 }
875
876 @Override
877 protected void onSaveInstanceState(Bundle outState) {
878 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
879
880 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
881 if (folders.size() > 0) {
882 final int count = folders.size();
883 long[] ids = new long[count];
884 for (int i = 0; i < count; i++) {
885 final FolderInfo info = folders.get(i).getInfo();
886 ids[i] = info.id;
887 }
888 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
889 } else {
890 super.onSaveInstanceState(outState);
891 }
892
Joe Onoratofb0ca672009-09-14 17:55:46 -0400893 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800894 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897
898 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
899 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
900 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
901
902 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
903 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
904 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
905 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
906 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
907 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
908 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
909 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
910 layout.getOccupiedCells());
911 }
912
913 if (mFolderInfo != null && mWaitingForResult) {
914 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
915 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
916 }
917 }
918
919 @Override
920 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700922
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700924 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800926 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 }
928
929 TextKeyListener.getInstance().release();
930
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 unbindDesktopItems();
934 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800935
936 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800937
938 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800939 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800940
941 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943
944 @Override
945 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700946 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 super.startActivityForResult(intent, requestCode);
948 }
949
950 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700951 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700953
Joe Onorato7bb17492009-09-24 17:51:01 -0700954 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700955
Karl Rosaen138a0412009-04-23 19:00:21 -0700956 // Slide the search widget to the top, if it's on the current screen,
957 // otherwise show the search dialog immediately.
958 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
959 if (searchWidget == null) {
960 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
961 } else {
962 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
963 // show the currently typed text in the search widget while sliding
964 searchWidget.setQuery(getTypedText());
965 }
966 }
Romain Guycbb89e42009-06-08 15:52:54 -0700967
Karl Rosaen138a0412009-04-23 19:00:21 -0700968 /**
969 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700970 *
971 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700972 */
Romain Guycbb89e42009-06-08 15:52:54 -0700973 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700974 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700975
Karl Rosaen138a0412009-04-23 19:00:21 -0700976 if (initialQuery == null) {
977 // Use any text typed in the launcher as the initial query
978 initialQuery = getTypedText();
979 clearTypedText();
980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 if (appSearchData == null) {
982 appSearchData = new Bundle();
983 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
984 }
Romain Guycbb89e42009-06-08 15:52:54 -0700985
Karl Rosaen138a0412009-04-23 19:00:21 -0700986 final SearchManager searchManager =
987 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
988
989 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
990 if (searchWidget != null) {
991 // This gets called when the user leaves the search dialog to go back to
992 // the Launcher.
993 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
994 public void onCancel() {
995 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700996 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700997 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700998 });
999 }
Romain Guycbb89e42009-06-08 15:52:54 -07001000
Karl Rosaen138a0412009-04-23 19:00:21 -07001001 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001002 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
1004
Karl Rosaen138a0412009-04-23 19:00:21 -07001005 /**
1006 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001007 */
Romain Guy5a941392009-04-28 15:18:25 -07001008 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001009 // Close search dialog
1010 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001011 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001012 // Restore search widget to its normal position
1013 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1014 if (searchWidget != null) {
1015 searchWidget.stopSearch(false);
1016 }
1017 }
Romain Guycbb89e42009-06-08 15:52:54 -07001018
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 @Override
1020 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 if (isWorkspaceLocked()) {
1022 return false;
1023 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024
1025 super.onCreateOptionsMenu(menu);
1026 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1027 .setIcon(android.R.drawable.ic_menu_add)
1028 .setAlphabeticShortcut('A');
1029 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1030 .setIcon(android.R.drawable.ic_menu_gallery)
1031 .setAlphabeticShortcut('W');
1032 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1033 .setIcon(android.R.drawable.ic_search_category_default)
1034 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1035 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1036 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1037 .setAlphabeticShortcut('N');
1038
1039 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001040 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1041 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042
1043 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1044 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1045 .setIntent(settings);
1046
1047 return true;
1048 }
1049
1050 @Override
1051 public boolean onPrepareOptionsMenu(Menu menu) {
1052 super.onPrepareOptionsMenu(menu);
1053
1054 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1055 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1056
1057 return true;
1058 }
1059
1060 @Override
1061 public boolean onOptionsItemSelected(MenuItem item) {
1062 switch (item.getItemId()) {
1063 case MENU_ADD:
1064 addItems();
1065 return true;
1066 case MENU_WALLPAPER_SETTINGS:
1067 startWallpaper();
1068 return true;
1069 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001070 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 return true;
1072 case MENU_NOTIFICATIONS:
1073 showNotifications();
1074 return true;
1075 }
1076
1077 return super.onOptionsItemSelected(item);
1078 }
Romain Guycbb89e42009-06-08 15:52:54 -07001079
Karl Rosaen138a0412009-04-23 19:00:21 -07001080 /**
1081 * Indicates that we want global search for this activity by setting the globalSearch
1082 * argument for {@link #startSearch} to true.
1083 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001085 @Override
1086 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001087 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001088 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001089 }
1090
Joe Onorato9c1289c2009-08-17 11:03:03 -04001091 public boolean isWorkspaceLocked() {
1092 return mWorkspaceLoading || mWaitingForResult;
1093 }
1094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001096 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 showAddDialog(mMenuAddInfo);
1098 }
1099
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001100 void addAppWidget(Intent data) {
1101 // TODO: catch bad widget exception when sent
1102 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001104 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1105 if (SEARCH_WIDGET.equals(customWidget)) {
1106 // We don't need this any more, since this isn't a real app widget.
1107 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1108 // add the search widget
1109 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001111 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1112
1113 if (appWidget.configure != null) {
1114 // Launch over to configure widget, if needed
1115 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1116 intent.setComponent(appWidget.configure);
1117 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1118
1119 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1120 } else {
1121 // Otherwise just add it
1122 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 }
1125 }
Romain Guycbb89e42009-06-08 15:52:54 -07001126
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 void addSearch() {
1128 final Widget info = Widget.makeSearch();
1129 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 final int[] xy = mCellCoordinates;
1132 final int spanX = info.spanX;
1133 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1138 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 final View view = mInflater.inflate(info.layoutResource, null);
1141 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001142 Search search = (Search) view.findViewById(R.id.widget_search);
1143 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001144
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1146 }
1147
Romain Guy73b979d2009-06-09 12:57:21 -07001148 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001149 // Handle case where user selected "Applications"
1150 String applicationName = getResources().getString(R.string.group_applications);
1151 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001152
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001153 if (applicationName != null && applicationName.equals(shortcutName)) {
1154 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1155 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001156
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001157 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1158 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001159 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001160 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001161 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 }
1164
1165 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001166 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001167 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001168 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001169
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001170 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001171 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001172 } else {
1173 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 }
1176
Joe Onorato9c1289c2009-08-17 11:03:03 -04001177 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 UserFolderInfo folderInfo = new UserFolderInfo();
1179 folderInfo.title = getText(R.string.folder_name);
1180
1181 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1182 cellInfo.screen = mWorkspace.getCurrentScreen();
1183 if (!findSingleSlot(cellInfo)) return;
1184
1185 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 LauncherModel.addItemToDatabase(this, folderInfo,
1187 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001189 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190
1191 // Create the view
1192 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1193 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1194 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 }
Romain Guycbb89e42009-06-08 15:52:54 -07001197
Joe Onorato9c1289c2009-08-17 11:03:03 -04001198 void removeFolder(FolderInfo folder) {
1199 mFolders.remove(folder.id);
1200 }
1201
1202 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 cellInfo.screen = mWorkspace.getCurrentScreen();
1204 if (!findSingleSlot(cellInfo)) return;
1205
1206 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1207
1208 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1210 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1212 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 }
1214 }
1215
1216 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1217 CellLayout.CellInfo cellInfo, boolean notify) {
1218
1219 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1220 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1221
1222 Drawable icon = null;
1223 boolean filtered = false;
1224 Intent.ShortcutIconResource iconResource = null;
1225
1226 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1227 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1228 try {
1229 iconResource = (Intent.ShortcutIconResource) extra;
1230 final PackageManager packageManager = context.getPackageManager();
1231 Resources resources = packageManager.getResourcesForApplication(
1232 iconResource.packageName);
1233 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1234 icon = resources.getDrawable(id);
1235 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001236 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 }
1238 }
1239
1240 if (icon == null) {
1241 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1242 }
1243
1244 final LiveFolderInfo info = new LiveFolderInfo();
1245 info.icon = icon;
1246 info.filtered = filtered;
1247 info.title = name;
1248 info.iconResource = iconResource;
1249 info.uri = data.getData();
1250 info.baseIntent = baseIntent;
1251 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1252 LiveFolders.DISPLAY_MODE_GRID);
1253
1254 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1255 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257
1258 return info;
1259 }
1260
1261 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1262 final int[] xy = new int[2];
1263 if (findSlot(cellInfo, xy, 1, 1)) {
1264 cellInfo.cellX = xy[0];
1265 cellInfo.cellY = xy[1];
1266 return true;
1267 }
1268 return false;
1269 }
1270
1271 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1272 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1273 boolean[] occupied = mSavedState != null ?
1274 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1275 cellInfo = mWorkspace.findAllVacantCells(occupied);
1276 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1277 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1278 return false;
1279 }
1280 }
1281 return true;
1282 }
1283
1284 private void showNotifications() {
1285 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1286 if (statusBar != null) {
1287 statusBar.expand();
1288 }
1289 }
1290
1291 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001292 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001294 Intent chooser = Intent.createChooser(pickWallpaper,
1295 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001296 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1297 // Removed in Eclair MR1
1298// WallpaperManager wm = (WallpaperManager)
1299// getSystemService(Context.WALLPAPER_SERVICE);
1300// WallpaperInfo wi = wm.getWallpaperInfo();
1301// if (wi != null && wi.getSettingsActivity() != null) {
1302// LabeledIntent li = new LabeledIntent(getPackageName(),
1303// R.string.configure_wallpaper, 0);
1304// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1305// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1306// }
Mike Clerona0618e42009-10-22 13:55:21 -07001307 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
1309
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001310 /**
1311 * Registers various content observers. The current implementation registers
1312 * only a favorites observer to keep track of the favorites applications.
1313 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001314 private void registerContentObservers() {
1315 ContentResolver resolver = getContentResolver();
1316 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1317 true, mWidgetObserver);
1318 }
1319
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 @Override
1321 public boolean dispatchKeyEvent(KeyEvent event) {
1322 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1323 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001325 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001326 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001327 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001328 dumpState();
1329 return true;
1330 }
1331 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001332 }
1333 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1334 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001335 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 return true;
1337 }
1338 }
1339
1340 return super.dispatchKeyEvent(event);
1341 }
1342
Joe Onorato88ec0992009-11-19 13:16:06 -08001343 @Override
1344 public void onBackPressed() {
1345 if (isAllAppsVisible()) {
1346 closeAllApps(true);
1347 } else {
1348 closeFolder();
1349 }
1350 dismissPreview(mPreviousView);
1351 dismissPreview(mNextView);
1352 }
1353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 private void closeFolder() {
1355 Folder folder = mWorkspace.getOpenFolder();
1356 if (folder != null) {
1357 closeFolder(folder);
1358 }
1359 }
1360
1361 void closeFolder(Folder folder) {
1362 folder.getInfo().opened = false;
1363 ViewGroup parent = (ViewGroup) folder.getParent();
1364 if (parent != null) {
1365 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001366 if (folder instanceof DropTarget) {
1367 // Live folders aren't DropTargets.
1368 mDragController.removeDropTarget((DropTarget)folder);
1369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
1371 folder.onClose();
1372 }
1373
1374 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001375 * Re-listen when widgets are reset.
1376 */
1377 private void onAppWidgetReset() {
1378 mAppWidgetHost.startListening();
1379 }
1380
1381 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001382 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1383 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001385 private void unbindDesktopItems() {
1386 for (ItemInfo item: mDesktopItems) {
1387 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 }
1389 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 /**
1392 * Launches the intent referred by the clicked shortcut.
1393 *
1394 * @param v The view representing the clicked shortcut.
1395 */
1396 public void onClick(View v) {
1397 Object tag = v.getTag();
1398 if (tag instanceof ApplicationInfo) {
1399 // Open shortcut
1400 final Intent intent = ((ApplicationInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001401 int[] pos = new int[2];
1402 v.getLocationOnScreen(pos);
1403 intent.setSourceBounds(
1404 new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 startActivitySafely(intent);
1406 } else if (tag instanceof FolderInfo) {
1407 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001408 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001409 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001410 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001411 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001412 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415 }
1416
1417 void startActivitySafely(Intent intent) {
1418 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1419 try {
1420 startActivity(intent);
1421 } catch (ActivityNotFoundException e) {
1422 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1423 } catch (SecurityException e) {
1424 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001425 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1427 "or use the exported attribute for this activity.", e);
1428 }
1429 }
1430
1431 private void handleFolderClick(FolderInfo folderInfo) {
1432 if (!folderInfo.opened) {
1433 // Close any open folder
1434 closeFolder();
1435 // Open the requested folder
1436 openFolder(folderInfo);
1437 } else {
1438 // Find the open folder...
1439 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1440 int folderScreen;
1441 if (openFolder != null) {
1442 folderScreen = mWorkspace.getScreenForView(openFolder);
1443 // .. and close it
1444 closeFolder(openFolder);
1445 if (folderScreen != mWorkspace.getCurrentScreen()) {
1446 // Close any folder open on the current screen
1447 closeFolder();
1448 // Pull the folder onto this screen
1449 openFolder(folderInfo);
1450 }
1451 }
1452 }
1453 }
1454
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 /**
1456 * Opens the user fodler described by the specified tag. The opening of the folder
1457 * is animated relative to the specified View. If the View is null, no animation
1458 * is played.
1459 *
1460 * @param folderInfo The FolderInfo describing the folder to open.
1461 */
1462 private void openFolder(FolderInfo folderInfo) {
1463 Folder openFolder;
1464
1465 if (folderInfo instanceof UserFolderInfo) {
1466 openFolder = UserFolder.fromXml(this);
1467 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001468 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 } else {
1470 return;
1471 }
1472
Joe Onorato00acb122009-08-04 16:04:30 -04001473 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 openFolder.setLauncher(this);
1475
1476 openFolder.bind(folderInfo);
1477 folderInfo.opened = true;
1478
1479 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1480 openFolder.onOpen();
1481 }
1482
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001484 switch (v.getId()) {
1485 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001486 if (!isAllAppsVisible()) {
1487 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1488 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1489 showPreviousPreview(v);
1490 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001491 return true;
1492 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001493 if (!isAllAppsVisible()) {
1494 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1495 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1496 showNextPreview(v);
1497 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001498 return true;
1499 }
1500
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 return false;
1503 }
1504
1505 if (!(v instanceof CellLayout)) {
1506 v = (View) v.getParent();
1507 }
1508
1509 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1510
1511 // This happens when long clicking an item with the dpad/trackball
1512 if (cellInfo == null) {
1513 return true;
1514 }
1515
1516 if (mWorkspace.allowLongPress()) {
1517 if (cellInfo.cell == null) {
1518 if (cellInfo.valid) {
1519 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001520 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 showAddDialog(cellInfo);
1522 }
1523 } else {
1524 if (!(cellInfo.cell instanceof Folder)) {
1525 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001526 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1527 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 mWorkspace.startDrag(cellInfo);
1529 }
1530 }
1531 }
1532 return true;
1533 }
1534
Romain Guye6b8e2f2009-11-10 11:56:55 -08001535 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001536 private void dismissPreview(final View v) {
1537 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001538 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001539 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1540 public void onDismiss() {
1541 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1542 int count = group.getChildCount();
1543 for (int i = 0; i < count; i++) {
1544 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1545 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001546 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1547 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1548
1549 v.setTag(R.id.workspace, null);
1550 v.setTag(R.id.icon, null);
1551 window.setOnDismissListener(null);
1552 }
1553 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001554 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001555 }
1556 v.setTag(null);
1557 }
1558
1559 private void showPreviousPreview(View anchor) {
1560 int current = mWorkspace.getCurrentScreen();
1561 if (current <= 0) return;
1562
Romain Guy9d31e9f2009-11-11 18:54:28 -08001563 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001564 }
1565
1566 private void showNextPreview(View anchor) {
1567 int current = mWorkspace.getCurrentScreen();
1568 if (current >= mWorkspace.getChildCount() - 1) return;
1569
Romain Guy9d31e9f2009-11-11 18:54:28 -08001570 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guya6abce82009-11-10 02:54:41 -08001571 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001572
Romain Guya6abce82009-11-10 02:54:41 -08001573 private void showPreviews(final View anchor, int start, int end) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001574 Resources resources = getResources();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001575
Romain Guya6abce82009-11-10 02:54:41 -08001576 Workspace workspace = mWorkspace;
1577 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001578
1579 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001580
1581 Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001582 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001583 int extraW = (int) ((r.left + r.right) * max);
1584 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001585
1586 int aW = cell.getWidth() - extraW;
1587 float w = aW / max;
1588
1589 int width = cell.getWidth();
1590 int height = cell.getHeight();
1591 int x = cell.getLeftPadding();
1592 int y = cell.getTopPadding();
1593 width -= (x + cell.getRightPadding());
1594 height -= (y + cell.getBottomPadding());
1595
1596 float scale = w / width;
1597
1598 int count = end - start;
1599
1600 final float sWidth = width * scale;
1601 float sHeight = height * scale;
1602
Romain Guye6b8e2f2009-11-10 11:56:55 -08001603 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001604
Romain Guye6b8e2f2009-11-10 11:56:55 -08001605 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1606 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001607
1608 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001609 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001610 cell = (CellLayout) workspace.getChildAt(i);
1611
Romain Guye6b8e2f2009-11-10 11:56:55 -08001612 Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
1613 Bitmap.Config.ARGB_8888);
1614
1615 Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001616 c.scale(scale, scale);
1617 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1618 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001619
Romain Guy9d31e9f2009-11-11 18:54:28 -08001620 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001621 image.setImageBitmap(bitmap);
1622 image.setTag(i);
1623 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001624 image.setOnFocusChangeListener(handler);
1625 image.setFocusable(true);
1626 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001627
1628 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001629 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1630
1631 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001632 }
Romain Guye6b8e2f2009-11-10 11:56:55 -08001633
1634 PopupWindow p = new PopupWindow(this);
1635 p.setContentView(preview);
1636 p.setWidth((int) (sWidth * count + extraW));
1637 p.setHeight((int) (sHeight + extraH));
1638 p.setAnimationStyle(R.style.AnimationPreview);
1639 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001640 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001641 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001642 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001643
Romain Guye6b8e2f2009-11-10 11:56:55 -08001644 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1645 public void onDismiss() {
1646 dismissPreview(anchor);
1647 }
1648 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001649
1650 anchor.setTag(p);
1651 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001652 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001653 }
1654
Romain Guy9d31e9f2009-11-11 18:54:28 -08001655 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001656 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001657
Romain Guye6b8e2f2009-11-10 11:56:55 -08001658 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001659 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001660 }
1661
1662 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001663 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001664 v.post(this);
1665 }
1666
1667 public void run() {
1668 dismissPreview(mAnchor);
1669 }
1670
1671 public void onFocusChange(View v, boolean hasFocus) {
1672 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001673 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001674 }
Romain Guya6abce82009-11-10 02:54:41 -08001675 }
1676 }
1677
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001678 View getDrawerHandle() {
1679 return mHandleView;
1680 }
1681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 Workspace getWorkspace() {
1683 return mWorkspace;
1684 }
1685
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 @Override
1687 protected Dialog onCreateDialog(int id) {
1688 switch (id) {
1689 case DIALOG_CREATE_SHORTCUT:
1690 return new CreateShortcut().createDialog();
1691 case DIALOG_RENAME_FOLDER:
1692 return new RenameFolder().createDialog();
1693 }
1694
1695 return super.onCreateDialog(id);
1696 }
1697
1698 @Override
1699 protected void onPrepareDialog(int id, Dialog dialog) {
1700 switch (id) {
1701 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 break;
1703 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001704 if (mFolderInfo != null) {
1705 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1706 final CharSequence text = mFolderInfo.title;
1707 input.setText(text);
1708 input.setSelection(0, text.length());
1709 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 break;
1711 }
1712 }
1713
1714 void showRenameDialog(FolderInfo info) {
1715 mFolderInfo = info;
1716 mWaitingForResult = true;
1717 showDialog(DIALOG_RENAME_FOLDER);
1718 }
1719
1720 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1721 mAddItemCellInfo = cellInfo;
1722 mWaitingForResult = true;
1723 showDialog(DIALOG_CREATE_SHORTCUT);
1724 }
1725
Romain Guy73b979d2009-06-09 12:57:21 -07001726 private void pickShortcut(int requestCode, int title) {
1727 Bundle bundle = new Bundle();
1728
1729 ArrayList<String> shortcutNames = new ArrayList<String>();
1730 shortcutNames.add(getString(R.string.group_applications));
1731 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1732
1733 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1734 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1735 R.drawable.ic_launcher_application));
1736 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1737
1738 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1739 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1740 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1741 pickIntent.putExtras(bundle);
1742
1743 startActivityForResult(pickIntent, requestCode);
1744 }
1745
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 private class RenameFolder {
1747 private EditText mInput;
1748
1749 Dialog createDialog() {
1750 mWaitingForResult = true;
1751 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1752 mInput = (EditText) layout.findViewById(R.id.folder_name);
1753
1754 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1755 builder.setIcon(0);
1756 builder.setTitle(getString(R.string.rename_folder_title));
1757 builder.setCancelable(true);
1758 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1759 public void onCancel(DialogInterface dialog) {
1760 cleanup();
1761 }
1762 });
1763 builder.setNegativeButton(getString(R.string.cancel_action),
1764 new Dialog.OnClickListener() {
1765 public void onClick(DialogInterface dialog, int which) {
1766 cleanup();
1767 }
1768 }
1769 );
1770 builder.setPositiveButton(getString(R.string.rename_action),
1771 new Dialog.OnClickListener() {
1772 public void onClick(DialogInterface dialog, int which) {
1773 changeFolderName();
1774 }
1775 }
1776 );
1777 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001778
1779 final AlertDialog dialog = builder.create();
1780 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1781 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001782 mInput.requestFocus();
1783 InputMethodManager inputManager = (InputMethodManager)
1784 getSystemService(Context.INPUT_METHOD_SERVICE);
1785 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001786 }
1787 });
1788
1789 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
1791
1792 private void changeFolderName() {
1793 final String name = mInput.getText().toString();
1794 if (!TextUtils.isEmpty(name)) {
1795 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001796 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 mFolderInfo.title = name;
1798 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1799
Joe Onorato9c1289c2009-08-17 11:03:03 -04001800 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001801 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001802 mModel.setWorkspaceDirty();
1803 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 } else {
1805 final FolderIcon folderIcon = (FolderIcon)
1806 mWorkspace.getViewForTag(mFolderInfo);
1807 if (folderIcon != null) {
1808 folderIcon.setText(name);
1809 getWorkspace().requestLayout();
1810 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001811 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001812 mModel.setWorkspaceDirty();
1813 mWorkspaceLoading = true;
1814 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 }
1816 }
1817 }
1818 cleanup();
1819 }
1820
1821 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 dismissDialog(DIALOG_RENAME_FOLDER);
1823 mWaitingForResult = false;
1824 mFolderInfo = null;
1825 }
1826 }
1827
Joe Onoratofb0ca672009-09-14 17:55:46 -04001828 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001829 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001830 }
1831
Joe Onorato3a8820b2009-11-10 15:06:42 -08001832 boolean isAllAppsOpaque() {
1833 return mAllAppsGrid.isOpaque();
1834 }
1835
1836 void showAllApps(boolean animated) {
1837 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato7bb17492009-09-24 17:51:01 -07001838 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001839
Joe Onorato3a8820b2009-11-10 15:06:42 -08001840 mWorkspace.startFading(false);
1841
Mike Cleronb6082fa2009-10-19 17:03:36 -07001842 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001843 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001844
Joe Onorato7c312c12009-08-13 21:36:53 -07001845 // TODO: fade these two too
1846 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001847 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001848 }
1849
Joe Onoratob2061212009-11-24 16:13:54 -05001850 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001851 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001852 * - Home from workspace
1853 * - from center screen
1854 * - from other screens
1855 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001856 * - from center screen
1857 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001858 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001859 * - from center screen
1860 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001861 * - Launch app from workspace and quit
1862 * - with back
1863 * - with home
1864 * - Launch app from all apps and quit
1865 * - with back
1866 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001867 * - Go to a screen that's not the default, then all
1868 * apps, and launch and app, and go back
1869 * - with back
1870 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001871 * - On workspace, long press power and go back
1872 * - with back
1873 * - with home
1874 * - On all apps, long press power and go back
1875 * - with back
1876 * - with home
1877 * - On workspace, power off
1878 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001879 * - Launch an app and turn off the screen while in that app
1880 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001881 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001882 * - From all apps
1883 * - From workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001884 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001885 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001886 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001887 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001888 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001889 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001890 mWorkspace.startFading(true);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001891
Joe Onorato7c312c12009-08-13 21:36:53 -07001892 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001893 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001894 mDeleteZone.setVisibility(View.VISIBLE);
1895 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001896 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001897 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001898 }
1899
Joe Onorato7c312c12009-08-13 21:36:53 -07001900 void lockAllApps() {
1901 // TODO
1902 }
1903
1904 void unlockAllApps() {
1905 // TODO
1906 }
1907
Joe Onorato7404ee42009-07-31 11:54:44 -07001908 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 * Displays the shortcut creation dialog and launches, if necessary, the
1910 * appropriate activity.
1911 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001912 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001913 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1914 DialogInterface.OnShowListener {
1915
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001917
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 Dialog createDialog() {
1919 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001920
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1924 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001925 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001926
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 builder.setInverseBackgroundForced(true);
1928
1929 AlertDialog dialog = builder.create();
1930 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001931 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001932 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 return dialog;
1935 }
1936
1937 public void onCancel(DialogInterface dialog) {
1938 mWaitingForResult = false;
1939 cleanup();
1940 }
1941
Romain Guycbb89e42009-06-08 15:52:54 -07001942 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001943 }
1944
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001946 try {
1947 dismissDialog(DIALOG_CREATE_SHORTCUT);
1948 } catch (Exception e) {
1949 // An exception is thrown if the dialog is not visible, which is fine
1950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 }
1952
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001953 /**
1954 * Handle the action clicked in the "Add to home" dialog.
1955 */
1956 public void onClick(DialogInterface dialog, int which) {
1957 Resources res = getResources();
1958 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001959
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001960 switch (which) {
1961 case AddAdapter.ITEM_SHORTCUT: {
1962 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001963 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001964 break;
1965 }
Romain Guycbb89e42009-06-08 15:52:54 -07001966
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001967 case AddAdapter.ITEM_APPWIDGET: {
1968 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001969
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001970 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1971 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1972 // add the search widget
1973 ArrayList<AppWidgetProviderInfo> customInfo =
1974 new ArrayList<AppWidgetProviderInfo>();
1975 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1976 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1977 info.label = getString(R.string.group_search);
1978 info.icon = R.drawable.ic_search_widget;
1979 customInfo.add(info);
1980 pickIntent.putParcelableArrayListExtra(
1981 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1982 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1983 Bundle b = new Bundle();
1984 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1985 customExtras.add(b);
1986 pickIntent.putParcelableArrayListExtra(
1987 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1988 // start the pick activity
1989 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1990 break;
1991 }
Romain Guycbb89e42009-06-08 15:52:54 -07001992
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001993 case AddAdapter.ITEM_LIVE_FOLDER: {
1994 // Insert extra item to handle inserting folder
1995 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001996
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001997 ArrayList<String> shortcutNames = new ArrayList<String>();
1998 shortcutNames.add(res.getString(R.string.group_folder));
1999 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002000
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002001 ArrayList<ShortcutIconResource> shortcutIcons =
2002 new ArrayList<ShortcutIconResource>();
2003 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2004 R.drawable.ic_launcher_folder));
2005 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2006
2007 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2008 pickIntent.putExtra(Intent.EXTRA_INTENT,
2009 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2010 pickIntent.putExtra(Intent.EXTRA_TITLE,
2011 getText(R.string.title_select_live_folder));
2012 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002013
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002014 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2015 break;
2016 }
2017
2018 case AddAdapter.ITEM_WALLPAPER: {
2019 startWallpaper();
2020 break;
2021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002024
2025 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07002026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028
2029 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002030 * Receives notifications when applications are added/removed.
2031 */
2032 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2033 @Override
2034 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002035 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002036 String reason = intent.getStringExtra("reason");
2037 if (!"homekey".equals(reason)) {
2038 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002039 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002040 animate = false;
2041 }
Joe Onoratob2061212009-11-24 16:13:54 -05002042 closeAllApps(animate);
2043 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002044 }
2045 }
2046
2047 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002048 * Receives notifications whenever the appwidgets are reset.
2049 */
2050 private class AppWidgetResetObserver extends ContentObserver {
2051 public AppWidgetResetObserver() {
2052 super(new Handler());
2053 }
2054
2055 @Override
2056 public void onChange(boolean selfChange) {
2057 onAppWidgetReset();
2058 }
2059 }
2060
2061 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002062 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002064 public int getCurrentWorkspaceScreen() {
2065 return mWorkspace.getCurrentScreen();
2066 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002067
Joe Onorato9c1289c2009-08-17 11:03:03 -04002068 /**
2069 * Refreshes the shortcuts shown on the workspace.
2070 *
2071 * Implementation of the method from LauncherModel.Callbacks.
2072 */
2073 public void startBinding() {
2074 final Workspace workspace = mWorkspace;
2075 int count = workspace.getChildCount();
2076 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002077 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002078 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2079 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002080
Joe Onorato9c1289c2009-08-17 11:03:03 -04002081 if (DEBUG_USER_INTERFACE) {
2082 android.widget.Button finishButton = new android.widget.Button(this);
2083 finishButton.setText("Finish");
2084 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2085
2086 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2087 public void onClick(View v) {
2088 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002089 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002090 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 }
2092 }
2093
2094 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095 * Bind the items start-end from the list.
2096 *
2097 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002099 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2100
2101 final Workspace workspace = mWorkspace;
2102
2103 for (int i=start; i<end; i++) {
2104 final ItemInfo item = shortcuts.get(i);
2105 mDesktopItems.add(item);
2106 switch (item.itemType) {
2107 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2108 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2109 final View shortcut = createShortcut((ApplicationInfo) item);
2110 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2111 false);
2112 break;
2113 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2114 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2115 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2116 (UserFolderInfo) item);
2117 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2118 false);
2119 break;
2120 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2121 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2122 R.layout.live_folder_icon, this,
2123 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2124 (LiveFolderInfo) item);
2125 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2126 false);
2127 break;
2128 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2129 final int screen = workspace.getCurrentScreen();
2130 final View view = mInflater.inflate(R.layout.widget_search,
2131 (ViewGroup) workspace.getChildAt(screen), false);
2132
2133 Search search = (Search) view.findViewById(R.id.widget_search);
2134 search.setLauncher(this);
2135
2136 final Widget widget = (Widget) item;
2137 view.setTag(widget);
2138
2139 workspace.addWidget(view, widget, false);
2140 break;
2141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 }
2143
Joe Onorato9c1289c2009-08-17 11:03:03 -04002144 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002145 }
2146
Joe Onorato9c1289c2009-08-17 11:03:03 -04002147 /**
2148 * Implementation of the method from LauncherModel.Callbacks.
2149 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002150 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2151 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002152 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002154
2155 /**
2156 * Add the views for a widget to the workspace.
2157 *
2158 * Implementation of the method from LauncherModel.Callbacks.
2159 */
2160 public void bindAppWidget(LauncherAppWidgetInfo item) {
2161 final Workspace workspace = mWorkspace;
2162
2163 final int appWidgetId = item.appWidgetId;
2164 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2165 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2166
Joe Onorato9c1289c2009-08-17 11:03:03 -04002167 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2168 item.hostView.setTag(item);
2169
2170 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2171 item.cellY, item.spanX, item.spanY, false);
2172
2173 workspace.requestLayout();
2174
2175 mDesktopItems.add(item);
2176 }
2177
2178 /**
2179 * Callback saying that there aren't any more items to bind.
2180 *
2181 * Implementation of the method from LauncherModel.Callbacks.
2182 */
2183 public void finishBindingItems() {
2184 if (mSavedState != null) {
2185 if (!mWorkspace.hasFocus()) {
2186 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2187 }
2188
2189 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2190 if (userFolders != null) {
2191 for (long folderId : userFolders) {
2192 final FolderInfo info = mFolders.get(folderId);
2193 if (info != null) {
2194 openFolder(info);
2195 }
2196 }
2197 final Folder openFolder = mWorkspace.getOpenFolder();
2198 if (openFolder != null) {
2199 openFolder.requestFocus();
2200 }
2201 }
2202
Joe Onorato9c1289c2009-08-17 11:03:03 -04002203 mSavedState = null;
2204 }
2205
2206 if (mSavedInstanceState != null) {
2207 super.onRestoreInstanceState(mSavedInstanceState);
2208 mSavedInstanceState = null;
2209 }
2210
Joe Onorato9c1289c2009-08-17 11:03:03 -04002211 mWorkspaceLoading = false;
2212 }
2213
2214 /**
2215 * Add the icons for all apps.
2216 *
2217 * Implementation of the method from LauncherModel.Callbacks.
2218 */
2219 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002220 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002221 }
2222
2223 /**
2224 * A package was installed.
2225 *
2226 * Implementation of the method from LauncherModel.Callbacks.
2227 */
2228 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2229 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002230 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002231 }
2232
2233 /**
2234 * A package was updated.
2235 *
2236 * Implementation of the method from LauncherModel.Callbacks.
2237 */
2238 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2239 removeDialog(DIALOG_CREATE_SHORTCUT);
2240 mWorkspace.updateShortcutsForPackage(packageName);
Joe Onoratof0be2132009-11-24 19:34:29 -05002241 mAllAppsGrid.updateApps(packageName, apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002242 }
2243
2244 /**
2245 * A package was uninstalled.
2246 *
2247 * Implementation of the method from LauncherModel.Callbacks.
2248 */
2249 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2250 removeDialog(DIALOG_CREATE_SHORTCUT);
2251 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002252 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002253 }
Joe Onoratobe386092009-11-17 17:32:16 -08002254
2255 /**
2256 * Prints out out state for debugging.
2257 */
2258 public void dumpState() {
2259 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002260 Log.d(TAG, "mSavedState=" + mSavedState);
2261 Log.d(TAG, "mIsNewIntent=" + mIsNewIntent);
2262 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2263 Log.d(TAG, "mRestoring=" + mRestoring);
2264 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2265 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2266 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2267 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002268 mModel.dumpState();
2269 mAllAppsGrid.dumpState();
2270 Log.d(TAG, "END launcher2 dump state");
2271 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272}