blob: 0150af240d623beece6cf6553d3ecf23e470c2e9 [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);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700440 }
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700442 @Override
443 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400444 // Flag the loader to stop early before switching
445 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700446
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700447 if (PROFILE_ROTATE) {
448 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
449 }
450 return null;
451 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 private boolean acceptFilter() {
454 final InputMethodManager inputManager = (InputMethodManager)
455 getSystemService(Context.INPUT_METHOD_SERVICE);
456 return !inputManager.isFullscreenMode();
457 }
458
459 @Override
460 public boolean onKeyDown(int keyCode, KeyEvent event) {
461 boolean handled = super.onKeyDown(keyCode, event);
462 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
463 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
464 keyCode, event);
465 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700466 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700467 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700468 // showSearchDialog()
469 // If there are multiple keystrokes before the search dialog takes focus,
470 // onSearchRequested() will be called for every keystroke,
471 // but it is idempotent, so it's fine.
472 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 }
474 }
475
476 return handled;
477 }
478
Karl Rosaen138a0412009-04-23 19:00:21 -0700479 private String getTypedText() {
480 return mDefaultKeySsb.toString();
481 }
482
483 private void clearTypedText() {
484 mDefaultKeySsb.clear();
485 mDefaultKeySsb.clearSpans();
486 Selection.setSelection(mDefaultKeySsb, 0);
487 }
488
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 /**
490 * Restores the previous state, if it exists.
491 *
492 * @param savedState The previous state.
493 */
494 private void restoreState(Bundle savedState) {
495 if (savedState == null) {
496 return;
497 }
498
Joe Onorato3a8820b2009-11-10 15:06:42 -0800499 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
500 if (allApps) {
501 showAllApps(false);
502 }
503
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
505 if (currentScreen > -1) {
506 mWorkspace.setCurrentScreen(currentScreen);
507 }
508
509 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
510 if (addScreen > -1) {
511 mAddItemCellInfo = new CellLayout.CellInfo();
512 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
513 addItemCellInfo.valid = true;
514 addItemCellInfo.screen = addScreen;
515 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
516 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
517 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
518 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
519 addItemCellInfo.findVacantCellsFromOccupied(
520 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
521 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
522 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
523 mRestoring = true;
524 }
525
526 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
527 if (renameFolder) {
528 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400529 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 mRestoring = true;
531 }
532 }
533
534 /**
535 * Finds all the views we need and configure them properly.
536 */
537 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400538 DragController dragController = mDragController;
539
Romain Guyb1b69f52009-08-10 15:10:15 -0700540 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400541 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542
Joe Onorato7c312c12009-08-13 21:36:53 -0700543 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700544 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700545 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700546 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700547 // Manage focusability manually since this thing is always visible
548 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700549
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
551 final Workspace workspace = mWorkspace;
552
Joe Onorato7c312c12009-08-13 21:36:53 -0700553 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
554 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555
Joe Onorato7c312c12009-08-13 21:36:53 -0700556 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700557 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700558 mHandleView.setOnClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800559
560 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
561 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
562
563 Drawable previous = mPreviousView.getDrawable();
564 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800565 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566
Romain Guy1fbc1c82009-11-09 20:43:08 -0800567 mPreviousView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800568 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800569
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400571 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573
574 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400575 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700576 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577
Joe Onorato00acb122009-08-04 16:04:30 -0400578 dragController.setDragScoller(workspace);
579 dragController.setDragListener(deleteZone);
580 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700581
Joe Onorato00acb122009-08-04 16:04:30 -0400582 // The order here is bottom to top.
583 dragController.addDropTarget(workspace);
584 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 }
586
Romain Guy8a73c512009-11-09 19:19:59 -0800587 @SuppressWarnings({"UnusedDeclaration"})
588 public void previousScreen(View v) {
589 mWorkspace.scrollLeft();
590 }
591
592 @SuppressWarnings({"UnusedDeclaration"})
593 public void nextScreen(View v) {
594 mWorkspace.scrollRight();
595 }
596
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 /**
598 * Creates a view representing a shortcut.
599 *
600 * @param info The data structure describing the shortcut.
601 *
602 * @return A View inflated from R.layout.application.
603 */
604 View createShortcut(ApplicationInfo info) {
605 return createShortcut(R.layout.application,
606 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
607 }
608
609 /**
610 * Creates a view representing a shortcut inflated from the specified resource.
611 *
612 * @param layoutResId The id of the XML layout used to create the shortcut.
613 * @param parent The group the shortcut belongs to.
614 * @param info The data structure describing the shortcut.
615 *
616 * @return A View inflated from layoutResId.
617 */
618 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
619 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
620
Joe Onorato5162ea92009-09-03 09:39:42 -0700621 if (info.icon == null) {
622 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
623 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700625 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 info.filtered = true;
627 }
628
629 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
630 favorite.setText(info.title);
631 favorite.setTag(info);
632 favorite.setOnClickListener(this);
633
634 return favorite;
635 }
636
637 /**
638 * Add an application shortcut to the workspace.
639 *
640 * @param data The intent describing the application.
641 * @param cellInfo The position on screen where to create the shortcut.
642 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 cellInfo.screen = mWorkspace.getCurrentScreen();
645 if (!findSingleSlot(cellInfo)) return;
646
Romain Guy73b979d2009-06-09 12:57:21 -0700647 final ApplicationInfo info = infoFromApplicationIntent(context, data);
648 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700650 }
651 }
652
653 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 ComponentName component = data.getComponent();
655 PackageManager packageManager = context.getPackageManager();
656 ActivityInfo activityInfo = null;
657 try {
658 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
659 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800660 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
Romain Guycbb89e42009-06-08 15:52:54 -0700662
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 if (activityInfo != null) {
664 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700665
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 itemInfo.title = activityInfo.loadLabel(packageManager);
667 if (itemInfo.title == null) {
668 itemInfo.title = activityInfo.name;
669 }
Romain Guycbb89e42009-06-08 15:52:54 -0700670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
672 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
673 itemInfo.icon = activityInfo.loadIcon(packageManager);
674 itemInfo.container = ItemInfo.NO_ID;
675
Romain Guy73b979d2009-06-09 12:57:21 -0700676 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
Romain Guy73b979d2009-06-09 12:57:21 -0700678
679 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
Romain Guycbb89e42009-06-08 15:52:54 -0700681
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 /**
683 * Add a shortcut to the workspace.
684 *
685 * @param data The intent describing the shortcut.
686 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400688 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 cellInfo.screen = mWorkspace.getCurrentScreen();
690 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700691
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
693
694 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
697 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 }
699 }
700
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700703 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700705 * @param data The intent describing the appWidgetId.
706 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700710 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700711
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800712 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700714 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700715
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 // Try finding open space on Launcher screen
721 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800722 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
723 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
724 return;
725 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700727 // Build Launcher-specific widget info and save to database
728 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 launcherInfo.spanX = spans[0];
730 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700731
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 LauncherModel.addItemToDatabase(this, launcherInfo,
733 LauncherSettings.Favorites.CONTAINER_DESKTOP,
734 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
735
736 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700738
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700740 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700741
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700742 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700744
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400746 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 }
748 }
Romain Guycbb89e42009-06-08 15:52:54 -0700749
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
751 mDesktopItems.remove(launcherInfo);
752 launcherInfo.hostView = null;
753 }
754
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700755 public LauncherAppWidgetHost getAppWidgetHost() {
756 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 }
Romain Guycbb89e42009-06-08 15:52:54 -0700758
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 static ApplicationInfo addShortcut(Context context, Intent data,
760 CellLayout.CellInfo cellInfo, boolean notify) {
761
Romain Guy73b979d2009-06-09 12:57:21 -0700762 final ApplicationInfo info = infoFromShortcutIntent(context, data);
763 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
764 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
765
766 return info;
767 }
768
769 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
771 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
772 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
773
774 Drawable icon = null;
775 boolean filtered = false;
776 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700777 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778
779 if (bitmap != null) {
780 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
781 filtered = true;
782 customIcon = true;
783 } else {
784 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700785 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700787 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 final PackageManager packageManager = context.getPackageManager();
789 Resources resources = packageManager.getResourcesForApplication(
790 iconResource.packageName);
791 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
792 icon = resources.getDrawable(id);
793 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800794 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 }
796 }
797 }
798
799 if (icon == null) {
800 icon = context.getPackageManager().getDefaultActivityIcon();
801 }
802
803 final ApplicationInfo info = new ApplicationInfo();
804 info.icon = icon;
805 info.filtered = filtered;
806 info.title = name;
807 info.intent = intent;
808 info.customIcon = customIcon;
809 info.iconResource = iconResource;
810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 return info;
812 }
813
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800814 void closeSystemDialogs() {
815 closeAllApps(false);
816 getWindow().closeAllPanels();
817
818 try {
819 dismissDialog(DIALOG_CREATE_SHORTCUT);
820 // Unlock the workspace if the dialog was showing
821 } catch (Exception e) {
822 // An exception is thrown if the dialog is not visible, which is fine
823 }
824
825 try {
826 dismissDialog(DIALOG_RENAME_FOLDER);
827 // Unlock the workspace if the dialog was showing
828 } catch (Exception e) {
829 // An exception is thrown if the dialog is not visible, which is fine
830 }
831 }
832
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 @Override
834 protected void onNewIntent(Intent intent) {
835 super.onNewIntent(intent);
836
837 // Close the menu
838 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800839 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700840
Joe Onorato742966b2009-10-07 10:26:17 -0700841 // Whatever we were doing is hereby canceled.
842 mWaitingForResult = false;
843
Mike LeBeau736cf282009-07-02 17:46:59 -0700844 // Set this flag so that onResume knows to close the search dialog if it's open,
845 // because this was a new intent (thus a press of 'home' or some such) rather than
846 // for example onResume being called when the user pressed the 'back' button.
847 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848
Joe Onorato3a8820b2009-11-10 15:06:42 -0800849 if (!mWorkspace.isDefaultScreenShowing()) {
850 mWorkspace.moveToDefaultScreen();
851 }
Romain Guy73b979d2009-06-09 12:57:21 -0700852
Joe Onorato3a8820b2009-11-10 15:06:42 -0800853 closeAllApps(false);
Romain Guy1dd3a072009-07-16 13:21:01 -0700854
Joe Onorato3a8820b2009-11-10 15:06:42 -0800855 final View v = getWindow().peekDecorView();
856 if (v != null && v.getWindowToken() != null) {
857 InputMethodManager imm = (InputMethodManager)getSystemService(
858 INPUT_METHOD_SERVICE);
859 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 }
861 }
862 }
863
864 @Override
865 protected void onRestoreInstanceState(Bundle savedInstanceState) {
866 // Do not call super here
867 mSavedInstanceState = savedInstanceState;
868 }
869
870 @Override
871 protected void onSaveInstanceState(Bundle outState) {
872 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
873
874 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
875 if (folders.size() > 0) {
876 final int count = folders.size();
877 long[] ids = new long[count];
878 for (int i = 0; i < count; i++) {
879 final FolderInfo info = folders.get(i).getInfo();
880 ids[i] = info.id;
881 }
882 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
883 } else {
884 super.onSaveInstanceState(outState);
885 }
886
Joe Onoratofb0ca672009-09-14 17:55:46 -0400887 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800888 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891
892 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
893 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
894 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
895
896 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
897 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
898 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
899 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
900 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
901 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
902 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
903 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
904 layout.getOccupiedCells());
905 }
906
907 if (mFolderInfo != null && mWaitingForResult) {
908 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
909 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
910 }
911 }
912
913 @Override
914 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700916
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700918 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800920 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 }
922
923 TextKeyListener.getInstance().release();
924
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 unbindDesktopItems();
928 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800929
930 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800931
932 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800933 dismissPreview(mNextView);
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800934
935 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
937
938 @Override
939 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700940 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 super.startActivityForResult(intent, requestCode);
942 }
943
944 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700945 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700947
Joe Onorato7bb17492009-09-24 17:51:01 -0700948 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700949
Karl Rosaen138a0412009-04-23 19:00:21 -0700950 // Slide the search widget to the top, if it's on the current screen,
951 // otherwise show the search dialog immediately.
952 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
953 if (searchWidget == null) {
954 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
955 } else {
956 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
957 // show the currently typed text in the search widget while sliding
958 searchWidget.setQuery(getTypedText());
959 }
960 }
Romain Guycbb89e42009-06-08 15:52:54 -0700961
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 /**
963 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700964 *
965 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700966 */
Romain Guycbb89e42009-06-08 15:52:54 -0700967 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700968 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700969
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 if (initialQuery == null) {
971 // Use any text typed in the launcher as the initial query
972 initialQuery = getTypedText();
973 clearTypedText();
974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 if (appSearchData == null) {
976 appSearchData = new Bundle();
977 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
978 }
Romain Guycbb89e42009-06-08 15:52:54 -0700979
Karl Rosaen138a0412009-04-23 19:00:21 -0700980 final SearchManager searchManager =
981 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
982
983 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
984 if (searchWidget != null) {
985 // This gets called when the user leaves the search dialog to go back to
986 // the Launcher.
987 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
988 public void onCancel() {
989 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700990 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700991 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700992 });
993 }
Romain Guycbb89e42009-06-08 15:52:54 -0700994
Karl Rosaen138a0412009-04-23 19:00:21 -0700995 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700996 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 }
998
Karl Rosaen138a0412009-04-23 19:00:21 -0700999 /**
1000 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001001 */
Romain Guy5a941392009-04-28 15:18:25 -07001002 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001003 // Close search dialog
1004 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001005 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001006 // Restore search widget to its normal position
1007 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1008 if (searchWidget != null) {
1009 searchWidget.stopSearch(false);
1010 }
1011 }
Romain Guycbb89e42009-06-08 15:52:54 -07001012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 @Override
1014 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015 if (isWorkspaceLocked()) {
1016 return false;
1017 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018
1019 super.onCreateOptionsMenu(menu);
1020 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1021 .setIcon(android.R.drawable.ic_menu_add)
1022 .setAlphabeticShortcut('A');
1023 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1024 .setIcon(android.R.drawable.ic_menu_gallery)
1025 .setAlphabeticShortcut('W');
1026 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1027 .setIcon(android.R.drawable.ic_search_category_default)
1028 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1029 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1030 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1031 .setAlphabeticShortcut('N');
1032
1033 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001034 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1035 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036
1037 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1038 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1039 .setIntent(settings);
1040
1041 return true;
1042 }
1043
1044 @Override
1045 public boolean onPrepareOptionsMenu(Menu menu) {
1046 super.onPrepareOptionsMenu(menu);
1047
1048 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1049 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1050
1051 return true;
1052 }
1053
1054 @Override
1055 public boolean onOptionsItemSelected(MenuItem item) {
1056 switch (item.getItemId()) {
1057 case MENU_ADD:
1058 addItems();
1059 return true;
1060 case MENU_WALLPAPER_SETTINGS:
1061 startWallpaper();
1062 return true;
1063 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001064 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 return true;
1066 case MENU_NOTIFICATIONS:
1067 showNotifications();
1068 return true;
1069 }
1070
1071 return super.onOptionsItemSelected(item);
1072 }
Romain Guycbb89e42009-06-08 15:52:54 -07001073
Karl Rosaen138a0412009-04-23 19:00:21 -07001074 /**
1075 * Indicates that we want global search for this activity by setting the globalSearch
1076 * argument for {@link #startSearch} to true.
1077 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001079 @Override
1080 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001081 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001082 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001083 }
1084
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085 public boolean isWorkspaceLocked() {
1086 return mWorkspaceLoading || mWaitingForResult;
1087 }
1088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001090 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 showAddDialog(mMenuAddInfo);
1092 }
1093
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001094 void addAppWidget(Intent data) {
1095 // TODO: catch bad widget exception when sent
1096 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001098 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1099 if (SEARCH_WIDGET.equals(customWidget)) {
1100 // We don't need this any more, since this isn't a real app widget.
1101 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1102 // add the search widget
1103 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001105 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1106
1107 if (appWidget.configure != null) {
1108 // Launch over to configure widget, if needed
1109 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1110 intent.setComponent(appWidget.configure);
1111 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1112
1113 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1114 } else {
1115 // Otherwise just add it
1116 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
1119 }
Romain Guycbb89e42009-06-08 15:52:54 -07001120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 void addSearch() {
1122 final Widget info = Widget.makeSearch();
1123 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 final int[] xy = mCellCoordinates;
1126 final int spanX = info.spanX;
1127 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1132 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 final View view = mInflater.inflate(info.layoutResource, null);
1135 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001136 Search search = (Search) view.findViewById(R.id.widget_search);
1137 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1140 }
1141
Romain Guy73b979d2009-06-09 12:57:21 -07001142 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001143 // Handle case where user selected "Applications"
1144 String applicationName = getResources().getString(R.string.group_applications);
1145 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001146
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001147 if (applicationName != null && applicationName.equals(shortcutName)) {
1148 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1149 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001150
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001151 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1152 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001153 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001154 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001155 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 }
1158
1159 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001160 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001161 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001162 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001163
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001164 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001165 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001166 } else {
1167 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170
Joe Onorato9c1289c2009-08-17 11:03:03 -04001171 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 UserFolderInfo folderInfo = new UserFolderInfo();
1173 folderInfo.title = getText(R.string.folder_name);
1174
1175 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1176 cellInfo.screen = mWorkspace.getCurrentScreen();
1177 if (!findSingleSlot(cellInfo)) return;
1178
1179 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001180 LauncherModel.addItemToDatabase(this, folderInfo,
1181 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001183 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184
1185 // Create the view
1186 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1187 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1188 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001189 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 }
Romain Guycbb89e42009-06-08 15:52:54 -07001191
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 void removeFolder(FolderInfo folder) {
1193 mFolders.remove(folder.id);
1194 }
1195
1196 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 cellInfo.screen = mWorkspace.getCurrentScreen();
1198 if (!findSingleSlot(cellInfo)) return;
1199
1200 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1201
1202 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1204 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1206 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 }
1208 }
1209
1210 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1211 CellLayout.CellInfo cellInfo, boolean notify) {
1212
1213 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1214 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1215
1216 Drawable icon = null;
1217 boolean filtered = false;
1218 Intent.ShortcutIconResource iconResource = null;
1219
1220 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1221 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1222 try {
1223 iconResource = (Intent.ShortcutIconResource) extra;
1224 final PackageManager packageManager = context.getPackageManager();
1225 Resources resources = packageManager.getResourcesForApplication(
1226 iconResource.packageName);
1227 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1228 icon = resources.getDrawable(id);
1229 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001230 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 }
1232 }
1233
1234 if (icon == null) {
1235 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1236 }
1237
1238 final LiveFolderInfo info = new LiveFolderInfo();
1239 info.icon = icon;
1240 info.filtered = filtered;
1241 info.title = name;
1242 info.iconResource = iconResource;
1243 info.uri = data.getData();
1244 info.baseIntent = baseIntent;
1245 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1246 LiveFolders.DISPLAY_MODE_GRID);
1247
1248 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1249 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001250 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251
1252 return info;
1253 }
1254
1255 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1256 final int[] xy = new int[2];
1257 if (findSlot(cellInfo, xy, 1, 1)) {
1258 cellInfo.cellX = xy[0];
1259 cellInfo.cellY = xy[1];
1260 return true;
1261 }
1262 return false;
1263 }
1264
1265 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1266 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1267 boolean[] occupied = mSavedState != null ?
1268 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1269 cellInfo = mWorkspace.findAllVacantCells(occupied);
1270 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1271 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1272 return false;
1273 }
1274 }
1275 return true;
1276 }
1277
1278 private void showNotifications() {
1279 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1280 if (statusBar != null) {
1281 statusBar.expand();
1282 }
1283 }
1284
1285 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001286 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001288 Intent chooser = Intent.createChooser(pickWallpaper,
1289 getText(R.string.chooser_wallpaper));
1290 WallpaperManager wm = (WallpaperManager)
1291 getSystemService(Context.WALLPAPER_SERVICE);
1292 WallpaperInfo wi = wm.getWallpaperInfo();
1293 if (wi != null && wi.getSettingsActivity() != null) {
1294 LabeledIntent li = new LabeledIntent(getPackageName(),
1295 R.string.configure_wallpaper, 0);
1296 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1297 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1298 }
Mike Clerona0618e42009-10-22 13:55:21 -07001299 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001302 /**
1303 * Registers various content observers. The current implementation registers
1304 * only a favorites observer to keep track of the favorites applications.
1305 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001306 private void registerContentObservers() {
1307 ContentResolver resolver = getContentResolver();
1308 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1309 true, mWidgetObserver);
1310 }
1311
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 @Override
1313 public boolean dispatchKeyEvent(KeyEvent event) {
1314 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1315 switch (event.getKeyCode()) {
1316 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 return true;
1318 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001319 return true;
1320 }
1321 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1322 switch (event.getKeyCode()) {
1323 case KeyEvent.KEYCODE_BACK:
1324 if (!event.isCanceled()) {
1325 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001326 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001327 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001328 } else {
1329 closeFolder();
1330 }
Romain Guya6abce82009-11-10 02:54:41 -08001331 dismissPreview(mPreviousView);
1332 dismissPreview(mNextView);
Dianne Hackborn67800862009-07-24 17:15:20 -07001333 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001334 return true;
1335 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 return true;
1337 }
1338 }
1339
1340 return super.dispatchKeyEvent(event);
1341 }
1342
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 private void closeFolder() {
1344 Folder folder = mWorkspace.getOpenFolder();
1345 if (folder != null) {
1346 closeFolder(folder);
1347 }
1348 }
1349
1350 void closeFolder(Folder folder) {
1351 folder.getInfo().opened = false;
1352 ViewGroup parent = (ViewGroup) folder.getParent();
1353 if (parent != null) {
1354 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001355 if (folder instanceof DropTarget) {
1356 // Live folders aren't DropTargets.
1357 mDragController.removeDropTarget((DropTarget)folder);
1358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
1360 folder.onClose();
1361 }
1362
1363 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001364 * Re-listen when widgets are reset.
1365 */
1366 private void onAppWidgetReset() {
1367 mAppWidgetHost.startListening();
1368 }
1369
1370 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001371 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1372 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001374 private void unbindDesktopItems() {
1375 for (ItemInfo item: mDesktopItems) {
1376 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
1378 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 /**
1381 * Launches the intent referred by the clicked shortcut.
1382 *
1383 * @param v The view representing the clicked shortcut.
1384 */
1385 public void onClick(View v) {
1386 Object tag = v.getTag();
1387 if (tag instanceof ApplicationInfo) {
1388 // Open shortcut
1389 final Intent intent = ((ApplicationInfo) tag).intent;
1390 startActivitySafely(intent);
1391 } else if (tag instanceof FolderInfo) {
1392 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001393 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001394 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001395 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001396 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001397 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
1400 }
1401
1402 void startActivitySafely(Intent intent) {
1403 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1404 try {
1405 startActivity(intent);
1406 } catch (ActivityNotFoundException e) {
1407 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1408 } catch (SecurityException e) {
1409 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001410 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1412 "or use the exported attribute for this activity.", e);
1413 }
1414 }
1415
1416 private void handleFolderClick(FolderInfo folderInfo) {
1417 if (!folderInfo.opened) {
1418 // Close any open folder
1419 closeFolder();
1420 // Open the requested folder
1421 openFolder(folderInfo);
1422 } else {
1423 // Find the open folder...
1424 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1425 int folderScreen;
1426 if (openFolder != null) {
1427 folderScreen = mWorkspace.getScreenForView(openFolder);
1428 // .. and close it
1429 closeFolder(openFolder);
1430 if (folderScreen != mWorkspace.getCurrentScreen()) {
1431 // Close any folder open on the current screen
1432 closeFolder();
1433 // Pull the folder onto this screen
1434 openFolder(folderInfo);
1435 }
1436 }
1437 }
1438 }
1439
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 /**
1441 * Opens the user fodler described by the specified tag. The opening of the folder
1442 * is animated relative to the specified View. If the View is null, no animation
1443 * is played.
1444 *
1445 * @param folderInfo The FolderInfo describing the folder to open.
1446 */
1447 private void openFolder(FolderInfo folderInfo) {
1448 Folder openFolder;
1449
1450 if (folderInfo instanceof UserFolderInfo) {
1451 openFolder = UserFolder.fromXml(this);
1452 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001453 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 } else {
1455 return;
1456 }
1457
Joe Onorato00acb122009-08-04 16:04:30 -04001458 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 openFolder.setLauncher(this);
1460
1461 openFolder.bind(folderInfo);
1462 folderInfo.opened = true;
1463
1464 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1465 openFolder.onOpen();
1466 }
1467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001469 switch (v.getId()) {
1470 case R.id.previous_screen:
1471 showPreviousPreview(v);
1472 return true;
1473 case R.id.next_screen:
1474 showNextPreview(v);
1475 return true;
1476 }
1477
Joe Onorato9c1289c2009-08-17 11:03:03 -04001478 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 return false;
1480 }
1481
1482 if (!(v instanceof CellLayout)) {
1483 v = (View) v.getParent();
1484 }
1485
1486 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1487
1488 // This happens when long clicking an item with the dpad/trackball
1489 if (cellInfo == null) {
1490 return true;
1491 }
1492
1493 if (mWorkspace.allowLongPress()) {
1494 if (cellInfo.cell == null) {
1495 if (cellInfo.valid) {
1496 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001497 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 showAddDialog(cellInfo);
1499 }
1500 } else {
1501 if (!(cellInfo.cell instanceof Folder)) {
1502 // User long pressed on an item
1503 mWorkspace.startDrag(cellInfo);
1504 }
1505 }
1506 }
1507 return true;
1508 }
1509
Romain Guye6b8e2f2009-11-10 11:56:55 -08001510 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001511 private void dismissPreview(final View v) {
1512 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001513 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001514 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1515 public void onDismiss() {
1516 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1517 int count = group.getChildCount();
1518 for (int i = 0; i < count; i++) {
1519 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1520 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001521 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1522 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1523
1524 v.setTag(R.id.workspace, null);
1525 v.setTag(R.id.icon, null);
1526 window.setOnDismissListener(null);
1527 }
1528 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001529 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001530 }
1531 v.setTag(null);
1532 }
1533
1534 private void showPreviousPreview(View anchor) {
1535 int current = mWorkspace.getCurrentScreen();
1536 if (current <= 0) return;
1537
Romain Guy9d31e9f2009-11-11 18:54:28 -08001538 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001539 }
1540
1541 private void showNextPreview(View anchor) {
1542 int current = mWorkspace.getCurrentScreen();
1543 if (current >= mWorkspace.getChildCount() - 1) return;
1544
Romain Guy9d31e9f2009-11-11 18:54:28 -08001545 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guya6abce82009-11-10 02:54:41 -08001546 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001547
Romain Guya6abce82009-11-10 02:54:41 -08001548 private void showPreviews(final View anchor, int start, int end) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001549 Resources resources = getResources();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001550
Romain Guya6abce82009-11-10 02:54:41 -08001551 Workspace workspace = mWorkspace;
1552 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001553
1554 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001555
1556 Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001557 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001558 int extraW = (int) ((r.left + r.right) * max);
1559 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001560
1561 int aW = cell.getWidth() - extraW;
1562 float w = aW / max;
1563
1564 int width = cell.getWidth();
1565 int height = cell.getHeight();
1566 int x = cell.getLeftPadding();
1567 int y = cell.getTopPadding();
1568 width -= (x + cell.getRightPadding());
1569 height -= (y + cell.getBottomPadding());
1570
1571 float scale = w / width;
1572
1573 int count = end - start;
1574
1575 final float sWidth = width * scale;
1576 float sHeight = height * scale;
1577
Romain Guye6b8e2f2009-11-10 11:56:55 -08001578 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001579
Romain Guye6b8e2f2009-11-10 11:56:55 -08001580 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1581 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001582
1583 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001584 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001585 cell = (CellLayout) workspace.getChildAt(i);
1586
Romain Guye6b8e2f2009-11-10 11:56:55 -08001587 Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
1588 Bitmap.Config.ARGB_8888);
1589
1590 Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001591 c.scale(scale, scale);
1592 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1593 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001594
Romain Guy9d31e9f2009-11-11 18:54:28 -08001595 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001596 image.setImageBitmap(bitmap);
1597 image.setTag(i);
1598 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001599 image.setOnFocusChangeListener(handler);
1600 image.setFocusable(true);
1601 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001602
1603 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001604 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1605
1606 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001607 }
Romain Guye6b8e2f2009-11-10 11:56:55 -08001608
1609 PopupWindow p = new PopupWindow(this);
1610 p.setContentView(preview);
1611 p.setWidth((int) (sWidth * count + extraW));
1612 p.setHeight((int) (sHeight + extraH));
1613 p.setAnimationStyle(R.style.AnimationPreview);
1614 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001615 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001616 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001617 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001618
Romain Guye6b8e2f2009-11-10 11:56:55 -08001619 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1620 public void onDismiss() {
1621 dismissPreview(anchor);
1622 }
1623 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001624
1625 anchor.setTag(p);
1626 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001627 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001628 }
1629
Romain Guy9d31e9f2009-11-11 18:54:28 -08001630 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001631 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001632
Romain Guye6b8e2f2009-11-10 11:56:55 -08001633 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001634 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001635 }
1636
1637 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001638 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001639 v.post(this);
1640 }
1641
1642 public void run() {
1643 dismissPreview(mAnchor);
1644 }
1645
1646 public void onFocusChange(View v, boolean hasFocus) {
1647 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001648 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001649 }
Romain Guya6abce82009-11-10 02:54:41 -08001650 }
1651 }
1652
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001653 View getDrawerHandle() {
1654 return mHandleView;
1655 }
1656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 Workspace getWorkspace() {
1658 return mWorkspace;
1659 }
1660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 @Override
1662 protected Dialog onCreateDialog(int id) {
1663 switch (id) {
1664 case DIALOG_CREATE_SHORTCUT:
1665 return new CreateShortcut().createDialog();
1666 case DIALOG_RENAME_FOLDER:
1667 return new RenameFolder().createDialog();
1668 }
1669
1670 return super.onCreateDialog(id);
1671 }
1672
1673 @Override
1674 protected void onPrepareDialog(int id, Dialog dialog) {
1675 switch (id) {
1676 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 break;
1678 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001679 if (mFolderInfo != null) {
1680 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1681 final CharSequence text = mFolderInfo.title;
1682 input.setText(text);
1683 input.setSelection(0, text.length());
1684 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 break;
1686 }
1687 }
1688
1689 void showRenameDialog(FolderInfo info) {
1690 mFolderInfo = info;
1691 mWaitingForResult = true;
1692 showDialog(DIALOG_RENAME_FOLDER);
1693 }
1694
1695 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1696 mAddItemCellInfo = cellInfo;
1697 mWaitingForResult = true;
1698 showDialog(DIALOG_CREATE_SHORTCUT);
1699 }
1700
Romain Guy73b979d2009-06-09 12:57:21 -07001701 private void pickShortcut(int requestCode, int title) {
1702 Bundle bundle = new Bundle();
1703
1704 ArrayList<String> shortcutNames = new ArrayList<String>();
1705 shortcutNames.add(getString(R.string.group_applications));
1706 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1707
1708 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1709 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1710 R.drawable.ic_launcher_application));
1711 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1712
1713 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1714 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1715 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1716 pickIntent.putExtras(bundle);
1717
1718 startActivityForResult(pickIntent, requestCode);
1719 }
1720
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 private class RenameFolder {
1722 private EditText mInput;
1723
1724 Dialog createDialog() {
1725 mWaitingForResult = true;
1726 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1727 mInput = (EditText) layout.findViewById(R.id.folder_name);
1728
1729 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1730 builder.setIcon(0);
1731 builder.setTitle(getString(R.string.rename_folder_title));
1732 builder.setCancelable(true);
1733 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1734 public void onCancel(DialogInterface dialog) {
1735 cleanup();
1736 }
1737 });
1738 builder.setNegativeButton(getString(R.string.cancel_action),
1739 new Dialog.OnClickListener() {
1740 public void onClick(DialogInterface dialog, int which) {
1741 cleanup();
1742 }
1743 }
1744 );
1745 builder.setPositiveButton(getString(R.string.rename_action),
1746 new Dialog.OnClickListener() {
1747 public void onClick(DialogInterface dialog, int which) {
1748 changeFolderName();
1749 }
1750 }
1751 );
1752 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001753
1754 final AlertDialog dialog = builder.create();
1755 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1756 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001757 mInput.requestFocus();
1758 InputMethodManager inputManager = (InputMethodManager)
1759 getSystemService(Context.INPUT_METHOD_SERVICE);
1760 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001761 }
1762 });
1763
1764 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 }
1766
1767 private void changeFolderName() {
1768 final String name = mInput.getText().toString();
1769 if (!TextUtils.isEmpty(name)) {
1770 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001771 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 mFolderInfo.title = name;
1773 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1774
Joe Onorato9c1289c2009-08-17 11:03:03 -04001775 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001776 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001777 mModel.setWorkspaceDirty();
1778 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 } else {
1780 final FolderIcon folderIcon = (FolderIcon)
1781 mWorkspace.getViewForTag(mFolderInfo);
1782 if (folderIcon != null) {
1783 folderIcon.setText(name);
1784 getWorkspace().requestLayout();
1785 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001786 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001787 mModel.setWorkspaceDirty();
1788 mWorkspaceLoading = true;
1789 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
1791 }
1792 }
1793 cleanup();
1794 }
1795
1796 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 dismissDialog(DIALOG_RENAME_FOLDER);
1798 mWaitingForResult = false;
1799 mFolderInfo = null;
1800 }
1801 }
1802
Joe Onoratofb0ca672009-09-14 17:55:46 -04001803 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001804 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001805 }
1806
Joe Onorato3a8820b2009-11-10 15:06:42 -08001807 boolean isAllAppsOpaque() {
1808 return mAllAppsGrid.isOpaque();
1809 }
1810
1811 void showAllApps(boolean animated) {
1812 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato7bb17492009-09-24 17:51:01 -07001813 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001814
Joe Onorato3a8820b2009-11-10 15:06:42 -08001815 mWorkspace.startFading(false);
1816
Mike Cleronb6082fa02009-10-19 17:03:36 -07001817 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001818 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001819
Joe Onorato7c312c12009-08-13 21:36:53 -07001820 // TODO: fade these two too
1821 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001822 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001823 }
1824
Joe Onorato7bb17492009-09-24 17:51:01 -07001825 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001826 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001827 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001828 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001829 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001830 mWorkspace.startFading(true);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001831
Joe Onorato7c312c12009-08-13 21:36:53 -07001832 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001833 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001834 mDeleteZone.setVisibility(View.VISIBLE);
1835 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001836 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001837 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001838 }
1839
Joe Onorato7c312c12009-08-13 21:36:53 -07001840 void lockAllApps() {
1841 // TODO
1842 }
1843
1844 void unlockAllApps() {
1845 // TODO
1846 }
1847
Joe Onorato7404ee42009-07-31 11:54:44 -07001848 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 * Displays the shortcut creation dialog and launches, if necessary, the
1850 * appropriate activity.
1851 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001852 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001853 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1854 DialogInterface.OnShowListener {
1855
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001857
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 Dialog createDialog() {
1859 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001860
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1864 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001865 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 builder.setInverseBackgroundForced(true);
1868
1869 AlertDialog dialog = builder.create();
1870 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001871 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001872 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 return dialog;
1875 }
1876
1877 public void onCancel(DialogInterface dialog) {
1878 mWaitingForResult = false;
1879 cleanup();
1880 }
1881
Romain Guycbb89e42009-06-08 15:52:54 -07001882 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001883 }
1884
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 dismissDialog(DIALOG_CREATE_SHORTCUT);
1887 }
1888
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001889 /**
1890 * Handle the action clicked in the "Add to home" dialog.
1891 */
1892 public void onClick(DialogInterface dialog, int which) {
1893 Resources res = getResources();
1894 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001895
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001896 switch (which) {
1897 case AddAdapter.ITEM_SHORTCUT: {
1898 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001899 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001900 break;
1901 }
Romain Guycbb89e42009-06-08 15:52:54 -07001902
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001903 case AddAdapter.ITEM_APPWIDGET: {
1904 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001905
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001906 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1907 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1908 // add the search widget
1909 ArrayList<AppWidgetProviderInfo> customInfo =
1910 new ArrayList<AppWidgetProviderInfo>();
1911 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1912 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1913 info.label = getString(R.string.group_search);
1914 info.icon = R.drawable.ic_search_widget;
1915 customInfo.add(info);
1916 pickIntent.putParcelableArrayListExtra(
1917 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1918 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1919 Bundle b = new Bundle();
1920 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1921 customExtras.add(b);
1922 pickIntent.putParcelableArrayListExtra(
1923 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1924 // start the pick activity
1925 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1926 break;
1927 }
Romain Guycbb89e42009-06-08 15:52:54 -07001928
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001929 case AddAdapter.ITEM_LIVE_FOLDER: {
1930 // Insert extra item to handle inserting folder
1931 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001932
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001933 ArrayList<String> shortcutNames = new ArrayList<String>();
1934 shortcutNames.add(res.getString(R.string.group_folder));
1935 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001936
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001937 ArrayList<ShortcutIconResource> shortcutIcons =
1938 new ArrayList<ShortcutIconResource>();
1939 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1940 R.drawable.ic_launcher_folder));
1941 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1942
1943 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1944 pickIntent.putExtra(Intent.EXTRA_INTENT,
1945 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1946 pickIntent.putExtra(Intent.EXTRA_TITLE,
1947 getText(R.string.title_select_live_folder));
1948 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001949
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001950 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1951 break;
1952 }
1953
1954 case AddAdapter.ITEM_WALLPAPER: {
1955 startWallpaper();
1956 break;
1957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
1959 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001960
1961 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 }
1964
1965 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001966 * Receives notifications when applications are added/removed.
1967 */
1968 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1969 @Override
1970 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001971 closeSystemDialogs();
1972 }
1973 }
1974
1975 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001976 * Receives notifications whenever the appwidgets are reset.
1977 */
1978 private class AppWidgetResetObserver extends ContentObserver {
1979 public AppWidgetResetObserver() {
1980 super(new Handler());
1981 }
1982
1983 @Override
1984 public void onChange(boolean selfChange) {
1985 onAppWidgetReset();
1986 }
1987 }
1988
1989 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001990 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 public int getCurrentWorkspaceScreen() {
1993 return mWorkspace.getCurrentScreen();
1994 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001995
Joe Onorato9c1289c2009-08-17 11:03:03 -04001996 /**
1997 * Refreshes the shortcuts shown on the workspace.
1998 *
1999 * Implementation of the method from LauncherModel.Callbacks.
2000 */
2001 public void startBinding() {
2002 final Workspace workspace = mWorkspace;
2003 int count = workspace.getChildCount();
2004 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002005 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2007 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002008
Joe Onorato9c1289c2009-08-17 11:03:03 -04002009 if (DEBUG_USER_INTERFACE) {
2010 android.widget.Button finishButton = new android.widget.Button(this);
2011 finishButton.setText("Finish");
2012 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2013
2014 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2015 public void onClick(View v) {
2016 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002017 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002018 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 }
2020 }
2021
2022 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002023 * Bind the items start-end from the list.
2024 *
2025 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002027 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2028
2029 final Workspace workspace = mWorkspace;
2030
2031 for (int i=start; i<end; i++) {
2032 final ItemInfo item = shortcuts.get(i);
2033 mDesktopItems.add(item);
2034 switch (item.itemType) {
2035 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2036 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2037 final View shortcut = createShortcut((ApplicationInfo) item);
2038 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2039 false);
2040 break;
2041 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2042 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2043 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2044 (UserFolderInfo) item);
2045 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2046 false);
2047 break;
2048 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2049 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2050 R.layout.live_folder_icon, this,
2051 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2052 (LiveFolderInfo) item);
2053 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2054 false);
2055 break;
2056 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2057 final int screen = workspace.getCurrentScreen();
2058 final View view = mInflater.inflate(R.layout.widget_search,
2059 (ViewGroup) workspace.getChildAt(screen), false);
2060
2061 Search search = (Search) view.findViewById(R.id.widget_search);
2062 search.setLauncher(this);
2063
2064 final Widget widget = (Widget) item;
2065 view.setTag(widget);
2066
2067 workspace.addWidget(view, widget, false);
2068 break;
2069 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 }
2071
Joe Onorato9c1289c2009-08-17 11:03:03 -04002072 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 }
2074
Joe Onorato9c1289c2009-08-17 11:03:03 -04002075 /**
2076 * Implementation of the method from LauncherModel.Callbacks.
2077 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002078 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2079 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002080 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002082
2083 /**
2084 * Add the views for a widget to the workspace.
2085 *
2086 * Implementation of the method from LauncherModel.Callbacks.
2087 */
2088 public void bindAppWidget(LauncherAppWidgetInfo item) {
2089 final Workspace workspace = mWorkspace;
2090
2091 final int appWidgetId = item.appWidgetId;
2092 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2093 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2094
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2096 item.hostView.setTag(item);
2097
2098 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2099 item.cellY, item.spanX, item.spanY, false);
2100
2101 workspace.requestLayout();
2102
2103 mDesktopItems.add(item);
2104 }
2105
2106 /**
2107 * Callback saying that there aren't any more items to bind.
2108 *
2109 * Implementation of the method from LauncherModel.Callbacks.
2110 */
2111 public void finishBindingItems() {
2112 if (mSavedState != null) {
2113 if (!mWorkspace.hasFocus()) {
2114 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2115 }
2116
2117 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2118 if (userFolders != null) {
2119 for (long folderId : userFolders) {
2120 final FolderInfo info = mFolders.get(folderId);
2121 if (info != null) {
2122 openFolder(info);
2123 }
2124 }
2125 final Folder openFolder = mWorkspace.getOpenFolder();
2126 if (openFolder != null) {
2127 openFolder.requestFocus();
2128 }
2129 }
2130
Joe Onorato9c1289c2009-08-17 11:03:03 -04002131 mSavedState = null;
2132 }
2133
2134 if (mSavedInstanceState != null) {
2135 super.onRestoreInstanceState(mSavedInstanceState);
2136 mSavedInstanceState = null;
2137 }
2138
Joe Onorato9c1289c2009-08-17 11:03:03 -04002139 mWorkspaceLoading = false;
2140 }
2141
2142 /**
2143 * Add the icons for all apps.
2144 *
2145 * Implementation of the method from LauncherModel.Callbacks.
2146 */
2147 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002148 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 }
2150
2151 /**
2152 * A package was installed.
2153 *
2154 * Implementation of the method from LauncherModel.Callbacks.
2155 */
2156 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2157 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002158 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002159 }
2160
2161 /**
2162 * A package was updated.
2163 *
2164 * Implementation of the method from LauncherModel.Callbacks.
2165 */
2166 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2167 removeDialog(DIALOG_CREATE_SHORTCUT);
2168 mWorkspace.updateShortcutsForPackage(packageName);
2169 }
2170
2171 /**
2172 * A package was uninstalled.
2173 *
2174 * Implementation of the method from LauncherModel.Callbacks.
2175 */
2176 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2177 removeDialog(DIALOG_CREATE_SHORTCUT);
2178 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002179 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002180 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181}