blob: a8235a2814e0aa46d6f4a1d628495bcad5ea931e [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;
Joe Onorato0d44e942009-11-16 18:20:51 -080058import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.view.KeyEvent;
60import android.view.LayoutInflater;
61import android.view.Menu;
62import android.view.MenuItem;
63import android.view.View;
64import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.View.OnLongClickListener;
66import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.widget.TextView;
69import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080070import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080071import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080072import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070073import android.appwidget.AppWidgetManager;
74import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040077import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070078import java.io.DataOutputStream;
79import java.io.FileNotFoundException;
80import java.io.IOException;
81import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082
83/**
84 * Default launcher application.
85 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040086public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080087 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080088 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 static final boolean LOGD = false;
90
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 static final boolean PROFILE_STARTUP = false;
92 static final boolean PROFILE_ROTATE = false;
93 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070094
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 private static final int WALLPAPER_SCREENS_SPAN = 2;
96
97 private static final int MENU_GROUP_ADD = 1;
98 private static final int MENU_ADD = Menu.FIRST + 1;
99 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
100 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
101 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700102 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103
104 private static final int REQUEST_CREATE_SHORTCUT = 1;
105 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700106 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 private static final int REQUEST_PICK_APPLICATION = 6;
108 private static final int REQUEST_PICK_SHORTCUT = 7;
109 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700110 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700111 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
113 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
114
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700115 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
116 static final String SEARCH_WIDGET = "search_widget";
117
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800118 static final int SCREEN_COUNT = 5;
119 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700121 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato7c312c12009-08-13 21:36:53 -0700123 static final int DIALOG_CREATE_SHORTCUT = 1;
124 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Romain Guy98d01652009-06-30 16:21:04 -0700126 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
128 // Type: int
129 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
130 // Type: boolean
131 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
132 // Type: long
133 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
148 // Type: int[]
149 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
150 // Type: boolean
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
152 // Type: long
153 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
154
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800158 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800160 private final BroadcastReceiver mCloseSystemDialogsReceiver
161 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800162 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private LayoutInflater mInflater;
165
Joe Onorato00acb122009-08-04 16:04:30 -0400166 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private AppWidgetManager mAppWidgetManager;
170 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private CellLayout.CellInfo mAddItemCellInfo;
173 private CellLayout.CellInfo mMenuAddInfo;
174 private final int[] mCellCoordinates = new int[2];
175 private FolderInfo mFolderInfo;
176
Joe Onorato7c312c12009-08-13 21:36:53 -0700177 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700179 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private Bundle mSavedState;
182
183 private SpannableStringBuilder mDefaultKeySsb = null;
184
Mike LeBeau736cf282009-07-02 17:46:59 -0700185 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 private boolean mWorkspaceLoading = true;
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private boolean mRestoring;
190 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
192 private Bundle mSavedInstanceState;
193
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 private LauncherModel mModel;
195
Romain Guy84f296c2009-11-04 15:00:44 -0800196 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
197 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700198
Romain Guy1fbc1c82009-11-09 20:43:08 -0800199 private ImageView mPreviousView;
200 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 @Override
203 protected void onCreate(Bundle savedInstanceState) {
204 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700205
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400207 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700209
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800210 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
211 registerReceiver(mCloseSystemDialogsReceiver, filter);
212
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
215 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 if (PROFILE_STARTUP) {
218 android.os.Debug.startMethodTracing("/sdcard/launcher");
219 }
220
221 checkForLocaleChange();
222 setWallpaperDimension();
223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 setContentView(R.layout.launcher);
225 setupViews();
226
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800227 registerContentObservers();
228
Joe Onorato7c312c12009-08-13 21:36:53 -0700229 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 mSavedState = savedInstanceState;
232 restoreState(mSavedState);
233
234 if (PROFILE_STARTUP) {
235 android.os.Debug.stopMethodTracing();
236 }
237
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700238 // We have a new AllAppsView, we need to re-bind everything, and it could have
239 // changed in our absence.
240 mModel.setAllAppsDirty();
241 mModel.setWorkspaceDirty();
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400244 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 }
246
247 // For handling default keys
248 mDefaultKeySsb = new SpannableStringBuilder();
249 Selection.setSelection(mDefaultKeySsb, 0);
250 }
Romain Guycbb89e42009-06-08 15:52:54 -0700251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700253 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
254 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700255
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 final Configuration configuration = getResources().getConfiguration();
257
Romain Guy98d01652009-06-30 16:21:04 -0700258 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 final String locale = configuration.locale.toString();
260
Romain Guy98d01652009-06-30 16:21:04 -0700261 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 final int mcc = configuration.mcc;
263
Romain Guy98d01652009-06-30 16:21:04 -0700264 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 final int mnc = configuration.mnc;
266
Romain Guy84f296c2009-11-04 15:00:44 -0800267 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
Romain Guy84f296c2009-11-04 15:00:44 -0800269 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700270 localeConfiguration.locale = locale;
271 localeConfiguration.mcc = mcc;
272 localeConfiguration.mnc = mnc;
273
274 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700276 }
277 }
278
279 private static class LocaleConfiguration {
280 public String locale;
281 public int mcc = -1;
282 public int mnc = -1;
283 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700284
Romain Guy98d01652009-06-30 16:21:04 -0700285 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
286 DataInputStream in = null;
287 try {
288 in = new DataInputStream(context.openFileInput(PREFERENCES));
289 configuration.locale = in.readUTF();
290 configuration.mcc = in.readInt();
291 configuration.mnc = in.readInt();
292 } catch (FileNotFoundException e) {
293 // Ignore
294 } catch (IOException e) {
295 // Ignore
296 } finally {
297 if (in != null) {
298 try {
299 in.close();
300 } catch (IOException e) {
301 // Ignore
302 }
303 }
304 }
305 }
306
307 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
308 DataOutputStream out = null;
309 try {
310 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
311 out.writeUTF(configuration.locale);
312 out.writeInt(configuration.mcc);
313 out.writeInt(configuration.mnc);
314 out.flush();
315 } catch (FileNotFoundException e) {
316 // Ignore
317 } catch (IOException e) {
318 //noinspection ResultOfMethodCallIgnored
319 context.getFileStreamPath(PREFERENCES).delete();
320 } finally {
321 if (out != null) {
322 try {
323 out.close();
324 } catch (IOException e) {
325 // Ignore
326 }
327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 }
329 }
330
331 static int getScreen() {
332 synchronized (sLock) {
333 return sScreen;
334 }
335 }
336
337 static void setScreen(int screen) {
338 synchronized (sLock) {
339 sScreen = screen;
340 }
341 }
342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700344 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345
346 Display display = getWindowManager().getDefaultDisplay();
347 boolean isPortrait = display.getWidth() < display.getHeight();
348
349 final int width = isPortrait ? display.getWidth() : display.getHeight();
350 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700351 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 }
353
354 @Override
355 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700356 mWaitingForResult = false;
357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 // The pattern used here is that a user PICKs a specific application,
359 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700360
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
362 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700363
Romain Guy94dabf12009-07-21 10:55:43 -0700364 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 switch (requestCode) {
366 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 break;
369 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700370 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 break;
372 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 break;
375 case REQUEST_PICK_LIVE_FOLDER:
376 addLiveFolder(data);
377 break;
378 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700381 case REQUEST_PICK_APPWIDGET:
382 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700384 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400385 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700387 case REQUEST_PICK_WALLPAPER:
388 // We just wanted the activity result here so we can clear mWaitingForResult
389 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
Romain Guy18042c82009-11-06 11:44:55 -0800391 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
392 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
393 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700394 // Clean up the appWidgetId if we canceled
395 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
396 if (appWidgetId != -1) {
397 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 }
401
402 @Override
403 protected void onResume() {
404 super.onResume();
405
406 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400407 mWorkspaceLoading = true;
408 mModel.startLoader(this, true);
409 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700411
Mike LeBeau736cf282009-07-02 17:46:59 -0700412 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
413 // onNewIntent), then close the search dialog if needed, because it probably
414 // came from the user pressing 'home' (rather than, for example, pressing 'back').
415 if (mIsNewIntent) {
416 // Post to a handler so that this happens after the search dialog tries to open
417 // itself again.
418 mWorkspace.post(new Runnable() {
419 public void run() {
420 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
421 ServiceManager.getService(Context.SEARCH_SERVICE));
422 try {
423 searchManagerService.stopSearch();
424 } catch (RemoteException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800425 Log.e(TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700426 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700427 }
428 });
429 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700430
Mike LeBeau736cf282009-07-02 17:46:59 -0700431 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 }
433
434 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435 protected void onPause() {
436 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800437 dismissPreview(mPreviousView);
438 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800439 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700440 }
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700442 @Override
443 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400444 // Flag the loader to stop early before switching
445 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700446
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700447 if (PROFILE_ROTATE) {
448 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
449 }
450 return null;
451 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 private boolean acceptFilter() {
454 final InputMethodManager inputManager = (InputMethodManager)
455 getSystemService(Context.INPUT_METHOD_SERVICE);
456 return !inputManager.isFullscreenMode();
457 }
458
459 @Override
460 public boolean onKeyDown(int keyCode, KeyEvent event) {
461 boolean handled = super.onKeyDown(keyCode, event);
462 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
463 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
464 keyCode, event);
465 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700466 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700467 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700468 // showSearchDialog()
469 // If there are multiple keystrokes before the search dialog takes focus,
470 // onSearchRequested() will be called for every keystroke,
471 // but it is idempotent, so it's fine.
472 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 }
474 }
475
476 return handled;
477 }
478
Karl Rosaen138a0412009-04-23 19:00:21 -0700479 private String getTypedText() {
480 return mDefaultKeySsb.toString();
481 }
482
483 private void clearTypedText() {
484 mDefaultKeySsb.clear();
485 mDefaultKeySsb.clearSpans();
486 Selection.setSelection(mDefaultKeySsb, 0);
487 }
488
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 /**
490 * Restores the previous state, if it exists.
491 *
492 * @param savedState The previous state.
493 */
494 private void restoreState(Bundle savedState) {
495 if (savedState == null) {
496 return;
497 }
498
Joe Onorato3a8820b2009-11-10 15:06:42 -0800499 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
500 if (allApps) {
501 showAllApps(false);
502 }
503
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
505 if (currentScreen > -1) {
506 mWorkspace.setCurrentScreen(currentScreen);
507 }
508
509 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
510 if (addScreen > -1) {
511 mAddItemCellInfo = new CellLayout.CellInfo();
512 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
513 addItemCellInfo.valid = true;
514 addItemCellInfo.screen = addScreen;
515 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
516 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
517 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
518 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
519 addItemCellInfo.findVacantCellsFromOccupied(
520 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
521 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
522 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
523 mRestoring = true;
524 }
525
526 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
527 if (renameFolder) {
528 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400529 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 mRestoring = true;
531 }
532 }
533
534 /**
535 * Finds all the views we need and configure them properly.
536 */
537 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400538 DragController dragController = mDragController;
539
Romain Guyb1b69f52009-08-10 15:10:15 -0700540 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400541 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542
Joe Onorato7c312c12009-08-13 21:36:53 -0700543 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700544 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700545 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700546 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700547 // Manage focusability manually since this thing is always visible
548 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700549
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
551 final Workspace workspace = mWorkspace;
552
Joe Onorato7c312c12009-08-13 21:36:53 -0700553 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
554 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555
Joe Onorato7c312c12009-08-13 21:36:53 -0700556 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700557 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700558 mHandleView.setOnClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800559
560 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
561 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
562
563 Drawable previous = mPreviousView.getDrawable();
564 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800565 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566
Joe Onorato0d44e942009-11-16 18:20:51 -0800567 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800568 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800569 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800570 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800571
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400573 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575
576 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400577 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700578 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579
Joe Onorato00acb122009-08-04 16:04:30 -0400580 dragController.setDragScoller(workspace);
581 dragController.setDragListener(deleteZone);
582 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700583
Joe Onorato00acb122009-08-04 16:04:30 -0400584 // The order here is bottom to top.
585 dragController.addDropTarget(workspace);
586 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 }
588
Romain Guy8a73c512009-11-09 19:19:59 -0800589 @SuppressWarnings({"UnusedDeclaration"})
590 public void previousScreen(View v) {
591 mWorkspace.scrollLeft();
592 }
593
594 @SuppressWarnings({"UnusedDeclaration"})
595 public void nextScreen(View v) {
596 mWorkspace.scrollRight();
597 }
598
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 /**
600 * Creates a view representing a shortcut.
601 *
602 * @param info The data structure describing the shortcut.
603 *
604 * @return A View inflated from R.layout.application.
605 */
606 View createShortcut(ApplicationInfo info) {
607 return createShortcut(R.layout.application,
608 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
609 }
610
611 /**
612 * Creates a view representing a shortcut inflated from the specified resource.
613 *
614 * @param layoutResId The id of the XML layout used to create the shortcut.
615 * @param parent The group the shortcut belongs to.
616 * @param info The data structure describing the shortcut.
617 *
618 * @return A View inflated from layoutResId.
619 */
620 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
621 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
622
Joe Onorato5162ea92009-09-03 09:39:42 -0700623 if (info.icon == null) {
624 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
625 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700627 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 info.filtered = true;
629 }
630
631 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
632 favorite.setText(info.title);
633 favorite.setTag(info);
634 favorite.setOnClickListener(this);
635
636 return favorite;
637 }
638
639 /**
640 * Add an application shortcut to the workspace.
641 *
642 * @param data The intent describing the application.
643 * @param cellInfo The position on screen where to create the shortcut.
644 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400645 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 cellInfo.screen = mWorkspace.getCurrentScreen();
647 if (!findSingleSlot(cellInfo)) return;
648
Romain Guy73b979d2009-06-09 12:57:21 -0700649 final ApplicationInfo info = infoFromApplicationIntent(context, data);
650 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700652 }
653 }
654
655 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 ComponentName component = data.getComponent();
657 PackageManager packageManager = context.getPackageManager();
658 ActivityInfo activityInfo = null;
659 try {
660 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
661 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800662 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
Romain Guycbb89e42009-06-08 15:52:54 -0700664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 if (activityInfo != null) {
666 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 itemInfo.title = activityInfo.loadLabel(packageManager);
669 if (itemInfo.title == null) {
670 itemInfo.title = activityInfo.name;
671 }
Romain Guycbb89e42009-06-08 15:52:54 -0700672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
674 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
675 itemInfo.icon = activityInfo.loadIcon(packageManager);
676 itemInfo.container = ItemInfo.NO_ID;
677
Romain Guy73b979d2009-06-09 12:57:21 -0700678 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
Romain Guy73b979d2009-06-09 12:57:21 -0700680
681 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
Romain Guycbb89e42009-06-08 15:52:54 -0700683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 /**
685 * Add a shortcut to the workspace.
686 *
687 * @param data The intent describing the shortcut.
688 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 cellInfo.screen = mWorkspace.getCurrentScreen();
692 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
695
696 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
699 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 }
701 }
702
Romain Guycbb89e42009-06-08 15:52:54 -0700703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700705 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 * @param data The intent describing the appWidgetId.
708 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700713
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800714 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700715
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700717
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700720 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 // Try finding open space on Launcher screen
723 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800724 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
725 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
726 return;
727 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700729 // Build Launcher-specific widget info and save to database
730 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 launcherInfo.spanX = spans[0];
732 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 LauncherModel.addItemToDatabase(this, launcherInfo,
735 LauncherSettings.Favorites.CONTAINER_DESKTOP,
736 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
737
738 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700740
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700742 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700743
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700744 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700746
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400748 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 }
750 }
Romain Guycbb89e42009-06-08 15:52:54 -0700751
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
753 mDesktopItems.remove(launcherInfo);
754 launcherInfo.hostView = null;
755 }
756
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700757 public LauncherAppWidgetHost getAppWidgetHost() {
758 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
Romain Guycbb89e42009-06-08 15:52:54 -0700760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 static ApplicationInfo addShortcut(Context context, Intent data,
762 CellLayout.CellInfo cellInfo, boolean notify) {
763
Romain Guy73b979d2009-06-09 12:57:21 -0700764 final ApplicationInfo info = infoFromShortcutIntent(context, data);
765 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
766 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
767
768 return info;
769 }
770
771 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
773 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
774 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
775
776 Drawable icon = null;
777 boolean filtered = false;
778 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700779 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780
781 if (bitmap != null) {
782 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
783 filtered = true;
784 customIcon = true;
785 } else {
786 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700787 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700789 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 final PackageManager packageManager = context.getPackageManager();
791 Resources resources = packageManager.getResourcesForApplication(
792 iconResource.packageName);
793 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
794 icon = resources.getDrawable(id);
795 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800796 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 }
798 }
799 }
800
801 if (icon == null) {
802 icon = context.getPackageManager().getDefaultActivityIcon();
803 }
804
805 final ApplicationInfo info = new ApplicationInfo();
806 info.icon = icon;
807 info.filtered = filtered;
808 info.title = name;
809 info.intent = intent;
810 info.customIcon = customIcon;
811 info.iconResource = iconResource;
812
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 return info;
814 }
815
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800816 void closeSystemDialogs() {
817 closeAllApps(false);
818 getWindow().closeAllPanels();
819
820 try {
821 dismissDialog(DIALOG_CREATE_SHORTCUT);
822 // Unlock the workspace if the dialog was showing
823 } catch (Exception e) {
824 // An exception is thrown if the dialog is not visible, which is fine
825 }
826
827 try {
828 dismissDialog(DIALOG_RENAME_FOLDER);
829 // Unlock the workspace if the dialog was showing
830 } catch (Exception e) {
831 // An exception is thrown if the dialog is not visible, which is fine
832 }
833 }
834
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 @Override
836 protected void onNewIntent(Intent intent) {
837 super.onNewIntent(intent);
838
839 // Close the menu
840 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800841 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700842
Joe Onorato742966b2009-10-07 10:26:17 -0700843 // Whatever we were doing is hereby canceled.
844 mWaitingForResult = false;
845
Mike LeBeau736cf282009-07-02 17:46:59 -0700846 // Set this flag so that onResume knows to close the search dialog if it's open,
847 // because this was a new intent (thus a press of 'home' or some such) rather than
848 // for example onResume being called when the user pressed the 'back' button.
849 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850
Joe Onorato3a8820b2009-11-10 15:06:42 -0800851 if (!mWorkspace.isDefaultScreenShowing()) {
852 mWorkspace.moveToDefaultScreen();
853 }
Romain Guy73b979d2009-06-09 12:57:21 -0700854
Joe Onorato3a8820b2009-11-10 15:06:42 -0800855 closeAllApps(false);
Romain Guy1dd3a072009-07-16 13:21:01 -0700856
Joe Onorato3a8820b2009-11-10 15:06:42 -0800857 final View v = getWindow().peekDecorView();
858 if (v != null && v.getWindowToken() != null) {
859 InputMethodManager imm = (InputMethodManager)getSystemService(
860 INPUT_METHOD_SERVICE);
861 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 }
863 }
864 }
865
866 @Override
867 protected void onRestoreInstanceState(Bundle savedInstanceState) {
868 // Do not call super here
869 mSavedInstanceState = savedInstanceState;
870 }
871
872 @Override
873 protected void onSaveInstanceState(Bundle outState) {
874 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
875
876 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
877 if (folders.size() > 0) {
878 final int count = folders.size();
879 long[] ids = new long[count];
880 for (int i = 0; i < count; i++) {
881 final FolderInfo info = folders.get(i).getInfo();
882 ids[i] = info.id;
883 }
884 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
885 } else {
886 super.onSaveInstanceState(outState);
887 }
888
Joe Onoratofb0ca672009-09-14 17:55:46 -0400889 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800890 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700892 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893
894 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
895 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
896 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
897
898 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
899 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
900 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
901 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
902 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
903 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
904 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
905 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
906 layout.getOccupiedCells());
907 }
908
909 if (mFolderInfo != null && mWaitingForResult) {
910 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
911 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
912 }
913 }
914
915 @Override
916 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700918
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700920 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800922 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924
925 TextKeyListener.getInstance().release();
926
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 unbindDesktopItems();
930 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800931
932 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800933
934 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800935 dismissPreview(mNextView);
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800936
937 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
940 @Override
941 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700942 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 super.startActivityForResult(intent, requestCode);
944 }
945
946 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700947 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700949
Joe Onorato7bb17492009-09-24 17:51:01 -0700950 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700951
Karl Rosaen138a0412009-04-23 19:00:21 -0700952 // Slide the search widget to the top, if it's on the current screen,
953 // otherwise show the search dialog immediately.
954 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
955 if (searchWidget == null) {
956 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
957 } else {
958 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
959 // show the currently typed text in the search widget while sliding
960 searchWidget.setQuery(getTypedText());
961 }
962 }
Romain Guycbb89e42009-06-08 15:52:54 -0700963
Karl Rosaen138a0412009-04-23 19:00:21 -0700964 /**
965 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700966 *
967 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700968 */
Romain Guycbb89e42009-06-08 15:52:54 -0700969 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700971
Karl Rosaen138a0412009-04-23 19:00:21 -0700972 if (initialQuery == null) {
973 // Use any text typed in the launcher as the initial query
974 initialQuery = getTypedText();
975 clearTypedText();
976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 if (appSearchData == null) {
978 appSearchData = new Bundle();
979 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
980 }
Romain Guycbb89e42009-06-08 15:52:54 -0700981
Karl Rosaen138a0412009-04-23 19:00:21 -0700982 final SearchManager searchManager =
983 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
984
985 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
986 if (searchWidget != null) {
987 // This gets called when the user leaves the search dialog to go back to
988 // the Launcher.
989 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
990 public void onCancel() {
991 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700992 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700993 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700994 });
995 }
Romain Guycbb89e42009-06-08 15:52:54 -0700996
Karl Rosaen138a0412009-04-23 19:00:21 -0700997 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700998 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
1000
Karl Rosaen138a0412009-04-23 19:00:21 -07001001 /**
1002 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001003 */
Romain Guy5a941392009-04-28 15:18:25 -07001004 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001005 // Close search dialog
1006 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001007 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001008 // Restore search widget to its normal position
1009 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1010 if (searchWidget != null) {
1011 searchWidget.stopSearch(false);
1012 }
1013 }
Romain Guycbb89e42009-06-08 15:52:54 -07001014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 @Override
1016 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017 if (isWorkspaceLocked()) {
1018 return false;
1019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020
1021 super.onCreateOptionsMenu(menu);
1022 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1023 .setIcon(android.R.drawable.ic_menu_add)
1024 .setAlphabeticShortcut('A');
1025 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1026 .setIcon(android.R.drawable.ic_menu_gallery)
1027 .setAlphabeticShortcut('W');
1028 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1029 .setIcon(android.R.drawable.ic_search_category_default)
1030 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1031 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1032 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1033 .setAlphabeticShortcut('N');
1034
1035 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001036 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1037 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038
1039 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1040 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1041 .setIntent(settings);
1042
1043 return true;
1044 }
1045
1046 @Override
1047 public boolean onPrepareOptionsMenu(Menu menu) {
1048 super.onPrepareOptionsMenu(menu);
1049
1050 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1051 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1052
1053 return true;
1054 }
1055
1056 @Override
1057 public boolean onOptionsItemSelected(MenuItem item) {
1058 switch (item.getItemId()) {
1059 case MENU_ADD:
1060 addItems();
1061 return true;
1062 case MENU_WALLPAPER_SETTINGS:
1063 startWallpaper();
1064 return true;
1065 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001066 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 return true;
1068 case MENU_NOTIFICATIONS:
1069 showNotifications();
1070 return true;
1071 }
1072
1073 return super.onOptionsItemSelected(item);
1074 }
Romain Guycbb89e42009-06-08 15:52:54 -07001075
Karl Rosaen138a0412009-04-23 19:00:21 -07001076 /**
1077 * Indicates that we want global search for this activity by setting the globalSearch
1078 * argument for {@link #startSearch} to true.
1079 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001081 @Override
1082 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001083 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001084 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001085 }
1086
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087 public boolean isWorkspaceLocked() {
1088 return mWorkspaceLoading || mWaitingForResult;
1089 }
1090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001092 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 showAddDialog(mMenuAddInfo);
1094 }
1095
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001096 void addAppWidget(Intent data) {
1097 // TODO: catch bad widget exception when sent
1098 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001100 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1101 if (SEARCH_WIDGET.equals(customWidget)) {
1102 // We don't need this any more, since this isn't a real app widget.
1103 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1104 // add the search widget
1105 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001107 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1108
1109 if (appWidget.configure != null) {
1110 // Launch over to configure widget, if needed
1111 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1112 intent.setComponent(appWidget.configure);
1113 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1114
1115 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1116 } else {
1117 // Otherwise just add it
1118 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1119 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 }
1121 }
Romain Guycbb89e42009-06-08 15:52:54 -07001122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 void addSearch() {
1124 final Widget info = Widget.makeSearch();
1125 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001126
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 final int[] xy = mCellCoordinates;
1128 final int spanX = info.spanX;
1129 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1134 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 final View view = mInflater.inflate(info.layoutResource, null);
1137 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001138 Search search = (Search) view.findViewById(R.id.widget_search);
1139 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1142 }
1143
Romain Guy73b979d2009-06-09 12:57:21 -07001144 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001145 // Handle case where user selected "Applications"
1146 String applicationName = getResources().getString(R.string.group_applications);
1147 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001148
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001149 if (applicationName != null && applicationName.equals(shortcutName)) {
1150 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1151 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001152
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001153 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1154 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001155 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001156 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001157 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 }
1160
1161 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001162 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001163 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001164 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001165
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001166 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001167 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001168 } else {
1169 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 }
1172
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 UserFolderInfo folderInfo = new UserFolderInfo();
1175 folderInfo.title = getText(R.string.folder_name);
1176
1177 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1178 cellInfo.screen = mWorkspace.getCurrentScreen();
1179 if (!findSingleSlot(cellInfo)) return;
1180
1181 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001182 LauncherModel.addItemToDatabase(this, folderInfo,
1183 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186
1187 // Create the view
1188 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1189 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1190 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 }
Romain Guycbb89e42009-06-08 15:52:54 -07001193
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 void removeFolder(FolderInfo folder) {
1195 mFolders.remove(folder.id);
1196 }
1197
1198 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 cellInfo.screen = mWorkspace.getCurrentScreen();
1200 if (!findSingleSlot(cellInfo)) return;
1201
1202 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1203
1204 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1206 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1208 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
1210 }
1211
1212 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1213 CellLayout.CellInfo cellInfo, boolean notify) {
1214
1215 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1216 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1217
1218 Drawable icon = null;
1219 boolean filtered = false;
1220 Intent.ShortcutIconResource iconResource = null;
1221
1222 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1223 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1224 try {
1225 iconResource = (Intent.ShortcutIconResource) extra;
1226 final PackageManager packageManager = context.getPackageManager();
1227 Resources resources = packageManager.getResourcesForApplication(
1228 iconResource.packageName);
1229 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1230 icon = resources.getDrawable(id);
1231 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001232 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 }
1234 }
1235
1236 if (icon == null) {
1237 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1238 }
1239
1240 final LiveFolderInfo info = new LiveFolderInfo();
1241 info.icon = icon;
1242 info.filtered = filtered;
1243 info.title = name;
1244 info.iconResource = iconResource;
1245 info.uri = data.getData();
1246 info.baseIntent = baseIntent;
1247 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1248 LiveFolders.DISPLAY_MODE_GRID);
1249
1250 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1251 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001252 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253
1254 return info;
1255 }
1256
1257 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1258 final int[] xy = new int[2];
1259 if (findSlot(cellInfo, xy, 1, 1)) {
1260 cellInfo.cellX = xy[0];
1261 cellInfo.cellY = xy[1];
1262 return true;
1263 }
1264 return false;
1265 }
1266
1267 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1268 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1269 boolean[] occupied = mSavedState != null ?
1270 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1271 cellInfo = mWorkspace.findAllVacantCells(occupied);
1272 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1273 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1274 return false;
1275 }
1276 }
1277 return true;
1278 }
1279
1280 private void showNotifications() {
1281 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1282 if (statusBar != null) {
1283 statusBar.expand();
1284 }
1285 }
1286
1287 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001288 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001290 Intent chooser = Intent.createChooser(pickWallpaper,
1291 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001292 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1293 // Removed in Eclair MR1
1294// WallpaperManager wm = (WallpaperManager)
1295// getSystemService(Context.WALLPAPER_SERVICE);
1296// WallpaperInfo wi = wm.getWallpaperInfo();
1297// if (wi != null && wi.getSettingsActivity() != null) {
1298// LabeledIntent li = new LabeledIntent(getPackageName(),
1299// R.string.configure_wallpaper, 0);
1300// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1301// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1302// }
Mike Clerona0618e42009-10-22 13:55:21 -07001303 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
1305
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001306 /**
1307 * Registers various content observers. The current implementation registers
1308 * only a favorites observer to keep track of the favorites applications.
1309 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001310 private void registerContentObservers() {
1311 ContentResolver resolver = getContentResolver();
1312 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1313 true, mWidgetObserver);
1314 }
1315
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 @Override
1317 public boolean dispatchKeyEvent(KeyEvent event) {
1318 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1319 switch (event.getKeyCode()) {
1320 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 return true;
1322 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001323 return true;
1324 }
1325 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1326 switch (event.getKeyCode()) {
1327 case KeyEvent.KEYCODE_BACK:
1328 if (!event.isCanceled()) {
1329 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001330 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001331 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001332 } else {
1333 closeFolder();
1334 }
Romain Guya6abce82009-11-10 02:54:41 -08001335 dismissPreview(mPreviousView);
1336 dismissPreview(mNextView);
Dianne Hackborn67800862009-07-24 17:15:20 -07001337 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001338 return true;
1339 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 return true;
1341 }
1342 }
1343
1344 return super.dispatchKeyEvent(event);
1345 }
1346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 private void closeFolder() {
1348 Folder folder = mWorkspace.getOpenFolder();
1349 if (folder != null) {
1350 closeFolder(folder);
1351 }
1352 }
1353
1354 void closeFolder(Folder folder) {
1355 folder.getInfo().opened = false;
1356 ViewGroup parent = (ViewGroup) folder.getParent();
1357 if (parent != null) {
1358 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001359 if (folder instanceof DropTarget) {
1360 // Live folders aren't DropTargets.
1361 mDragController.removeDropTarget((DropTarget)folder);
1362 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
1364 folder.onClose();
1365 }
1366
1367 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001368 * Re-listen when widgets are reset.
1369 */
1370 private void onAppWidgetReset() {
1371 mAppWidgetHost.startListening();
1372 }
1373
1374 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1376 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001378 private void unbindDesktopItems() {
1379 for (ItemInfo item: mDesktopItems) {
1380 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 }
1382 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001383
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 /**
1385 * Launches the intent referred by the clicked shortcut.
1386 *
1387 * @param v The view representing the clicked shortcut.
1388 */
1389 public void onClick(View v) {
1390 Object tag = v.getTag();
1391 if (tag instanceof ApplicationInfo) {
1392 // Open shortcut
1393 final Intent intent = ((ApplicationInfo) tag).intent;
1394 startActivitySafely(intent);
1395 } else if (tag instanceof FolderInfo) {
1396 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001397 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001398 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001399 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001400 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001401 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001402 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
1404 }
1405
1406 void startActivitySafely(Intent intent) {
1407 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1408 try {
1409 startActivity(intent);
1410 } catch (ActivityNotFoundException e) {
1411 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1412 } catch (SecurityException e) {
1413 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001414 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1416 "or use the exported attribute for this activity.", e);
1417 }
1418 }
1419
1420 private void handleFolderClick(FolderInfo folderInfo) {
1421 if (!folderInfo.opened) {
1422 // Close any open folder
1423 closeFolder();
1424 // Open the requested folder
1425 openFolder(folderInfo);
1426 } else {
1427 // Find the open folder...
1428 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1429 int folderScreen;
1430 if (openFolder != null) {
1431 folderScreen = mWorkspace.getScreenForView(openFolder);
1432 // .. and close it
1433 closeFolder(openFolder);
1434 if (folderScreen != mWorkspace.getCurrentScreen()) {
1435 // Close any folder open on the current screen
1436 closeFolder();
1437 // Pull the folder onto this screen
1438 openFolder(folderInfo);
1439 }
1440 }
1441 }
1442 }
1443
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 /**
1445 * Opens the user fodler described by the specified tag. The opening of the folder
1446 * is animated relative to the specified View. If the View is null, no animation
1447 * is played.
1448 *
1449 * @param folderInfo The FolderInfo describing the folder to open.
1450 */
1451 private void openFolder(FolderInfo folderInfo) {
1452 Folder openFolder;
1453
1454 if (folderInfo instanceof UserFolderInfo) {
1455 openFolder = UserFolder.fromXml(this);
1456 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001457 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 } else {
1459 return;
1460 }
1461
Joe Onorato00acb122009-08-04 16:04:30 -04001462 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 openFolder.setLauncher(this);
1464
1465 openFolder.bind(folderInfo);
1466 folderInfo.opened = true;
1467
1468 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1469 openFolder.onOpen();
1470 }
1471
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001473 switch (v.getId()) {
1474 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001475 if (!isAllAppsVisible()) {
1476 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1477 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1478 showPreviousPreview(v);
1479 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001480 return true;
1481 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001482 if (!isAllAppsVisible()) {
1483 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1484 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1485 showNextPreview(v);
1486 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001487 return true;
1488 }
1489
Joe Onorato9c1289c2009-08-17 11:03:03 -04001490 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 return false;
1492 }
1493
1494 if (!(v instanceof CellLayout)) {
1495 v = (View) v.getParent();
1496 }
1497
1498 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1499
1500 // This happens when long clicking an item with the dpad/trackball
1501 if (cellInfo == null) {
1502 return true;
1503 }
1504
1505 if (mWorkspace.allowLongPress()) {
1506 if (cellInfo.cell == null) {
1507 if (cellInfo.valid) {
1508 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001509 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 showAddDialog(cellInfo);
1511 }
1512 } else {
1513 if (!(cellInfo.cell instanceof Folder)) {
1514 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001515 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1516 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 mWorkspace.startDrag(cellInfo);
1518 }
1519 }
1520 }
1521 return true;
1522 }
1523
Romain Guye6b8e2f2009-11-10 11:56:55 -08001524 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001525 private void dismissPreview(final View v) {
1526 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001527 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001528 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1529 public void onDismiss() {
1530 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1531 int count = group.getChildCount();
1532 for (int i = 0; i < count; i++) {
1533 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1534 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001535 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1536 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1537
1538 v.setTag(R.id.workspace, null);
1539 v.setTag(R.id.icon, null);
1540 window.setOnDismissListener(null);
1541 }
1542 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001543 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001544 }
1545 v.setTag(null);
1546 }
1547
1548 private void showPreviousPreview(View anchor) {
1549 int current = mWorkspace.getCurrentScreen();
1550 if (current <= 0) return;
1551
Romain Guy9d31e9f2009-11-11 18:54:28 -08001552 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001553 }
1554
1555 private void showNextPreview(View anchor) {
1556 int current = mWorkspace.getCurrentScreen();
1557 if (current >= mWorkspace.getChildCount() - 1) return;
1558
Romain Guy9d31e9f2009-11-11 18:54:28 -08001559 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guya6abce82009-11-10 02:54:41 -08001560 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001561
Romain Guya6abce82009-11-10 02:54:41 -08001562 private void showPreviews(final View anchor, int start, int end) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001563 Resources resources = getResources();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001564
Romain Guya6abce82009-11-10 02:54:41 -08001565 Workspace workspace = mWorkspace;
1566 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001567
1568 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001569
1570 Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001571 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001572 int extraW = (int) ((r.left + r.right) * max);
1573 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001574
1575 int aW = cell.getWidth() - extraW;
1576 float w = aW / max;
1577
1578 int width = cell.getWidth();
1579 int height = cell.getHeight();
1580 int x = cell.getLeftPadding();
1581 int y = cell.getTopPadding();
1582 width -= (x + cell.getRightPadding());
1583 height -= (y + cell.getBottomPadding());
1584
1585 float scale = w / width;
1586
1587 int count = end - start;
1588
1589 final float sWidth = width * scale;
1590 float sHeight = height * scale;
1591
Romain Guye6b8e2f2009-11-10 11:56:55 -08001592 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001593
Romain Guye6b8e2f2009-11-10 11:56:55 -08001594 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1595 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001596
1597 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001598 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001599 cell = (CellLayout) workspace.getChildAt(i);
1600
Romain Guye6b8e2f2009-11-10 11:56:55 -08001601 Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
1602 Bitmap.Config.ARGB_8888);
1603
1604 Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001605 c.scale(scale, scale);
1606 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1607 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001608
Romain Guy9d31e9f2009-11-11 18:54:28 -08001609 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001610 image.setImageBitmap(bitmap);
1611 image.setTag(i);
1612 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001613 image.setOnFocusChangeListener(handler);
1614 image.setFocusable(true);
1615 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001616
1617 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001618 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1619
1620 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001621 }
Romain Guye6b8e2f2009-11-10 11:56:55 -08001622
1623 PopupWindow p = new PopupWindow(this);
1624 p.setContentView(preview);
1625 p.setWidth((int) (sWidth * count + extraW));
1626 p.setHeight((int) (sHeight + extraH));
1627 p.setAnimationStyle(R.style.AnimationPreview);
1628 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001629 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001630 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001631 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001632
Romain Guye6b8e2f2009-11-10 11:56:55 -08001633 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1634 public void onDismiss() {
1635 dismissPreview(anchor);
1636 }
1637 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001638
1639 anchor.setTag(p);
1640 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001641 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001642 }
1643
Romain Guy9d31e9f2009-11-11 18:54:28 -08001644 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001645 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001646
Romain Guye6b8e2f2009-11-10 11:56:55 -08001647 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001648 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001649 }
1650
1651 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001652 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001653 v.post(this);
1654 }
1655
1656 public void run() {
1657 dismissPreview(mAnchor);
1658 }
1659
1660 public void onFocusChange(View v, boolean hasFocus) {
1661 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001662 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001663 }
Romain Guya6abce82009-11-10 02:54:41 -08001664 }
1665 }
1666
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001667 View getDrawerHandle() {
1668 return mHandleView;
1669 }
1670
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 Workspace getWorkspace() {
1672 return mWorkspace;
1673 }
1674
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 @Override
1676 protected Dialog onCreateDialog(int id) {
1677 switch (id) {
1678 case DIALOG_CREATE_SHORTCUT:
1679 return new CreateShortcut().createDialog();
1680 case DIALOG_RENAME_FOLDER:
1681 return new RenameFolder().createDialog();
1682 }
1683
1684 return super.onCreateDialog(id);
1685 }
1686
1687 @Override
1688 protected void onPrepareDialog(int id, Dialog dialog) {
1689 switch (id) {
1690 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 break;
1692 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001693 if (mFolderInfo != null) {
1694 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1695 final CharSequence text = mFolderInfo.title;
1696 input.setText(text);
1697 input.setSelection(0, text.length());
1698 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 break;
1700 }
1701 }
1702
1703 void showRenameDialog(FolderInfo info) {
1704 mFolderInfo = info;
1705 mWaitingForResult = true;
1706 showDialog(DIALOG_RENAME_FOLDER);
1707 }
1708
1709 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1710 mAddItemCellInfo = cellInfo;
1711 mWaitingForResult = true;
1712 showDialog(DIALOG_CREATE_SHORTCUT);
1713 }
1714
Romain Guy73b979d2009-06-09 12:57:21 -07001715 private void pickShortcut(int requestCode, int title) {
1716 Bundle bundle = new Bundle();
1717
1718 ArrayList<String> shortcutNames = new ArrayList<String>();
1719 shortcutNames.add(getString(R.string.group_applications));
1720 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1721
1722 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1723 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1724 R.drawable.ic_launcher_application));
1725 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1726
1727 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1728 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1729 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1730 pickIntent.putExtras(bundle);
1731
1732 startActivityForResult(pickIntent, requestCode);
1733 }
1734
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 private class RenameFolder {
1736 private EditText mInput;
1737
1738 Dialog createDialog() {
1739 mWaitingForResult = true;
1740 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1741 mInput = (EditText) layout.findViewById(R.id.folder_name);
1742
1743 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1744 builder.setIcon(0);
1745 builder.setTitle(getString(R.string.rename_folder_title));
1746 builder.setCancelable(true);
1747 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1748 public void onCancel(DialogInterface dialog) {
1749 cleanup();
1750 }
1751 });
1752 builder.setNegativeButton(getString(R.string.cancel_action),
1753 new Dialog.OnClickListener() {
1754 public void onClick(DialogInterface dialog, int which) {
1755 cleanup();
1756 }
1757 }
1758 );
1759 builder.setPositiveButton(getString(R.string.rename_action),
1760 new Dialog.OnClickListener() {
1761 public void onClick(DialogInterface dialog, int which) {
1762 changeFolderName();
1763 }
1764 }
1765 );
1766 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001767
1768 final AlertDialog dialog = builder.create();
1769 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1770 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001771 mInput.requestFocus();
1772 InputMethodManager inputManager = (InputMethodManager)
1773 getSystemService(Context.INPUT_METHOD_SERVICE);
1774 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001775 }
1776 });
1777
1778 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 }
1780
1781 private void changeFolderName() {
1782 final String name = mInput.getText().toString();
1783 if (!TextUtils.isEmpty(name)) {
1784 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001785 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 mFolderInfo.title = name;
1787 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1788
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001790 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001791 mModel.setWorkspaceDirty();
1792 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 } else {
1794 final FolderIcon folderIcon = (FolderIcon)
1795 mWorkspace.getViewForTag(mFolderInfo);
1796 if (folderIcon != null) {
1797 folderIcon.setText(name);
1798 getWorkspace().requestLayout();
1799 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001800 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001801 mModel.setWorkspaceDirty();
1802 mWorkspaceLoading = true;
1803 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 }
1805 }
1806 }
1807 cleanup();
1808 }
1809
1810 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 dismissDialog(DIALOG_RENAME_FOLDER);
1812 mWaitingForResult = false;
1813 mFolderInfo = null;
1814 }
1815 }
1816
Joe Onoratofb0ca672009-09-14 17:55:46 -04001817 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001818 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001819 }
1820
Joe Onorato3a8820b2009-11-10 15:06:42 -08001821 boolean isAllAppsOpaque() {
1822 return mAllAppsGrid.isOpaque();
1823 }
1824
1825 void showAllApps(boolean animated) {
1826 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato7bb17492009-09-24 17:51:01 -07001827 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001828
Joe Onorato3a8820b2009-11-10 15:06:42 -08001829 mWorkspace.startFading(false);
1830
Mike Cleronb6082fa02009-10-19 17:03:36 -07001831 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001832 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001833
Joe Onorato7c312c12009-08-13 21:36:53 -07001834 // TODO: fade these two too
1835 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001836 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001837 }
1838
Joe Onorato7bb17492009-09-24 17:51:01 -07001839 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001840 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001841 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001842 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001843 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001844 mWorkspace.startFading(true);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001845
Joe Onorato7c312c12009-08-13 21:36:53 -07001846 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001847 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001848 mDeleteZone.setVisibility(View.VISIBLE);
1849 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001850 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001851 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001852 }
1853
Joe Onorato7c312c12009-08-13 21:36:53 -07001854 void lockAllApps() {
1855 // TODO
1856 }
1857
1858 void unlockAllApps() {
1859 // TODO
1860 }
1861
Joe Onorato7404ee42009-07-31 11:54:44 -07001862 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 * Displays the shortcut creation dialog and launches, if necessary, the
1864 * appropriate activity.
1865 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001866 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001867 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1868 DialogInterface.OnShowListener {
1869
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001871
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 Dialog createDialog() {
1873 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001874
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001876
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1878 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001879 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001880
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 builder.setInverseBackgroundForced(true);
1882
1883 AlertDialog dialog = builder.create();
1884 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001885 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001886 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 return dialog;
1889 }
1890
1891 public void onCancel(DialogInterface dialog) {
1892 mWaitingForResult = false;
1893 cleanup();
1894 }
1895
Romain Guycbb89e42009-06-08 15:52:54 -07001896 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001897 }
1898
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 dismissDialog(DIALOG_CREATE_SHORTCUT);
1901 }
1902
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001903 /**
1904 * Handle the action clicked in the "Add to home" dialog.
1905 */
1906 public void onClick(DialogInterface dialog, int which) {
1907 Resources res = getResources();
1908 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001909
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001910 switch (which) {
1911 case AddAdapter.ITEM_SHORTCUT: {
1912 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001913 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001914 break;
1915 }
Romain Guycbb89e42009-06-08 15:52:54 -07001916
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001917 case AddAdapter.ITEM_APPWIDGET: {
1918 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001919
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001920 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1921 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1922 // add the search widget
1923 ArrayList<AppWidgetProviderInfo> customInfo =
1924 new ArrayList<AppWidgetProviderInfo>();
1925 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1926 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1927 info.label = getString(R.string.group_search);
1928 info.icon = R.drawable.ic_search_widget;
1929 customInfo.add(info);
1930 pickIntent.putParcelableArrayListExtra(
1931 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1932 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1933 Bundle b = new Bundle();
1934 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1935 customExtras.add(b);
1936 pickIntent.putParcelableArrayListExtra(
1937 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1938 // start the pick activity
1939 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1940 break;
1941 }
Romain Guycbb89e42009-06-08 15:52:54 -07001942
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001943 case AddAdapter.ITEM_LIVE_FOLDER: {
1944 // Insert extra item to handle inserting folder
1945 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001946
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001947 ArrayList<String> shortcutNames = new ArrayList<String>();
1948 shortcutNames.add(res.getString(R.string.group_folder));
1949 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001950
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001951 ArrayList<ShortcutIconResource> shortcutIcons =
1952 new ArrayList<ShortcutIconResource>();
1953 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1954 R.drawable.ic_launcher_folder));
1955 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1956
1957 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1958 pickIntent.putExtra(Intent.EXTRA_INTENT,
1959 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1960 pickIntent.putExtra(Intent.EXTRA_TITLE,
1961 getText(R.string.title_select_live_folder));
1962 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001963
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001964 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1965 break;
1966 }
1967
1968 case AddAdapter.ITEM_WALLPAPER: {
1969 startWallpaper();
1970 break;
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001974
1975 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
1978
1979 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001980 * Receives notifications when applications are added/removed.
1981 */
1982 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1983 @Override
1984 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001985 closeSystemDialogs();
1986 }
1987 }
1988
1989 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001990 * Receives notifications whenever the appwidgets are reset.
1991 */
1992 private class AppWidgetResetObserver extends ContentObserver {
1993 public AppWidgetResetObserver() {
1994 super(new Handler());
1995 }
1996
1997 @Override
1998 public void onChange(boolean selfChange) {
1999 onAppWidgetReset();
2000 }
2001 }
2002
2003 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002004 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006 public int getCurrentWorkspaceScreen() {
2007 return mWorkspace.getCurrentScreen();
2008 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002009
Joe Onorato9c1289c2009-08-17 11:03:03 -04002010 /**
2011 * Refreshes the shortcuts shown on the workspace.
2012 *
2013 * Implementation of the method from LauncherModel.Callbacks.
2014 */
2015 public void startBinding() {
2016 final Workspace workspace = mWorkspace;
2017 int count = workspace.getChildCount();
2018 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002019 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002020 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2021 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002022
Joe Onorato9c1289c2009-08-17 11:03:03 -04002023 if (DEBUG_USER_INTERFACE) {
2024 android.widget.Button finishButton = new android.widget.Button(this);
2025 finishButton.setText("Finish");
2026 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2027
2028 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2029 public void onClick(View v) {
2030 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002031 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002032 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 }
2034 }
2035
2036 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002037 * Bind the items start-end from the list.
2038 *
2039 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002041 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2042
2043 final Workspace workspace = mWorkspace;
2044
2045 for (int i=start; i<end; i++) {
2046 final ItemInfo item = shortcuts.get(i);
2047 mDesktopItems.add(item);
2048 switch (item.itemType) {
2049 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2050 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2051 final View shortcut = createShortcut((ApplicationInfo) item);
2052 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2053 false);
2054 break;
2055 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2056 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2057 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2058 (UserFolderInfo) item);
2059 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2060 false);
2061 break;
2062 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2063 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2064 R.layout.live_folder_icon, this,
2065 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2066 (LiveFolderInfo) item);
2067 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2068 false);
2069 break;
2070 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2071 final int screen = workspace.getCurrentScreen();
2072 final View view = mInflater.inflate(R.layout.widget_search,
2073 (ViewGroup) workspace.getChildAt(screen), false);
2074
2075 Search search = (Search) view.findViewById(R.id.widget_search);
2076 search.setLauncher(this);
2077
2078 final Widget widget = (Widget) item;
2079 view.setTag(widget);
2080
2081 workspace.addWidget(view, widget, false);
2082 break;
2083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 }
2085
Joe Onorato9c1289c2009-08-17 11:03:03 -04002086 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
2088
Joe Onorato9c1289c2009-08-17 11:03:03 -04002089 /**
2090 * Implementation of the method from LauncherModel.Callbacks.
2091 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002092 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2093 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002094 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002096
2097 /**
2098 * Add the views for a widget to the workspace.
2099 *
2100 * Implementation of the method from LauncherModel.Callbacks.
2101 */
2102 public void bindAppWidget(LauncherAppWidgetInfo item) {
2103 final Workspace workspace = mWorkspace;
2104
2105 final int appWidgetId = item.appWidgetId;
2106 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2107 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2108
Joe Onorato9c1289c2009-08-17 11:03:03 -04002109 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2110 item.hostView.setTag(item);
2111
2112 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2113 item.cellY, item.spanX, item.spanY, false);
2114
2115 workspace.requestLayout();
2116
2117 mDesktopItems.add(item);
2118 }
2119
2120 /**
2121 * Callback saying that there aren't any more items to bind.
2122 *
2123 * Implementation of the method from LauncherModel.Callbacks.
2124 */
2125 public void finishBindingItems() {
2126 if (mSavedState != null) {
2127 if (!mWorkspace.hasFocus()) {
2128 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2129 }
2130
2131 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2132 if (userFolders != null) {
2133 for (long folderId : userFolders) {
2134 final FolderInfo info = mFolders.get(folderId);
2135 if (info != null) {
2136 openFolder(info);
2137 }
2138 }
2139 final Folder openFolder = mWorkspace.getOpenFolder();
2140 if (openFolder != null) {
2141 openFolder.requestFocus();
2142 }
2143 }
2144
Joe Onorato9c1289c2009-08-17 11:03:03 -04002145 mSavedState = null;
2146 }
2147
2148 if (mSavedInstanceState != null) {
2149 super.onRestoreInstanceState(mSavedInstanceState);
2150 mSavedInstanceState = null;
2151 }
2152
Joe Onorato9c1289c2009-08-17 11:03:03 -04002153 mWorkspaceLoading = false;
2154 }
2155
2156 /**
2157 * Add the icons for all apps.
2158 *
2159 * Implementation of the method from LauncherModel.Callbacks.
2160 */
2161 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002162 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002163 }
2164
2165 /**
2166 * A package was installed.
2167 *
2168 * Implementation of the method from LauncherModel.Callbacks.
2169 */
2170 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2171 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002172 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002173 }
2174
2175 /**
2176 * A package was updated.
2177 *
2178 * Implementation of the method from LauncherModel.Callbacks.
2179 */
2180 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2181 removeDialog(DIALOG_CREATE_SHORTCUT);
2182 mWorkspace.updateShortcutsForPackage(packageName);
2183 }
2184
2185 /**
2186 * A package was uninstalled.
2187 *
2188 * Implementation of the method from LauncherModel.Callbacks.
2189 */
2190 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2191 removeDialog(DIALOG_CREATE_SHORTCUT);
2192 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002193 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002194 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195}