blob: 859a75e5ce515219363d1be8081864d9d3d08fd6 [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) {
592 mWorkspace.scrollLeft();
593 }
594
595 @SuppressWarnings({"UnusedDeclaration"})
596 public void nextScreen(View v) {
597 mWorkspace.scrollRight();
598 }
599
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 /**
601 * Creates a view representing a shortcut.
602 *
603 * @param info The data structure describing the shortcut.
604 *
605 * @return A View inflated from R.layout.application.
606 */
607 View createShortcut(ApplicationInfo info) {
608 return createShortcut(R.layout.application,
609 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
610 }
611
612 /**
613 * Creates a view representing a shortcut inflated from the specified resource.
614 *
615 * @param layoutResId The id of the XML layout used to create the shortcut.
616 * @param parent The group the shortcut belongs to.
617 * @param info The data structure describing the shortcut.
618 *
619 * @return A View inflated from layoutResId.
620 */
621 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
622 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
623
Joe Onorato5162ea92009-09-03 09:39:42 -0700624 if (info.icon == null) {
625 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
626 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700628 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 info.filtered = true;
630 }
631
632 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
633 favorite.setText(info.title);
634 favorite.setTag(info);
635 favorite.setOnClickListener(this);
636
637 return favorite;
638 }
639
640 /**
641 * Add an application shortcut to the workspace.
642 *
643 * @param data The intent describing the application.
644 * @param cellInfo The position on screen where to create the shortcut.
645 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400646 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 cellInfo.screen = mWorkspace.getCurrentScreen();
648 if (!findSingleSlot(cellInfo)) return;
649
Romain Guy73b979d2009-06-09 12:57:21 -0700650 final ApplicationInfo info = infoFromApplicationIntent(context, data);
651 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400652 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700653 }
654 }
655
656 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 ComponentName component = data.getComponent();
658 PackageManager packageManager = context.getPackageManager();
659 ActivityInfo activityInfo = null;
660 try {
661 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
662 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800663 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
Romain Guycbb89e42009-06-08 15:52:54 -0700665
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 if (activityInfo != null) {
667 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700668
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 itemInfo.title = activityInfo.loadLabel(packageManager);
670 if (itemInfo.title == null) {
671 itemInfo.title = activityInfo.name;
672 }
Romain Guycbb89e42009-06-08 15:52:54 -0700673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
675 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
676 itemInfo.icon = activityInfo.loadIcon(packageManager);
677 itemInfo.container = ItemInfo.NO_ID;
678
Romain Guy73b979d2009-06-09 12:57:21 -0700679 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
Romain Guy73b979d2009-06-09 12:57:21 -0700681
682 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
Romain Guycbb89e42009-06-08 15:52:54 -0700684
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 /**
686 * Add a shortcut to the workspace.
687 *
688 * @param data The intent describing the shortcut.
689 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 cellInfo.screen = mWorkspace.getCurrentScreen();
693 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
696
697 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400699 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
700 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 }
702 }
703
Romain Guycbb89e42009-06-08 15:52:54 -0700704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708 * @param data The intent describing the appWidgetId.
709 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400711 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700713 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700714
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800715 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700716
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700718
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700719 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700721 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700722
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 // Try finding open space on Launcher screen
724 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800725 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
726 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
727 return;
728 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700730 // Build Launcher-specific widget info and save to database
731 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 launcherInfo.spanX = spans[0];
733 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700734
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 LauncherModel.addItemToDatabase(this, launcherInfo,
736 LauncherSettings.Favorites.CONTAINER_DESKTOP,
737 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
738
739 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700741
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700743 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700744
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700745 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700747
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
751 }
Romain Guycbb89e42009-06-08 15:52:54 -0700752
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
754 mDesktopItems.remove(launcherInfo);
755 launcherInfo.hostView = null;
756 }
757
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700758 public LauncherAppWidgetHost getAppWidgetHost() {
759 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
Romain Guycbb89e42009-06-08 15:52:54 -0700761
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 static ApplicationInfo addShortcut(Context context, Intent data,
763 CellLayout.CellInfo cellInfo, boolean notify) {
764
Romain Guy73b979d2009-06-09 12:57:21 -0700765 final ApplicationInfo info = infoFromShortcutIntent(context, data);
766 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
767 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
768
769 return info;
770 }
771
772 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
774 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
775 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
776
777 Drawable icon = null;
778 boolean filtered = false;
779 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700780 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781
782 if (bitmap != null) {
783 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
784 filtered = true;
785 customIcon = true;
786 } else {
787 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700788 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700790 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 final PackageManager packageManager = context.getPackageManager();
792 Resources resources = packageManager.getResourcesForApplication(
793 iconResource.packageName);
794 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
795 icon = resources.getDrawable(id);
796 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800797 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 }
799 }
800 }
801
802 if (icon == null) {
803 icon = context.getPackageManager().getDefaultActivityIcon();
804 }
805
806 final ApplicationInfo info = new ApplicationInfo();
807 info.icon = icon;
808 info.filtered = filtered;
809 info.title = name;
810 info.intent = intent;
811 info.customIcon = customIcon;
812 info.iconResource = iconResource;
813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 return info;
815 }
816
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800817 void closeSystemDialogs() {
818 closeAllApps(false);
819 getWindow().closeAllPanels();
820
821 try {
822 dismissDialog(DIALOG_CREATE_SHORTCUT);
823 // Unlock the workspace if the dialog was showing
824 } catch (Exception e) {
825 // An exception is thrown if the dialog is not visible, which is fine
826 }
827
828 try {
829 dismissDialog(DIALOG_RENAME_FOLDER);
830 // Unlock the workspace if the dialog was showing
831 } catch (Exception e) {
832 // An exception is thrown if the dialog is not visible, which is fine
833 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800834
835 // Whatever we were doing is hereby canceled.
836 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800837 }
838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 @Override
840 protected void onNewIntent(Intent intent) {
841 super.onNewIntent(intent);
842
843 // Close the menu
844 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800845 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800846 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700847
Mike LeBeau736cf282009-07-02 17:46:59 -0700848 // Set this flag so that onResume knows to close the search dialog if it's open,
849 // because this was a new intent (thus a press of 'home' or some such) rather than
850 // for example onResume being called when the user pressed the 'back' button.
851 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852
Joe Onorato3a8820b2009-11-10 15:06:42 -0800853 if (!mWorkspace.isDefaultScreenShowing()) {
854 mWorkspace.moveToDefaultScreen();
855 }
Romain Guy73b979d2009-06-09 12:57:21 -0700856
Joe Onorato3a8820b2009-11-10 15:06:42 -0800857 closeAllApps(false);
Romain Guy1dd3a072009-07-16 13:21:01 -0700858
Joe Onorato3a8820b2009-11-10 15:06:42 -0800859 final View v = getWindow().peekDecorView();
860 if (v != null && v.getWindowToken() != null) {
861 InputMethodManager imm = (InputMethodManager)getSystemService(
862 INPUT_METHOD_SERVICE);
863 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
865 }
866 }
867
868 @Override
869 protected void onRestoreInstanceState(Bundle savedInstanceState) {
870 // Do not call super here
871 mSavedInstanceState = savedInstanceState;
872 }
873
874 @Override
875 protected void onSaveInstanceState(Bundle outState) {
876 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
877
878 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
879 if (folders.size() > 0) {
880 final int count = folders.size();
881 long[] ids = new long[count];
882 for (int i = 0; i < count; i++) {
883 final FolderInfo info = folders.get(i).getInfo();
884 ids[i] = info.id;
885 }
886 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
887 } else {
888 super.onSaveInstanceState(outState);
889 }
890
Joe Onoratofb0ca672009-09-14 17:55:46 -0400891 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800892 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700894 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895
896 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
897 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
898 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
899
900 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
901 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
902 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
903 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
904 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
905 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
906 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
907 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
908 layout.getOccupiedCells());
909 }
910
911 if (mFolderInfo != null && mWaitingForResult) {
912 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
913 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
914 }
915 }
916
917 @Override
918 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700920
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700922 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800924 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 }
926
927 TextKeyListener.getInstance().release();
928
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 unbindDesktopItems();
932 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800933
934 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800935
936 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800937 dismissPreview(mNextView);
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800938
939 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
942 @Override
943 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700944 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 super.startActivityForResult(intent, requestCode);
946 }
947
948 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700949 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700951
Joe Onorato7bb17492009-09-24 17:51:01 -0700952 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700953
Karl Rosaen138a0412009-04-23 19:00:21 -0700954 // Slide the search widget to the top, if it's on the current screen,
955 // otherwise show the search dialog immediately.
956 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
957 if (searchWidget == null) {
958 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
959 } else {
960 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
961 // show the currently typed text in the search widget while sliding
962 searchWidget.setQuery(getTypedText());
963 }
964 }
Romain Guycbb89e42009-06-08 15:52:54 -0700965
Karl Rosaen138a0412009-04-23 19:00:21 -0700966 /**
967 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700968 *
969 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 */
Romain Guycbb89e42009-06-08 15:52:54 -0700971 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700972 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700973
Karl Rosaen138a0412009-04-23 19:00:21 -0700974 if (initialQuery == null) {
975 // Use any text typed in the launcher as the initial query
976 initialQuery = getTypedText();
977 clearTypedText();
978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 if (appSearchData == null) {
980 appSearchData = new Bundle();
981 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
982 }
Romain Guycbb89e42009-06-08 15:52:54 -0700983
Karl Rosaen138a0412009-04-23 19:00:21 -0700984 final SearchManager searchManager =
985 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
986
987 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
988 if (searchWidget != null) {
989 // This gets called when the user leaves the search dialog to go back to
990 // the Launcher.
991 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
992 public void onCancel() {
993 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700994 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700995 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700996 });
997 }
Romain Guycbb89e42009-06-08 15:52:54 -0700998
Karl Rosaen138a0412009-04-23 19:00:21 -0700999 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001000 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
1002
Karl Rosaen138a0412009-04-23 19:00:21 -07001003 /**
1004 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001005 */
Romain Guy5a941392009-04-28 15:18:25 -07001006 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001007 // Close search dialog
1008 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001009 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001010 // Restore search widget to its normal position
1011 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1012 if (searchWidget != null) {
1013 searchWidget.stopSearch(false);
1014 }
1015 }
Romain Guycbb89e42009-06-08 15:52:54 -07001016
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 @Override
1018 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019 if (isWorkspaceLocked()) {
1020 return false;
1021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022
1023 super.onCreateOptionsMenu(menu);
1024 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1025 .setIcon(android.R.drawable.ic_menu_add)
1026 .setAlphabeticShortcut('A');
1027 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1028 .setIcon(android.R.drawable.ic_menu_gallery)
1029 .setAlphabeticShortcut('W');
1030 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1031 .setIcon(android.R.drawable.ic_search_category_default)
1032 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1033 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1034 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1035 .setAlphabeticShortcut('N');
1036
1037 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001038 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1039 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040
1041 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1042 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1043 .setIntent(settings);
1044
1045 return true;
1046 }
1047
1048 @Override
1049 public boolean onPrepareOptionsMenu(Menu menu) {
1050 super.onPrepareOptionsMenu(menu);
1051
1052 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1053 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1054
1055 return true;
1056 }
1057
1058 @Override
1059 public boolean onOptionsItemSelected(MenuItem item) {
1060 switch (item.getItemId()) {
1061 case MENU_ADD:
1062 addItems();
1063 return true;
1064 case MENU_WALLPAPER_SETTINGS:
1065 startWallpaper();
1066 return true;
1067 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001068 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 return true;
1070 case MENU_NOTIFICATIONS:
1071 showNotifications();
1072 return true;
1073 }
1074
1075 return super.onOptionsItemSelected(item);
1076 }
Romain Guycbb89e42009-06-08 15:52:54 -07001077
Karl Rosaen138a0412009-04-23 19:00:21 -07001078 /**
1079 * Indicates that we want global search for this activity by setting the globalSearch
1080 * argument for {@link #startSearch} to true.
1081 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001083 @Override
1084 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001085 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001086 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001087 }
1088
Joe Onorato9c1289c2009-08-17 11:03:03 -04001089 public boolean isWorkspaceLocked() {
1090 return mWorkspaceLoading || mWaitingForResult;
1091 }
1092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001094 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 showAddDialog(mMenuAddInfo);
1096 }
1097
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001098 void addAppWidget(Intent data) {
1099 // TODO: catch bad widget exception when sent
1100 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001102 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1103 if (SEARCH_WIDGET.equals(customWidget)) {
1104 // We don't need this any more, since this isn't a real app widget.
1105 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1106 // add the search widget
1107 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001109 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1110
1111 if (appWidget.configure != null) {
1112 // Launch over to configure widget, if needed
1113 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1114 intent.setComponent(appWidget.configure);
1115 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1116
1117 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1118 } else {
1119 // Otherwise just add it
1120 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 }
1123 }
Romain Guycbb89e42009-06-08 15:52:54 -07001124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 void addSearch() {
1126 final Widget info = Widget.makeSearch();
1127 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 final int[] xy = mCellCoordinates;
1130 final int spanX = info.spanX;
1131 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1136 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001137
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 final View view = mInflater.inflate(info.layoutResource, null);
1139 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001140 Search search = (Search) view.findViewById(R.id.widget_search);
1141 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1144 }
1145
Romain Guy73b979d2009-06-09 12:57:21 -07001146 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001147 // Handle case where user selected "Applications"
1148 String applicationName = getResources().getString(R.string.group_applications);
1149 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001150
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001151 if (applicationName != null && applicationName.equals(shortcutName)) {
1152 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1153 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001154
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001155 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1156 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001157 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001158 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001159 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 }
1162
1163 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001164 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001165 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001166 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001167
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001168 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001169 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001170 } else {
1171 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 }
1174
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 UserFolderInfo folderInfo = new UserFolderInfo();
1177 folderInfo.title = getText(R.string.folder_name);
1178
1179 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1180 cellInfo.screen = mWorkspace.getCurrentScreen();
1181 if (!findSingleSlot(cellInfo)) return;
1182
1183 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001184 LauncherModel.addItemToDatabase(this, folderInfo,
1185 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001187 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188
1189 // Create the view
1190 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1191 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1192 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 }
Romain Guycbb89e42009-06-08 15:52:54 -07001195
Joe Onorato9c1289c2009-08-17 11:03:03 -04001196 void removeFolder(FolderInfo folder) {
1197 mFolders.remove(folder.id);
1198 }
1199
1200 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 cellInfo.screen = mWorkspace.getCurrentScreen();
1202 if (!findSingleSlot(cellInfo)) return;
1203
1204 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1205
1206 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1208 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001209 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1210 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 }
1212 }
1213
1214 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1215 CellLayout.CellInfo cellInfo, boolean notify) {
1216
1217 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1218 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1219
1220 Drawable icon = null;
1221 boolean filtered = false;
1222 Intent.ShortcutIconResource iconResource = null;
1223
1224 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1225 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1226 try {
1227 iconResource = (Intent.ShortcutIconResource) extra;
1228 final PackageManager packageManager = context.getPackageManager();
1229 Resources resources = packageManager.getResourcesForApplication(
1230 iconResource.packageName);
1231 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1232 icon = resources.getDrawable(id);
1233 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001234 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
1236 }
1237
1238 if (icon == null) {
1239 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1240 }
1241
1242 final LiveFolderInfo info = new LiveFolderInfo();
1243 info.icon = icon;
1244 info.filtered = filtered;
1245 info.title = name;
1246 info.iconResource = iconResource;
1247 info.uri = data.getData();
1248 info.baseIntent = baseIntent;
1249 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1250 LiveFolders.DISPLAY_MODE_GRID);
1251
1252 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1253 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001254 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255
1256 return info;
1257 }
1258
1259 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1260 final int[] xy = new int[2];
1261 if (findSlot(cellInfo, xy, 1, 1)) {
1262 cellInfo.cellX = xy[0];
1263 cellInfo.cellY = xy[1];
1264 return true;
1265 }
1266 return false;
1267 }
1268
1269 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1270 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1271 boolean[] occupied = mSavedState != null ?
1272 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1273 cellInfo = mWorkspace.findAllVacantCells(occupied);
1274 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1275 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1276 return false;
1277 }
1278 }
1279 return true;
1280 }
1281
1282 private void showNotifications() {
1283 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1284 if (statusBar != null) {
1285 statusBar.expand();
1286 }
1287 }
1288
1289 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001290 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001292 Intent chooser = Intent.createChooser(pickWallpaper,
1293 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001294 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1295 // Removed in Eclair MR1
1296// WallpaperManager wm = (WallpaperManager)
1297// getSystemService(Context.WALLPAPER_SERVICE);
1298// WallpaperInfo wi = wm.getWallpaperInfo();
1299// if (wi != null && wi.getSettingsActivity() != null) {
1300// LabeledIntent li = new LabeledIntent(getPackageName(),
1301// R.string.configure_wallpaper, 0);
1302// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1303// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1304// }
Mike Clerona0618e42009-10-22 13:55:21 -07001305 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
1307
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001308 /**
1309 * Registers various content observers. The current implementation registers
1310 * only a favorites observer to keep track of the favorites applications.
1311 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001312 private void registerContentObservers() {
1313 ContentResolver resolver = getContentResolver();
1314 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1315 true, mWidgetObserver);
1316 }
1317
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 @Override
1319 public boolean dispatchKeyEvent(KeyEvent event) {
1320 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1321 switch (event.getKeyCode()) {
1322 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 return true;
1324 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001325 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001326 case KeyEvent.KEYCODE_VOLUME_DOWN:
1327 if (SystemProperties.getInt("launcher2.dumpstate", 0) != 0) {
1328 dumpState();
1329 return true;
1330 }
1331 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001332 }
1333 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1334 switch (event.getKeyCode()) {
1335 case KeyEvent.KEYCODE_BACK:
Joe Onorato247226b2009-11-18 17:43:22 -08001336 if (event.isTracking() && !event.isCanceled()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001337 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001338 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001339 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001340 } else {
1341 closeFolder();
1342 }
Romain Guya6abce82009-11-10 02:54:41 -08001343 dismissPreview(mPreviousView);
1344 dismissPreview(mNextView);
Dianne Hackborn67800862009-07-24 17:15:20 -07001345 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001346 return true;
1347 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 return true;
1349 }
1350 }
1351
1352 return super.dispatchKeyEvent(event);
1353 }
1354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 private void closeFolder() {
1356 Folder folder = mWorkspace.getOpenFolder();
1357 if (folder != null) {
1358 closeFolder(folder);
1359 }
1360 }
1361
1362 void closeFolder(Folder folder) {
1363 folder.getInfo().opened = false;
1364 ViewGroup parent = (ViewGroup) folder.getParent();
1365 if (parent != null) {
1366 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001367 if (folder instanceof DropTarget) {
1368 // Live folders aren't DropTargets.
1369 mDragController.removeDropTarget((DropTarget)folder);
1370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 }
1372 folder.onClose();
1373 }
1374
1375 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001376 * Re-listen when widgets are reset.
1377 */
1378 private void onAppWidgetReset() {
1379 mAppWidgetHost.startListening();
1380 }
1381
1382 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001383 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1384 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386 private void unbindDesktopItems() {
1387 for (ItemInfo item: mDesktopItems) {
1388 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 /**
1393 * Launches the intent referred by the clicked shortcut.
1394 *
1395 * @param v The view representing the clicked shortcut.
1396 */
1397 public void onClick(View v) {
1398 Object tag = v.getTag();
1399 if (tag instanceof ApplicationInfo) {
1400 // Open shortcut
1401 final Intent intent = ((ApplicationInfo) tag).intent;
1402 startActivitySafely(intent);
1403 } else if (tag instanceof FolderInfo) {
1404 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001405 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001406 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001407 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001408 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001409 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001410 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 }
1412 }
1413
1414 void startActivitySafely(Intent intent) {
1415 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1416 try {
1417 startActivity(intent);
1418 } catch (ActivityNotFoundException e) {
1419 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1420 } catch (SecurityException e) {
1421 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001422 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1424 "or use the exported attribute for this activity.", e);
1425 }
1426 }
1427
1428 private void handleFolderClick(FolderInfo folderInfo) {
1429 if (!folderInfo.opened) {
1430 // Close any open folder
1431 closeFolder();
1432 // Open the requested folder
1433 openFolder(folderInfo);
1434 } else {
1435 // Find the open folder...
1436 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1437 int folderScreen;
1438 if (openFolder != null) {
1439 folderScreen = mWorkspace.getScreenForView(openFolder);
1440 // .. and close it
1441 closeFolder(openFolder);
1442 if (folderScreen != mWorkspace.getCurrentScreen()) {
1443 // Close any folder open on the current screen
1444 closeFolder();
1445 // Pull the folder onto this screen
1446 openFolder(folderInfo);
1447 }
1448 }
1449 }
1450 }
1451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 /**
1453 * Opens the user fodler described by the specified tag. The opening of the folder
1454 * is animated relative to the specified View. If the View is null, no animation
1455 * is played.
1456 *
1457 * @param folderInfo The FolderInfo describing the folder to open.
1458 */
1459 private void openFolder(FolderInfo folderInfo) {
1460 Folder openFolder;
1461
1462 if (folderInfo instanceof UserFolderInfo) {
1463 openFolder = UserFolder.fromXml(this);
1464 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001465 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 } else {
1467 return;
1468 }
1469
Joe Onorato00acb122009-08-04 16:04:30 -04001470 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 openFolder.setLauncher(this);
1472
1473 openFolder.bind(folderInfo);
1474 folderInfo.opened = true;
1475
1476 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1477 openFolder.onOpen();
1478 }
1479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001481 switch (v.getId()) {
1482 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001483 if (!isAllAppsVisible()) {
1484 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1485 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1486 showPreviousPreview(v);
1487 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001488 return true;
1489 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001490 if (!isAllAppsVisible()) {
1491 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1492 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1493 showNextPreview(v);
1494 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001495 return true;
1496 }
1497
Joe Onorato9c1289c2009-08-17 11:03:03 -04001498 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 return false;
1500 }
1501
1502 if (!(v instanceof CellLayout)) {
1503 v = (View) v.getParent();
1504 }
1505
1506 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1507
1508 // This happens when long clicking an item with the dpad/trackball
1509 if (cellInfo == null) {
1510 return true;
1511 }
1512
1513 if (mWorkspace.allowLongPress()) {
1514 if (cellInfo.cell == null) {
1515 if (cellInfo.valid) {
1516 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001517 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 showAddDialog(cellInfo);
1519 }
1520 } else {
1521 if (!(cellInfo.cell instanceof Folder)) {
1522 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001523 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1524 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 mWorkspace.startDrag(cellInfo);
1526 }
1527 }
1528 }
1529 return true;
1530 }
1531
Romain Guye6b8e2f2009-11-10 11:56:55 -08001532 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001533 private void dismissPreview(final View v) {
1534 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001535 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001536 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1537 public void onDismiss() {
1538 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1539 int count = group.getChildCount();
1540 for (int i = 0; i < count; i++) {
1541 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1542 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001543 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1544 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1545
1546 v.setTag(R.id.workspace, null);
1547 v.setTag(R.id.icon, null);
1548 window.setOnDismissListener(null);
1549 }
1550 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001551 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001552 }
1553 v.setTag(null);
1554 }
1555
1556 private void showPreviousPreview(View anchor) {
1557 int current = mWorkspace.getCurrentScreen();
1558 if (current <= 0) return;
1559
Romain Guy9d31e9f2009-11-11 18:54:28 -08001560 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001561 }
1562
1563 private void showNextPreview(View anchor) {
1564 int current = mWorkspace.getCurrentScreen();
1565 if (current >= mWorkspace.getChildCount() - 1) return;
1566
Romain Guy9d31e9f2009-11-11 18:54:28 -08001567 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guya6abce82009-11-10 02:54:41 -08001568 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001569
Romain Guya6abce82009-11-10 02:54:41 -08001570 private void showPreviews(final View anchor, int start, int end) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001571 Resources resources = getResources();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001572
Romain Guya6abce82009-11-10 02:54:41 -08001573 Workspace workspace = mWorkspace;
1574 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001575
1576 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001577
1578 Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001579 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001580 int extraW = (int) ((r.left + r.right) * max);
1581 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001582
1583 int aW = cell.getWidth() - extraW;
1584 float w = aW / max;
1585
1586 int width = cell.getWidth();
1587 int height = cell.getHeight();
1588 int x = cell.getLeftPadding();
1589 int y = cell.getTopPadding();
1590 width -= (x + cell.getRightPadding());
1591 height -= (y + cell.getBottomPadding());
1592
1593 float scale = w / width;
1594
1595 int count = end - start;
1596
1597 final float sWidth = width * scale;
1598 float sHeight = height * scale;
1599
Romain Guye6b8e2f2009-11-10 11:56:55 -08001600 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001601
Romain Guye6b8e2f2009-11-10 11:56:55 -08001602 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1603 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001604
1605 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001606 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001607 cell = (CellLayout) workspace.getChildAt(i);
1608
Romain Guye6b8e2f2009-11-10 11:56:55 -08001609 Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
1610 Bitmap.Config.ARGB_8888);
1611
1612 Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001613 c.scale(scale, scale);
1614 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1615 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001616
Romain Guy9d31e9f2009-11-11 18:54:28 -08001617 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001618 image.setImageBitmap(bitmap);
1619 image.setTag(i);
1620 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001621 image.setOnFocusChangeListener(handler);
1622 image.setFocusable(true);
1623 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001624
1625 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001626 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1627
1628 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001629 }
Romain Guye6b8e2f2009-11-10 11:56:55 -08001630
1631 PopupWindow p = new PopupWindow(this);
1632 p.setContentView(preview);
1633 p.setWidth((int) (sWidth * count + extraW));
1634 p.setHeight((int) (sHeight + extraH));
1635 p.setAnimationStyle(R.style.AnimationPreview);
1636 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001637 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001638 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001639 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001640
Romain Guye6b8e2f2009-11-10 11:56:55 -08001641 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1642 public void onDismiss() {
1643 dismissPreview(anchor);
1644 }
1645 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001646
1647 anchor.setTag(p);
1648 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001649 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001650 }
1651
Romain Guy9d31e9f2009-11-11 18:54:28 -08001652 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001653 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001654
Romain Guye6b8e2f2009-11-10 11:56:55 -08001655 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001656 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001657 }
1658
1659 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001660 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001661 v.post(this);
1662 }
1663
1664 public void run() {
1665 dismissPreview(mAnchor);
1666 }
1667
1668 public void onFocusChange(View v, boolean hasFocus) {
1669 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001670 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001671 }
Romain Guya6abce82009-11-10 02:54:41 -08001672 }
1673 }
1674
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001675 View getDrawerHandle() {
1676 return mHandleView;
1677 }
1678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 Workspace getWorkspace() {
1680 return mWorkspace;
1681 }
1682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 @Override
1684 protected Dialog onCreateDialog(int id) {
1685 switch (id) {
1686 case DIALOG_CREATE_SHORTCUT:
1687 return new CreateShortcut().createDialog();
1688 case DIALOG_RENAME_FOLDER:
1689 return new RenameFolder().createDialog();
1690 }
1691
1692 return super.onCreateDialog(id);
1693 }
1694
1695 @Override
1696 protected void onPrepareDialog(int id, Dialog dialog) {
1697 switch (id) {
1698 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 break;
1700 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001701 if (mFolderInfo != null) {
1702 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1703 final CharSequence text = mFolderInfo.title;
1704 input.setText(text);
1705 input.setSelection(0, text.length());
1706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 break;
1708 }
1709 }
1710
1711 void showRenameDialog(FolderInfo info) {
1712 mFolderInfo = info;
1713 mWaitingForResult = true;
1714 showDialog(DIALOG_RENAME_FOLDER);
1715 }
1716
1717 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1718 mAddItemCellInfo = cellInfo;
1719 mWaitingForResult = true;
1720 showDialog(DIALOG_CREATE_SHORTCUT);
1721 }
1722
Romain Guy73b979d2009-06-09 12:57:21 -07001723 private void pickShortcut(int requestCode, int title) {
1724 Bundle bundle = new Bundle();
1725
1726 ArrayList<String> shortcutNames = new ArrayList<String>();
1727 shortcutNames.add(getString(R.string.group_applications));
1728 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1729
1730 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1731 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1732 R.drawable.ic_launcher_application));
1733 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1734
1735 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1736 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1737 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1738 pickIntent.putExtras(bundle);
1739
1740 startActivityForResult(pickIntent, requestCode);
1741 }
1742
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 private class RenameFolder {
1744 private EditText mInput;
1745
1746 Dialog createDialog() {
1747 mWaitingForResult = true;
1748 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1749 mInput = (EditText) layout.findViewById(R.id.folder_name);
1750
1751 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1752 builder.setIcon(0);
1753 builder.setTitle(getString(R.string.rename_folder_title));
1754 builder.setCancelable(true);
1755 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1756 public void onCancel(DialogInterface dialog) {
1757 cleanup();
1758 }
1759 });
1760 builder.setNegativeButton(getString(R.string.cancel_action),
1761 new Dialog.OnClickListener() {
1762 public void onClick(DialogInterface dialog, int which) {
1763 cleanup();
1764 }
1765 }
1766 );
1767 builder.setPositiveButton(getString(R.string.rename_action),
1768 new Dialog.OnClickListener() {
1769 public void onClick(DialogInterface dialog, int which) {
1770 changeFolderName();
1771 }
1772 }
1773 );
1774 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001775
1776 final AlertDialog dialog = builder.create();
1777 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1778 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001779 mInput.requestFocus();
1780 InputMethodManager inputManager = (InputMethodManager)
1781 getSystemService(Context.INPUT_METHOD_SERVICE);
1782 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001783 }
1784 });
1785
1786 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 }
1788
1789 private void changeFolderName() {
1790 final String name = mInput.getText().toString();
1791 if (!TextUtils.isEmpty(name)) {
1792 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 mFolderInfo.title = name;
1795 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1796
Joe Onorato9c1289c2009-08-17 11:03:03 -04001797 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001798 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001799 mModel.setWorkspaceDirty();
1800 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 } else {
1802 final FolderIcon folderIcon = (FolderIcon)
1803 mWorkspace.getViewForTag(mFolderInfo);
1804 if (folderIcon != null) {
1805 folderIcon.setText(name);
1806 getWorkspace().requestLayout();
1807 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001808 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001809 mModel.setWorkspaceDirty();
1810 mWorkspaceLoading = true;
1811 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
1813 }
1814 }
1815 cleanup();
1816 }
1817
1818 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 dismissDialog(DIALOG_RENAME_FOLDER);
1820 mWaitingForResult = false;
1821 mFolderInfo = null;
1822 }
1823 }
1824
Joe Onoratofb0ca672009-09-14 17:55:46 -04001825 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001826 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001827 }
1828
Joe Onorato3a8820b2009-11-10 15:06:42 -08001829 boolean isAllAppsOpaque() {
1830 return mAllAppsGrid.isOpaque();
1831 }
1832
1833 void showAllApps(boolean animated) {
1834 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato7bb17492009-09-24 17:51:01 -07001835 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001836
Joe Onorato3a8820b2009-11-10 15:06:42 -08001837 mWorkspace.startFading(false);
1838
Mike Cleronb6082fa02009-10-19 17:03:36 -07001839 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001840 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001841
Joe Onorato7c312c12009-08-13 21:36:53 -07001842 // TODO: fade these two too
1843 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001844 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001845 }
1846
Joe Onorato7bb17492009-09-24 17:51:01 -07001847 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001848 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001849 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001850 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001851 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001852 mWorkspace.startFading(true);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001853
Joe Onorato7c312c12009-08-13 21:36:53 -07001854 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001855 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001856 mDeleteZone.setVisibility(View.VISIBLE);
1857 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001858 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001859 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001860 }
1861
Joe Onorato7c312c12009-08-13 21:36:53 -07001862 void lockAllApps() {
1863 // TODO
1864 }
1865
1866 void unlockAllApps() {
1867 // TODO
1868 }
1869
Joe Onorato7404ee42009-07-31 11:54:44 -07001870 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 * Displays the shortcut creation dialog and launches, if necessary, the
1872 * appropriate activity.
1873 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001874 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001875 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1876 DialogInterface.OnShowListener {
1877
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001879
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 Dialog createDialog() {
1881 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001882
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001884
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1886 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001887 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001888
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 builder.setInverseBackgroundForced(true);
1890
1891 AlertDialog dialog = builder.create();
1892 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001893 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001894 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 return dialog;
1897 }
1898
1899 public void onCancel(DialogInterface dialog) {
1900 mWaitingForResult = false;
1901 cleanup();
1902 }
1903
Romain Guycbb89e42009-06-08 15:52:54 -07001904 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001905 }
1906
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 dismissDialog(DIALOG_CREATE_SHORTCUT);
1909 }
1910
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001911 /**
1912 * Handle the action clicked in the "Add to home" dialog.
1913 */
1914 public void onClick(DialogInterface dialog, int which) {
1915 Resources res = getResources();
1916 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001917
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001918 switch (which) {
1919 case AddAdapter.ITEM_SHORTCUT: {
1920 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001921 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001922 break;
1923 }
Romain Guycbb89e42009-06-08 15:52:54 -07001924
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001925 case AddAdapter.ITEM_APPWIDGET: {
1926 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001927
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001928 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1929 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1930 // add the search widget
1931 ArrayList<AppWidgetProviderInfo> customInfo =
1932 new ArrayList<AppWidgetProviderInfo>();
1933 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1934 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1935 info.label = getString(R.string.group_search);
1936 info.icon = R.drawable.ic_search_widget;
1937 customInfo.add(info);
1938 pickIntent.putParcelableArrayListExtra(
1939 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1940 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1941 Bundle b = new Bundle();
1942 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1943 customExtras.add(b);
1944 pickIntent.putParcelableArrayListExtra(
1945 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1946 // start the pick activity
1947 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1948 break;
1949 }
Romain Guycbb89e42009-06-08 15:52:54 -07001950
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001951 case AddAdapter.ITEM_LIVE_FOLDER: {
1952 // Insert extra item to handle inserting folder
1953 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001954
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001955 ArrayList<String> shortcutNames = new ArrayList<String>();
1956 shortcutNames.add(res.getString(R.string.group_folder));
1957 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001958
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001959 ArrayList<ShortcutIconResource> shortcutIcons =
1960 new ArrayList<ShortcutIconResource>();
1961 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1962 R.drawable.ic_launcher_folder));
1963 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1964
1965 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1966 pickIntent.putExtra(Intent.EXTRA_INTENT,
1967 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1968 pickIntent.putExtra(Intent.EXTRA_TITLE,
1969 getText(R.string.title_select_live_folder));
1970 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001971
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001972 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1973 break;
1974 }
1975
1976 case AddAdapter.ITEM_WALLPAPER: {
1977 startWallpaper();
1978 break;
1979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 }
1981 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001982
1983 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 }
1986
1987 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001988 * Receives notifications when applications are added/removed.
1989 */
1990 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1991 @Override
1992 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001993 closeSystemDialogs();
1994 }
1995 }
1996
1997 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001998 * Receives notifications whenever the appwidgets are reset.
1999 */
2000 private class AppWidgetResetObserver extends ContentObserver {
2001 public AppWidgetResetObserver() {
2002 super(new Handler());
2003 }
2004
2005 @Override
2006 public void onChange(boolean selfChange) {
2007 onAppWidgetReset();
2008 }
2009 }
2010
2011 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002012 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002014 public int getCurrentWorkspaceScreen() {
2015 return mWorkspace.getCurrentScreen();
2016 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002017
Joe Onorato9c1289c2009-08-17 11:03:03 -04002018 /**
2019 * Refreshes the shortcuts shown on the workspace.
2020 *
2021 * Implementation of the method from LauncherModel.Callbacks.
2022 */
2023 public void startBinding() {
2024 final Workspace workspace = mWorkspace;
2025 int count = workspace.getChildCount();
2026 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002027 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002028 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2029 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002030
Joe Onorato9c1289c2009-08-17 11:03:03 -04002031 if (DEBUG_USER_INTERFACE) {
2032 android.widget.Button finishButton = new android.widget.Button(this);
2033 finishButton.setText("Finish");
2034 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2035
2036 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2037 public void onClick(View v) {
2038 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002039 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002040 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 }
2042 }
2043
2044 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002045 * Bind the items start-end from the list.
2046 *
2047 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002049 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2050
2051 final Workspace workspace = mWorkspace;
2052
2053 for (int i=start; i<end; i++) {
2054 final ItemInfo item = shortcuts.get(i);
2055 mDesktopItems.add(item);
2056 switch (item.itemType) {
2057 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2058 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2059 final View shortcut = createShortcut((ApplicationInfo) item);
2060 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2061 false);
2062 break;
2063 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2064 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2065 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2066 (UserFolderInfo) item);
2067 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2068 false);
2069 break;
2070 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2071 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2072 R.layout.live_folder_icon, this,
2073 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2074 (LiveFolderInfo) item);
2075 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2076 false);
2077 break;
2078 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2079 final int screen = workspace.getCurrentScreen();
2080 final View view = mInflater.inflate(R.layout.widget_search,
2081 (ViewGroup) workspace.getChildAt(screen), false);
2082
2083 Search search = (Search) view.findViewById(R.id.widget_search);
2084 search.setLauncher(this);
2085
2086 final Widget widget = (Widget) item;
2087 view.setTag(widget);
2088
2089 workspace.addWidget(view, widget, false);
2090 break;
2091 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 }
2093
Joe Onorato9c1289c2009-08-17 11:03:03 -04002094 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 }
2096
Joe Onorato9c1289c2009-08-17 11:03:03 -04002097 /**
2098 * Implementation of the method from LauncherModel.Callbacks.
2099 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002100 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2101 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002102 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002104
2105 /**
2106 * Add the views for a widget to the workspace.
2107 *
2108 * Implementation of the method from LauncherModel.Callbacks.
2109 */
2110 public void bindAppWidget(LauncherAppWidgetInfo item) {
2111 final Workspace workspace = mWorkspace;
2112
2113 final int appWidgetId = item.appWidgetId;
2114 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2115 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2116
Joe Onorato9c1289c2009-08-17 11:03:03 -04002117 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2118 item.hostView.setTag(item);
2119
2120 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2121 item.cellY, item.spanX, item.spanY, false);
2122
2123 workspace.requestLayout();
2124
2125 mDesktopItems.add(item);
2126 }
2127
2128 /**
2129 * Callback saying that there aren't any more items to bind.
2130 *
2131 * Implementation of the method from LauncherModel.Callbacks.
2132 */
2133 public void finishBindingItems() {
2134 if (mSavedState != null) {
2135 if (!mWorkspace.hasFocus()) {
2136 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2137 }
2138
2139 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2140 if (userFolders != null) {
2141 for (long folderId : userFolders) {
2142 final FolderInfo info = mFolders.get(folderId);
2143 if (info != null) {
2144 openFolder(info);
2145 }
2146 }
2147 final Folder openFolder = mWorkspace.getOpenFolder();
2148 if (openFolder != null) {
2149 openFolder.requestFocus();
2150 }
2151 }
2152
Joe Onorato9c1289c2009-08-17 11:03:03 -04002153 mSavedState = null;
2154 }
2155
2156 if (mSavedInstanceState != null) {
2157 super.onRestoreInstanceState(mSavedInstanceState);
2158 mSavedInstanceState = null;
2159 }
2160
Joe Onorato9c1289c2009-08-17 11:03:03 -04002161 mWorkspaceLoading = false;
2162 }
2163
2164 /**
2165 * Add the icons for all apps.
2166 *
2167 * Implementation of the method from LauncherModel.Callbacks.
2168 */
2169 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002170 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002171 }
2172
2173 /**
2174 * A package was installed.
2175 *
2176 * Implementation of the method from LauncherModel.Callbacks.
2177 */
2178 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2179 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002180 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181 }
2182
2183 /**
2184 * A package was updated.
2185 *
2186 * Implementation of the method from LauncherModel.Callbacks.
2187 */
2188 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2189 removeDialog(DIALOG_CREATE_SHORTCUT);
2190 mWorkspace.updateShortcutsForPackage(packageName);
2191 }
2192
2193 /**
2194 * A package was uninstalled.
2195 *
2196 * Implementation of the method from LauncherModel.Callbacks.
2197 */
2198 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2199 removeDialog(DIALOG_CREATE_SHORTCUT);
2200 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002201 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002202 }
Joe Onoratobe386092009-11-17 17:32:16 -08002203
2204 /**
2205 * Prints out out state for debugging.
2206 */
2207 public void dumpState() {
2208 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002209 Log.d(TAG, "mSavedState=" + mSavedState);
2210 Log.d(TAG, "mIsNewIntent=" + mIsNewIntent);
2211 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2212 Log.d(TAG, "mRestoring=" + mRestoring);
2213 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2214 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2215 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2216 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002217 mModel.dumpState();
2218 mAllAppsGrid.dumpState();
2219 Log.d(TAG, "END launcher2 dump state");
2220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221}