blob: 300b3b9180130cc78d1a9778020bd4f1a1808812 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
20import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070022import android.app.ISearchManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.SearchManager;
24import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070025import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080027import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080029import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070033import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.ActivityInfo;
36import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070039import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080042import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080043import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080045import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080047import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.os.Parcelable;
49import android.os.RemoteException;
50import android.os.ServiceManager;
Joe Onoratobe386092009-11-17 17:32:16 -080051import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070052import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.text.Selection;
54import android.text.SpannableStringBuilder;
55import android.text.TextUtils;
56import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070057import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080059import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.view.KeyEvent;
61import android.view.LayoutInflater;
62import android.view.Menu;
63import android.view.MenuItem;
64import android.view.View;
65import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.view.View.OnLongClickListener;
67import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.widget.TextView;
70import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080071import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080072import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080073import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070074import android.appwidget.AppWidgetManager;
75import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040078import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070079import java.io.DataOutputStream;
80import java.io.FileNotFoundException;
81import java.io.IOException;
82import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
84/**
85 * Default launcher application.
86 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040087public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080088 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080089 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 static final boolean LOGD = false;
91
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 static final boolean PROFILE_STARTUP = false;
93 static final boolean PROFILE_ROTATE = false;
94 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070095
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 private static final int WALLPAPER_SCREENS_SPAN = 2;
97
98 private static final int MENU_GROUP_ADD = 1;
99 private static final int MENU_ADD = Menu.FIRST + 1;
100 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
101 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
102 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700103 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104
105 private static final int REQUEST_CREATE_SHORTCUT = 1;
106 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700107 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 private static final int REQUEST_PICK_APPLICATION = 6;
109 private static final int REQUEST_PICK_SHORTCUT = 7;
110 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700111 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700112 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
114 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
115
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700116 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
117 static final String SEARCH_WIDGET = "search_widget";
118
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800119 static final int SCREEN_COUNT = 5;
120 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700122 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato7c312c12009-08-13 21:36:53 -0700124 static final int DIALOG_CREATE_SHORTCUT = 1;
125 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Romain Guy98d01652009-06-30 16:21:04 -0700127 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 // Type: int
130 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
131 // Type: boolean
132 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
133 // Type: long
134 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
149 // Type: int[]
150 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
151 // Type: boolean
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
153 // Type: long
154 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
155
Joe Onorato9c1289c2009-08-17 11:03:03 -0400156 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800159 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800161 private final BroadcastReceiver mCloseSystemDialogsReceiver
162 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800163 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private LayoutInflater mInflater;
166
Joe Onorato00acb122009-08-04 16:04:30 -0400167 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700169
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private AppWidgetManager mAppWidgetManager;
171 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private CellLayout.CellInfo mAddItemCellInfo;
174 private CellLayout.CellInfo mMenuAddInfo;
175 private final int[] mCellCoordinates = new int[2];
176 private FolderInfo mFolderInfo;
177
Joe Onorato7c312c12009-08-13 21:36:53 -0700178 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700179 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700180 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private Bundle mSavedState;
183
184 private SpannableStringBuilder mDefaultKeySsb = null;
185
Mike LeBeau736cf282009-07-02 17:46:59 -0700186 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 private boolean mWorkspaceLoading = true;
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private boolean mRestoring;
191 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
193 private Bundle mSavedInstanceState;
194
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 private LauncherModel mModel;
196
Romain Guy84f296c2009-11-04 15:00:44 -0800197 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
198 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700199
Romain Guy1fbc1c82009-11-09 20:43:08 -0800200 private ImageView mPreviousView;
201 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 @Override
204 protected void onCreate(Bundle savedInstanceState) {
205 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700206
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400208 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800211 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
212 registerReceiver(mCloseSystemDialogsReceiver, filter);
213
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
216 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 if (PROFILE_STARTUP) {
219 android.os.Debug.startMethodTracing("/sdcard/launcher");
220 }
221
222 checkForLocaleChange();
223 setWallpaperDimension();
224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 setContentView(R.layout.launcher);
226 setupViews();
227
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800228 registerContentObservers();
229
Joe Onorato7c312c12009-08-13 21:36:53 -0700230 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 mSavedState = savedInstanceState;
233 restoreState(mSavedState);
234
235 if (PROFILE_STARTUP) {
236 android.os.Debug.stopMethodTracing();
237 }
238
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700239 // We have a new AllAppsView, we need to re-bind everything, and it could have
240 // changed in our absence.
241 mModel.setAllAppsDirty();
242 mModel.setWorkspaceDirty();
243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 }
247
248 // For handling default keys
249 mDefaultKeySsb = new SpannableStringBuilder();
250 Selection.setSelection(mDefaultKeySsb, 0);
251 }
Romain Guycbb89e42009-06-08 15:52:54 -0700252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700254 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
255 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 final Configuration configuration = getResources().getConfiguration();
258
Romain Guy98d01652009-06-30 16:21:04 -0700259 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 final String locale = configuration.locale.toString();
261
Romain Guy98d01652009-06-30 16:21:04 -0700262 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 final int mcc = configuration.mcc;
264
Romain Guy98d01652009-06-30 16:21:04 -0700265 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 final int mnc = configuration.mnc;
267
Romain Guy84f296c2009-11-04 15:00:44 -0800268 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
Romain Guy84f296c2009-11-04 15:00:44 -0800270 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700271 localeConfiguration.locale = locale;
272 localeConfiguration.mcc = mcc;
273 localeConfiguration.mnc = mnc;
274
275 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700277 }
278 }
279
280 private static class LocaleConfiguration {
281 public String locale;
282 public int mcc = -1;
283 public int mnc = -1;
284 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700285
Romain Guy98d01652009-06-30 16:21:04 -0700286 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
287 DataInputStream in = null;
288 try {
289 in = new DataInputStream(context.openFileInput(PREFERENCES));
290 configuration.locale = in.readUTF();
291 configuration.mcc = in.readInt();
292 configuration.mnc = in.readInt();
293 } catch (FileNotFoundException e) {
294 // Ignore
295 } catch (IOException e) {
296 // Ignore
297 } finally {
298 if (in != null) {
299 try {
300 in.close();
301 } catch (IOException e) {
302 // Ignore
303 }
304 }
305 }
306 }
307
308 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
309 DataOutputStream out = null;
310 try {
311 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
312 out.writeUTF(configuration.locale);
313 out.writeInt(configuration.mcc);
314 out.writeInt(configuration.mnc);
315 out.flush();
316 } catch (FileNotFoundException e) {
317 // Ignore
318 } catch (IOException e) {
319 //noinspection ResultOfMethodCallIgnored
320 context.getFileStreamPath(PREFERENCES).delete();
321 } finally {
322 if (out != null) {
323 try {
324 out.close();
325 } catch (IOException e) {
326 // Ignore
327 }
328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 }
330 }
331
332 static int getScreen() {
333 synchronized (sLock) {
334 return sScreen;
335 }
336 }
337
338 static void setScreen(int screen) {
339 synchronized (sLock) {
340 sScreen = screen;
341 }
342 }
343
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700345 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346
347 Display display = getWindowManager().getDefaultDisplay();
348 boolean isPortrait = display.getWidth() < display.getHeight();
349
350 final int width = isPortrait ? display.getWidth() : display.getHeight();
351 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700352 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 }
354
355 @Override
356 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700357 mWaitingForResult = false;
358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 // The pattern used here is that a user PICKs a specific application,
360 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700361
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
363 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700364
Romain Guy94dabf12009-07-21 10:55:43 -0700365 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 switch (requestCode) {
367 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 break;
370 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700371 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 break;
373 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400374 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 break;
376 case REQUEST_PICK_LIVE_FOLDER:
377 addLiveFolder(data);
378 break;
379 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 case REQUEST_PICK_APPWIDGET:
383 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700385 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400386 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700388 case REQUEST_PICK_WALLPAPER:
389 // We just wanted the activity result here so we can clear mWaitingForResult
390 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 }
Romain Guy18042c82009-11-06 11:44:55 -0800392 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
393 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
394 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700395 // Clean up the appWidgetId if we canceled
396 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
397 if (appWidgetId != -1) {
398 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 }
400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
402
403 @Override
404 protected void onResume() {
405 super.onResume();
406
407 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400408 mWorkspaceLoading = true;
409 mModel.startLoader(this, true);
410 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700412
Mike LeBeau736cf282009-07-02 17:46:59 -0700413 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
414 // onNewIntent), then close the search dialog if needed, because it probably
415 // came from the user pressing 'home' (rather than, for example, pressing 'back').
416 if (mIsNewIntent) {
417 // Post to a handler so that this happens after the search dialog tries to open
418 // itself again.
419 mWorkspace.post(new Runnable() {
420 public void run() {
421 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
422 ServiceManager.getService(Context.SEARCH_SERVICE));
423 try {
424 searchManagerService.stopSearch();
425 } catch (RemoteException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800426 Log.e(TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700427 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700428 }
429 });
430 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700431
Mike LeBeau736cf282009-07-02 17:46:59 -0700432 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 }
434
435 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700436 protected void onPause() {
437 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800438 dismissPreview(mPreviousView);
439 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800440 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700441 }
Romain Guycbb89e42009-06-08 15:52:54 -0700442
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700443 @Override
444 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400445 // Flag the loader to stop early before switching
446 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700447
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700448 if (PROFILE_ROTATE) {
449 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
450 }
451 return null;
452 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 private boolean acceptFilter() {
455 final InputMethodManager inputManager = (InputMethodManager)
456 getSystemService(Context.INPUT_METHOD_SERVICE);
457 return !inputManager.isFullscreenMode();
458 }
459
460 @Override
461 public boolean onKeyDown(int keyCode, KeyEvent event) {
462 boolean handled = super.onKeyDown(keyCode, event);
463 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
464 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
465 keyCode, event);
466 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700467 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700468 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700469 // showSearchDialog()
470 // If there are multiple keystrokes before the search dialog takes focus,
471 // onSearchRequested() will be called for every keystroke,
472 // but it is idempotent, so it's fine.
473 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
475 }
476
477 return handled;
478 }
479
Karl Rosaen138a0412009-04-23 19:00:21 -0700480 private String getTypedText() {
481 return mDefaultKeySsb.toString();
482 }
483
484 private void clearTypedText() {
485 mDefaultKeySsb.clear();
486 mDefaultKeySsb.clearSpans();
487 Selection.setSelection(mDefaultKeySsb, 0);
488 }
489
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 /**
491 * Restores the previous state, if it exists.
492 *
493 * @param savedState The previous state.
494 */
495 private void restoreState(Bundle savedState) {
496 if (savedState == null) {
497 return;
498 }
499
Joe Onorato3a8820b2009-11-10 15:06:42 -0800500 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
501 if (allApps) {
502 showAllApps(false);
503 }
504
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
506 if (currentScreen > -1) {
507 mWorkspace.setCurrentScreen(currentScreen);
508 }
509
510 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
511 if (addScreen > -1) {
512 mAddItemCellInfo = new CellLayout.CellInfo();
513 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
514 addItemCellInfo.valid = true;
515 addItemCellInfo.screen = addScreen;
516 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
517 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
518 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
519 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
520 addItemCellInfo.findVacantCellsFromOccupied(
521 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
522 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
523 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
524 mRestoring = true;
525 }
526
527 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
528 if (renameFolder) {
529 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400530 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 mRestoring = true;
532 }
533 }
534
535 /**
536 * Finds all the views we need and configure them properly.
537 */
538 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400539 DragController dragController = mDragController;
540
Romain Guyb1b69f52009-08-10 15:10:15 -0700541 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400542 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543
Joe Onorato7c312c12009-08-13 21:36:53 -0700544 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700545 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700546 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700547 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700548 // Manage focusability manually since this thing is always visible
549 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700550
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
552 final Workspace workspace = mWorkspace;
553
Joe Onorato7c312c12009-08-13 21:36:53 -0700554 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
555 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556
Joe Onorato7c312c12009-08-13 21:36:53 -0700557 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700558 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700559 mHandleView.setOnClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800560
561 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
562 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
563
564 Drawable previous = mPreviousView.getDrawable();
565 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800566 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567
Joe Onorato0d44e942009-11-16 18:20:51 -0800568 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800569 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800570 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800571 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800572
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400574 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576
577 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400578 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700579 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580
Joe Onorato00acb122009-08-04 16:04:30 -0400581 dragController.setDragScoller(workspace);
582 dragController.setDragListener(deleteZone);
583 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700584
Joe Onorato00acb122009-08-04 16:04:30 -0400585 // The order here is bottom to top.
586 dragController.addDropTarget(workspace);
587 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 }
589
Romain Guy8a73c512009-11-09 19:19:59 -0800590 @SuppressWarnings({"UnusedDeclaration"})
591 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800592 if (!isAllAppsVisible()) {
593 mWorkspace.scrollLeft();
594 }
Romain Guy8a73c512009-11-09 19:19:59 -0800595 }
596
597 @SuppressWarnings({"UnusedDeclaration"})
598 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800599 if (!isAllAppsVisible()) {
600 mWorkspace.scrollRight();
601 }
Romain Guy8a73c512009-11-09 19:19:59 -0800602 }
603
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 /**
605 * Creates a view representing a shortcut.
606 *
607 * @param info The data structure describing the shortcut.
608 *
609 * @return A View inflated from R.layout.application.
610 */
611 View createShortcut(ApplicationInfo info) {
612 return createShortcut(R.layout.application,
613 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
614 }
615
616 /**
617 * Creates a view representing a shortcut inflated from the specified resource.
618 *
619 * @param layoutResId The id of the XML layout used to create the shortcut.
620 * @param parent The group the shortcut belongs to.
621 * @param info The data structure describing the shortcut.
622 *
623 * @return A View inflated from layoutResId.
624 */
625 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
626 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
627
Joe Onorato5162ea92009-09-03 09:39:42 -0700628 if (info.icon == null) {
629 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
630 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700632 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 info.filtered = true;
634 }
635
636 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
637 favorite.setText(info.title);
638 favorite.setTag(info);
639 favorite.setOnClickListener(this);
640
641 return favorite;
642 }
643
644 /**
645 * Add an application shortcut to the workspace.
646 *
647 * @param data The intent describing the application.
648 * @param cellInfo The position on screen where to create the shortcut.
649 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 cellInfo.screen = mWorkspace.getCurrentScreen();
652 if (!findSingleSlot(cellInfo)) return;
653
Romain Guy73b979d2009-06-09 12:57:21 -0700654 final ApplicationInfo info = infoFromApplicationIntent(context, data);
655 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400656 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700657 }
658 }
659
660 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 ComponentName component = data.getComponent();
662 PackageManager packageManager = context.getPackageManager();
663 ActivityInfo activityInfo = null;
664 try {
665 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
666 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800667 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
Romain Guycbb89e42009-06-08 15:52:54 -0700669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 if (activityInfo != null) {
671 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 itemInfo.title = activityInfo.loadLabel(packageManager);
674 if (itemInfo.title == null) {
675 itemInfo.title = activityInfo.name;
676 }
Romain Guycbb89e42009-06-08 15:52:54 -0700677
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
679 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
680 itemInfo.icon = activityInfo.loadIcon(packageManager);
681 itemInfo.container = ItemInfo.NO_ID;
682
Romain Guy73b979d2009-06-09 12:57:21 -0700683 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
Romain Guy73b979d2009-06-09 12:57:21 -0700685
686 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 }
Romain Guycbb89e42009-06-08 15:52:54 -0700688
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 /**
690 * Add a shortcut to the workspace.
691 *
692 * @param data The intent describing the shortcut.
693 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400695 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 cellInfo.screen = mWorkspace.getCurrentScreen();
697 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700698
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
700
701 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
704 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 }
706 }
707
Romain Guycbb89e42009-06-08 15:52:54 -0700708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700710 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 * @param data The intent describing the appWidgetId.
713 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400715 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700718
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800719 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700720
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700721 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700722
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700723 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700725 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700726
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 // Try finding open space on Launcher screen
728 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800729 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
730 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
731 return;
732 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700734 // Build Launcher-specific widget info and save to database
735 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 launcherInfo.spanX = spans[0];
737 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700738
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 LauncherModel.addItemToDatabase(this, launcherInfo,
740 LauncherSettings.Favorites.CONTAINER_DESKTOP,
741 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
742
743 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700745
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700747 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700748
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700749 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700751
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 }
755 }
Romain Guycbb89e42009-06-08 15:52:54 -0700756
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
758 mDesktopItems.remove(launcherInfo);
759 launcherInfo.hostView = null;
760 }
761
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700762 public LauncherAppWidgetHost getAppWidgetHost() {
763 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 }
Romain Guycbb89e42009-06-08 15:52:54 -0700765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 static ApplicationInfo addShortcut(Context context, Intent data,
767 CellLayout.CellInfo cellInfo, boolean notify) {
768
Romain Guy73b979d2009-06-09 12:57:21 -0700769 final ApplicationInfo info = infoFromShortcutIntent(context, data);
770 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
771 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
772
773 return info;
774 }
775
776 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
778 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
779 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
780
781 Drawable icon = null;
782 boolean filtered = false;
783 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700784 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785
786 if (bitmap != null) {
787 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
788 filtered = true;
789 customIcon = true;
790 } else {
791 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700792 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700794 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 final PackageManager packageManager = context.getPackageManager();
796 Resources resources = packageManager.getResourcesForApplication(
797 iconResource.packageName);
798 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
799 icon = resources.getDrawable(id);
800 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800801 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 }
803 }
804 }
805
806 if (icon == null) {
807 icon = context.getPackageManager().getDefaultActivityIcon();
808 }
809
810 final ApplicationInfo info = new ApplicationInfo();
811 info.icon = icon;
812 info.filtered = filtered;
813 info.title = name;
814 info.intent = intent;
815 info.customIcon = customIcon;
816 info.iconResource = iconResource;
817
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 return info;
819 }
820
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800821 void closeSystemDialogs() {
822 closeAllApps(false);
823 getWindow().closeAllPanels();
824
825 try {
826 dismissDialog(DIALOG_CREATE_SHORTCUT);
827 // Unlock the workspace if the dialog was showing
828 } catch (Exception e) {
829 // An exception is thrown if the dialog is not visible, which is fine
830 }
831
832 try {
833 dismissDialog(DIALOG_RENAME_FOLDER);
834 // Unlock the workspace if the dialog was showing
835 } catch (Exception e) {
836 // An exception is thrown if the dialog is not visible, which is fine
837 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800838
839 // Whatever we were doing is hereby canceled.
840 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800841 }
842
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 @Override
844 protected void onNewIntent(Intent intent) {
845 super.onNewIntent(intent);
846
847 // Close the menu
848 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800849 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800850 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700851
Mike LeBeau736cf282009-07-02 17:46:59 -0700852 // Set this flag so that onResume knows to close the search dialog if it's open,
853 // because this was a new intent (thus a press of 'home' or some such) rather than
854 // for example onResume being called when the user pressed the 'back' button.
855 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856
Joe Onorato3a8820b2009-11-10 15:06:42 -0800857 if (!mWorkspace.isDefaultScreenShowing()) {
858 mWorkspace.moveToDefaultScreen();
859 }
Romain Guy73b979d2009-06-09 12:57:21 -0700860
Joe Onorato3a8820b2009-11-10 15:06:42 -0800861 closeAllApps(false);
Romain Guy1dd3a072009-07-16 13:21:01 -0700862
Joe Onorato3a8820b2009-11-10 15:06:42 -0800863 final View v = getWindow().peekDecorView();
864 if (v != null && v.getWindowToken() != null) {
865 InputMethodManager imm = (InputMethodManager)getSystemService(
866 INPUT_METHOD_SERVICE);
867 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 }
869 }
870 }
871
872 @Override
873 protected void onRestoreInstanceState(Bundle savedInstanceState) {
874 // Do not call super here
875 mSavedInstanceState = savedInstanceState;
876 }
877
878 @Override
879 protected void onSaveInstanceState(Bundle outState) {
880 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
881
882 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
883 if (folders.size() > 0) {
884 final int count = folders.size();
885 long[] ids = new long[count];
886 for (int i = 0; i < count; i++) {
887 final FolderInfo info = folders.get(i).getInfo();
888 ids[i] = info.id;
889 }
890 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
891 } else {
892 super.onSaveInstanceState(outState);
893 }
894
Joe Onoratofb0ca672009-09-14 17:55:46 -0400895 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800896 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899
900 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
901 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
902 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
903
904 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
905 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
906 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
907 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
908 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
909 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
910 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
911 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
912 layout.getOccupiedCells());
913 }
914
915 if (mFolderInfo != null && mWaitingForResult) {
916 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
917 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
918 }
919 }
920
921 @Override
922 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700924
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700926 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800928 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 }
930
931 TextKeyListener.getInstance().release();
932
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934
Joe Onorato9c1289c2009-08-17 11:03:03 -0400935 unbindDesktopItems();
936 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800937
938 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800939
940 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800941 dismissPreview(mNextView);
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800942
943 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945
946 @Override
947 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700948 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 super.startActivityForResult(intent, requestCode);
950 }
951
952 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700953 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700955
Joe Onorato7bb17492009-09-24 17:51:01 -0700956 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700957
Karl Rosaen138a0412009-04-23 19:00:21 -0700958 // Slide the search widget to the top, if it's on the current screen,
959 // otherwise show the search dialog immediately.
960 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
961 if (searchWidget == null) {
962 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
963 } else {
964 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
965 // show the currently typed text in the search widget while sliding
966 searchWidget.setQuery(getTypedText());
967 }
968 }
Romain Guycbb89e42009-06-08 15:52:54 -0700969
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 /**
971 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700972 *
973 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700974 */
Romain Guycbb89e42009-06-08 15:52:54 -0700975 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700976 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700977
Karl Rosaen138a0412009-04-23 19:00:21 -0700978 if (initialQuery == null) {
979 // Use any text typed in the launcher as the initial query
980 initialQuery = getTypedText();
981 clearTypedText();
982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 if (appSearchData == null) {
984 appSearchData = new Bundle();
985 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
986 }
Romain Guycbb89e42009-06-08 15:52:54 -0700987
Karl Rosaen138a0412009-04-23 19:00:21 -0700988 final SearchManager searchManager =
989 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
990
991 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
992 if (searchWidget != null) {
993 // This gets called when the user leaves the search dialog to go back to
994 // the Launcher.
995 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
996 public void onCancel() {
997 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700998 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700999 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001000 });
1001 }
Romain Guycbb89e42009-06-08 15:52:54 -07001002
Karl Rosaen138a0412009-04-23 19:00:21 -07001003 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001004 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
1006
Karl Rosaen138a0412009-04-23 19:00:21 -07001007 /**
1008 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001009 */
Romain Guy5a941392009-04-28 15:18:25 -07001010 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001011 // Close search dialog
1012 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001013 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001014 // Restore search widget to its normal position
1015 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1016 if (searchWidget != null) {
1017 searchWidget.stopSearch(false);
1018 }
1019 }
Romain Guycbb89e42009-06-08 15:52:54 -07001020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 @Override
1022 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 if (isWorkspaceLocked()) {
1024 return false;
1025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026
1027 super.onCreateOptionsMenu(menu);
1028 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1029 .setIcon(android.R.drawable.ic_menu_add)
1030 .setAlphabeticShortcut('A');
1031 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1032 .setIcon(android.R.drawable.ic_menu_gallery)
1033 .setAlphabeticShortcut('W');
1034 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1035 .setIcon(android.R.drawable.ic_search_category_default)
1036 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1037 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1038 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1039 .setAlphabeticShortcut('N');
1040
1041 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001042 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1043 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044
1045 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1046 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1047 .setIntent(settings);
1048
1049 return true;
1050 }
1051
1052 @Override
1053 public boolean onPrepareOptionsMenu(Menu menu) {
1054 super.onPrepareOptionsMenu(menu);
1055
1056 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1057 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1058
1059 return true;
1060 }
1061
1062 @Override
1063 public boolean onOptionsItemSelected(MenuItem item) {
1064 switch (item.getItemId()) {
1065 case MENU_ADD:
1066 addItems();
1067 return true;
1068 case MENU_WALLPAPER_SETTINGS:
1069 startWallpaper();
1070 return true;
1071 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001072 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 return true;
1074 case MENU_NOTIFICATIONS:
1075 showNotifications();
1076 return true;
1077 }
1078
1079 return super.onOptionsItemSelected(item);
1080 }
Romain Guycbb89e42009-06-08 15:52:54 -07001081
Karl Rosaen138a0412009-04-23 19:00:21 -07001082 /**
1083 * Indicates that we want global search for this activity by setting the globalSearch
1084 * argument for {@link #startSearch} to true.
1085 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001087 @Override
1088 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001089 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001090 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001091 }
1092
Joe Onorato9c1289c2009-08-17 11:03:03 -04001093 public boolean isWorkspaceLocked() {
1094 return mWorkspaceLoading || mWaitingForResult;
1095 }
1096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001098 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 showAddDialog(mMenuAddInfo);
1100 }
1101
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001102 void addAppWidget(Intent data) {
1103 // TODO: catch bad widget exception when sent
1104 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001106 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1107 if (SEARCH_WIDGET.equals(customWidget)) {
1108 // We don't need this any more, since this isn't a real app widget.
1109 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1110 // add the search widget
1111 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001113 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1114
1115 if (appWidget.configure != null) {
1116 // Launch over to configure widget, if needed
1117 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1118 intent.setComponent(appWidget.configure);
1119 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1120
1121 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1122 } else {
1123 // Otherwise just add it
1124 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 }
1127 }
Romain Guycbb89e42009-06-08 15:52:54 -07001128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 void addSearch() {
1130 final Widget info = Widget.makeSearch();
1131 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 final int[] xy = mCellCoordinates;
1134 final int spanX = info.spanX;
1135 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1140 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 final View view = mInflater.inflate(info.layoutResource, null);
1143 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001144 Search search = (Search) view.findViewById(R.id.widget_search);
1145 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1148 }
1149
Romain Guy73b979d2009-06-09 12:57:21 -07001150 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001151 // Handle case where user selected "Applications"
1152 String applicationName = getResources().getString(R.string.group_applications);
1153 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001154
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001155 if (applicationName != null && applicationName.equals(shortcutName)) {
1156 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1157 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001158
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001159 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1160 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001161 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001162 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001163 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001164 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 }
1166
1167 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001168 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001169 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001170 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001171
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001172 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001174 } else {
1175 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 }
1178
Joe Onorato9c1289c2009-08-17 11:03:03 -04001179 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 UserFolderInfo folderInfo = new UserFolderInfo();
1181 folderInfo.title = getText(R.string.folder_name);
1182
1183 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1184 cellInfo.screen = mWorkspace.getCurrentScreen();
1185 if (!findSingleSlot(cellInfo)) return;
1186
1187 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 LauncherModel.addItemToDatabase(this, folderInfo,
1189 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192
1193 // Create the view
1194 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1195 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1196 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001197 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 }
Romain Guycbb89e42009-06-08 15:52:54 -07001199
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 void removeFolder(FolderInfo folder) {
1201 mFolders.remove(folder.id);
1202 }
1203
1204 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 cellInfo.screen = mWorkspace.getCurrentScreen();
1206 if (!findSingleSlot(cellInfo)) return;
1207
1208 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1209
1210 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1212 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001213 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1214 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
1216 }
1217
1218 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1219 CellLayout.CellInfo cellInfo, boolean notify) {
1220
1221 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1222 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1223
1224 Drawable icon = null;
1225 boolean filtered = false;
1226 Intent.ShortcutIconResource iconResource = null;
1227
1228 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1229 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1230 try {
1231 iconResource = (Intent.ShortcutIconResource) extra;
1232 final PackageManager packageManager = context.getPackageManager();
1233 Resources resources = packageManager.getResourcesForApplication(
1234 iconResource.packageName);
1235 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1236 icon = resources.getDrawable(id);
1237 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001238 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 }
1240 }
1241
1242 if (icon == null) {
1243 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1244 }
1245
1246 final LiveFolderInfo info = new LiveFolderInfo();
1247 info.icon = icon;
1248 info.filtered = filtered;
1249 info.title = name;
1250 info.iconResource = iconResource;
1251 info.uri = data.getData();
1252 info.baseIntent = baseIntent;
1253 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1254 LiveFolders.DISPLAY_MODE_GRID);
1255
1256 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1257 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001258 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259
1260 return info;
1261 }
1262
1263 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1264 final int[] xy = new int[2];
1265 if (findSlot(cellInfo, xy, 1, 1)) {
1266 cellInfo.cellX = xy[0];
1267 cellInfo.cellY = xy[1];
1268 return true;
1269 }
1270 return false;
1271 }
1272
1273 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1274 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1275 boolean[] occupied = mSavedState != null ?
1276 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1277 cellInfo = mWorkspace.findAllVacantCells(occupied);
1278 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1279 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1280 return false;
1281 }
1282 }
1283 return true;
1284 }
1285
1286 private void showNotifications() {
1287 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1288 if (statusBar != null) {
1289 statusBar.expand();
1290 }
1291 }
1292
1293 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001294 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001296 Intent chooser = Intent.createChooser(pickWallpaper,
1297 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001298 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1299 // Removed in Eclair MR1
1300// WallpaperManager wm = (WallpaperManager)
1301// getSystemService(Context.WALLPAPER_SERVICE);
1302// WallpaperInfo wi = wm.getWallpaperInfo();
1303// if (wi != null && wi.getSettingsActivity() != null) {
1304// LabeledIntent li = new LabeledIntent(getPackageName(),
1305// R.string.configure_wallpaper, 0);
1306// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1307// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1308// }
Mike Clerona0618e42009-10-22 13:55:21 -07001309 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
1311
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001312 /**
1313 * Registers various content observers. The current implementation registers
1314 * only a favorites observer to keep track of the favorites applications.
1315 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001316 private void registerContentObservers() {
1317 ContentResolver resolver = getContentResolver();
1318 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1319 true, mWidgetObserver);
1320 }
1321
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 @Override
1323 public boolean dispatchKeyEvent(KeyEvent event) {
1324 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1325 switch (event.getKeyCode()) {
1326 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 return true;
1328 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001329 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001330 case KeyEvent.KEYCODE_VOLUME_DOWN:
1331 if (SystemProperties.getInt("launcher2.dumpstate", 0) != 0) {
1332 dumpState();
1333 return true;
1334 }
1335 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001336 }
1337 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1338 switch (event.getKeyCode()) {
1339 case KeyEvent.KEYCODE_BACK:
Joe Onorato247226b2009-11-18 17:43:22 -08001340 if (event.isTracking() && !event.isCanceled()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001341 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001342 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001343 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001344 } else {
1345 closeFolder();
1346 }
Romain Guya6abce82009-11-10 02:54:41 -08001347 dismissPreview(mPreviousView);
1348 dismissPreview(mNextView);
Dianne Hackborn67800862009-07-24 17:15:20 -07001349 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001350 return true;
1351 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 return true;
1353 }
1354 }
1355
1356 return super.dispatchKeyEvent(event);
1357 }
1358
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 private void closeFolder() {
1360 Folder folder = mWorkspace.getOpenFolder();
1361 if (folder != null) {
1362 closeFolder(folder);
1363 }
1364 }
1365
1366 void closeFolder(Folder folder) {
1367 folder.getInfo().opened = false;
1368 ViewGroup parent = (ViewGroup) folder.getParent();
1369 if (parent != null) {
1370 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001371 if (folder instanceof DropTarget) {
1372 // Live folders aren't DropTargets.
1373 mDragController.removeDropTarget((DropTarget)folder);
1374 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 }
1376 folder.onClose();
1377 }
1378
1379 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001380 * Re-listen when widgets are reset.
1381 */
1382 private void onAppWidgetReset() {
1383 mAppWidgetHost.startListening();
1384 }
1385
1386 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001387 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1388 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001390 private void unbindDesktopItems() {
1391 for (ItemInfo item: mDesktopItems) {
1392 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
1394 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 /**
1397 * Launches the intent referred by the clicked shortcut.
1398 *
1399 * @param v The view representing the clicked shortcut.
1400 */
1401 public void onClick(View v) {
1402 Object tag = v.getTag();
1403 if (tag instanceof ApplicationInfo) {
1404 // Open shortcut
1405 final Intent intent = ((ApplicationInfo) tag).intent;
1406 startActivitySafely(intent);
1407 } else if (tag instanceof FolderInfo) {
1408 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001409 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001410 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001411 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001412 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001413 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001414 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 }
1416 }
1417
1418 void startActivitySafely(Intent intent) {
1419 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1420 try {
1421 startActivity(intent);
1422 } catch (ActivityNotFoundException e) {
1423 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1424 } catch (SecurityException e) {
1425 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001426 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1428 "or use the exported attribute for this activity.", e);
1429 }
1430 }
1431
1432 private void handleFolderClick(FolderInfo folderInfo) {
1433 if (!folderInfo.opened) {
1434 // Close any open folder
1435 closeFolder();
1436 // Open the requested folder
1437 openFolder(folderInfo);
1438 } else {
1439 // Find the open folder...
1440 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1441 int folderScreen;
1442 if (openFolder != null) {
1443 folderScreen = mWorkspace.getScreenForView(openFolder);
1444 // .. and close it
1445 closeFolder(openFolder);
1446 if (folderScreen != mWorkspace.getCurrentScreen()) {
1447 // Close any folder open on the current screen
1448 closeFolder();
1449 // Pull the folder onto this screen
1450 openFolder(folderInfo);
1451 }
1452 }
1453 }
1454 }
1455
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 /**
1457 * Opens the user fodler described by the specified tag. The opening of the folder
1458 * is animated relative to the specified View. If the View is null, no animation
1459 * is played.
1460 *
1461 * @param folderInfo The FolderInfo describing the folder to open.
1462 */
1463 private void openFolder(FolderInfo folderInfo) {
1464 Folder openFolder;
1465
1466 if (folderInfo instanceof UserFolderInfo) {
1467 openFolder = UserFolder.fromXml(this);
1468 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001469 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 } else {
1471 return;
1472 }
1473
Joe Onorato00acb122009-08-04 16:04:30 -04001474 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 openFolder.setLauncher(this);
1476
1477 openFolder.bind(folderInfo);
1478 folderInfo.opened = true;
1479
1480 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1481 openFolder.onOpen();
1482 }
1483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001485 switch (v.getId()) {
1486 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001487 if (!isAllAppsVisible()) {
1488 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1489 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1490 showPreviousPreview(v);
1491 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001492 return true;
1493 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001494 if (!isAllAppsVisible()) {
1495 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1496 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1497 showNextPreview(v);
1498 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001499 return true;
1500 }
1501
Joe Onorato9c1289c2009-08-17 11:03:03 -04001502 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 return false;
1504 }
1505
1506 if (!(v instanceof CellLayout)) {
1507 v = (View) v.getParent();
1508 }
1509
1510 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1511
1512 // This happens when long clicking an item with the dpad/trackball
1513 if (cellInfo == null) {
1514 return true;
1515 }
1516
1517 if (mWorkspace.allowLongPress()) {
1518 if (cellInfo.cell == null) {
1519 if (cellInfo.valid) {
1520 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001521 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 showAddDialog(cellInfo);
1523 }
1524 } else {
1525 if (!(cellInfo.cell instanceof Folder)) {
1526 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001527 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1528 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 mWorkspace.startDrag(cellInfo);
1530 }
1531 }
1532 }
1533 return true;
1534 }
1535
Romain Guye6b8e2f2009-11-10 11:56:55 -08001536 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001537 private void dismissPreview(final View v) {
1538 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001539 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001540 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1541 public void onDismiss() {
1542 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1543 int count = group.getChildCount();
1544 for (int i = 0; i < count; i++) {
1545 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1546 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001547 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1548 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1549
1550 v.setTag(R.id.workspace, null);
1551 v.setTag(R.id.icon, null);
1552 window.setOnDismissListener(null);
1553 }
1554 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001555 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001556 }
1557 v.setTag(null);
1558 }
1559
1560 private void showPreviousPreview(View anchor) {
1561 int current = mWorkspace.getCurrentScreen();
1562 if (current <= 0) return;
1563
Romain Guy9d31e9f2009-11-11 18:54:28 -08001564 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001565 }
1566
1567 private void showNextPreview(View anchor) {
1568 int current = mWorkspace.getCurrentScreen();
1569 if (current >= mWorkspace.getChildCount() - 1) return;
1570
Romain Guy9d31e9f2009-11-11 18:54:28 -08001571 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guya6abce82009-11-10 02:54:41 -08001572 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001573
Romain Guya6abce82009-11-10 02:54:41 -08001574 private void showPreviews(final View anchor, int start, int end) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001575 Resources resources = getResources();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001576
Romain Guya6abce82009-11-10 02:54:41 -08001577 Workspace workspace = mWorkspace;
1578 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001579
1580 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001581
1582 Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001583 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001584 int extraW = (int) ((r.left + r.right) * max);
1585 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001586
1587 int aW = cell.getWidth() - extraW;
1588 float w = aW / max;
1589
1590 int width = cell.getWidth();
1591 int height = cell.getHeight();
1592 int x = cell.getLeftPadding();
1593 int y = cell.getTopPadding();
1594 width -= (x + cell.getRightPadding());
1595 height -= (y + cell.getBottomPadding());
1596
1597 float scale = w / width;
1598
1599 int count = end - start;
1600
1601 final float sWidth = width * scale;
1602 float sHeight = height * scale;
1603
Romain Guye6b8e2f2009-11-10 11:56:55 -08001604 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001605
Romain Guye6b8e2f2009-11-10 11:56:55 -08001606 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1607 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001608
1609 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001610 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001611 cell = (CellLayout) workspace.getChildAt(i);
1612
Romain Guye6b8e2f2009-11-10 11:56:55 -08001613 Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
1614 Bitmap.Config.ARGB_8888);
1615
1616 Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001617 c.scale(scale, scale);
1618 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1619 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001620
Romain Guy9d31e9f2009-11-11 18:54:28 -08001621 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001622 image.setImageBitmap(bitmap);
1623 image.setTag(i);
1624 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001625 image.setOnFocusChangeListener(handler);
1626 image.setFocusable(true);
1627 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001628
1629 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001630 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1631
1632 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001633 }
Romain Guye6b8e2f2009-11-10 11:56:55 -08001634
1635 PopupWindow p = new PopupWindow(this);
1636 p.setContentView(preview);
1637 p.setWidth((int) (sWidth * count + extraW));
1638 p.setHeight((int) (sHeight + extraH));
1639 p.setAnimationStyle(R.style.AnimationPreview);
1640 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001641 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001642 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001643 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001644
Romain Guye6b8e2f2009-11-10 11:56:55 -08001645 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1646 public void onDismiss() {
1647 dismissPreview(anchor);
1648 }
1649 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001650
1651 anchor.setTag(p);
1652 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001653 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001654 }
1655
Romain Guy9d31e9f2009-11-11 18:54:28 -08001656 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001657 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001658
Romain Guye6b8e2f2009-11-10 11:56:55 -08001659 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001660 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001661 }
1662
1663 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001664 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001665 v.post(this);
1666 }
1667
1668 public void run() {
1669 dismissPreview(mAnchor);
1670 }
1671
1672 public void onFocusChange(View v, boolean hasFocus) {
1673 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001674 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001675 }
Romain Guya6abce82009-11-10 02:54:41 -08001676 }
1677 }
1678
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001679 View getDrawerHandle() {
1680 return mHandleView;
1681 }
1682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 Workspace getWorkspace() {
1684 return mWorkspace;
1685 }
1686
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 @Override
1688 protected Dialog onCreateDialog(int id) {
1689 switch (id) {
1690 case DIALOG_CREATE_SHORTCUT:
1691 return new CreateShortcut().createDialog();
1692 case DIALOG_RENAME_FOLDER:
1693 return new RenameFolder().createDialog();
1694 }
1695
1696 return super.onCreateDialog(id);
1697 }
1698
1699 @Override
1700 protected void onPrepareDialog(int id, Dialog dialog) {
1701 switch (id) {
1702 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 break;
1704 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001705 if (mFolderInfo != null) {
1706 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1707 final CharSequence text = mFolderInfo.title;
1708 input.setText(text);
1709 input.setSelection(0, text.length());
1710 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 break;
1712 }
1713 }
1714
1715 void showRenameDialog(FolderInfo info) {
1716 mFolderInfo = info;
1717 mWaitingForResult = true;
1718 showDialog(DIALOG_RENAME_FOLDER);
1719 }
1720
1721 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1722 mAddItemCellInfo = cellInfo;
1723 mWaitingForResult = true;
1724 showDialog(DIALOG_CREATE_SHORTCUT);
1725 }
1726
Romain Guy73b979d2009-06-09 12:57:21 -07001727 private void pickShortcut(int requestCode, int title) {
1728 Bundle bundle = new Bundle();
1729
1730 ArrayList<String> shortcutNames = new ArrayList<String>();
1731 shortcutNames.add(getString(R.string.group_applications));
1732 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1733
1734 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1735 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1736 R.drawable.ic_launcher_application));
1737 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1738
1739 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1740 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1741 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1742 pickIntent.putExtras(bundle);
1743
1744 startActivityForResult(pickIntent, requestCode);
1745 }
1746
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 private class RenameFolder {
1748 private EditText mInput;
1749
1750 Dialog createDialog() {
1751 mWaitingForResult = true;
1752 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1753 mInput = (EditText) layout.findViewById(R.id.folder_name);
1754
1755 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1756 builder.setIcon(0);
1757 builder.setTitle(getString(R.string.rename_folder_title));
1758 builder.setCancelable(true);
1759 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1760 public void onCancel(DialogInterface dialog) {
1761 cleanup();
1762 }
1763 });
1764 builder.setNegativeButton(getString(R.string.cancel_action),
1765 new Dialog.OnClickListener() {
1766 public void onClick(DialogInterface dialog, int which) {
1767 cleanup();
1768 }
1769 }
1770 );
1771 builder.setPositiveButton(getString(R.string.rename_action),
1772 new Dialog.OnClickListener() {
1773 public void onClick(DialogInterface dialog, int which) {
1774 changeFolderName();
1775 }
1776 }
1777 );
1778 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001779
1780 final AlertDialog dialog = builder.create();
1781 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1782 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001783 mInput.requestFocus();
1784 InputMethodManager inputManager = (InputMethodManager)
1785 getSystemService(Context.INPUT_METHOD_SERVICE);
1786 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001787 }
1788 });
1789
1790 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 }
1792
1793 private void changeFolderName() {
1794 final String name = mInput.getText().toString();
1795 if (!TextUtils.isEmpty(name)) {
1796 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001797 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 mFolderInfo.title = name;
1799 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1800
Joe Onorato9c1289c2009-08-17 11:03:03 -04001801 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001802 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001803 mModel.setWorkspaceDirty();
1804 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 } else {
1806 final FolderIcon folderIcon = (FolderIcon)
1807 mWorkspace.getViewForTag(mFolderInfo);
1808 if (folderIcon != null) {
1809 folderIcon.setText(name);
1810 getWorkspace().requestLayout();
1811 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001812 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001813 mModel.setWorkspaceDirty();
1814 mWorkspaceLoading = true;
1815 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 }
1817 }
1818 }
1819 cleanup();
1820 }
1821
1822 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 dismissDialog(DIALOG_RENAME_FOLDER);
1824 mWaitingForResult = false;
1825 mFolderInfo = null;
1826 }
1827 }
1828
Joe Onoratofb0ca672009-09-14 17:55:46 -04001829 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001830 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001831 }
1832
Joe Onorato3a8820b2009-11-10 15:06:42 -08001833 boolean isAllAppsOpaque() {
1834 return mAllAppsGrid.isOpaque();
1835 }
1836
1837 void showAllApps(boolean animated) {
1838 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato7bb17492009-09-24 17:51:01 -07001839 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001840
Joe Onorato3a8820b2009-11-10 15:06:42 -08001841 mWorkspace.startFading(false);
1842
Mike Cleronb6082fa02009-10-19 17:03:36 -07001843 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001844 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001845
Joe Onorato7c312c12009-08-13 21:36:53 -07001846 // TODO: fade these two too
1847 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001848 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001849 }
1850
Joe Onorato7bb17492009-09-24 17:51:01 -07001851 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001852 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001853 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001854 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001855 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001856 mWorkspace.startFading(true);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001857
Joe Onorato7c312c12009-08-13 21:36:53 -07001858 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001859 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001860 mDeleteZone.setVisibility(View.VISIBLE);
1861 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001862 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001863 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001864 }
1865
Joe Onorato7c312c12009-08-13 21:36:53 -07001866 void lockAllApps() {
1867 // TODO
1868 }
1869
1870 void unlockAllApps() {
1871 // TODO
1872 }
1873
Joe Onorato7404ee42009-07-31 11:54:44 -07001874 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 * Displays the shortcut creation dialog and launches, if necessary, the
1876 * appropriate activity.
1877 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001878 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001879 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1880 DialogInterface.OnShowListener {
1881
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001883
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 Dialog createDialog() {
1885 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001886
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001888
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1890 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001891 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001892
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 builder.setInverseBackgroundForced(true);
1894
1895 AlertDialog dialog = builder.create();
1896 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001897 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001898 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 return dialog;
1901 }
1902
1903 public void onCancel(DialogInterface dialog) {
1904 mWaitingForResult = false;
1905 cleanup();
1906 }
1907
Romain Guycbb89e42009-06-08 15:52:54 -07001908 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001909 }
1910
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 dismissDialog(DIALOG_CREATE_SHORTCUT);
1913 }
1914
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001915 /**
1916 * Handle the action clicked in the "Add to home" dialog.
1917 */
1918 public void onClick(DialogInterface dialog, int which) {
1919 Resources res = getResources();
1920 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001921
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001922 switch (which) {
1923 case AddAdapter.ITEM_SHORTCUT: {
1924 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001925 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001926 break;
1927 }
Romain Guycbb89e42009-06-08 15:52:54 -07001928
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001929 case AddAdapter.ITEM_APPWIDGET: {
1930 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001931
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001932 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1933 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1934 // add the search widget
1935 ArrayList<AppWidgetProviderInfo> customInfo =
1936 new ArrayList<AppWidgetProviderInfo>();
1937 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1938 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1939 info.label = getString(R.string.group_search);
1940 info.icon = R.drawable.ic_search_widget;
1941 customInfo.add(info);
1942 pickIntent.putParcelableArrayListExtra(
1943 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1944 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1945 Bundle b = new Bundle();
1946 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1947 customExtras.add(b);
1948 pickIntent.putParcelableArrayListExtra(
1949 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1950 // start the pick activity
1951 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1952 break;
1953 }
Romain Guycbb89e42009-06-08 15:52:54 -07001954
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001955 case AddAdapter.ITEM_LIVE_FOLDER: {
1956 // Insert extra item to handle inserting folder
1957 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001958
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001959 ArrayList<String> shortcutNames = new ArrayList<String>();
1960 shortcutNames.add(res.getString(R.string.group_folder));
1961 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001962
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001963 ArrayList<ShortcutIconResource> shortcutIcons =
1964 new ArrayList<ShortcutIconResource>();
1965 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1966 R.drawable.ic_launcher_folder));
1967 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1968
1969 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1970 pickIntent.putExtra(Intent.EXTRA_INTENT,
1971 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1972 pickIntent.putExtra(Intent.EXTRA_TITLE,
1973 getText(R.string.title_select_live_folder));
1974 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001975
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001976 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1977 break;
1978 }
1979
1980 case AddAdapter.ITEM_WALLPAPER: {
1981 startWallpaper();
1982 break;
1983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 }
1985 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001986
1987 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
1990
1991 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001992 * Receives notifications when applications are added/removed.
1993 */
1994 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1995 @Override
1996 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001997 closeSystemDialogs();
1998 }
1999 }
2000
2001 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002002 * Receives notifications whenever the appwidgets are reset.
2003 */
2004 private class AppWidgetResetObserver extends ContentObserver {
2005 public AppWidgetResetObserver() {
2006 super(new Handler());
2007 }
2008
2009 @Override
2010 public void onChange(boolean selfChange) {
2011 onAppWidgetReset();
2012 }
2013 }
2014
2015 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002016 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002018 public int getCurrentWorkspaceScreen() {
2019 return mWorkspace.getCurrentScreen();
2020 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002021
Joe Onorato9c1289c2009-08-17 11:03:03 -04002022 /**
2023 * Refreshes the shortcuts shown on the workspace.
2024 *
2025 * Implementation of the method from LauncherModel.Callbacks.
2026 */
2027 public void startBinding() {
2028 final Workspace workspace = mWorkspace;
2029 int count = workspace.getChildCount();
2030 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002031 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002032 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2033 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002034
Joe Onorato9c1289c2009-08-17 11:03:03 -04002035 if (DEBUG_USER_INTERFACE) {
2036 android.widget.Button finishButton = new android.widget.Button(this);
2037 finishButton.setText("Finish");
2038 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2039
2040 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2041 public void onClick(View v) {
2042 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002044 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
2046 }
2047
2048 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002049 * Bind the items start-end from the list.
2050 *
2051 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002053 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2054
2055 final Workspace workspace = mWorkspace;
2056
2057 for (int i=start; i<end; i++) {
2058 final ItemInfo item = shortcuts.get(i);
2059 mDesktopItems.add(item);
2060 switch (item.itemType) {
2061 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2062 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2063 final View shortcut = createShortcut((ApplicationInfo) item);
2064 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2065 false);
2066 break;
2067 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2068 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2069 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2070 (UserFolderInfo) item);
2071 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2072 false);
2073 break;
2074 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2075 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2076 R.layout.live_folder_icon, this,
2077 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2078 (LiveFolderInfo) item);
2079 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2080 false);
2081 break;
2082 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2083 final int screen = workspace.getCurrentScreen();
2084 final View view = mInflater.inflate(R.layout.widget_search,
2085 (ViewGroup) workspace.getChildAt(screen), false);
2086
2087 Search search = (Search) view.findViewById(R.id.widget_search);
2088 search.setLauncher(this);
2089
2090 final Widget widget = (Widget) item;
2091 view.setTag(widget);
2092
2093 workspace.addWidget(view, widget, false);
2094 break;
2095 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 }
2097
Joe Onorato9c1289c2009-08-17 11:03:03 -04002098 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 }
2100
Joe Onorato9c1289c2009-08-17 11:03:03 -04002101 /**
2102 * Implementation of the method from LauncherModel.Callbacks.
2103 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002104 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2105 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002106 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002108
2109 /**
2110 * Add the views for a widget to the workspace.
2111 *
2112 * Implementation of the method from LauncherModel.Callbacks.
2113 */
2114 public void bindAppWidget(LauncherAppWidgetInfo item) {
2115 final Workspace workspace = mWorkspace;
2116
2117 final int appWidgetId = item.appWidgetId;
2118 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2119 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2120
Joe Onorato9c1289c2009-08-17 11:03:03 -04002121 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2122 item.hostView.setTag(item);
2123
2124 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2125 item.cellY, item.spanX, item.spanY, false);
2126
2127 workspace.requestLayout();
2128
2129 mDesktopItems.add(item);
2130 }
2131
2132 /**
2133 * Callback saying that there aren't any more items to bind.
2134 *
2135 * Implementation of the method from LauncherModel.Callbacks.
2136 */
2137 public void finishBindingItems() {
2138 if (mSavedState != null) {
2139 if (!mWorkspace.hasFocus()) {
2140 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2141 }
2142
2143 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2144 if (userFolders != null) {
2145 for (long folderId : userFolders) {
2146 final FolderInfo info = mFolders.get(folderId);
2147 if (info != null) {
2148 openFolder(info);
2149 }
2150 }
2151 final Folder openFolder = mWorkspace.getOpenFolder();
2152 if (openFolder != null) {
2153 openFolder.requestFocus();
2154 }
2155 }
2156
Joe Onorato9c1289c2009-08-17 11:03:03 -04002157 mSavedState = null;
2158 }
2159
2160 if (mSavedInstanceState != null) {
2161 super.onRestoreInstanceState(mSavedInstanceState);
2162 mSavedInstanceState = null;
2163 }
2164
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 mWorkspaceLoading = false;
2166 }
2167
2168 /**
2169 * Add the icons for all apps.
2170 *
2171 * Implementation of the method from LauncherModel.Callbacks.
2172 */
2173 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002174 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002175 }
2176
2177 /**
2178 * A package was installed.
2179 *
2180 * Implementation of the method from LauncherModel.Callbacks.
2181 */
2182 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2183 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002184 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002185 }
2186
2187 /**
2188 * A package was updated.
2189 *
2190 * Implementation of the method from LauncherModel.Callbacks.
2191 */
2192 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2193 removeDialog(DIALOG_CREATE_SHORTCUT);
2194 mWorkspace.updateShortcutsForPackage(packageName);
2195 }
2196
2197 /**
2198 * A package was uninstalled.
2199 *
2200 * Implementation of the method from LauncherModel.Callbacks.
2201 */
2202 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2203 removeDialog(DIALOG_CREATE_SHORTCUT);
2204 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002205 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002206 }
Joe Onoratobe386092009-11-17 17:32:16 -08002207
2208 /**
2209 * Prints out out state for debugging.
2210 */
2211 public void dumpState() {
2212 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002213 Log.d(TAG, "mSavedState=" + mSavedState);
2214 Log.d(TAG, "mIsNewIntent=" + mIsNewIntent);
2215 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2216 Log.d(TAG, "mRestoring=" + mRestoring);
2217 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2218 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2219 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2220 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002221 mModel.dumpState();
2222 mAllAppsGrid.dumpState();
2223 Log.d(TAG, "END launcher2 dump state");
2224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225}