blob: 6ca46243f82810371dd52256dd17821d352be81b [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 Hackborn8355ae32009-09-07 21:47:51 -070025import android.app.WallpaperInfo;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080028import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080030import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070034import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.pm.ActivityInfo;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070037import android.content.pm.LabeledIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.pm.PackageManager;
39import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070041import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080042import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080044import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080045import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080047import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080049import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Parcelable;
51import android.os.RemoteException;
52import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070053import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.text.Selection;
55import android.text.SpannableStringBuilder;
56import android.text.TextUtils;
57import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070058import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.view.Display;
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
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private boolean mRestoring;
191 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
193 private Bundle mSavedInstanceState;
194
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 private LauncherModel mModel;
196
Romain Guy84f296c2009-11-04 15:00:44 -0800197 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
198 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700199
Romain Guy1fbc1c82009-11-09 20:43:08 -0800200 private ImageView mPreviousView;
201 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 @Override
204 protected void onCreate(Bundle savedInstanceState) {
205 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700206
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400208 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800211 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
212 registerReceiver(mCloseSystemDialogsReceiver, filter);
213
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
216 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 if (PROFILE_STARTUP) {
219 android.os.Debug.startMethodTracing("/sdcard/launcher");
220 }
221
222 checkForLocaleChange();
223 setWallpaperDimension();
224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 setContentView(R.layout.launcher);
226 setupViews();
227
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800228 registerContentObservers();
229
Joe Onorato7c312c12009-08-13 21:36:53 -0700230 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 mSavedState = savedInstanceState;
233 restoreState(mSavedState);
234
235 if (PROFILE_STARTUP) {
236 android.os.Debug.stopMethodTracing();
237 }
238
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700239 // We have a new AllAppsView, we need to re-bind everything, and it could have
240 // changed in our absence.
241 mModel.setAllAppsDirty();
242 mModel.setWorkspaceDirty();
243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 }
247
248 // For handling default keys
249 mDefaultKeySsb = new SpannableStringBuilder();
250 Selection.setSelection(mDefaultKeySsb, 0);
251 }
Romain Guycbb89e42009-06-08 15:52:54 -0700252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700254 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
255 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 final Configuration configuration = getResources().getConfiguration();
258
Romain Guy98d01652009-06-30 16:21:04 -0700259 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 final String locale = configuration.locale.toString();
261
Romain Guy98d01652009-06-30 16:21:04 -0700262 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 final int mcc = configuration.mcc;
264
Romain Guy98d01652009-06-30 16:21:04 -0700265 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 final int mnc = configuration.mnc;
267
Romain Guy84f296c2009-11-04 15:00:44 -0800268 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
Romain Guy84f296c2009-11-04 15:00:44 -0800270 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700271 localeConfiguration.locale = locale;
272 localeConfiguration.mcc = mcc;
273 localeConfiguration.mnc = mnc;
274
275 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700277 }
278 }
279
280 private static class LocaleConfiguration {
281 public String locale;
282 public int mcc = -1;
283 public int mnc = -1;
284 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700285
Romain Guy98d01652009-06-30 16:21:04 -0700286 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
287 DataInputStream in = null;
288 try {
289 in = new DataInputStream(context.openFileInput(PREFERENCES));
290 configuration.locale = in.readUTF();
291 configuration.mcc = in.readInt();
292 configuration.mnc = in.readInt();
293 } catch (FileNotFoundException e) {
294 // Ignore
295 } catch (IOException e) {
296 // Ignore
297 } finally {
298 if (in != null) {
299 try {
300 in.close();
301 } catch (IOException e) {
302 // Ignore
303 }
304 }
305 }
306 }
307
308 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
309 DataOutputStream out = null;
310 try {
311 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
312 out.writeUTF(configuration.locale);
313 out.writeInt(configuration.mcc);
314 out.writeInt(configuration.mnc);
315 out.flush();
316 } catch (FileNotFoundException e) {
317 // Ignore
318 } catch (IOException e) {
319 //noinspection ResultOfMethodCallIgnored
320 context.getFileStreamPath(PREFERENCES).delete();
321 } finally {
322 if (out != null) {
323 try {
324 out.close();
325 } catch (IOException e) {
326 // Ignore
327 }
328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 }
330 }
331
332 static int getScreen() {
333 synchronized (sLock) {
334 return sScreen;
335 }
336 }
337
338 static void setScreen(int screen) {
339 synchronized (sLock) {
340 sScreen = screen;
341 }
342 }
343
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700345 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346
347 Display display = getWindowManager().getDefaultDisplay();
348 boolean isPortrait = display.getWidth() < display.getHeight();
349
350 final int width = isPortrait ? display.getWidth() : display.getHeight();
351 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700352 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 }
354
355 @Override
356 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700357 mWaitingForResult = false;
358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 // The pattern used here is that a user PICKs a specific application,
360 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700361
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
363 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700364
Romain Guy94dabf12009-07-21 10:55:43 -0700365 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 switch (requestCode) {
367 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 break;
370 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700371 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 break;
373 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400374 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 break;
376 case REQUEST_PICK_LIVE_FOLDER:
377 addLiveFolder(data);
378 break;
379 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 case REQUEST_PICK_APPWIDGET:
383 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700385 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400386 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700388 case REQUEST_PICK_WALLPAPER:
389 // We just wanted the activity result here so we can clear mWaitingForResult
390 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 }
Romain Guy18042c82009-11-06 11:44:55 -0800392 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
393 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
394 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700395 // Clean up the appWidgetId if we canceled
396 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
397 if (appWidgetId != -1) {
398 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 }
400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
402
403 @Override
404 protected void onResume() {
405 super.onResume();
406
407 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400408 mWorkspaceLoading = true;
409 mModel.startLoader(this, true);
410 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700412
Mike LeBeau736cf282009-07-02 17:46:59 -0700413 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
414 // onNewIntent), then close the search dialog if needed, because it probably
415 // came from the user pressing 'home' (rather than, for example, pressing 'back').
416 if (mIsNewIntent) {
417 // Post to a handler so that this happens after the search dialog tries to open
418 // itself again.
419 mWorkspace.post(new Runnable() {
420 public void run() {
421 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
422 ServiceManager.getService(Context.SEARCH_SERVICE));
423 try {
424 searchManagerService.stopSearch();
425 } catch (RemoteException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800426 Log.e(TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700427 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700428 }
429 });
430 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700431
Mike LeBeau736cf282009-07-02 17:46:59 -0700432 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 }
434
435 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700436 protected void onPause() {
437 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800438 dismissPreview(mPreviousView);
439 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800440 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700441 }
Romain Guycbb89e42009-06-08 15:52:54 -0700442
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700443 @Override
444 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400445 // Flag the loader to stop early before switching
446 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700447
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700448 if (PROFILE_ROTATE) {
449 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
450 }
451 return null;
452 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 private boolean acceptFilter() {
455 final InputMethodManager inputManager = (InputMethodManager)
456 getSystemService(Context.INPUT_METHOD_SERVICE);
457 return !inputManager.isFullscreenMode();
458 }
459
460 @Override
461 public boolean onKeyDown(int keyCode, KeyEvent event) {
462 boolean handled = super.onKeyDown(keyCode, event);
463 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
464 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
465 keyCode, event);
466 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700467 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700468 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700469 // showSearchDialog()
470 // If there are multiple keystrokes before the search dialog takes focus,
471 // onSearchRequested() will be called for every keystroke,
472 // but it is idempotent, so it's fine.
473 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
475 }
476
477 return handled;
478 }
479
Karl Rosaen138a0412009-04-23 19:00:21 -0700480 private String getTypedText() {
481 return mDefaultKeySsb.toString();
482 }
483
484 private void clearTypedText() {
485 mDefaultKeySsb.clear();
486 mDefaultKeySsb.clearSpans();
487 Selection.setSelection(mDefaultKeySsb, 0);
488 }
489
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 /**
491 * Restores the previous state, if it exists.
492 *
493 * @param savedState The previous state.
494 */
495 private void restoreState(Bundle savedState) {
496 if (savedState == null) {
497 return;
498 }
499
Joe Onorato3a8820b2009-11-10 15:06:42 -0800500 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
501 if (allApps) {
502 showAllApps(false);
503 }
504
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
506 if (currentScreen > -1) {
507 mWorkspace.setCurrentScreen(currentScreen);
508 }
509
510 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
511 if (addScreen > -1) {
512 mAddItemCellInfo = new CellLayout.CellInfo();
513 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
514 addItemCellInfo.valid = true;
515 addItemCellInfo.screen = addScreen;
516 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
517 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
518 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
519 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
520 addItemCellInfo.findVacantCellsFromOccupied(
521 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
522 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
523 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
524 mRestoring = true;
525 }
526
527 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
528 if (renameFolder) {
529 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400530 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 mRestoring = true;
532 }
533 }
534
535 /**
536 * Finds all the views we need and configure them properly.
537 */
538 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400539 DragController dragController = mDragController;
540
Romain Guyb1b69f52009-08-10 15:10:15 -0700541 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400542 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543
Joe Onorato7c312c12009-08-13 21:36:53 -0700544 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700545 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700546 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700547 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700548 // Manage focusability manually since this thing is always visible
549 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700550
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
552 final Workspace workspace = mWorkspace;
553
Joe Onorato7c312c12009-08-13 21:36:53 -0700554 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
555 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556
Joe Onorato7c312c12009-08-13 21:36:53 -0700557 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700558 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700559 mHandleView.setOnClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800560
561 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
562 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
563
564 Drawable previous = mPreviousView.getDrawable();
565 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800566 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567
Romain Guy1fbc1c82009-11-09 20:43:08 -0800568 mPreviousView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800569 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800570
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400572 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574
575 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400576 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700577 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578
Joe Onorato00acb122009-08-04 16:04:30 -0400579 dragController.setDragScoller(workspace);
580 dragController.setDragListener(deleteZone);
581 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700582
Joe Onorato00acb122009-08-04 16:04:30 -0400583 // The order here is bottom to top.
584 dragController.addDropTarget(workspace);
585 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 }
587
Romain Guy8a73c512009-11-09 19:19:59 -0800588 @SuppressWarnings({"UnusedDeclaration"})
589 public void previousScreen(View v) {
590 mWorkspace.scrollLeft();
591 }
592
593 @SuppressWarnings({"UnusedDeclaration"})
594 public void nextScreen(View v) {
595 mWorkspace.scrollRight();
596 }
597
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 /**
599 * Creates a view representing a shortcut.
600 *
601 * @param info The data structure describing the shortcut.
602 *
603 * @return A View inflated from R.layout.application.
604 */
605 View createShortcut(ApplicationInfo info) {
606 return createShortcut(R.layout.application,
607 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
608 }
609
610 /**
611 * Creates a view representing a shortcut inflated from the specified resource.
612 *
613 * @param layoutResId The id of the XML layout used to create the shortcut.
614 * @param parent The group the shortcut belongs to.
615 * @param info The data structure describing the shortcut.
616 *
617 * @return A View inflated from layoutResId.
618 */
619 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
620 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
621
Joe Onorato5162ea92009-09-03 09:39:42 -0700622 if (info.icon == null) {
623 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
624 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700626 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 info.filtered = true;
628 }
629
630 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
631 favorite.setText(info.title);
632 favorite.setTag(info);
633 favorite.setOnClickListener(this);
634
635 return favorite;
636 }
637
638 /**
639 * Add an application shortcut to the workspace.
640 *
641 * @param data The intent describing the application.
642 * @param cellInfo The position on screen where to create the shortcut.
643 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 cellInfo.screen = mWorkspace.getCurrentScreen();
646 if (!findSingleSlot(cellInfo)) return;
647
Romain Guy73b979d2009-06-09 12:57:21 -0700648 final ApplicationInfo info = infoFromApplicationIntent(context, data);
649 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700651 }
652 }
653
654 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 ComponentName component = data.getComponent();
656 PackageManager packageManager = context.getPackageManager();
657 ActivityInfo activityInfo = null;
658 try {
659 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
660 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800661 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
Romain Guycbb89e42009-06-08 15:52:54 -0700663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 if (activityInfo != null) {
665 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 itemInfo.title = activityInfo.loadLabel(packageManager);
668 if (itemInfo.title == null) {
669 itemInfo.title = activityInfo.name;
670 }
Romain Guycbb89e42009-06-08 15:52:54 -0700671
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
673 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
674 itemInfo.icon = activityInfo.loadIcon(packageManager);
675 itemInfo.container = ItemInfo.NO_ID;
676
Romain Guy73b979d2009-06-09 12:57:21 -0700677 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
Romain Guy73b979d2009-06-09 12:57:21 -0700679
680 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
Romain Guycbb89e42009-06-08 15:52:54 -0700682
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 /**
684 * Add a shortcut to the workspace.
685 *
686 * @param data The intent describing the shortcut.
687 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400689 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 cellInfo.screen = mWorkspace.getCurrentScreen();
691 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
694
695 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
698 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 }
700 }
701
Romain Guycbb89e42009-06-08 15:52:54 -0700702
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700704 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 * @param data The intent describing the appWidgetId.
707 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400709 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700711 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700712
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800713 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700714
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700715 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700716
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700719 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700720
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 // Try finding open space on Launcher screen
722 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800723 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
724 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
725 return;
726 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 // Build Launcher-specific widget info and save to database
729 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 launcherInfo.spanX = spans[0];
731 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 LauncherModel.addItemToDatabase(this, launcherInfo,
734 LauncherSettings.Favorites.CONTAINER_DESKTOP,
735 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
736
737 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700741 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700742
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700743 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700745
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 }
749 }
Romain Guycbb89e42009-06-08 15:52:54 -0700750
Joe Onorato9c1289c2009-08-17 11:03:03 -0400751 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
752 mDesktopItems.remove(launcherInfo);
753 launcherInfo.hostView = null;
754 }
755
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700756 public LauncherAppWidgetHost getAppWidgetHost() {
757 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
Romain Guycbb89e42009-06-08 15:52:54 -0700759
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 static ApplicationInfo addShortcut(Context context, Intent data,
761 CellLayout.CellInfo cellInfo, boolean notify) {
762
Romain Guy73b979d2009-06-09 12:57:21 -0700763 final ApplicationInfo info = infoFromShortcutIntent(context, data);
764 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
765 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
766
767 return info;
768 }
769
770 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
772 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
773 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
774
775 Drawable icon = null;
776 boolean filtered = false;
777 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700778 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779
780 if (bitmap != null) {
781 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
782 filtered = true;
783 customIcon = true;
784 } else {
785 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700786 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700788 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 final PackageManager packageManager = context.getPackageManager();
790 Resources resources = packageManager.getResourcesForApplication(
791 iconResource.packageName);
792 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
793 icon = resources.getDrawable(id);
794 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800795 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 }
797 }
798 }
799
800 if (icon == null) {
801 icon = context.getPackageManager().getDefaultActivityIcon();
802 }
803
804 final ApplicationInfo info = new ApplicationInfo();
805 info.icon = icon;
806 info.filtered = filtered;
807 info.title = name;
808 info.intent = intent;
809 info.customIcon = customIcon;
810 info.iconResource = iconResource;
811
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 return info;
813 }
814
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800815 void closeSystemDialogs() {
816 closeAllApps(false);
817 getWindow().closeAllPanels();
818
819 try {
820 dismissDialog(DIALOG_CREATE_SHORTCUT);
821 // Unlock the workspace if the dialog was showing
822 } catch (Exception e) {
823 // An exception is thrown if the dialog is not visible, which is fine
824 }
825
826 try {
827 dismissDialog(DIALOG_RENAME_FOLDER);
828 // Unlock the workspace if the dialog was showing
829 } catch (Exception e) {
830 // An exception is thrown if the dialog is not visible, which is fine
831 }
832 }
833
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 @Override
835 protected void onNewIntent(Intent intent) {
836 super.onNewIntent(intent);
837
838 // Close the menu
839 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800840 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700841
Joe Onorato742966b2009-10-07 10:26:17 -0700842 // Whatever we were doing is hereby canceled.
843 mWaitingForResult = false;
844
Mike LeBeau736cf282009-07-02 17:46:59 -0700845 // Set this flag so that onResume knows to close the search dialog if it's open,
846 // because this was a new intent (thus a press of 'home' or some such) rather than
847 // for example onResume being called when the user pressed the 'back' button.
848 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849
Joe Onorato3a8820b2009-11-10 15:06:42 -0800850 if (!mWorkspace.isDefaultScreenShowing()) {
851 mWorkspace.moveToDefaultScreen();
852 }
Romain Guy73b979d2009-06-09 12:57:21 -0700853
Joe Onorato3a8820b2009-11-10 15:06:42 -0800854 closeAllApps(false);
Romain Guy1dd3a072009-07-16 13:21:01 -0700855
Joe Onorato3a8820b2009-11-10 15:06:42 -0800856 final View v = getWindow().peekDecorView();
857 if (v != null && v.getWindowToken() != null) {
858 InputMethodManager imm = (InputMethodManager)getSystemService(
859 INPUT_METHOD_SERVICE);
860 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 }
862 }
863 }
864
865 @Override
866 protected void onRestoreInstanceState(Bundle savedInstanceState) {
867 // Do not call super here
868 mSavedInstanceState = savedInstanceState;
869 }
870
871 @Override
872 protected void onSaveInstanceState(Bundle outState) {
873 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
874
875 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
876 if (folders.size() > 0) {
877 final int count = folders.size();
878 long[] ids = new long[count];
879 for (int i = 0; i < count; i++) {
880 final FolderInfo info = folders.get(i).getInfo();
881 ids[i] = info.id;
882 }
883 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
884 } else {
885 super.onSaveInstanceState(outState);
886 }
887
Joe Onoratofb0ca672009-09-14 17:55:46 -0400888 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800889 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892
893 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
894 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
895 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
896
897 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
898 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
899 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
900 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
901 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
902 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
903 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
904 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
905 layout.getOccupiedCells());
906 }
907
908 if (mFolderInfo != null && mWaitingForResult) {
909 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
910 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
911 }
912 }
913
914 @Override
915 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700917
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700919 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800921 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 }
923
924 TextKeyListener.getInstance().release();
925
Joe Onorato9c1289c2009-08-17 11:03:03 -0400926 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927
Joe Onorato9c1289c2009-08-17 11:03:03 -0400928 unbindDesktopItems();
929 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800930
931 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800932
933 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800934 dismissPreview(mNextView);
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800935
936 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
938
939 @Override
940 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700941 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 super.startActivityForResult(intent, requestCode);
943 }
944
945 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700946 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700948
Joe Onorato7bb17492009-09-24 17:51:01 -0700949 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700950
Karl Rosaen138a0412009-04-23 19:00:21 -0700951 // Slide the search widget to the top, if it's on the current screen,
952 // otherwise show the search dialog immediately.
953 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
954 if (searchWidget == null) {
955 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
956 } else {
957 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
958 // show the currently typed text in the search widget while sliding
959 searchWidget.setQuery(getTypedText());
960 }
961 }
Romain Guycbb89e42009-06-08 15:52:54 -0700962
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 /**
964 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700965 *
966 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 */
Romain Guycbb89e42009-06-08 15:52:54 -0700968 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700970
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 if (initialQuery == null) {
972 // Use any text typed in the launcher as the initial query
973 initialQuery = getTypedText();
974 clearTypedText();
975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 if (appSearchData == null) {
977 appSearchData = new Bundle();
978 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
979 }
Romain Guycbb89e42009-06-08 15:52:54 -0700980
Karl Rosaen138a0412009-04-23 19:00:21 -0700981 final SearchManager searchManager =
982 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
983
984 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
985 if (searchWidget != null) {
986 // This gets called when the user leaves the search dialog to go back to
987 // the Launcher.
988 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
989 public void onCancel() {
990 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700991 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700992 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700993 });
994 }
Romain Guycbb89e42009-06-08 15:52:54 -0700995
Karl Rosaen138a0412009-04-23 19:00:21 -0700996 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700997 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 }
999
Karl Rosaen138a0412009-04-23 19:00:21 -07001000 /**
1001 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001002 */
Romain Guy5a941392009-04-28 15:18:25 -07001003 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001004 // Close search dialog
1005 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001006 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001007 // Restore search widget to its normal position
1008 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1009 if (searchWidget != null) {
1010 searchWidget.stopSearch(false);
1011 }
1012 }
Romain Guycbb89e42009-06-08 15:52:54 -07001013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 @Override
1015 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001016 if (isWorkspaceLocked()) {
1017 return false;
1018 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019
1020 super.onCreateOptionsMenu(menu);
1021 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1022 .setIcon(android.R.drawable.ic_menu_add)
1023 .setAlphabeticShortcut('A');
1024 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1025 .setIcon(android.R.drawable.ic_menu_gallery)
1026 .setAlphabeticShortcut('W');
1027 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1028 .setIcon(android.R.drawable.ic_search_category_default)
1029 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1030 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1031 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1032 .setAlphabeticShortcut('N');
1033
1034 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001035 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1036 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037
1038 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1039 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1040 .setIntent(settings);
1041
1042 return true;
1043 }
1044
1045 @Override
1046 public boolean onPrepareOptionsMenu(Menu menu) {
1047 super.onPrepareOptionsMenu(menu);
1048
1049 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1050 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1051
1052 return true;
1053 }
1054
1055 @Override
1056 public boolean onOptionsItemSelected(MenuItem item) {
1057 switch (item.getItemId()) {
1058 case MENU_ADD:
1059 addItems();
1060 return true;
1061 case MENU_WALLPAPER_SETTINGS:
1062 startWallpaper();
1063 return true;
1064 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001065 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 return true;
1067 case MENU_NOTIFICATIONS:
1068 showNotifications();
1069 return true;
1070 }
1071
1072 return super.onOptionsItemSelected(item);
1073 }
Romain Guycbb89e42009-06-08 15:52:54 -07001074
Karl Rosaen138a0412009-04-23 19:00:21 -07001075 /**
1076 * Indicates that we want global search for this activity by setting the globalSearch
1077 * argument for {@link #startSearch} to true.
1078 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001080 @Override
1081 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001082 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001083 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001084 }
1085
Joe Onorato9c1289c2009-08-17 11:03:03 -04001086 public boolean isWorkspaceLocked() {
1087 return mWorkspaceLoading || mWaitingForResult;
1088 }
1089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001091 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 showAddDialog(mMenuAddInfo);
1093 }
1094
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095 void addAppWidget(Intent data) {
1096 // TODO: catch bad widget exception when sent
1097 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001099 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1100 if (SEARCH_WIDGET.equals(customWidget)) {
1101 // We don't need this any more, since this isn't a real app widget.
1102 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1103 // add the search widget
1104 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001106 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1107
1108 if (appWidget.configure != null) {
1109 // Launch over to configure widget, if needed
1110 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1111 intent.setComponent(appWidget.configure);
1112 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1113
1114 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1115 } else {
1116 // Otherwise just add it
1117 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1118 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 }
1120 }
Romain Guycbb89e42009-06-08 15:52:54 -07001121
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 void addSearch() {
1123 final Widget info = Widget.makeSearch();
1124 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 final int[] xy = mCellCoordinates;
1127 final int spanX = info.spanX;
1128 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1133 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 final View view = mInflater.inflate(info.layoutResource, null);
1136 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001137 Search search = (Search) view.findViewById(R.id.widget_search);
1138 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1141 }
1142
Romain Guy73b979d2009-06-09 12:57:21 -07001143 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001144 // Handle case where user selected "Applications"
1145 String applicationName = getResources().getString(R.string.group_applications);
1146 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001147
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001148 if (applicationName != null && applicationName.equals(shortcutName)) {
1149 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1150 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001152 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1153 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001154 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001155 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001156 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 }
1159
1160 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001161 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001162 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001163 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001164
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001165 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001166 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001167 } else {
1168 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 }
1171
Joe Onorato9c1289c2009-08-17 11:03:03 -04001172 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 UserFolderInfo folderInfo = new UserFolderInfo();
1174 folderInfo.title = getText(R.string.folder_name);
1175
1176 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1177 cellInfo.screen = mWorkspace.getCurrentScreen();
1178 if (!findSingleSlot(cellInfo)) return;
1179
1180 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001181 LauncherModel.addItemToDatabase(this, folderInfo,
1182 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001184 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185
1186 // Create the view
1187 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1188 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1189 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001190 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 }
Romain Guycbb89e42009-06-08 15:52:54 -07001192
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 void removeFolder(FolderInfo folder) {
1194 mFolders.remove(folder.id);
1195 }
1196
1197 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 cellInfo.screen = mWorkspace.getCurrentScreen();
1199 if (!findSingleSlot(cellInfo)) return;
1200
1201 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1202
1203 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1205 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1207 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 }
1209 }
1210
1211 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1212 CellLayout.CellInfo cellInfo, boolean notify) {
1213
1214 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1215 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1216
1217 Drawable icon = null;
1218 boolean filtered = false;
1219 Intent.ShortcutIconResource iconResource = null;
1220
1221 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1222 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1223 try {
1224 iconResource = (Intent.ShortcutIconResource) extra;
1225 final PackageManager packageManager = context.getPackageManager();
1226 Resources resources = packageManager.getResourcesForApplication(
1227 iconResource.packageName);
1228 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1229 icon = resources.getDrawable(id);
1230 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001231 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 }
1233 }
1234
1235 if (icon == null) {
1236 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1237 }
1238
1239 final LiveFolderInfo info = new LiveFolderInfo();
1240 info.icon = icon;
1241 info.filtered = filtered;
1242 info.title = name;
1243 info.iconResource = iconResource;
1244 info.uri = data.getData();
1245 info.baseIntent = baseIntent;
1246 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1247 LiveFolders.DISPLAY_MODE_GRID);
1248
1249 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1250 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001251 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252
1253 return info;
1254 }
1255
1256 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1257 final int[] xy = new int[2];
1258 if (findSlot(cellInfo, xy, 1, 1)) {
1259 cellInfo.cellX = xy[0];
1260 cellInfo.cellY = xy[1];
1261 return true;
1262 }
1263 return false;
1264 }
1265
1266 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1267 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1268 boolean[] occupied = mSavedState != null ?
1269 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1270 cellInfo = mWorkspace.findAllVacantCells(occupied);
1271 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1272 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1273 return false;
1274 }
1275 }
1276 return true;
1277 }
1278
1279 private void showNotifications() {
1280 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1281 if (statusBar != null) {
1282 statusBar.expand();
1283 }
1284 }
1285
1286 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001287 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001289 Intent chooser = Intent.createChooser(pickWallpaper,
1290 getText(R.string.chooser_wallpaper));
1291 WallpaperManager wm = (WallpaperManager)
1292 getSystemService(Context.WALLPAPER_SERVICE);
1293 WallpaperInfo wi = wm.getWallpaperInfo();
1294 if (wi != null && wi.getSettingsActivity() != null) {
1295 LabeledIntent li = new LabeledIntent(getPackageName(),
1296 R.string.configure_wallpaper, 0);
1297 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1298 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1299 }
Mike Clerona0618e42009-10-22 13:55:21 -07001300 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001303 /**
1304 * Registers various content observers. The current implementation registers
1305 * only a favorites observer to keep track of the favorites applications.
1306 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001307 private void registerContentObservers() {
1308 ContentResolver resolver = getContentResolver();
1309 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1310 true, mWidgetObserver);
1311 }
1312
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 @Override
1314 public boolean dispatchKeyEvent(KeyEvent event) {
1315 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1316 switch (event.getKeyCode()) {
1317 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 return true;
1319 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001320 return true;
1321 }
1322 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1323 switch (event.getKeyCode()) {
1324 case KeyEvent.KEYCODE_BACK:
1325 if (!event.isCanceled()) {
1326 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001327 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001328 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001329 } else {
1330 closeFolder();
1331 }
Romain Guya6abce82009-11-10 02:54:41 -08001332 dismissPreview(mPreviousView);
1333 dismissPreview(mNextView);
Dianne Hackborn67800862009-07-24 17:15:20 -07001334 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001335 return true;
1336 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 return true;
1338 }
1339 }
1340
1341 return super.dispatchKeyEvent(event);
1342 }
1343
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 private void closeFolder() {
1345 Folder folder = mWorkspace.getOpenFolder();
1346 if (folder != null) {
1347 closeFolder(folder);
1348 }
1349 }
1350
1351 void closeFolder(Folder folder) {
1352 folder.getInfo().opened = false;
1353 ViewGroup parent = (ViewGroup) folder.getParent();
1354 if (parent != null) {
1355 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001356 if (folder instanceof DropTarget) {
1357 // Live folders aren't DropTargets.
1358 mDragController.removeDropTarget((DropTarget)folder);
1359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361 folder.onClose();
1362 }
1363
1364 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001365 * Re-listen when widgets are reset.
1366 */
1367 private void onAppWidgetReset() {
1368 mAppWidgetHost.startListening();
1369 }
1370
1371 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1373 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 private void unbindDesktopItems() {
1376 for (ItemInfo item: mDesktopItems) {
1377 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
1379 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 /**
1382 * Launches the intent referred by the clicked shortcut.
1383 *
1384 * @param v The view representing the clicked shortcut.
1385 */
1386 public void onClick(View v) {
1387 Object tag = v.getTag();
1388 if (tag instanceof ApplicationInfo) {
1389 // Open shortcut
1390 final Intent intent = ((ApplicationInfo) tag).intent;
1391 startActivitySafely(intent);
1392 } else if (tag instanceof FolderInfo) {
1393 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001394 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001395 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001396 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001397 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001398 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 }
1401 }
1402
1403 void startActivitySafely(Intent intent) {
1404 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1405 try {
1406 startActivity(intent);
1407 } catch (ActivityNotFoundException e) {
1408 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1409 } catch (SecurityException e) {
1410 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001411 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1413 "or use the exported attribute for this activity.", e);
1414 }
1415 }
1416
1417 private void handleFolderClick(FolderInfo folderInfo) {
1418 if (!folderInfo.opened) {
1419 // Close any open folder
1420 closeFolder();
1421 // Open the requested folder
1422 openFolder(folderInfo);
1423 } else {
1424 // Find the open folder...
1425 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1426 int folderScreen;
1427 if (openFolder != null) {
1428 folderScreen = mWorkspace.getScreenForView(openFolder);
1429 // .. and close it
1430 closeFolder(openFolder);
1431 if (folderScreen != mWorkspace.getCurrentScreen()) {
1432 // Close any folder open on the current screen
1433 closeFolder();
1434 // Pull the folder onto this screen
1435 openFolder(folderInfo);
1436 }
1437 }
1438 }
1439 }
1440
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 /**
1442 * Opens the user fodler described by the specified tag. The opening of the folder
1443 * is animated relative to the specified View. If the View is null, no animation
1444 * is played.
1445 *
1446 * @param folderInfo The FolderInfo describing the folder to open.
1447 */
1448 private void openFolder(FolderInfo folderInfo) {
1449 Folder openFolder;
1450
1451 if (folderInfo instanceof UserFolderInfo) {
1452 openFolder = UserFolder.fromXml(this);
1453 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001454 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 } else {
1456 return;
1457 }
1458
Joe Onorato00acb122009-08-04 16:04:30 -04001459 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 openFolder.setLauncher(this);
1461
1462 openFolder.bind(folderInfo);
1463 folderInfo.opened = true;
1464
1465 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1466 openFolder.onOpen();
1467 }
1468
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001470 switch (v.getId()) {
1471 case R.id.previous_screen:
1472 showPreviousPreview(v);
1473 return true;
1474 case R.id.next_screen:
1475 showNextPreview(v);
1476 return true;
1477 }
1478
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 return false;
1481 }
1482
1483 if (!(v instanceof CellLayout)) {
1484 v = (View) v.getParent();
1485 }
1486
1487 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1488
1489 // This happens when long clicking an item with the dpad/trackball
1490 if (cellInfo == null) {
1491 return true;
1492 }
1493
1494 if (mWorkspace.allowLongPress()) {
1495 if (cellInfo.cell == null) {
1496 if (cellInfo.valid) {
1497 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001498 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 showAddDialog(cellInfo);
1500 }
1501 } else {
1502 if (!(cellInfo.cell instanceof Folder)) {
1503 // User long pressed on an item
1504 mWorkspace.startDrag(cellInfo);
1505 }
1506 }
1507 }
1508 return true;
1509 }
1510
Romain Guye6b8e2f2009-11-10 11:56:55 -08001511 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001512 private void dismissPreview(final View v) {
1513 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001514 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001515 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1516 public void onDismiss() {
1517 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1518 int count = group.getChildCount();
1519 for (int i = 0; i < count; i++) {
1520 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1521 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001522 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1523 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1524
1525 v.setTag(R.id.workspace, null);
1526 v.setTag(R.id.icon, null);
1527 window.setOnDismissListener(null);
1528 }
1529 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001530 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001531 }
1532 v.setTag(null);
1533 }
1534
1535 private void showPreviousPreview(View anchor) {
1536 int current = mWorkspace.getCurrentScreen();
1537 if (current <= 0) return;
1538
Romain Guy9d31e9f2009-11-11 18:54:28 -08001539 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001540 }
1541
1542 private void showNextPreview(View anchor) {
1543 int current = mWorkspace.getCurrentScreen();
1544 if (current >= mWorkspace.getChildCount() - 1) return;
1545
Romain Guy9d31e9f2009-11-11 18:54:28 -08001546 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guya6abce82009-11-10 02:54:41 -08001547 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001548
Romain Guya6abce82009-11-10 02:54:41 -08001549 private void showPreviews(final View anchor, int start, int end) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001550 Resources resources = getResources();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001551
Romain Guya6abce82009-11-10 02:54:41 -08001552 Workspace workspace = mWorkspace;
1553 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001554
1555 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001556
1557 Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001558 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001559 int extraW = (int) ((r.left + r.right) * max);
1560 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001561
1562 int aW = cell.getWidth() - extraW;
1563 float w = aW / max;
1564
1565 int width = cell.getWidth();
1566 int height = cell.getHeight();
1567 int x = cell.getLeftPadding();
1568 int y = cell.getTopPadding();
1569 width -= (x + cell.getRightPadding());
1570 height -= (y + cell.getBottomPadding());
1571
1572 float scale = w / width;
1573
1574 int count = end - start;
1575
1576 final float sWidth = width * scale;
1577 float sHeight = height * scale;
1578
Romain Guye6b8e2f2009-11-10 11:56:55 -08001579 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001580
Romain Guye6b8e2f2009-11-10 11:56:55 -08001581 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1582 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001583
1584 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001585 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001586 cell = (CellLayout) workspace.getChildAt(i);
1587
Romain Guye6b8e2f2009-11-10 11:56:55 -08001588 Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
1589 Bitmap.Config.ARGB_8888);
1590
1591 Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001592 c.scale(scale, scale);
1593 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1594 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001595
Romain Guy9d31e9f2009-11-11 18:54:28 -08001596 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001597 image.setImageBitmap(bitmap);
1598 image.setTag(i);
1599 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001600 image.setOnFocusChangeListener(handler);
1601 image.setFocusable(true);
1602 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001603
1604 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001605 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1606
1607 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001608 }
Romain Guye6b8e2f2009-11-10 11:56:55 -08001609
1610 PopupWindow p = new PopupWindow(this);
1611 p.setContentView(preview);
1612 p.setWidth((int) (sWidth * count + extraW));
1613 p.setHeight((int) (sHeight + extraH));
1614 p.setAnimationStyle(R.style.AnimationPreview);
1615 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001616 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001617 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001618 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001619
Romain Guye6b8e2f2009-11-10 11:56:55 -08001620 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1621 public void onDismiss() {
1622 dismissPreview(anchor);
1623 }
1624 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001625
1626 anchor.setTag(p);
1627 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001628 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001629 }
1630
Romain Guy9d31e9f2009-11-11 18:54:28 -08001631 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001632 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001633
Romain Guye6b8e2f2009-11-10 11:56:55 -08001634 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001635 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001636 }
1637
1638 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001639 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001640 v.post(this);
1641 }
1642
1643 public void run() {
1644 dismissPreview(mAnchor);
1645 }
1646
1647 public void onFocusChange(View v, boolean hasFocus) {
1648 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001649 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001650 }
Romain Guya6abce82009-11-10 02:54:41 -08001651 }
1652 }
1653
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001654 View getDrawerHandle() {
1655 return mHandleView;
1656 }
1657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 Workspace getWorkspace() {
1659 return mWorkspace;
1660 }
1661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 @Override
1663 protected Dialog onCreateDialog(int id) {
1664 switch (id) {
1665 case DIALOG_CREATE_SHORTCUT:
1666 return new CreateShortcut().createDialog();
1667 case DIALOG_RENAME_FOLDER:
1668 return new RenameFolder().createDialog();
1669 }
1670
1671 return super.onCreateDialog(id);
1672 }
1673
1674 @Override
1675 protected void onPrepareDialog(int id, Dialog dialog) {
1676 switch (id) {
1677 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 break;
1679 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001680 if (mFolderInfo != null) {
1681 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1682 final CharSequence text = mFolderInfo.title;
1683 input.setText(text);
1684 input.setSelection(0, text.length());
1685 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 break;
1687 }
1688 }
1689
1690 void showRenameDialog(FolderInfo info) {
1691 mFolderInfo = info;
1692 mWaitingForResult = true;
1693 showDialog(DIALOG_RENAME_FOLDER);
1694 }
1695
1696 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1697 mAddItemCellInfo = cellInfo;
1698 mWaitingForResult = true;
1699 showDialog(DIALOG_CREATE_SHORTCUT);
1700 }
1701
Romain Guy73b979d2009-06-09 12:57:21 -07001702 private void pickShortcut(int requestCode, int title) {
1703 Bundle bundle = new Bundle();
1704
1705 ArrayList<String> shortcutNames = new ArrayList<String>();
1706 shortcutNames.add(getString(R.string.group_applications));
1707 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1708
1709 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1710 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1711 R.drawable.ic_launcher_application));
1712 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1713
1714 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1715 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1716 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1717 pickIntent.putExtras(bundle);
1718
1719 startActivityForResult(pickIntent, requestCode);
1720 }
1721
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 private class RenameFolder {
1723 private EditText mInput;
1724
1725 Dialog createDialog() {
1726 mWaitingForResult = true;
1727 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1728 mInput = (EditText) layout.findViewById(R.id.folder_name);
1729
1730 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1731 builder.setIcon(0);
1732 builder.setTitle(getString(R.string.rename_folder_title));
1733 builder.setCancelable(true);
1734 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1735 public void onCancel(DialogInterface dialog) {
1736 cleanup();
1737 }
1738 });
1739 builder.setNegativeButton(getString(R.string.cancel_action),
1740 new Dialog.OnClickListener() {
1741 public void onClick(DialogInterface dialog, int which) {
1742 cleanup();
1743 }
1744 }
1745 );
1746 builder.setPositiveButton(getString(R.string.rename_action),
1747 new Dialog.OnClickListener() {
1748 public void onClick(DialogInterface dialog, int which) {
1749 changeFolderName();
1750 }
1751 }
1752 );
1753 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001754
1755 final AlertDialog dialog = builder.create();
1756 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1757 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001758 mInput.requestFocus();
1759 InputMethodManager inputManager = (InputMethodManager)
1760 getSystemService(Context.INPUT_METHOD_SERVICE);
1761 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001762 }
1763 });
1764
1765 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 }
1767
1768 private void changeFolderName() {
1769 final String name = mInput.getText().toString();
1770 if (!TextUtils.isEmpty(name)) {
1771 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001772 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 mFolderInfo.title = name;
1774 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1775
Joe Onorato9c1289c2009-08-17 11:03:03 -04001776 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001777 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001778 mModel.setWorkspaceDirty();
1779 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 } else {
1781 final FolderIcon folderIcon = (FolderIcon)
1782 mWorkspace.getViewForTag(mFolderInfo);
1783 if (folderIcon != null) {
1784 folderIcon.setText(name);
1785 getWorkspace().requestLayout();
1786 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001787 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001788 mModel.setWorkspaceDirty();
1789 mWorkspaceLoading = true;
1790 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 }
1792 }
1793 }
1794 cleanup();
1795 }
1796
1797 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 dismissDialog(DIALOG_RENAME_FOLDER);
1799 mWaitingForResult = false;
1800 mFolderInfo = null;
1801 }
1802 }
1803
Joe Onoratofb0ca672009-09-14 17:55:46 -04001804 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001805 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001806 }
1807
Joe Onorato3a8820b2009-11-10 15:06:42 -08001808 boolean isAllAppsOpaque() {
1809 return mAllAppsGrid.isOpaque();
1810 }
1811
1812 void showAllApps(boolean animated) {
1813 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato7bb17492009-09-24 17:51:01 -07001814 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001815
Joe Onorato3a8820b2009-11-10 15:06:42 -08001816 mWorkspace.startFading(false);
1817
Mike Cleronb6082fa2009-10-19 17:03:36 -07001818 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001819 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001820
Joe Onorato7c312c12009-08-13 21:36:53 -07001821 // TODO: fade these two too
1822 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001823 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001824 }
1825
Joe Onorato7bb17492009-09-24 17:51:01 -07001826 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001827 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001828 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001829 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001830 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001831 mWorkspace.startFading(true);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001832
Joe Onorato7c312c12009-08-13 21:36:53 -07001833 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001834 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001835 mDeleteZone.setVisibility(View.VISIBLE);
1836 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001837 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001838 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001839 }
1840
Joe Onorato7c312c12009-08-13 21:36:53 -07001841 void lockAllApps() {
1842 // TODO
1843 }
1844
1845 void unlockAllApps() {
1846 // TODO
1847 }
1848
Joe Onorato7404ee42009-07-31 11:54:44 -07001849 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 * Displays the shortcut creation dialog and launches, if necessary, the
1851 * appropriate activity.
1852 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001853 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001854 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1855 DialogInterface.OnShowListener {
1856
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001858
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 Dialog createDialog() {
1860 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001863
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1865 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001866 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001867
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 builder.setInverseBackgroundForced(true);
1869
1870 AlertDialog dialog = builder.create();
1871 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001872 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001873 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 return dialog;
1876 }
1877
1878 public void onCancel(DialogInterface dialog) {
1879 mWaitingForResult = false;
1880 cleanup();
1881 }
1882
Romain Guycbb89e42009-06-08 15:52:54 -07001883 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001884 }
1885
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 dismissDialog(DIALOG_CREATE_SHORTCUT);
1888 }
1889
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001890 /**
1891 * Handle the action clicked in the "Add to home" dialog.
1892 */
1893 public void onClick(DialogInterface dialog, int which) {
1894 Resources res = getResources();
1895 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001896
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001897 switch (which) {
1898 case AddAdapter.ITEM_SHORTCUT: {
1899 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001900 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001901 break;
1902 }
Romain Guycbb89e42009-06-08 15:52:54 -07001903
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001904 case AddAdapter.ITEM_APPWIDGET: {
1905 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001906
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001907 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1908 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1909 // add the search widget
1910 ArrayList<AppWidgetProviderInfo> customInfo =
1911 new ArrayList<AppWidgetProviderInfo>();
1912 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1913 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1914 info.label = getString(R.string.group_search);
1915 info.icon = R.drawable.ic_search_widget;
1916 customInfo.add(info);
1917 pickIntent.putParcelableArrayListExtra(
1918 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1919 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1920 Bundle b = new Bundle();
1921 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1922 customExtras.add(b);
1923 pickIntent.putParcelableArrayListExtra(
1924 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1925 // start the pick activity
1926 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1927 break;
1928 }
Romain Guycbb89e42009-06-08 15:52:54 -07001929
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001930 case AddAdapter.ITEM_LIVE_FOLDER: {
1931 // Insert extra item to handle inserting folder
1932 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001933
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001934 ArrayList<String> shortcutNames = new ArrayList<String>();
1935 shortcutNames.add(res.getString(R.string.group_folder));
1936 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001937
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001938 ArrayList<ShortcutIconResource> shortcutIcons =
1939 new ArrayList<ShortcutIconResource>();
1940 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1941 R.drawable.ic_launcher_folder));
1942 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1943
1944 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1945 pickIntent.putExtra(Intent.EXTRA_INTENT,
1946 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1947 pickIntent.putExtra(Intent.EXTRA_TITLE,
1948 getText(R.string.title_select_live_folder));
1949 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001950
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001951 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1952 break;
1953 }
1954
1955 case AddAdapter.ITEM_WALLPAPER: {
1956 startWallpaper();
1957 break;
1958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
1960 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001961
1962 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
1965
1966 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001967 * Receives notifications when applications are added/removed.
1968 */
1969 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1970 @Override
1971 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001972 closeSystemDialogs();
1973 }
1974 }
1975
1976 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001977 * Receives notifications whenever the appwidgets are reset.
1978 */
1979 private class AppWidgetResetObserver extends ContentObserver {
1980 public AppWidgetResetObserver() {
1981 super(new Handler());
1982 }
1983
1984 @Override
1985 public void onChange(boolean selfChange) {
1986 onAppWidgetReset();
1987 }
1988 }
1989
1990 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001991 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001993 public int getCurrentWorkspaceScreen() {
1994 return mWorkspace.getCurrentScreen();
1995 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001996
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997 /**
1998 * Refreshes the shortcuts shown on the workspace.
1999 *
2000 * Implementation of the method from LauncherModel.Callbacks.
2001 */
2002 public void startBinding() {
2003 final Workspace workspace = mWorkspace;
2004 int count = workspace.getChildCount();
2005 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002006 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002007 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2008 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002009
Joe Onorato9c1289c2009-08-17 11:03:03 -04002010 if (DEBUG_USER_INTERFACE) {
2011 android.widget.Button finishButton = new android.widget.Button(this);
2012 finishButton.setText("Finish");
2013 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2014
2015 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2016 public void onClick(View v) {
2017 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002018 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002019 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
2021 }
2022
2023 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002024 * Bind the items start-end from the list.
2025 *
2026 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002028 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2029
2030 final Workspace workspace = mWorkspace;
2031
2032 for (int i=start; i<end; i++) {
2033 final ItemInfo item = shortcuts.get(i);
2034 mDesktopItems.add(item);
2035 switch (item.itemType) {
2036 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2037 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2038 final View shortcut = createShortcut((ApplicationInfo) item);
2039 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2040 false);
2041 break;
2042 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2043 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2044 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2045 (UserFolderInfo) item);
2046 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2047 false);
2048 break;
2049 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2050 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2051 R.layout.live_folder_icon, this,
2052 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2053 (LiveFolderInfo) item);
2054 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2055 false);
2056 break;
2057 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2058 final int screen = workspace.getCurrentScreen();
2059 final View view = mInflater.inflate(R.layout.widget_search,
2060 (ViewGroup) workspace.getChildAt(screen), false);
2061
2062 Search search = (Search) view.findViewById(R.id.widget_search);
2063 search.setLauncher(this);
2064
2065 final Widget widget = (Widget) item;
2066 view.setTag(widget);
2067
2068 workspace.addWidget(view, widget, false);
2069 break;
2070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
2072
Joe Onorato9c1289c2009-08-17 11:03:03 -04002073 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
2075
Joe Onorato9c1289c2009-08-17 11:03:03 -04002076 /**
2077 * Implementation of the method from LauncherModel.Callbacks.
2078 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002079 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2080 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002081 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002083
2084 /**
2085 * Add the views for a widget to the workspace.
2086 *
2087 * Implementation of the method from LauncherModel.Callbacks.
2088 */
2089 public void bindAppWidget(LauncherAppWidgetInfo item) {
2090 final Workspace workspace = mWorkspace;
2091
2092 final int appWidgetId = item.appWidgetId;
2093 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2094 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2095
Joe Onorato9c1289c2009-08-17 11:03:03 -04002096 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2097 item.hostView.setTag(item);
2098
2099 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2100 item.cellY, item.spanX, item.spanY, false);
2101
2102 workspace.requestLayout();
2103
2104 mDesktopItems.add(item);
2105 }
2106
2107 /**
2108 * Callback saying that there aren't any more items to bind.
2109 *
2110 * Implementation of the method from LauncherModel.Callbacks.
2111 */
2112 public void finishBindingItems() {
2113 if (mSavedState != null) {
2114 if (!mWorkspace.hasFocus()) {
2115 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2116 }
2117
2118 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2119 if (userFolders != null) {
2120 for (long folderId : userFolders) {
2121 final FolderInfo info = mFolders.get(folderId);
2122 if (info != null) {
2123 openFolder(info);
2124 }
2125 }
2126 final Folder openFolder = mWorkspace.getOpenFolder();
2127 if (openFolder != null) {
2128 openFolder.requestFocus();
2129 }
2130 }
2131
Joe Onorato9c1289c2009-08-17 11:03:03 -04002132 mSavedState = null;
2133 }
2134
2135 if (mSavedInstanceState != null) {
2136 super.onRestoreInstanceState(mSavedInstanceState);
2137 mSavedInstanceState = null;
2138 }
2139
Joe Onorato9c1289c2009-08-17 11:03:03 -04002140 mWorkspaceLoading = false;
2141 }
2142
2143 /**
2144 * Add the icons for all apps.
2145 *
2146 * Implementation of the method from LauncherModel.Callbacks.
2147 */
2148 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002149 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002150 }
2151
2152 /**
2153 * A package was installed.
2154 *
2155 * Implementation of the method from LauncherModel.Callbacks.
2156 */
2157 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2158 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002159 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002160 }
2161
2162 /**
2163 * A package was updated.
2164 *
2165 * Implementation of the method from LauncherModel.Callbacks.
2166 */
2167 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2168 removeDialog(DIALOG_CREATE_SHORTCUT);
2169 mWorkspace.updateShortcutsForPackage(packageName);
2170 }
2171
2172 /**
2173 * A package was uninstalled.
2174 *
2175 * Implementation of the method from LauncherModel.Callbacks.
2176 */
2177 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2178 removeDialog(DIALOG_CREATE_SHORTCUT);
2179 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002180 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182}