blob: c04176f13f970fb08af292c4f8341e84ce958555 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
20import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070022import android.app.ISearchManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.SearchManager;
24import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070025import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080027import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080029import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070033import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.ActivityInfo;
36import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070039import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080042import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080043import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080045import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080047import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.os.Parcelable;
49import android.os.RemoteException;
50import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.text.Selection;
53import android.text.SpannableStringBuilder;
54import android.text.TextUtils;
55import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070056import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.view.KeyEvent;
59import android.view.LayoutInflater;
60import android.view.Menu;
61import android.view.MenuItem;
62import android.view.View;
63import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.view.View.OnLongClickListener;
65import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.widget.TextView;
68import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080069import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080070import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080071import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070072import android.appwidget.AppWidgetManager;
73import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040076import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070077import java.io.DataOutputStream;
78import java.io.FileNotFoundException;
79import java.io.IOException;
80import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081
82/**
83 * Default launcher application.
84 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040085public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080086 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080087 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 static final boolean LOGD = false;
89
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 static final boolean PROFILE_STARTUP = false;
91 static final boolean PROFILE_ROTATE = false;
92 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 private static final int WALLPAPER_SCREENS_SPAN = 2;
95
96 private static final int MENU_GROUP_ADD = 1;
97 private static final int MENU_ADD = Menu.FIRST + 1;
98 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
99 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
100 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700101 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
103 private static final int REQUEST_CREATE_SHORTCUT = 1;
104 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700105 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 private static final int REQUEST_PICK_APPLICATION = 6;
107 private static final int REQUEST_PICK_SHORTCUT = 7;
108 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700109 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700110 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
112 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
113
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700114 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
115 static final String SEARCH_WIDGET = "search_widget";
116
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800117 static final int SCREEN_COUNT = 5;
118 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700120 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Joe Onorato7c312c12009-08-13 21:36:53 -0700122 static final int DIALOG_CREATE_SHORTCUT = 1;
123 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Romain Guy98d01652009-06-30 16:21:04 -0700125 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 // Type: int
128 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
129 // Type: boolean
130 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
131 // Type: long
132 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
133 // Type: int
134 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
147 // Type: int[]
148 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
149 // Type: boolean
150 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
151 // Type: long
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
153
Joe Onorato9c1289c2009-08-17 11:03:03 -0400154 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800157 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800159 private final BroadcastReceiver mCloseSystemDialogsReceiver
160 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800161 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private LayoutInflater mInflater;
164
Joe Onorato00acb122009-08-04 16:04:30 -0400165 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700167
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700168 private AppWidgetManager mAppWidgetManager;
169 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private CellLayout.CellInfo mAddItemCellInfo;
172 private CellLayout.CellInfo mMenuAddInfo;
173 private final int[] mCellCoordinates = new int[2];
174 private FolderInfo mFolderInfo;
175
Joe Onorato7c312c12009-08-13 21:36:53 -0700176 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700178 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private Bundle mSavedState;
181
182 private SpannableStringBuilder mDefaultKeySsb = null;
183
Mike LeBeau736cf282009-07-02 17:46:59 -0700184 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 private boolean mWorkspaceLoading = true;
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private boolean mRestoring;
189 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
191 private Bundle mSavedInstanceState;
192
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 private LauncherModel mModel;
194
Romain Guy84f296c2009-11-04 15:00:44 -0800195 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
196 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700197
Romain Guy1fbc1c82009-11-09 20:43:08 -0800198 private ImageView mPreviousView;
199 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 @Override
202 protected void onCreate(Bundle savedInstanceState) {
203 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700204
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400206 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700208
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800209 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
210 registerReceiver(mCloseSystemDialogsReceiver, filter);
211
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
214 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 if (PROFILE_STARTUP) {
217 android.os.Debug.startMethodTracing("/sdcard/launcher");
218 }
219
220 checkForLocaleChange();
221 setWallpaperDimension();
222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 setContentView(R.layout.launcher);
224 setupViews();
225
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800226 registerContentObservers();
227
Joe Onorato7c312c12009-08-13 21:36:53 -0700228 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 mSavedState = savedInstanceState;
231 restoreState(mSavedState);
232
233 if (PROFILE_STARTUP) {
234 android.os.Debug.stopMethodTracing();
235 }
236
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700237 // We have a new AllAppsView, we need to re-bind everything, and it could have
238 // changed in our absence.
239 mModel.setAllAppsDirty();
240 mModel.setWorkspaceDirty();
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400243 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 }
245
246 // For handling default keys
247 mDefaultKeySsb = new SpannableStringBuilder();
248 Selection.setSelection(mDefaultKeySsb, 0);
249 }
Romain Guycbb89e42009-06-08 15:52:54 -0700250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700252 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
253 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 final Configuration configuration = getResources().getConfiguration();
256
Romain Guy98d01652009-06-30 16:21:04 -0700257 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 final String locale = configuration.locale.toString();
259
Romain Guy98d01652009-06-30 16:21:04 -0700260 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 final int mcc = configuration.mcc;
262
Romain Guy98d01652009-06-30 16:21:04 -0700263 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 final int mnc = configuration.mnc;
265
Romain Guy84f296c2009-11-04 15:00:44 -0800266 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267
Romain Guy84f296c2009-11-04 15:00:44 -0800268 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700269 localeConfiguration.locale = locale;
270 localeConfiguration.mcc = mcc;
271 localeConfiguration.mnc = mnc;
272
273 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700275 }
276 }
277
278 private static class LocaleConfiguration {
279 public String locale;
280 public int mcc = -1;
281 public int mnc = -1;
282 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700283
Romain Guy98d01652009-06-30 16:21:04 -0700284 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
285 DataInputStream in = null;
286 try {
287 in = new DataInputStream(context.openFileInput(PREFERENCES));
288 configuration.locale = in.readUTF();
289 configuration.mcc = in.readInt();
290 configuration.mnc = in.readInt();
291 } catch (FileNotFoundException e) {
292 // Ignore
293 } catch (IOException e) {
294 // Ignore
295 } finally {
296 if (in != null) {
297 try {
298 in.close();
299 } catch (IOException e) {
300 // Ignore
301 }
302 }
303 }
304 }
305
306 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
307 DataOutputStream out = null;
308 try {
309 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
310 out.writeUTF(configuration.locale);
311 out.writeInt(configuration.mcc);
312 out.writeInt(configuration.mnc);
313 out.flush();
314 } catch (FileNotFoundException e) {
315 // Ignore
316 } catch (IOException e) {
317 //noinspection ResultOfMethodCallIgnored
318 context.getFileStreamPath(PREFERENCES).delete();
319 } finally {
320 if (out != null) {
321 try {
322 out.close();
323 } catch (IOException e) {
324 // Ignore
325 }
326 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 }
328 }
329
330 static int getScreen() {
331 synchronized (sLock) {
332 return sScreen;
333 }
334 }
335
336 static void setScreen(int screen) {
337 synchronized (sLock) {
338 sScreen = screen;
339 }
340 }
341
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700343 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344
345 Display display = getWindowManager().getDefaultDisplay();
346 boolean isPortrait = display.getWidth() < display.getHeight();
347
348 final int width = isPortrait ? display.getWidth() : display.getHeight();
349 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700350 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 }
352
353 @Override
354 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700355 mWaitingForResult = false;
356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 // The pattern used here is that a user PICKs a specific application,
358 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
361 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700362
Romain Guy94dabf12009-07-21 10:55:43 -0700363 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 switch (requestCode) {
365 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 break;
368 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700369 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 break;
371 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400372 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 break;
374 case REQUEST_PICK_LIVE_FOLDER:
375 addLiveFolder(data);
376 break;
377 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700380 case REQUEST_PICK_APPWIDGET:
381 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700383 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400384 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700386 case REQUEST_PICK_WALLPAPER:
387 // We just wanted the activity result here so we can clear mWaitingForResult
388 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 }
Romain Guy18042c82009-11-06 11:44:55 -0800390 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
391 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
392 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700393 // Clean up the appWidgetId if we canceled
394 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
395 if (appWidgetId != -1) {
396 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 }
398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 }
400
401 @Override
402 protected void onResume() {
403 super.onResume();
404
405 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400406 mWorkspaceLoading = true;
407 mModel.startLoader(this, true);
408 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700410
Mike LeBeau736cf282009-07-02 17:46:59 -0700411 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
412 // onNewIntent), then close the search dialog if needed, because it probably
413 // came from the user pressing 'home' (rather than, for example, pressing 'back').
414 if (mIsNewIntent) {
415 // Post to a handler so that this happens after the search dialog tries to open
416 // itself again.
417 mWorkspace.post(new Runnable() {
418 public void run() {
419 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
420 ServiceManager.getService(Context.SEARCH_SERVICE));
421 try {
422 searchManagerService.stopSearch();
423 } catch (RemoteException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800424 Log.e(TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700425 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700426 }
427 });
428 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700429
Mike LeBeau736cf282009-07-02 17:46:59 -0700430 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
432
433 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700434 protected void onPause() {
435 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800436 dismissPreview(mPreviousView);
437 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800438 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700439 }
Romain Guycbb89e42009-06-08 15:52:54 -0700440
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700441 @Override
442 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400443 // Flag the loader to stop early before switching
444 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700445
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700446 if (PROFILE_ROTATE) {
447 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
448 }
449 return null;
450 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 private boolean acceptFilter() {
453 final InputMethodManager inputManager = (InputMethodManager)
454 getSystemService(Context.INPUT_METHOD_SERVICE);
455 return !inputManager.isFullscreenMode();
456 }
457
458 @Override
459 public boolean onKeyDown(int keyCode, KeyEvent event) {
460 boolean handled = super.onKeyDown(keyCode, event);
461 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
462 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
463 keyCode, event);
464 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700465 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700466 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700467 // showSearchDialog()
468 // If there are multiple keystrokes before the search dialog takes focus,
469 // onSearchRequested() will be called for every keystroke,
470 // but it is idempotent, so it's fine.
471 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
473 }
474
475 return handled;
476 }
477
Karl Rosaen138a0412009-04-23 19:00:21 -0700478 private String getTypedText() {
479 return mDefaultKeySsb.toString();
480 }
481
482 private void clearTypedText() {
483 mDefaultKeySsb.clear();
484 mDefaultKeySsb.clearSpans();
485 Selection.setSelection(mDefaultKeySsb, 0);
486 }
487
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 /**
489 * Restores the previous state, if it exists.
490 *
491 * @param savedState The previous state.
492 */
493 private void restoreState(Bundle savedState) {
494 if (savedState == null) {
495 return;
496 }
497
Joe Onorato3a8820b2009-11-10 15:06:42 -0800498 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
499 if (allApps) {
500 showAllApps(false);
501 }
502
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800503 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
504 if (currentScreen > -1) {
505 mWorkspace.setCurrentScreen(currentScreen);
506 }
507
508 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
509 if (addScreen > -1) {
510 mAddItemCellInfo = new CellLayout.CellInfo();
511 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
512 addItemCellInfo.valid = true;
513 addItemCellInfo.screen = addScreen;
514 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
515 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
516 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
517 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
518 addItemCellInfo.findVacantCellsFromOccupied(
519 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
520 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
521 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
522 mRestoring = true;
523 }
524
525 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
526 if (renameFolder) {
527 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400528 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529 mRestoring = true;
530 }
531 }
532
533 /**
534 * Finds all the views we need and configure them properly.
535 */
536 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400537 DragController dragController = mDragController;
538
Romain Guyb1b69f52009-08-10 15:10:15 -0700539 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400540 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541
Joe Onorato7c312c12009-08-13 21:36:53 -0700542 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700543 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700544 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700545 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700546 // Manage focusability manually since this thing is always visible
547 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
550 final Workspace workspace = mWorkspace;
551
Joe Onorato7c312c12009-08-13 21:36:53 -0700552 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
553 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554
Joe Onorato7c312c12009-08-13 21:36:53 -0700555 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700556 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700557 mHandleView.setOnClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800558
559 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
560 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
561
562 Drawable previous = mPreviousView.getDrawable();
563 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800564 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565
Romain Guy1fbc1c82009-11-09 20:43:08 -0800566 mPreviousView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800567 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800568
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400570 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572
573 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400574 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700575 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576
Joe Onorato00acb122009-08-04 16:04:30 -0400577 dragController.setDragScoller(workspace);
578 dragController.setDragListener(deleteZone);
579 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700580
Joe Onorato00acb122009-08-04 16:04:30 -0400581 // The order here is bottom to top.
582 dragController.addDropTarget(workspace);
583 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 }
585
Romain Guy8a73c512009-11-09 19:19:59 -0800586 @SuppressWarnings({"UnusedDeclaration"})
587 public void previousScreen(View v) {
588 mWorkspace.scrollLeft();
589 }
590
591 @SuppressWarnings({"UnusedDeclaration"})
592 public void nextScreen(View v) {
593 mWorkspace.scrollRight();
594 }
595
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 /**
597 * Creates a view representing a shortcut.
598 *
599 * @param info The data structure describing the shortcut.
600 *
601 * @return A View inflated from R.layout.application.
602 */
603 View createShortcut(ApplicationInfo info) {
604 return createShortcut(R.layout.application,
605 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
606 }
607
608 /**
609 * Creates a view representing a shortcut inflated from the specified resource.
610 *
611 * @param layoutResId The id of the XML layout used to create the shortcut.
612 * @param parent The group the shortcut belongs to.
613 * @param info The data structure describing the shortcut.
614 *
615 * @return A View inflated from layoutResId.
616 */
617 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
618 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
619
Joe Onorato5162ea92009-09-03 09:39:42 -0700620 if (info.icon == null) {
621 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
622 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700624 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 info.filtered = true;
626 }
627
628 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
629 favorite.setText(info.title);
630 favorite.setTag(info);
631 favorite.setOnClickListener(this);
632
633 return favorite;
634 }
635
636 /**
637 * Add an application shortcut to the workspace.
638 *
639 * @param data The intent describing the application.
640 * @param cellInfo The position on screen where to create the shortcut.
641 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 cellInfo.screen = mWorkspace.getCurrentScreen();
644 if (!findSingleSlot(cellInfo)) return;
645
Romain Guy73b979d2009-06-09 12:57:21 -0700646 final ApplicationInfo info = infoFromApplicationIntent(context, data);
647 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700649 }
650 }
651
652 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 ComponentName component = data.getComponent();
654 PackageManager packageManager = context.getPackageManager();
655 ActivityInfo activityInfo = null;
656 try {
657 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
658 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800659 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Romain Guycbb89e42009-06-08 15:52:54 -0700661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 if (activityInfo != null) {
663 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 itemInfo.title = activityInfo.loadLabel(packageManager);
666 if (itemInfo.title == null) {
667 itemInfo.title = activityInfo.name;
668 }
Romain Guycbb89e42009-06-08 15:52:54 -0700669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
671 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
672 itemInfo.icon = activityInfo.loadIcon(packageManager);
673 itemInfo.container = ItemInfo.NO_ID;
674
Romain Guy73b979d2009-06-09 12:57:21 -0700675 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
Romain Guy73b979d2009-06-09 12:57:21 -0700677
678 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
Romain Guycbb89e42009-06-08 15:52:54 -0700680
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 /**
682 * Add a shortcut to the workspace.
683 *
684 * @param data The intent describing the shortcut.
685 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400687 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 cellInfo.screen = mWorkspace.getCurrentScreen();
689 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700690
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
692
693 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400695 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
696 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 }
698 }
699
Romain Guycbb89e42009-06-08 15:52:54 -0700700
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700702 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700704 * @param data The intent describing the appWidgetId.
705 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700709 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700710
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800711 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700712
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700713 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700714
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700715 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700718
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 // Try finding open space on Launcher screen
720 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800721 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
722 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
723 return;
724 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700726 // Build Launcher-specific widget info and save to database
727 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 launcherInfo.spanX = spans[0];
729 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 LauncherModel.addItemToDatabase(this, launcherInfo,
732 LauncherSettings.Favorites.CONTAINER_DESKTOP,
733 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
734
735 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700737
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700739 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700740
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700741 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700743
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400745 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 }
747 }
Romain Guycbb89e42009-06-08 15:52:54 -0700748
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
750 mDesktopItems.remove(launcherInfo);
751 launcherInfo.hostView = null;
752 }
753
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700754 public LauncherAppWidgetHost getAppWidgetHost() {
755 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 }
Romain Guycbb89e42009-06-08 15:52:54 -0700757
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 static ApplicationInfo addShortcut(Context context, Intent data,
759 CellLayout.CellInfo cellInfo, boolean notify) {
760
Romain Guy73b979d2009-06-09 12:57:21 -0700761 final ApplicationInfo info = infoFromShortcutIntent(context, data);
762 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
763 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
764
765 return info;
766 }
767
768 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
770 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
771 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
772
773 Drawable icon = null;
774 boolean filtered = false;
775 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700776 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777
778 if (bitmap != null) {
779 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
780 filtered = true;
781 customIcon = true;
782 } else {
783 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700784 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700786 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 final PackageManager packageManager = context.getPackageManager();
788 Resources resources = packageManager.getResourcesForApplication(
789 iconResource.packageName);
790 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
791 icon = resources.getDrawable(id);
792 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800793 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 }
795 }
796 }
797
798 if (icon == null) {
799 icon = context.getPackageManager().getDefaultActivityIcon();
800 }
801
802 final ApplicationInfo info = new ApplicationInfo();
803 info.icon = icon;
804 info.filtered = filtered;
805 info.title = name;
806 info.intent = intent;
807 info.customIcon = customIcon;
808 info.iconResource = iconResource;
809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 return info;
811 }
812
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800813 void closeSystemDialogs() {
814 closeAllApps(false);
815 getWindow().closeAllPanels();
816
817 try {
818 dismissDialog(DIALOG_CREATE_SHORTCUT);
819 // Unlock the workspace if the dialog was showing
820 } catch (Exception e) {
821 // An exception is thrown if the dialog is not visible, which is fine
822 }
823
824 try {
825 dismissDialog(DIALOG_RENAME_FOLDER);
826 // Unlock the workspace if the dialog was showing
827 } catch (Exception e) {
828 // An exception is thrown if the dialog is not visible, which is fine
829 }
830 }
831
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 @Override
833 protected void onNewIntent(Intent intent) {
834 super.onNewIntent(intent);
835
836 // Close the menu
837 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800838 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700839
Joe Onorato742966b2009-10-07 10:26:17 -0700840 // Whatever we were doing is hereby canceled.
841 mWaitingForResult = false;
842
Mike LeBeau736cf282009-07-02 17:46:59 -0700843 // Set this flag so that onResume knows to close the search dialog if it's open,
844 // because this was a new intent (thus a press of 'home' or some such) rather than
845 // for example onResume being called when the user pressed the 'back' button.
846 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847
Joe Onorato3a8820b2009-11-10 15:06:42 -0800848 if (!mWorkspace.isDefaultScreenShowing()) {
849 mWorkspace.moveToDefaultScreen();
850 }
Romain Guy73b979d2009-06-09 12:57:21 -0700851
Joe Onorato3a8820b2009-11-10 15:06:42 -0800852 closeAllApps(false);
Romain Guy1dd3a072009-07-16 13:21:01 -0700853
Joe Onorato3a8820b2009-11-10 15:06:42 -0800854 final View v = getWindow().peekDecorView();
855 if (v != null && v.getWindowToken() != null) {
856 InputMethodManager imm = (InputMethodManager)getSystemService(
857 INPUT_METHOD_SERVICE);
858 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 }
860 }
861 }
862
863 @Override
864 protected void onRestoreInstanceState(Bundle savedInstanceState) {
865 // Do not call super here
866 mSavedInstanceState = savedInstanceState;
867 }
868
869 @Override
870 protected void onSaveInstanceState(Bundle outState) {
871 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
872
873 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
874 if (folders.size() > 0) {
875 final int count = folders.size();
876 long[] ids = new long[count];
877 for (int i = 0; i < count; i++) {
878 final FolderInfo info = folders.get(i).getInfo();
879 ids[i] = info.id;
880 }
881 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
882 } else {
883 super.onSaveInstanceState(outState);
884 }
885
Joe Onoratofb0ca672009-09-14 17:55:46 -0400886 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800887 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700889 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890
891 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
892 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
893 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
894
895 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
896 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
897 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
898 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
899 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
900 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
901 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
902 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
903 layout.getOccupiedCells());
904 }
905
906 if (mFolderInfo != null && mWaitingForResult) {
907 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
908 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
909 }
910 }
911
912 @Override
913 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700915
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700917 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800919 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 }
921
922 TextKeyListener.getInstance().release();
923
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925
Joe Onorato9c1289c2009-08-17 11:03:03 -0400926 unbindDesktopItems();
927 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800928
929 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800930
931 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800932 dismissPreview(mNextView);
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800933
934 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
936
937 @Override
938 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700939 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 super.startActivityForResult(intent, requestCode);
941 }
942
943 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700944 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700946
Joe Onorato7bb17492009-09-24 17:51:01 -0700947 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700948
Karl Rosaen138a0412009-04-23 19:00:21 -0700949 // Slide the search widget to the top, if it's on the current screen,
950 // otherwise show the search dialog immediately.
951 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
952 if (searchWidget == null) {
953 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
954 } else {
955 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
956 // show the currently typed text in the search widget while sliding
957 searchWidget.setQuery(getTypedText());
958 }
959 }
Romain Guycbb89e42009-06-08 15:52:54 -0700960
Karl Rosaen138a0412009-04-23 19:00:21 -0700961 /**
962 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700963 *
964 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700965 */
Romain Guycbb89e42009-06-08 15:52:54 -0700966 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700968
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 if (initialQuery == null) {
970 // Use any text typed in the launcher as the initial query
971 initialQuery = getTypedText();
972 clearTypedText();
973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 if (appSearchData == null) {
975 appSearchData = new Bundle();
976 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
977 }
Romain Guycbb89e42009-06-08 15:52:54 -0700978
Karl Rosaen138a0412009-04-23 19:00:21 -0700979 final SearchManager searchManager =
980 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
981
982 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
983 if (searchWidget != null) {
984 // This gets called when the user leaves the search dialog to go back to
985 // the Launcher.
986 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
987 public void onCancel() {
988 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700989 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700990 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700991 });
992 }
Romain Guycbb89e42009-06-08 15:52:54 -0700993
Karl Rosaen138a0412009-04-23 19:00:21 -0700994 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700995 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
997
Karl Rosaen138a0412009-04-23 19:00:21 -0700998 /**
999 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001000 */
Romain Guy5a941392009-04-28 15:18:25 -07001001 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001002 // Close search dialog
1003 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001004 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001005 // Restore search widget to its normal position
1006 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1007 if (searchWidget != null) {
1008 searchWidget.stopSearch(false);
1009 }
1010 }
Romain Guycbb89e42009-06-08 15:52:54 -07001011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 @Override
1013 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001014 if (isWorkspaceLocked()) {
1015 return false;
1016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017
1018 super.onCreateOptionsMenu(menu);
1019 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1020 .setIcon(android.R.drawable.ic_menu_add)
1021 .setAlphabeticShortcut('A');
1022 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1023 .setIcon(android.R.drawable.ic_menu_gallery)
1024 .setAlphabeticShortcut('W');
1025 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1026 .setIcon(android.R.drawable.ic_search_category_default)
1027 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1028 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1029 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1030 .setAlphabeticShortcut('N');
1031
1032 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001033 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1034 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035
1036 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1037 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1038 .setIntent(settings);
1039
1040 return true;
1041 }
1042
1043 @Override
1044 public boolean onPrepareOptionsMenu(Menu menu) {
1045 super.onPrepareOptionsMenu(menu);
1046
1047 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1048 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1049
1050 return true;
1051 }
1052
1053 @Override
1054 public boolean onOptionsItemSelected(MenuItem item) {
1055 switch (item.getItemId()) {
1056 case MENU_ADD:
1057 addItems();
1058 return true;
1059 case MENU_WALLPAPER_SETTINGS:
1060 startWallpaper();
1061 return true;
1062 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001063 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 return true;
1065 case MENU_NOTIFICATIONS:
1066 showNotifications();
1067 return true;
1068 }
1069
1070 return super.onOptionsItemSelected(item);
1071 }
Romain Guycbb89e42009-06-08 15:52:54 -07001072
Karl Rosaen138a0412009-04-23 19:00:21 -07001073 /**
1074 * Indicates that we want global search for this activity by setting the globalSearch
1075 * argument for {@link #startSearch} to true.
1076 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001078 @Override
1079 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001080 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001081 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001082 }
1083
Joe Onorato9c1289c2009-08-17 11:03:03 -04001084 public boolean isWorkspaceLocked() {
1085 return mWorkspaceLoading || mWaitingForResult;
1086 }
1087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001089 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 showAddDialog(mMenuAddInfo);
1091 }
1092
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001093 void addAppWidget(Intent data) {
1094 // TODO: catch bad widget exception when sent
1095 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001097 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1098 if (SEARCH_WIDGET.equals(customWidget)) {
1099 // We don't need this any more, since this isn't a real app widget.
1100 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1101 // add the search widget
1102 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001104 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1105
1106 if (appWidget.configure != null) {
1107 // Launch over to configure widget, if needed
1108 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1109 intent.setComponent(appWidget.configure);
1110 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1111
1112 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1113 } else {
1114 // Otherwise just add it
1115 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
1118 }
Romain Guycbb89e42009-06-08 15:52:54 -07001119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 void addSearch() {
1121 final Widget info = Widget.makeSearch();
1122 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 final int[] xy = mCellCoordinates;
1125 final int spanX = info.spanX;
1126 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001127
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1131 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 final View view = mInflater.inflate(info.layoutResource, null);
1134 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001135 Search search = (Search) view.findViewById(R.id.widget_search);
1136 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001137
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1139 }
1140
Romain Guy73b979d2009-06-09 12:57:21 -07001141 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001142 // Handle case where user selected "Applications"
1143 String applicationName = getResources().getString(R.string.group_applications);
1144 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001145
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001146 if (applicationName != null && applicationName.equals(shortcutName)) {
1147 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1148 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001149
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001150 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1151 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001152 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001153 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001154 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001155 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 }
1157
1158 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001159 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001160 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001161 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001162
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001163 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001164 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001165 } else {
1166 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 }
1169
Joe Onorato9c1289c2009-08-17 11:03:03 -04001170 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 UserFolderInfo folderInfo = new UserFolderInfo();
1172 folderInfo.title = getText(R.string.folder_name);
1173
1174 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1175 cellInfo.screen = mWorkspace.getCurrentScreen();
1176 if (!findSingleSlot(cellInfo)) return;
1177
1178 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001179 LauncherModel.addItemToDatabase(this, folderInfo,
1180 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001182 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183
1184 // Create the view
1185 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1186 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1187 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 }
Romain Guycbb89e42009-06-08 15:52:54 -07001190
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 void removeFolder(FolderInfo folder) {
1192 mFolders.remove(folder.id);
1193 }
1194
1195 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 cellInfo.screen = mWorkspace.getCurrentScreen();
1197 if (!findSingleSlot(cellInfo)) return;
1198
1199 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1200
1201 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1203 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001204 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1205 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 }
1207 }
1208
1209 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1210 CellLayout.CellInfo cellInfo, boolean notify) {
1211
1212 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1213 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1214
1215 Drawable icon = null;
1216 boolean filtered = false;
1217 Intent.ShortcutIconResource iconResource = null;
1218
1219 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1220 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1221 try {
1222 iconResource = (Intent.ShortcutIconResource) extra;
1223 final PackageManager packageManager = context.getPackageManager();
1224 Resources resources = packageManager.getResourcesForApplication(
1225 iconResource.packageName);
1226 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1227 icon = resources.getDrawable(id);
1228 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001229 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
1231 }
1232
1233 if (icon == null) {
1234 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1235 }
1236
1237 final LiveFolderInfo info = new LiveFolderInfo();
1238 info.icon = icon;
1239 info.filtered = filtered;
1240 info.title = name;
1241 info.iconResource = iconResource;
1242 info.uri = data.getData();
1243 info.baseIntent = baseIntent;
1244 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1245 LiveFolders.DISPLAY_MODE_GRID);
1246
1247 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1248 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001249 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250
1251 return info;
1252 }
1253
1254 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1255 final int[] xy = new int[2];
1256 if (findSlot(cellInfo, xy, 1, 1)) {
1257 cellInfo.cellX = xy[0];
1258 cellInfo.cellY = xy[1];
1259 return true;
1260 }
1261 return false;
1262 }
1263
1264 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1265 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1266 boolean[] occupied = mSavedState != null ?
1267 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1268 cellInfo = mWorkspace.findAllVacantCells(occupied);
1269 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1270 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1271 return false;
1272 }
1273 }
1274 return true;
1275 }
1276
1277 private void showNotifications() {
1278 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1279 if (statusBar != null) {
1280 statusBar.expand();
1281 }
1282 }
1283
1284 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001285 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001287 Intent chooser = Intent.createChooser(pickWallpaper,
1288 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001289 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1290 // Removed in Eclair MR1
1291// WallpaperManager wm = (WallpaperManager)
1292// getSystemService(Context.WALLPAPER_SERVICE);
1293// WallpaperInfo wi = wm.getWallpaperInfo();
1294// if (wi != null && wi.getSettingsActivity() != null) {
1295// LabeledIntent li = new LabeledIntent(getPackageName(),
1296// R.string.configure_wallpaper, 0);
1297// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1298// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1299// }
Mike Clerona0618e42009-10-22 13:55:21 -07001300 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001303 /**
1304 * Registers various content observers. The current implementation registers
1305 * only a favorites observer to keep track of the favorites applications.
1306 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001307 private void registerContentObservers() {
1308 ContentResolver resolver = getContentResolver();
1309 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1310 true, mWidgetObserver);
1311 }
1312
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 @Override
1314 public boolean dispatchKeyEvent(KeyEvent event) {
1315 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1316 switch (event.getKeyCode()) {
1317 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 return true;
1319 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001320 return true;
1321 }
1322 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1323 switch (event.getKeyCode()) {
1324 case KeyEvent.KEYCODE_BACK:
1325 if (!event.isCanceled()) {
1326 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001327 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001328 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001329 } else {
1330 closeFolder();
1331 }
Romain Guya6abce82009-11-10 02:54:41 -08001332 dismissPreview(mPreviousView);
1333 dismissPreview(mNextView);
Dianne Hackborn67800862009-07-24 17:15:20 -07001334 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001335 return true;
1336 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 return true;
1338 }
1339 }
1340
1341 return super.dispatchKeyEvent(event);
1342 }
1343
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 private void closeFolder() {
1345 Folder folder = mWorkspace.getOpenFolder();
1346 if (folder != null) {
1347 closeFolder(folder);
1348 }
1349 }
1350
1351 void closeFolder(Folder folder) {
1352 folder.getInfo().opened = false;
1353 ViewGroup parent = (ViewGroup) folder.getParent();
1354 if (parent != null) {
1355 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001356 if (folder instanceof DropTarget) {
1357 // Live folders aren't DropTargets.
1358 mDragController.removeDropTarget((DropTarget)folder);
1359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361 folder.onClose();
1362 }
1363
1364 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001365 * Re-listen when widgets are reset.
1366 */
1367 private void onAppWidgetReset() {
1368 mAppWidgetHost.startListening();
1369 }
1370
1371 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1373 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 private void unbindDesktopItems() {
1376 for (ItemInfo item: mDesktopItems) {
1377 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
1379 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 /**
1382 * Launches the intent referred by the clicked shortcut.
1383 *
1384 * @param v The view representing the clicked shortcut.
1385 */
1386 public void onClick(View v) {
1387 Object tag = v.getTag();
1388 if (tag instanceof ApplicationInfo) {
1389 // Open shortcut
1390 final Intent intent = ((ApplicationInfo) tag).intent;
1391 startActivitySafely(intent);
1392 } else if (tag instanceof FolderInfo) {
1393 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001394 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001395 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001396 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001397 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001398 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 }
1401 }
1402
1403 void startActivitySafely(Intent intent) {
1404 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1405 try {
1406 startActivity(intent);
1407 } catch (ActivityNotFoundException e) {
1408 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1409 } catch (SecurityException e) {
1410 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001411 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1413 "or use the exported attribute for this activity.", e);
1414 }
1415 }
1416
1417 private void handleFolderClick(FolderInfo folderInfo) {
1418 if (!folderInfo.opened) {
1419 // Close any open folder
1420 closeFolder();
1421 // Open the requested folder
1422 openFolder(folderInfo);
1423 } else {
1424 // Find the open folder...
1425 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1426 int folderScreen;
1427 if (openFolder != null) {
1428 folderScreen = mWorkspace.getScreenForView(openFolder);
1429 // .. and close it
1430 closeFolder(openFolder);
1431 if (folderScreen != mWorkspace.getCurrentScreen()) {
1432 // Close any folder open on the current screen
1433 closeFolder();
1434 // Pull the folder onto this screen
1435 openFolder(folderInfo);
1436 }
1437 }
1438 }
1439 }
1440
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 /**
1442 * Opens the user fodler described by the specified tag. The opening of the folder
1443 * is animated relative to the specified View. If the View is null, no animation
1444 * is played.
1445 *
1446 * @param folderInfo The FolderInfo describing the folder to open.
1447 */
1448 private void openFolder(FolderInfo folderInfo) {
1449 Folder openFolder;
1450
1451 if (folderInfo instanceof UserFolderInfo) {
1452 openFolder = UserFolder.fromXml(this);
1453 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001454 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 } else {
1456 return;
1457 }
1458
Joe Onorato00acb122009-08-04 16:04:30 -04001459 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 openFolder.setLauncher(this);
1461
1462 openFolder.bind(folderInfo);
1463 folderInfo.opened = true;
1464
1465 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1466 openFolder.onOpen();
1467 }
1468
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001470 switch (v.getId()) {
1471 case R.id.previous_screen:
1472 showPreviousPreview(v);
1473 return true;
1474 case R.id.next_screen:
1475 showNextPreview(v);
1476 return true;
1477 }
1478
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 return false;
1481 }
1482
1483 if (!(v instanceof CellLayout)) {
1484 v = (View) v.getParent();
1485 }
1486
1487 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1488
1489 // This happens when long clicking an item with the dpad/trackball
1490 if (cellInfo == null) {
1491 return true;
1492 }
1493
1494 if (mWorkspace.allowLongPress()) {
1495 if (cellInfo.cell == null) {
1496 if (cellInfo.valid) {
1497 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001498 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 showAddDialog(cellInfo);
1500 }
1501 } else {
1502 if (!(cellInfo.cell instanceof Folder)) {
1503 // User long pressed on an item
1504 mWorkspace.startDrag(cellInfo);
1505 }
1506 }
1507 }
1508 return true;
1509 }
1510
Romain Guye6b8e2f2009-11-10 11:56:55 -08001511 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001512 private void dismissPreview(final View v) {
1513 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001514 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001515 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1516 public void onDismiss() {
1517 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1518 int count = group.getChildCount();
1519 for (int i = 0; i < count; i++) {
1520 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1521 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001522 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1523 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1524
1525 v.setTag(R.id.workspace, null);
1526 v.setTag(R.id.icon, null);
1527 window.setOnDismissListener(null);
1528 }
1529 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001530 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001531 }
1532 v.setTag(null);
1533 }
1534
1535 private void showPreviousPreview(View anchor) {
1536 int current = mWorkspace.getCurrentScreen();
1537 if (current <= 0) return;
1538
Romain Guy9d31e9f2009-11-11 18:54:28 -08001539 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001540 }
1541
1542 private void showNextPreview(View anchor) {
1543 int current = mWorkspace.getCurrentScreen();
1544 if (current >= mWorkspace.getChildCount() - 1) return;
1545
Romain Guy9d31e9f2009-11-11 18:54:28 -08001546 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guya6abce82009-11-10 02:54:41 -08001547 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001548
Romain Guya6abce82009-11-10 02:54:41 -08001549 private void showPreviews(final View anchor, int start, int end) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001550 Resources resources = getResources();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001551
Romain Guya6abce82009-11-10 02:54:41 -08001552 Workspace workspace = mWorkspace;
1553 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001554
1555 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001556
1557 Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001558 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001559 int extraW = (int) ((r.left + r.right) * max);
1560 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001561
1562 int aW = cell.getWidth() - extraW;
1563 float w = aW / max;
1564
1565 int width = cell.getWidth();
1566 int height = cell.getHeight();
1567 int x = cell.getLeftPadding();
1568 int y = cell.getTopPadding();
1569 width -= (x + cell.getRightPadding());
1570 height -= (y + cell.getBottomPadding());
1571
1572 float scale = w / width;
1573
1574 int count = end - start;
1575
1576 final float sWidth = width * scale;
1577 float sHeight = height * scale;
1578
Romain Guye6b8e2f2009-11-10 11:56:55 -08001579 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001580
Romain Guye6b8e2f2009-11-10 11:56:55 -08001581 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1582 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001583
1584 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001585 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001586 cell = (CellLayout) workspace.getChildAt(i);
1587
Romain Guye6b8e2f2009-11-10 11:56:55 -08001588 Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
1589 Bitmap.Config.ARGB_8888);
1590
1591 Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001592 c.scale(scale, scale);
1593 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1594 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001595
Romain Guy9d31e9f2009-11-11 18:54:28 -08001596 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001597 image.setImageBitmap(bitmap);
1598 image.setTag(i);
1599 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001600 image.setOnFocusChangeListener(handler);
1601 image.setFocusable(true);
1602 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001603
1604 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001605 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1606
1607 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001608 }
Romain Guye6b8e2f2009-11-10 11:56:55 -08001609
1610 PopupWindow p = new PopupWindow(this);
1611 p.setContentView(preview);
1612 p.setWidth((int) (sWidth * count + extraW));
1613 p.setHeight((int) (sHeight + extraH));
1614 p.setAnimationStyle(R.style.AnimationPreview);
1615 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001616 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001617 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001618 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001619
Romain Guye6b8e2f2009-11-10 11:56:55 -08001620 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1621 public void onDismiss() {
1622 dismissPreview(anchor);
1623 }
1624 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001625
1626 anchor.setTag(p);
1627 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001628 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001629 }
1630
Romain Guy9d31e9f2009-11-11 18:54:28 -08001631 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001632 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001633
Romain Guye6b8e2f2009-11-10 11:56:55 -08001634 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001635 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001636 }
1637
1638 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001639 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001640 v.post(this);
1641 }
1642
1643 public void run() {
1644 dismissPreview(mAnchor);
1645 }
1646
1647 public void onFocusChange(View v, boolean hasFocus) {
1648 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001649 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001650 }
Romain Guya6abce82009-11-10 02:54:41 -08001651 }
1652 }
1653
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001654 View getDrawerHandle() {
1655 return mHandleView;
1656 }
1657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 Workspace getWorkspace() {
1659 return mWorkspace;
1660 }
1661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 @Override
1663 protected Dialog onCreateDialog(int id) {
1664 switch (id) {
1665 case DIALOG_CREATE_SHORTCUT:
1666 return new CreateShortcut().createDialog();
1667 case DIALOG_RENAME_FOLDER:
1668 return new RenameFolder().createDialog();
1669 }
1670
1671 return super.onCreateDialog(id);
1672 }
1673
1674 @Override
1675 protected void onPrepareDialog(int id, Dialog dialog) {
1676 switch (id) {
1677 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 break;
1679 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001680 if (mFolderInfo != null) {
1681 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1682 final CharSequence text = mFolderInfo.title;
1683 input.setText(text);
1684 input.setSelection(0, text.length());
1685 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 break;
1687 }
1688 }
1689
1690 void showRenameDialog(FolderInfo info) {
1691 mFolderInfo = info;
1692 mWaitingForResult = true;
1693 showDialog(DIALOG_RENAME_FOLDER);
1694 }
1695
1696 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1697 mAddItemCellInfo = cellInfo;
1698 mWaitingForResult = true;
1699 showDialog(DIALOG_CREATE_SHORTCUT);
1700 }
1701
Romain Guy73b979d2009-06-09 12:57:21 -07001702 private void pickShortcut(int requestCode, int title) {
1703 Bundle bundle = new Bundle();
1704
1705 ArrayList<String> shortcutNames = new ArrayList<String>();
1706 shortcutNames.add(getString(R.string.group_applications));
1707 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1708
1709 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1710 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1711 R.drawable.ic_launcher_application));
1712 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1713
1714 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1715 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1716 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1717 pickIntent.putExtras(bundle);
1718
1719 startActivityForResult(pickIntent, requestCode);
1720 }
1721
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 private class RenameFolder {
1723 private EditText mInput;
1724
1725 Dialog createDialog() {
1726 mWaitingForResult = true;
1727 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1728 mInput = (EditText) layout.findViewById(R.id.folder_name);
1729
1730 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1731 builder.setIcon(0);
1732 builder.setTitle(getString(R.string.rename_folder_title));
1733 builder.setCancelable(true);
1734 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1735 public void onCancel(DialogInterface dialog) {
1736 cleanup();
1737 }
1738 });
1739 builder.setNegativeButton(getString(R.string.cancel_action),
1740 new Dialog.OnClickListener() {
1741 public void onClick(DialogInterface dialog, int which) {
1742 cleanup();
1743 }
1744 }
1745 );
1746 builder.setPositiveButton(getString(R.string.rename_action),
1747 new Dialog.OnClickListener() {
1748 public void onClick(DialogInterface dialog, int which) {
1749 changeFolderName();
1750 }
1751 }
1752 );
1753 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001754
1755 final AlertDialog dialog = builder.create();
1756 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1757 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001758 mInput.requestFocus();
1759 InputMethodManager inputManager = (InputMethodManager)
1760 getSystemService(Context.INPUT_METHOD_SERVICE);
1761 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001762 }
1763 });
1764
1765 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 }
1767
1768 private void changeFolderName() {
1769 final String name = mInput.getText().toString();
1770 if (!TextUtils.isEmpty(name)) {
1771 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001772 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 mFolderInfo.title = name;
1774 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1775
Joe Onorato9c1289c2009-08-17 11:03:03 -04001776 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001777 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001778 mModel.setWorkspaceDirty();
1779 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 } else {
1781 final FolderIcon folderIcon = (FolderIcon)
1782 mWorkspace.getViewForTag(mFolderInfo);
1783 if (folderIcon != null) {
1784 folderIcon.setText(name);
1785 getWorkspace().requestLayout();
1786 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001787 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001788 mModel.setWorkspaceDirty();
1789 mWorkspaceLoading = true;
1790 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 }
1792 }
1793 }
1794 cleanup();
1795 }
1796
1797 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 dismissDialog(DIALOG_RENAME_FOLDER);
1799 mWaitingForResult = false;
1800 mFolderInfo = null;
1801 }
1802 }
1803
Joe Onoratofb0ca672009-09-14 17:55:46 -04001804 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001805 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001806 }
1807
Joe Onorato3a8820b2009-11-10 15:06:42 -08001808 boolean isAllAppsOpaque() {
1809 return mAllAppsGrid.isOpaque();
1810 }
1811
1812 void showAllApps(boolean animated) {
1813 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato7bb17492009-09-24 17:51:01 -07001814 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001815
Joe Onorato3a8820b2009-11-10 15:06:42 -08001816 mWorkspace.startFading(false);
1817
Mike Cleronb6082fa02009-10-19 17:03:36 -07001818 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001819 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001820
Joe Onorato7c312c12009-08-13 21:36:53 -07001821 // TODO: fade these two too
1822 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001823 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001824 }
1825
Joe Onorato7bb17492009-09-24 17:51:01 -07001826 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001827 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001828 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001829 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001830 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001831 mWorkspace.startFading(true);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001832
Joe Onorato7c312c12009-08-13 21:36:53 -07001833 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001834 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001835 mDeleteZone.setVisibility(View.VISIBLE);
1836 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001837 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001838 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001839 }
1840
Joe Onorato7c312c12009-08-13 21:36:53 -07001841 void lockAllApps() {
1842 // TODO
1843 }
1844
1845 void unlockAllApps() {
1846 // TODO
1847 }
1848
Joe Onorato7404ee42009-07-31 11:54:44 -07001849 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 * Displays the shortcut creation dialog and launches, if necessary, the
1851 * appropriate activity.
1852 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001853 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001854 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1855 DialogInterface.OnShowListener {
1856
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001858
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 Dialog createDialog() {
1860 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001863
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1865 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001866 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001867
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 builder.setInverseBackgroundForced(true);
1869
1870 AlertDialog dialog = builder.create();
1871 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001872 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001873 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 return dialog;
1876 }
1877
1878 public void onCancel(DialogInterface dialog) {
1879 mWaitingForResult = false;
1880 cleanup();
1881 }
1882
Romain Guycbb89e42009-06-08 15:52:54 -07001883 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001884 }
1885
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 dismissDialog(DIALOG_CREATE_SHORTCUT);
1888 }
1889
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001890 /**
1891 * Handle the action clicked in the "Add to home" dialog.
1892 */
1893 public void onClick(DialogInterface dialog, int which) {
1894 Resources res = getResources();
1895 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001896
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001897 switch (which) {
1898 case AddAdapter.ITEM_SHORTCUT: {
1899 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001900 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001901 break;
1902 }
Romain Guycbb89e42009-06-08 15:52:54 -07001903
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001904 case AddAdapter.ITEM_APPWIDGET: {
1905 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001906
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001907 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1908 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1909 // add the search widget
1910 ArrayList<AppWidgetProviderInfo> customInfo =
1911 new ArrayList<AppWidgetProviderInfo>();
1912 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1913 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1914 info.label = getString(R.string.group_search);
1915 info.icon = R.drawable.ic_search_widget;
1916 customInfo.add(info);
1917 pickIntent.putParcelableArrayListExtra(
1918 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1919 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1920 Bundle b = new Bundle();
1921 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1922 customExtras.add(b);
1923 pickIntent.putParcelableArrayListExtra(
1924 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1925 // start the pick activity
1926 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1927 break;
1928 }
Romain Guycbb89e42009-06-08 15:52:54 -07001929
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001930 case AddAdapter.ITEM_LIVE_FOLDER: {
1931 // Insert extra item to handle inserting folder
1932 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001933
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001934 ArrayList<String> shortcutNames = new ArrayList<String>();
1935 shortcutNames.add(res.getString(R.string.group_folder));
1936 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001937
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001938 ArrayList<ShortcutIconResource> shortcutIcons =
1939 new ArrayList<ShortcutIconResource>();
1940 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1941 R.drawable.ic_launcher_folder));
1942 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1943
1944 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1945 pickIntent.putExtra(Intent.EXTRA_INTENT,
1946 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1947 pickIntent.putExtra(Intent.EXTRA_TITLE,
1948 getText(R.string.title_select_live_folder));
1949 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001950
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001951 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1952 break;
1953 }
1954
1955 case AddAdapter.ITEM_WALLPAPER: {
1956 startWallpaper();
1957 break;
1958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
1960 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001961
1962 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
1965
1966 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001967 * Receives notifications when applications are added/removed.
1968 */
1969 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1970 @Override
1971 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001972 closeSystemDialogs();
1973 }
1974 }
1975
1976 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001977 * Receives notifications whenever the appwidgets are reset.
1978 */
1979 private class AppWidgetResetObserver extends ContentObserver {
1980 public AppWidgetResetObserver() {
1981 super(new Handler());
1982 }
1983
1984 @Override
1985 public void onChange(boolean selfChange) {
1986 onAppWidgetReset();
1987 }
1988 }
1989
1990 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001991 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001993 public int getCurrentWorkspaceScreen() {
1994 return mWorkspace.getCurrentScreen();
1995 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001996
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997 /**
1998 * Refreshes the shortcuts shown on the workspace.
1999 *
2000 * Implementation of the method from LauncherModel.Callbacks.
2001 */
2002 public void startBinding() {
2003 final Workspace workspace = mWorkspace;
2004 int count = workspace.getChildCount();
2005 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002006 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002007 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2008 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002009
Joe Onorato9c1289c2009-08-17 11:03:03 -04002010 if (DEBUG_USER_INTERFACE) {
2011 android.widget.Button finishButton = new android.widget.Button(this);
2012 finishButton.setText("Finish");
2013 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2014
2015 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2016 public void onClick(View v) {
2017 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002018 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002019 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
2021 }
2022
2023 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002024 * Bind the items start-end from the list.
2025 *
2026 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002028 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2029
2030 final Workspace workspace = mWorkspace;
2031
2032 for (int i=start; i<end; i++) {
2033 final ItemInfo item = shortcuts.get(i);
2034 mDesktopItems.add(item);
2035 switch (item.itemType) {
2036 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2037 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2038 final View shortcut = createShortcut((ApplicationInfo) item);
2039 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2040 false);
2041 break;
2042 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2043 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2044 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2045 (UserFolderInfo) item);
2046 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2047 false);
2048 break;
2049 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2050 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2051 R.layout.live_folder_icon, this,
2052 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2053 (LiveFolderInfo) item);
2054 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2055 false);
2056 break;
2057 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2058 final int screen = workspace.getCurrentScreen();
2059 final View view = mInflater.inflate(R.layout.widget_search,
2060 (ViewGroup) workspace.getChildAt(screen), false);
2061
2062 Search search = (Search) view.findViewById(R.id.widget_search);
2063 search.setLauncher(this);
2064
2065 final Widget widget = (Widget) item;
2066 view.setTag(widget);
2067
2068 workspace.addWidget(view, widget, false);
2069 break;
2070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
2072
Joe Onorato9c1289c2009-08-17 11:03:03 -04002073 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
2075
Joe Onorato9c1289c2009-08-17 11:03:03 -04002076 /**
2077 * Implementation of the method from LauncherModel.Callbacks.
2078 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002079 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2080 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002081 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002083
2084 /**
2085 * Add the views for a widget to the workspace.
2086 *
2087 * Implementation of the method from LauncherModel.Callbacks.
2088 */
2089 public void bindAppWidget(LauncherAppWidgetInfo item) {
2090 final Workspace workspace = mWorkspace;
2091
2092 final int appWidgetId = item.appWidgetId;
2093 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2094 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2095
Joe Onorato9c1289c2009-08-17 11:03:03 -04002096 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2097 item.hostView.setTag(item);
2098
2099 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2100 item.cellY, item.spanX, item.spanY, false);
2101
2102 workspace.requestLayout();
2103
2104 mDesktopItems.add(item);
2105 }
2106
2107 /**
2108 * Callback saying that there aren't any more items to bind.
2109 *
2110 * Implementation of the method from LauncherModel.Callbacks.
2111 */
2112 public void finishBindingItems() {
2113 if (mSavedState != null) {
2114 if (!mWorkspace.hasFocus()) {
2115 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2116 }
2117
2118 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2119 if (userFolders != null) {
2120 for (long folderId : userFolders) {
2121 final FolderInfo info = mFolders.get(folderId);
2122 if (info != null) {
2123 openFolder(info);
2124 }
2125 }
2126 final Folder openFolder = mWorkspace.getOpenFolder();
2127 if (openFolder != null) {
2128 openFolder.requestFocus();
2129 }
2130 }
2131
Joe Onorato9c1289c2009-08-17 11:03:03 -04002132 mSavedState = null;
2133 }
2134
2135 if (mSavedInstanceState != null) {
2136 super.onRestoreInstanceState(mSavedInstanceState);
2137 mSavedInstanceState = null;
2138 }
2139
Joe Onorato9c1289c2009-08-17 11:03:03 -04002140 mWorkspaceLoading = false;
2141 }
2142
2143 /**
2144 * Add the icons for all apps.
2145 *
2146 * Implementation of the method from LauncherModel.Callbacks.
2147 */
2148 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002149 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002150 }
2151
2152 /**
2153 * A package was installed.
2154 *
2155 * Implementation of the method from LauncherModel.Callbacks.
2156 */
2157 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2158 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002159 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002160 }
2161
2162 /**
2163 * A package was updated.
2164 *
2165 * Implementation of the method from LauncherModel.Callbacks.
2166 */
2167 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2168 removeDialog(DIALOG_CREATE_SHORTCUT);
2169 mWorkspace.updateShortcutsForPackage(packageName);
2170 }
2171
2172 /**
2173 * A package was uninstalled.
2174 *
2175 * Implementation of the method from LauncherModel.Callbacks.
2176 */
2177 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2178 removeDialog(DIALOG_CREATE_SHORTCUT);
2179 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002180 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182}