blob: 9bcb537883cb3d15e40472d61481d6f6299c1e64 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Context;
29import android.content.DialogInterface;
30import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070031import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.ActivityInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070036import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.graphics.drawable.Drawable;
39import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.os.Bundle;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070041import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070042import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070043import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.os.Parcelable;
45import android.os.RemoteException;
46import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070047import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.text.Selection;
49import android.text.SpannableStringBuilder;
50import android.text.TextUtils;
51import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070052import static android.util.Log.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070053import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.view.KeyEvent;
56import android.view.LayoutInflater;
57import android.view.Menu;
58import android.view.MenuItem;
59import android.view.View;
60import android.view.ViewGroup;
Romain Guyb1b69f52009-08-10 15:10:15 -070061import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.view.View.OnLongClickListener;
63import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.widget.TextView;
66import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070067import android.appwidget.AppWidgetManager;
68import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
70import java.lang.ref.WeakReference;
71import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040072import java.util.HashMap;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070073import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070074import java.io.DataOutputStream;
75import java.io.FileNotFoundException;
76import java.io.IOException;
77import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
79/**
80 * Default launcher application.
81 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040082public final class Launcher extends Activity
83 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040085 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 static final boolean LOGD = false;
87
Joe Onorato9c1289c2009-08-17 11:03:03 -040088 static final boolean PROFILE_STARTUP = false;
89 static final boolean PROFILE_ROTATE = false;
90 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070091
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 private static final int WALLPAPER_SCREENS_SPAN = 2;
93
94 private static final int MENU_GROUP_ADD = 1;
95 private static final int MENU_ADD = Menu.FIRST + 1;
96 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
97 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
98 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -070099 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100
101 private static final int REQUEST_CREATE_SHORTCUT = 1;
102 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700103 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 private static final int REQUEST_PICK_APPLICATION = 6;
105 private static final int REQUEST_PICK_SHORTCUT = 7;
106 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700107 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
109 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
110
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700111 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
112 static final String SEARCH_WIDGET = "search_widget";
113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 static final int SCREEN_COUNT = 3;
115 static final int DEFAULT_SCREN = 1;
116 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700117 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato7c312c12009-08-13 21:36:53 -0700119 static final int DIALOG_CREATE_SHORTCUT = 1;
120 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Romain Guy98d01652009-06-30 16:21:04 -0700122 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
124 // Type: int
125 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
126 // Type: boolean
127 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
128 // Type: long
129 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
130 // Type: int
131 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
144 // Type: int[]
145 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
146 // Type: boolean
147 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
148 // Type: long
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
150
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final Object sLock = new Object();
154 private static int sScreen = DEFAULT_SCREN;
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 private LayoutInflater mInflater;
157
Joe Onorato00acb122009-08-04 16:04:30 -0400158 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700160
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700161 private AppWidgetManager mAppWidgetManager;
162 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private CellLayout.CellInfo mAddItemCellInfo;
165 private CellLayout.CellInfo mMenuAddInfo;
166 private final int[] mCellCoordinates = new int[2];
167 private FolderInfo mFolderInfo;
168
Joe Onorato7c312c12009-08-13 21:36:53 -0700169 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700171 private AllAppsView mAllAppsGrid;
172 private boolean mAllAppsVisible;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 private Bundle mSavedState;
175
176 private SpannableStringBuilder mDefaultKeySsb = null;
177
Mike LeBeau736cf282009-07-02 17:46:59 -0700178 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 private boolean mWorkspaceLoading = true;
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private boolean mRestoring;
183 private boolean mWaitingForResult;
184 private boolean mLocaleChanged;
185
Dianne Hackborn67800862009-07-24 17:15:20 -0700186 private boolean mHomeDown;
187 private boolean mBackDown;
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private Bundle mSavedInstanceState;
190
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 private LauncherModel mModel;
192
193 private ArrayList<ItemInfo> mDesktopItems = new ArrayList();
194 private static HashMap<Long, FolderInfo> mFolders = new HashMap();
195 private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList();
Romain Guycbb89e42009-06-08 15:52:54 -0700196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 @Override
198 protected void onCreate(Bundle savedInstanceState) {
199 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700200
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700206 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
207 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 if (PROFILE_STARTUP) {
210 android.os.Debug.startMethodTracing("/sdcard/launcher");
211 }
212
213 checkForLocaleChange();
214 setWallpaperDimension();
215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 setContentView(R.layout.launcher);
217 setupViews();
218
Joe Onorato7c312c12009-08-13 21:36:53 -0700219 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 mSavedState = savedInstanceState;
222 restoreState(mSavedState);
223
224 if (PROFILE_STARTUP) {
225 android.os.Debug.stopMethodTracing();
226 }
227
228 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 }
231
232 // For handling default keys
233 mDefaultKeySsb = new SpannableStringBuilder();
234 Selection.setSelection(mDefaultKeySsb, 0);
235 }
Romain Guycbb89e42009-06-08 15:52:54 -0700236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700238 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
239 readConfiguration(this, localeConfiguration);
240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 final Configuration configuration = getResources().getConfiguration();
242
Romain Guy98d01652009-06-30 16:21:04 -0700243 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 final String locale = configuration.locale.toString();
245
Romain Guy98d01652009-06-30 16:21:04 -0700246 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 final int mcc = configuration.mcc;
248
Romain Guy98d01652009-06-30 16:21:04 -0700249 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 final int mnc = configuration.mnc;
251
252 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
253
254 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700255 localeConfiguration.locale = locale;
256 localeConfiguration.mcc = mcc;
257 localeConfiguration.mnc = mnc;
258
259 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400260 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700261 }
262 }
263
264 private static class LocaleConfiguration {
265 public String locale;
266 public int mcc = -1;
267 public int mnc = -1;
268 }
269
270 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
271 DataInputStream in = null;
272 try {
273 in = new DataInputStream(context.openFileInput(PREFERENCES));
274 configuration.locale = in.readUTF();
275 configuration.mcc = in.readInt();
276 configuration.mnc = in.readInt();
277 } catch (FileNotFoundException e) {
278 // Ignore
279 } catch (IOException e) {
280 // Ignore
281 } finally {
282 if (in != null) {
283 try {
284 in.close();
285 } catch (IOException e) {
286 // Ignore
287 }
288 }
289 }
290 }
291
292 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
293 DataOutputStream out = null;
294 try {
295 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
296 out.writeUTF(configuration.locale);
297 out.writeInt(configuration.mcc);
298 out.writeInt(configuration.mnc);
299 out.flush();
300 } catch (FileNotFoundException e) {
301 // Ignore
302 } catch (IOException e) {
303 //noinspection ResultOfMethodCallIgnored
304 context.getFileStreamPath(PREFERENCES).delete();
305 } finally {
306 if (out != null) {
307 try {
308 out.close();
309 } catch (IOException e) {
310 // Ignore
311 }
312 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 }
314 }
315
316 static int getScreen() {
317 synchronized (sLock) {
318 return sScreen;
319 }
320 }
321
322 static void setScreen(int screen) {
323 synchronized (sLock) {
324 sScreen = screen;
325 }
326 }
327
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700329 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330
331 Display display = getWindowManager().getDefaultDisplay();
332 boolean isPortrait = display.getWidth() < display.getHeight();
333
334 final int width = isPortrait ? display.getWidth() : display.getHeight();
335 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700336 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 }
338
339 @Override
340 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700341 mWaitingForResult = false;
342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 // The pattern used here is that a user PICKs a specific application,
344 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700345
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
347 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700348
Romain Guy94dabf12009-07-21 10:55:43 -0700349 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 switch (requestCode) {
351 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400352 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 break;
354 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700355 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 break;
357 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400358 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 break;
360 case REQUEST_PICK_LIVE_FOLDER:
361 addLiveFolder(data);
362 break;
363 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400364 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700366 case REQUEST_PICK_APPWIDGET:
367 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700369 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400370 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 break;
372 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700373 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700375 // Clean up the appWidgetId if we canceled
376 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
377 if (appWidgetId != -1) {
378 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 }
380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 }
382
383 @Override
384 protected void onResume() {
385 super.onResume();
386
Joe Onorato9c1289c2009-08-17 11:03:03 -0400387 Log.d(TAG, "onResume mRestoring=" + mRestoring);
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390 mWorkspaceLoading = true;
391 mModel.startLoader(this, true);
392 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700394
395 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
396 // onNewIntent), then close the search dialog if needed, because it probably
397 // came from the user pressing 'home' (rather than, for example, pressing 'back').
398 if (mIsNewIntent) {
399 // Post to a handler so that this happens after the search dialog tries to open
400 // itself again.
401 mWorkspace.post(new Runnable() {
402 public void run() {
403 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
404 ServiceManager.getService(Context.SEARCH_SERVICE));
405 try {
406 searchManagerService.stopSearch();
407 } catch (RemoteException e) {
408 e(LOG_TAG, "error stopping search", e);
409 }
410 }
411 });
412 }
413
414 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 }
416
417 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700418 protected void onPause() {
419 super.onPause();
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700420 closeAllAppsDialog(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700421 }
Romain Guycbb89e42009-06-08 15:52:54 -0700422
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700423 @Override
424 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425 // Flag the loader to stop early before switching
426 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700428 if (PROFILE_ROTATE) {
429 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
430 }
431 return null;
432 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 private boolean acceptFilter() {
435 final InputMethodManager inputManager = (InputMethodManager)
436 getSystemService(Context.INPUT_METHOD_SERVICE);
437 return !inputManager.isFullscreenMode();
438 }
439
440 @Override
441 public boolean onKeyDown(int keyCode, KeyEvent event) {
442 boolean handled = super.onKeyDown(keyCode, event);
443 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
444 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
445 keyCode, event);
446 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700447 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700448 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700449 // showSearchDialog()
450 // If there are multiple keystrokes before the search dialog takes focus,
451 // onSearchRequested() will be called for every keystroke,
452 // but it is idempotent, so it's fine.
453 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
455 }
456
457 return handled;
458 }
459
Karl Rosaen138a0412009-04-23 19:00:21 -0700460 private String getTypedText() {
461 return mDefaultKeySsb.toString();
462 }
463
464 private void clearTypedText() {
465 mDefaultKeySsb.clear();
466 mDefaultKeySsb.clearSpans();
467 Selection.setSelection(mDefaultKeySsb, 0);
468 }
469
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 /**
471 * Restores the previous state, if it exists.
472 *
473 * @param savedState The previous state.
474 */
475 private void restoreState(Bundle savedState) {
476 if (savedState == null) {
477 return;
478 }
479
480 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
481 if (currentScreen > -1) {
482 mWorkspace.setCurrentScreen(currentScreen);
483 }
484
485 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
486 if (addScreen > -1) {
487 mAddItemCellInfo = new CellLayout.CellInfo();
488 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
489 addItemCellInfo.valid = true;
490 addItemCellInfo.screen = addScreen;
491 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
492 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
493 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
494 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
495 addItemCellInfo.findVacantCellsFromOccupied(
496 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
497 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
498 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
499 mRestoring = true;
500 }
501
502 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
503 if (renameFolder) {
504 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400505 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800506 mRestoring = true;
507 }
508 }
509
510 /**
511 * Finds all the views we need and configure them properly.
512 */
513 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400514 mDragController = new DragController(this);
515 DragController dragController = mDragController;
516
Romain Guyb1b69f52009-08-10 15:10:15 -0700517 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400518 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800519
Joe Onorato7c312c12009-08-13 21:36:53 -0700520 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700521 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700522 mAllAppsGrid.setDragController(dragController);
Joe Onorato7c312c12009-08-13 21:36:53 -0700523
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
525 final Workspace workspace = mWorkspace;
526
Joe Onorato7c312c12009-08-13 21:36:53 -0700527 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
528 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529
Joe Onorato7c312c12009-08-13 21:36:53 -0700530 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700531 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700532 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700533 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700534 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700535 handleIocon.setCrossFadeEnabled(true);
536 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400539 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541
542 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400543 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700544 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545
Joe Onorato00acb122009-08-04 16:04:30 -0400546 dragController.setDragScoller(workspace);
547 dragController.setDragListener(deleteZone);
548 dragController.setScrollView(dragLayer);
549
550 // The order here is bottom to top.
551 dragController.addDropTarget(workspace);
552 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 }
554
555 /**
556 * Creates a view representing a shortcut.
557 *
558 * @param info The data structure describing the shortcut.
559 *
560 * @return A View inflated from R.layout.application.
561 */
562 View createShortcut(ApplicationInfo info) {
563 return createShortcut(R.layout.application,
564 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
565 }
566
567 /**
568 * Creates a view representing a shortcut inflated from the specified resource.
569 *
570 * @param layoutResId The id of the XML layout used to create the shortcut.
571 * @param parent The group the shortcut belongs to.
572 * @param info The data structure describing the shortcut.
573 *
574 * @return A View inflated from layoutResId.
575 */
576 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
577 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
578
Joe Onorato5162ea92009-09-03 09:39:42 -0700579 if (info.icon == null) {
580 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
581 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700583 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 info.filtered = true;
585 }
586
587 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
588 favorite.setText(info.title);
589 favorite.setTag(info);
590 favorite.setOnClickListener(this);
591
592 return favorite;
593 }
594
595 /**
596 * Add an application shortcut to the workspace.
597 *
598 * @param data The intent describing the application.
599 * @param cellInfo The position on screen where to create the shortcut.
600 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400601 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 cellInfo.screen = mWorkspace.getCurrentScreen();
603 if (!findSingleSlot(cellInfo)) return;
604
Romain Guy73b979d2009-06-09 12:57:21 -0700605 final ApplicationInfo info = infoFromApplicationIntent(context, data);
606 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400607 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700608 }
609 }
610
611 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 ComponentName component = data.getComponent();
613 PackageManager packageManager = context.getPackageManager();
614 ActivityInfo activityInfo = null;
615 try {
616 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
617 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700618 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 }
Romain Guycbb89e42009-06-08 15:52:54 -0700620
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 if (activityInfo != null) {
622 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 itemInfo.title = activityInfo.loadLabel(packageManager);
625 if (itemInfo.title == null) {
626 itemInfo.title = activityInfo.name;
627 }
Romain Guycbb89e42009-06-08 15:52:54 -0700628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
630 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
631 itemInfo.icon = activityInfo.loadIcon(packageManager);
632 itemInfo.container = ItemInfo.NO_ID;
633
Romain Guy73b979d2009-06-09 12:57:21 -0700634 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 }
Romain Guy73b979d2009-06-09 12:57:21 -0700636
637 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 }
Romain Guycbb89e42009-06-08 15:52:54 -0700639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 /**
641 * Add a shortcut to the workspace.
642 *
643 * @param data The intent describing the shortcut.
644 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400646 private void completeAddShortcut(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;
Romain Guycbb89e42009-06-08 15:52:54 -0700649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
651
652 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
655 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 }
657 }
658
Romain Guycbb89e42009-06-08 15:52:54 -0700659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700661 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700663 * @param data The intent describing the appWidgetId.
664 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700668 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700669
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700670 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700671
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700672 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700673
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700674 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700677
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 // Try finding open space on Launcher screen
679 final int[] xy = mCellCoordinates;
680 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
681
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700682 // Build Launcher-specific widget info and save to database
683 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 launcherInfo.spanX = spans[0];
685 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700686
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 LauncherModel.addItemToDatabase(this, launcherInfo,
688 LauncherSettings.Favorites.CONTAINER_DESKTOP,
689 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
690
691 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400692 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700695 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700696
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700699
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 }
703 }
Romain Guycbb89e42009-06-08 15:52:54 -0700704
Joe Onorato9c1289c2009-08-17 11:03:03 -0400705 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
706 mDesktopItems.remove(launcherInfo);
707 launcherInfo.hostView = null;
708 }
709
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700710 public LauncherAppWidgetHost getAppWidgetHost() {
711 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 static ApplicationInfo addShortcut(Context context, Intent data,
715 CellLayout.CellInfo cellInfo, boolean notify) {
716
Romain Guy73b979d2009-06-09 12:57:21 -0700717 final ApplicationInfo info = infoFromShortcutIntent(context, data);
718 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
719 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
720
721 return info;
722 }
723
724 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
726 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
727 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
728
729 Drawable icon = null;
730 boolean filtered = false;
731 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700732 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733
734 if (bitmap != null) {
735 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
736 filtered = true;
737 customIcon = true;
738 } else {
739 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700740 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700742 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 final PackageManager packageManager = context.getPackageManager();
744 Resources resources = packageManager.getResourcesForApplication(
745 iconResource.packageName);
746 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
747 icon = resources.getDrawable(id);
748 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700749 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
751 }
752 }
753
754 if (icon == null) {
755 icon = context.getPackageManager().getDefaultActivityIcon();
756 }
757
758 final ApplicationInfo info = new ApplicationInfo();
759 info.icon = icon;
760 info.filtered = filtered;
761 info.title = name;
762 info.intent = intent;
763 info.customIcon = customIcon;
764 info.iconResource = iconResource;
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 return info;
767 }
768
769 @Override
770 protected void onNewIntent(Intent intent) {
771 super.onNewIntent(intent);
772
773 // Close the menu
774 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
775 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700776
777 // Set this flag so that onResume knows to close the search dialog if it's open,
778 // because this was a new intent (thus a press of 'home' or some such) rather than
779 // for example onResume being called when the user pressed the 'back' button.
780 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781
782 try {
783 dismissDialog(DIALOG_CREATE_SHORTCUT);
784 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 } catch (Exception e) {
786 // An exception is thrown if the dialog is not visible, which is fine
787 }
788
789 try {
790 dismissDialog(DIALOG_RENAME_FOLDER);
791 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 } catch (Exception e) {
793 // An exception is thrown if the dialog is not visible, which is fine
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
797 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700798
Romain Guy94dabf12009-07-21 10:55:43 -0700799 if (!mWorkspace.isDefaultScreenShowing()) {
800 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700802
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700803 closeAllAppsDialog(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700804
805 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 if (v != null && v.getWindowToken() != null) {
807 InputMethodManager imm = (InputMethodManager)getSystemService(
808 INPUT_METHOD_SERVICE);
809 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
810 }
811 } else {
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700812 closeAllAppsDialog(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 }
814 }
815 }
816
817 @Override
818 protected void onRestoreInstanceState(Bundle savedInstanceState) {
819 // Do not call super here
820 mSavedInstanceState = savedInstanceState;
821 }
822
823 @Override
824 protected void onSaveInstanceState(Bundle outState) {
825 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
826
827 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
828 if (folders.size() > 0) {
829 final int count = folders.size();
830 long[] ids = new long[count];
831 for (int i = 0; i < count; i++) {
832 final FolderInfo info = folders.get(i).getInfo();
833 ids[i] = info.id;
834 }
835 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
836 } else {
837 super.onSaveInstanceState(outState);
838 }
839
Romain Guy3cf604f2009-06-16 13:12:53 -0700840 final boolean isConfigurationChange = getChangingConfigurations() != 0;
841
Romain Guy5a941392009-04-28 15:18:25 -0700842 // When the drawer is opened and we are saving the state because of a
843 // configuration change
Joe Onorato7c312c12009-08-13 21:36:53 -0700844 if (mAllAppsVisible && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847
848 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
849 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
850 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
851
852 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
853 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
854 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
855 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
856 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
857 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
858 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
859 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
860 layout.getOccupiedCells());
861 }
862
863 if (mFolderInfo != null && mWaitingForResult) {
864 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
865 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
866 }
867 }
868
869 @Override
870 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700872
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700874 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700876 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 }
878
879 TextKeyListener.getInstance().release();
880
Joe Onorato9c1289c2009-08-17 11:03:03 -0400881 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883 unbindDesktopItems();
884 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886
887 @Override
888 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700889 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 super.startActivityForResult(intent, requestCode);
891 }
892
893 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700894 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700896
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700897 closeAllAppsDialog(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700898
Karl Rosaen138a0412009-04-23 19:00:21 -0700899 // Slide the search widget to the top, if it's on the current screen,
900 // otherwise show the search dialog immediately.
901 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
902 if (searchWidget == null) {
903 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
904 } else {
905 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
906 // show the currently typed text in the search widget while sliding
907 searchWidget.setQuery(getTypedText());
908 }
909 }
Romain Guycbb89e42009-06-08 15:52:54 -0700910
Karl Rosaen138a0412009-04-23 19:00:21 -0700911 /**
912 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700913 *
914 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700915 */
Romain Guycbb89e42009-06-08 15:52:54 -0700916 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700917 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700918
Karl Rosaen138a0412009-04-23 19:00:21 -0700919 if (initialQuery == null) {
920 // Use any text typed in the launcher as the initial query
921 initialQuery = getTypedText();
922 clearTypedText();
923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 if (appSearchData == null) {
925 appSearchData = new Bundle();
926 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
927 }
Romain Guycbb89e42009-06-08 15:52:54 -0700928
Karl Rosaen138a0412009-04-23 19:00:21 -0700929 final SearchManager searchManager =
930 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
931
932 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
933 if (searchWidget != null) {
934 // This gets called when the user leaves the search dialog to go back to
935 // the Launcher.
936 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
937 public void onCancel() {
938 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700939 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700940 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700941 });
942 }
Romain Guycbb89e42009-06-08 15:52:54 -0700943
Karl Rosaen138a0412009-04-23 19:00:21 -0700944 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700945 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 }
947
Karl Rosaen138a0412009-04-23 19:00:21 -0700948 /**
949 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700950 */
Romain Guy5a941392009-04-28 15:18:25 -0700951 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700952 // Close search dialog
953 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100954 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700955 // Restore search widget to its normal position
956 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
957 if (searchWidget != null) {
958 searchWidget.stopSearch(false);
959 }
960 }
Romain Guycbb89e42009-06-08 15:52:54 -0700961
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 @Override
963 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400964 if (isWorkspaceLocked()) {
965 return false;
966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967
968 super.onCreateOptionsMenu(menu);
969 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
970 .setIcon(android.R.drawable.ic_menu_add)
971 .setAlphabeticShortcut('A');
972 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
973 .setIcon(android.R.drawable.ic_menu_gallery)
974 .setAlphabeticShortcut('W');
975 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
976 .setIcon(android.R.drawable.ic_search_category_default)
977 .setAlphabeticShortcut(SearchManager.MENU_KEY);
978 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
979 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
980 .setAlphabeticShortcut('N');
981
982 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700983 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
984 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985
986 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
987 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
988 .setIntent(settings);
989
990 return true;
991 }
992
993 @Override
994 public boolean onPrepareOptionsMenu(Menu menu) {
995 super.onPrepareOptionsMenu(menu);
996
997 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
998 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
999
1000 return true;
1001 }
1002
1003 @Override
1004 public boolean onOptionsItemSelected(MenuItem item) {
1005 switch (item.getItemId()) {
1006 case MENU_ADD:
1007 addItems();
1008 return true;
1009 case MENU_WALLPAPER_SETTINGS:
1010 startWallpaper();
1011 return true;
1012 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001013 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 return true;
1015 case MENU_NOTIFICATIONS:
1016 showNotifications();
1017 return true;
1018 }
1019
1020 return super.onOptionsItemSelected(item);
1021 }
Romain Guycbb89e42009-06-08 15:52:54 -07001022
Karl Rosaen138a0412009-04-23 19:00:21 -07001023 /**
1024 * Indicates that we want global search for this activity by setting the globalSearch
1025 * argument for {@link #startSearch} to true.
1026 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001028 @Override
1029 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001030 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001031 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001032 }
1033
Joe Onorato9c1289c2009-08-17 11:03:03 -04001034 public boolean isWorkspaceLocked() {
1035 return mWorkspaceLoading || mWaitingForResult;
1036 }
1037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 private void addItems() {
1039 showAddDialog(mMenuAddInfo);
1040 }
1041
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001042 void addAppWidget(Intent data) {
1043 // TODO: catch bad widget exception when sent
1044 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001046 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1047 if (SEARCH_WIDGET.equals(customWidget)) {
1048 // We don't need this any more, since this isn't a real app widget.
1049 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1050 // add the search widget
1051 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001053 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1054
1055 if (appWidget.configure != null) {
1056 // Launch over to configure widget, if needed
1057 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1058 intent.setComponent(appWidget.configure);
1059 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1060
1061 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1062 } else {
1063 // Otherwise just add it
1064 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 }
1067 }
Romain Guycbb89e42009-06-08 15:52:54 -07001068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 void addSearch() {
1070 final Widget info = Widget.makeSearch();
1071 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001072
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 final int[] xy = mCellCoordinates;
1074 final int spanX = info.spanX;
1075 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1080 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 final View view = mInflater.inflate(info.layoutResource, null);
1083 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001084 Search search = (Search) view.findViewById(R.id.widget_search);
1085 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1088 }
1089
Romain Guy73b979d2009-06-09 12:57:21 -07001090 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001091 // Handle case where user selected "Applications"
1092 String applicationName = getResources().getString(R.string.group_applications);
1093 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001094
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001095 if (applicationName != null && applicationName.equals(shortcutName)) {
1096 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1097 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001098
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001099 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1100 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001101 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001102 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001103 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001104 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 }
1106
1107 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001108 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001109 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001110 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001111
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001112 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001113 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001114 } else {
1115 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
1118
Joe Onorato9c1289c2009-08-17 11:03:03 -04001119 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 UserFolderInfo folderInfo = new UserFolderInfo();
1121 folderInfo.title = getText(R.string.folder_name);
1122
1123 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1124 cellInfo.screen = mWorkspace.getCurrentScreen();
1125 if (!findSingleSlot(cellInfo)) return;
1126
1127 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001128 LauncherModel.addItemToDatabase(this, folderInfo,
1129 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132
1133 // Create the view
1134 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1135 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1136 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001137 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
Romain Guycbb89e42009-06-08 15:52:54 -07001139
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140 void removeFolder(FolderInfo folder) {
1141 mFolders.remove(folder.id);
1142 }
1143
1144 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 cellInfo.screen = mWorkspace.getCurrentScreen();
1146 if (!findSingleSlot(cellInfo)) return;
1147
1148 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1149
1150 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1152 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001153 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1154 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
1156 }
1157
1158 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1159 CellLayout.CellInfo cellInfo, boolean notify) {
1160
1161 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1162 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1163
1164 Drawable icon = null;
1165 boolean filtered = false;
1166 Intent.ShortcutIconResource iconResource = null;
1167
1168 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1169 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1170 try {
1171 iconResource = (Intent.ShortcutIconResource) extra;
1172 final PackageManager packageManager = context.getPackageManager();
1173 Resources resources = packageManager.getResourcesForApplication(
1174 iconResource.packageName);
1175 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1176 icon = resources.getDrawable(id);
1177 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001178 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 }
1180 }
1181
1182 if (icon == null) {
1183 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1184 }
1185
1186 final LiveFolderInfo info = new LiveFolderInfo();
1187 info.icon = icon;
1188 info.filtered = filtered;
1189 info.title = name;
1190 info.iconResource = iconResource;
1191 info.uri = data.getData();
1192 info.baseIntent = baseIntent;
1193 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1194 LiveFolders.DISPLAY_MODE_GRID);
1195
1196 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1197 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001198 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199
1200 return info;
1201 }
1202
1203 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1204 final int[] xy = new int[2];
1205 if (findSlot(cellInfo, xy, 1, 1)) {
1206 cellInfo.cellX = xy[0];
1207 cellInfo.cellY = xy[1];
1208 return true;
1209 }
1210 return false;
1211 }
1212
1213 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1214 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1215 boolean[] occupied = mSavedState != null ?
1216 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1217 cellInfo = mWorkspace.findAllVacantCells(occupied);
1218 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1219 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1220 return false;
1221 }
1222 }
1223 return true;
1224 }
1225
1226 private void showNotifications() {
1227 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1228 if (statusBar != null) {
1229 statusBar.expand();
1230 }
1231 }
1232
1233 private void startWallpaper() {
1234 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1235 startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper)));
1236 }
1237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001239 public void onWindowFocusChanged(boolean hasFocus) {
1240 super.onWindowFocusChanged(hasFocus);
1241 if (!hasFocus) {
1242 mBackDown = mHomeDown = false;
1243 }
1244 }
1245
1246 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 public boolean dispatchKeyEvent(KeyEvent event) {
1248 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1249 switch (event.getKeyCode()) {
1250 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001251 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 return true;
1253 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001254 mHomeDown = true;
1255 return true;
1256 }
1257 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1258 switch (event.getKeyCode()) {
1259 case KeyEvent.KEYCODE_BACK:
1260 if (!event.isCanceled()) {
1261 mWorkspace.dispatchKeyEvent(event);
Joe Onorato7c312c12009-08-13 21:36:53 -07001262 if (mAllAppsVisible) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001263 closeAllAppsDialog(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001264 } else {
1265 closeFolder();
1266 }
1267 }
1268 mBackDown = false;
1269 return true;
1270 case KeyEvent.KEYCODE_HOME:
1271 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 return true;
1273 }
1274 }
1275
1276 return super.dispatchKeyEvent(event);
1277 }
1278
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 private void closeFolder() {
1280 Folder folder = mWorkspace.getOpenFolder();
1281 if (folder != null) {
1282 closeFolder(folder);
1283 }
1284 }
1285
1286 void closeFolder(Folder folder) {
1287 folder.getInfo().opened = false;
1288 ViewGroup parent = (ViewGroup) folder.getParent();
1289 if (parent != null) {
1290 parent.removeView(folder);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001291 // TODO: this line crashes.
1292 //mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 }
1294 folder.onClose();
1295 }
1296
1297 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001298 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1299 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001301 private void unbindDesktopItems() {
1302 for (ItemInfo item: mDesktopItems) {
1303 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
1305 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001306
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 /**
1308 * Launches the intent referred by the clicked shortcut.
1309 *
1310 * @param v The view representing the clicked shortcut.
1311 */
1312 public void onClick(View v) {
1313 Object tag = v.getTag();
1314 if (tag instanceof ApplicationInfo) {
1315 // Open shortcut
1316 final Intent intent = ((ApplicationInfo) tag).intent;
1317 startActivitySafely(intent);
1318 } else if (tag instanceof FolderInfo) {
1319 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001320 } else if (v == mHandleView) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001321 if (mAllAppsVisible) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001322 // TODO how can we be here?
1323 } else {
Joe Onorato45d43462009-07-31 14:36:51 -07001324 showAllAppsDialog();
Joe Onorato7404ee42009-07-31 11:54:44 -07001325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
1327 }
1328
1329 void startActivitySafely(Intent intent) {
1330 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1331 try {
1332 startActivity(intent);
1333 } catch (ActivityNotFoundException e) {
1334 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1335 } catch (SecurityException e) {
1336 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001337 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1339 "or use the exported attribute for this activity.", e);
1340 }
1341 }
1342
1343 private void handleFolderClick(FolderInfo folderInfo) {
1344 if (!folderInfo.opened) {
1345 // Close any open folder
1346 closeFolder();
1347 // Open the requested folder
1348 openFolder(folderInfo);
1349 } else {
1350 // Find the open folder...
1351 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1352 int folderScreen;
1353 if (openFolder != null) {
1354 folderScreen = mWorkspace.getScreenForView(openFolder);
1355 // .. and close it
1356 closeFolder(openFolder);
1357 if (folderScreen != mWorkspace.getCurrentScreen()) {
1358 // Close any folder open on the current screen
1359 closeFolder();
1360 // Pull the folder onto this screen
1361 openFolder(folderInfo);
1362 }
1363 }
1364 }
1365 }
1366
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 /**
1368 * Opens the user fodler described by the specified tag. The opening of the folder
1369 * is animated relative to the specified View. If the View is null, no animation
1370 * is played.
1371 *
1372 * @param folderInfo The FolderInfo describing the folder to open.
1373 */
1374 private void openFolder(FolderInfo folderInfo) {
1375 Folder openFolder;
1376
1377 if (folderInfo instanceof UserFolderInfo) {
1378 openFolder = UserFolder.fromXml(this);
1379 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001380 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 } else {
1382 return;
1383 }
1384
Joe Onorato00acb122009-08-04 16:04:30 -04001385 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 openFolder.setLauncher(this);
1387
1388 openFolder.bind(folderInfo);
1389 folderInfo.opened = true;
1390
1391 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1392 openFolder.onOpen();
1393 }
1394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001396 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 return false;
1398 }
1399
1400 if (!(v instanceof CellLayout)) {
1401 v = (View) v.getParent();
1402 }
1403
1404 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1405
1406 // This happens when long clicking an item with the dpad/trackball
1407 if (cellInfo == null) {
1408 return true;
1409 }
1410
1411 if (mWorkspace.allowLongPress()) {
1412 if (cellInfo.cell == null) {
1413 if (cellInfo.valid) {
1414 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001415 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 showAddDialog(cellInfo);
1417 }
1418 } else {
1419 if (!(cellInfo.cell instanceof Folder)) {
1420 // User long pressed on an item
1421 mWorkspace.startDrag(cellInfo);
1422 }
1423 }
1424 }
1425 return true;
1426 }
1427
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001428 View getDrawerHandle() {
1429 return mHandleView;
1430 }
1431
Joe Onorato7c312c12009-08-13 21:36:53 -07001432 boolean isDrawerDown() { // TODO rename to isAllAppsVisible()
1433 return /* TODO !mDrawer.isMoving() && */ !mAllAppsVisible;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 }
1435
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 Workspace getWorkspace() {
1437 return mWorkspace;
1438 }
1439
Joe Onorato7404ee42009-07-31 11:54:44 -07001440 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 GridView getApplicationsGrid() {
1442 return mAllAppsGrid;
1443 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001444 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445
1446 @Override
1447 protected Dialog onCreateDialog(int id) {
1448 switch (id) {
1449 case DIALOG_CREATE_SHORTCUT:
1450 return new CreateShortcut().createDialog();
1451 case DIALOG_RENAME_FOLDER:
1452 return new RenameFolder().createDialog();
1453 }
1454
1455 return super.onCreateDialog(id);
1456 }
1457
1458 @Override
1459 protected void onPrepareDialog(int id, Dialog dialog) {
1460 switch (id) {
1461 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 break;
1463 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001464 if (mFolderInfo != null) {
1465 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1466 final CharSequence text = mFolderInfo.title;
1467 input.setText(text);
1468 input.setSelection(0, text.length());
1469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 break;
1471 }
1472 }
1473
1474 void showRenameDialog(FolderInfo info) {
1475 mFolderInfo = info;
1476 mWaitingForResult = true;
1477 showDialog(DIALOG_RENAME_FOLDER);
1478 }
1479
1480 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1481 mAddItemCellInfo = cellInfo;
1482 mWaitingForResult = true;
1483 showDialog(DIALOG_CREATE_SHORTCUT);
1484 }
1485
Romain Guy73b979d2009-06-09 12:57:21 -07001486 private void pickShortcut(int requestCode, int title) {
1487 Bundle bundle = new Bundle();
1488
1489 ArrayList<String> shortcutNames = new ArrayList<String>();
1490 shortcutNames.add(getString(R.string.group_applications));
1491 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1492
1493 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1494 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1495 R.drawable.ic_launcher_application));
1496 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1497
1498 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1499 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1500 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1501 pickIntent.putExtras(bundle);
1502
1503 startActivityForResult(pickIntent, requestCode);
1504 }
1505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 private class RenameFolder {
1507 private EditText mInput;
1508
1509 Dialog createDialog() {
1510 mWaitingForResult = true;
1511 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1512 mInput = (EditText) layout.findViewById(R.id.folder_name);
1513
1514 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1515 builder.setIcon(0);
1516 builder.setTitle(getString(R.string.rename_folder_title));
1517 builder.setCancelable(true);
1518 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1519 public void onCancel(DialogInterface dialog) {
1520 cleanup();
1521 }
1522 });
1523 builder.setNegativeButton(getString(R.string.cancel_action),
1524 new Dialog.OnClickListener() {
1525 public void onClick(DialogInterface dialog, int which) {
1526 cleanup();
1527 }
1528 }
1529 );
1530 builder.setPositiveButton(getString(R.string.rename_action),
1531 new Dialog.OnClickListener() {
1532 public void onClick(DialogInterface dialog, int which) {
1533 changeFolderName();
1534 }
1535 }
1536 );
1537 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001538
1539 final AlertDialog dialog = builder.create();
1540 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1541 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001542 }
1543 });
1544
1545 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
1547
1548 private void changeFolderName() {
1549 final String name = mInput.getText().toString();
1550 if (!TextUtils.isEmpty(name)) {
1551 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001552 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 mFolderInfo.title = name;
1554 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1555
Joe Onorato9c1289c2009-08-17 11:03:03 -04001556 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001557 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001558 mModel.setWorkspaceDirty();
1559 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 } else {
1561 final FolderIcon folderIcon = (FolderIcon)
1562 mWorkspace.getViewForTag(mFolderInfo);
1563 if (folderIcon != null) {
1564 folderIcon.setText(name);
1565 getWorkspace().requestLayout();
1566 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001567 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001568 mModel.setWorkspaceDirty();
1569 mWorkspaceLoading = true;
1570 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
1572 }
1573 }
1574 cleanup();
1575 }
1576
1577 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 dismissDialog(DIALOG_RENAME_FOLDER);
1579 mWaitingForResult = false;
1580 mFolderInfo = null;
1581 }
1582 }
1583
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001584 void showAllAppsDialog() {
Joe Onorato7c312c12009-08-13 21:36:53 -07001585 mAllAppsVisible = true;
Joe Onorato006b25f2009-09-03 11:38:43 -07001586 mAllAppsGrid.show();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001587 mWorkspace.hide();
Joe Onorato7c312c12009-08-13 21:36:53 -07001588
1589 // TODO: fade these two too
1590 mDeleteZone.setVisibility(View.GONE);
1591 mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001592 }
1593
1594 void closeAllAppsDialog(boolean animated) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001595 if (mAllAppsVisible) {
1596 Log.d(LOG_TAG, "closing all apps");
Joe Onorato006b25f2009-09-03 11:38:43 -07001597 mAllAppsGrid.hide(animated);
Joe Onorato7c312c12009-08-13 21:36:53 -07001598 mAllAppsVisible = false;
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001599 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1600 mWorkspace.show();
Joe Onorato7c312c12009-08-13 21:36:53 -07001601
1602 // TODO: fade these two too
1603 mDeleteZone.setVisibility(View.VISIBLE);
1604 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001605 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001606 }
1607
Joe Onorato7c312c12009-08-13 21:36:53 -07001608 void lockAllApps() {
1609 // TODO
1610 }
1611
1612 void unlockAllApps() {
1613 // TODO
1614 }
1615
Joe Onorato7404ee42009-07-31 11:54:44 -07001616 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 * Displays the shortcut creation dialog and launches, if necessary, the
1618 * appropriate activity.
1619 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001620 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001621 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1622 DialogInterface.OnShowListener {
1623
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001625
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 Dialog createDialog() {
1627 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001628
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001630
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1632 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001633 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001634
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 builder.setInverseBackgroundForced(true);
1636
1637 AlertDialog dialog = builder.create();
1638 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001639 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001640 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 return dialog;
1643 }
1644
1645 public void onCancel(DialogInterface dialog) {
1646 mWaitingForResult = false;
1647 cleanup();
1648 }
1649
Romain Guycbb89e42009-06-08 15:52:54 -07001650 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001651 }
1652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 dismissDialog(DIALOG_CREATE_SHORTCUT);
1655 }
1656
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001657 /**
1658 * Handle the action clicked in the "Add to home" dialog.
1659 */
1660 public void onClick(DialogInterface dialog, int which) {
1661 Resources res = getResources();
1662 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001663
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001664 switch (which) {
1665 case AddAdapter.ITEM_SHORTCUT: {
1666 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001667 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001668 break;
1669 }
Romain Guycbb89e42009-06-08 15:52:54 -07001670
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001671 case AddAdapter.ITEM_APPWIDGET: {
1672 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001673
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001674 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1675 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1676 // add the search widget
1677 ArrayList<AppWidgetProviderInfo> customInfo =
1678 new ArrayList<AppWidgetProviderInfo>();
1679 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1680 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1681 info.label = getString(R.string.group_search);
1682 info.icon = R.drawable.ic_search_widget;
1683 customInfo.add(info);
1684 pickIntent.putParcelableArrayListExtra(
1685 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1686 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1687 Bundle b = new Bundle();
1688 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1689 customExtras.add(b);
1690 pickIntent.putParcelableArrayListExtra(
1691 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1692 // start the pick activity
1693 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1694 break;
1695 }
Romain Guycbb89e42009-06-08 15:52:54 -07001696
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001697 case AddAdapter.ITEM_LIVE_FOLDER: {
1698 // Insert extra item to handle inserting folder
1699 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001700
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001701 ArrayList<String> shortcutNames = new ArrayList<String>();
1702 shortcutNames.add(res.getString(R.string.group_folder));
1703 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001704
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001705 ArrayList<ShortcutIconResource> shortcutIcons =
1706 new ArrayList<ShortcutIconResource>();
1707 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1708 R.drawable.ic_launcher_folder));
1709 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1710
1711 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1712 pickIntent.putExtra(Intent.EXTRA_INTENT,
1713 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1714 pickIntent.putExtra(Intent.EXTRA_TITLE,
1715 getText(R.string.title_select_live_folder));
1716 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001717
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001718 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1719 break;
1720 }
1721
1722 case AddAdapter.ITEM_WALLPAPER: {
1723 startWallpaper();
1724 break;
1725 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 }
1727 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001728
1729 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001730 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 }
1732
1733 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001734 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001736 public int getCurrentWorkspaceScreen() {
1737 return mWorkspace.getCurrentScreen();
1738 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001739
Joe Onorato9c1289c2009-08-17 11:03:03 -04001740 /**
1741 * Refreshes the shortcuts shown on the workspace.
1742 *
1743 * Implementation of the method from LauncherModel.Callbacks.
1744 */
1745 public void startBinding() {
1746 final Workspace workspace = mWorkspace;
1747 int count = workspace.getChildCount();
1748 for (int i = 0; i < count; i++) {
1749 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1750 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001751
Joe Onorato9c1289c2009-08-17 11:03:03 -04001752 if (DEBUG_USER_INTERFACE) {
1753 android.widget.Button finishButton = new android.widget.Button(this);
1754 finishButton.setText("Finish");
1755 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1756
1757 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1758 public void onClick(View v) {
1759 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001760 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001761 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 }
1763 }
1764
1765 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766 * Bind the items start-end from the list.
1767 *
1768 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001770 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1771
1772 final Workspace workspace = mWorkspace;
1773
1774 for (int i=start; i<end; i++) {
1775 final ItemInfo item = shortcuts.get(i);
1776 mDesktopItems.add(item);
1777 switch (item.itemType) {
1778 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1779 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1780 final View shortcut = createShortcut((ApplicationInfo) item);
1781 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1782 false);
1783 break;
1784 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1785 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1786 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1787 (UserFolderInfo) item);
1788 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1789 false);
1790 break;
1791 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1792 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1793 R.layout.live_folder_icon, this,
1794 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1795 (LiveFolderInfo) item);
1796 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1797 false);
1798 break;
1799 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1800 final int screen = workspace.getCurrentScreen();
1801 final View view = mInflater.inflate(R.layout.widget_search,
1802 (ViewGroup) workspace.getChildAt(screen), false);
1803
1804 Search search = (Search) view.findViewById(R.id.widget_search);
1805 search.setLauncher(this);
1806
1807 final Widget widget = (Widget) item;
1808 view.setTag(widget);
1809
1810 workspace.addWidget(view, widget, false);
1811 break;
1812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 }
1814
Joe Onorato9c1289c2009-08-17 11:03:03 -04001815 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 }
1817
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818 /**
1819 * Implementation of the method from LauncherModel.Callbacks.
1820 */
1821 void bindFolders(HashMap<Long, FolderInfo> folders) {
1822 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001824
1825 /**
1826 * Add the views for a widget to the workspace.
1827 *
1828 * Implementation of the method from LauncherModel.Callbacks.
1829 */
1830 public void bindAppWidget(LauncherAppWidgetInfo item) {
1831 final Workspace workspace = mWorkspace;
1832
1833 final int appWidgetId = item.appWidgetId;
1834 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1835 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1836
1837 if (true) {
1838 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1839 appWidgetId, appWidgetInfo));
1840 }
1841
1842 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1843 item.hostView.setTag(item);
1844
1845 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1846 item.cellY, item.spanX, item.spanY, false);
1847
1848 workspace.requestLayout();
1849
1850 mDesktopItems.add(item);
1851 }
1852
1853 /**
1854 * Callback saying that there aren't any more items to bind.
1855 *
1856 * Implementation of the method from LauncherModel.Callbacks.
1857 */
1858 public void finishBindingItems() {
1859 if (mSavedState != null) {
1860 if (!mWorkspace.hasFocus()) {
1861 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1862 }
1863
1864 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1865 if (userFolders != null) {
1866 for (long folderId : userFolders) {
1867 final FolderInfo info = mFolders.get(folderId);
1868 if (info != null) {
1869 openFolder(info);
1870 }
1871 }
1872 final Folder openFolder = mWorkspace.getOpenFolder();
1873 if (openFolder != null) {
1874 openFolder.requestFocus();
1875 }
1876 }
1877
1878 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1879 if (allApps) {
1880 showAllAppsDialog();
1881 }
1882
1883 mSavedState = null;
1884 }
1885
1886 if (mSavedInstanceState != null) {
1887 super.onRestoreInstanceState(mSavedInstanceState);
1888 mSavedInstanceState = null;
1889 }
1890
1891 /* TODO
1892 if (mAllAppsVisible && !mDrawer.hasFocus()) {
1893 mDrawer.requestFocus();
1894 }
1895 */
1896
1897 Log.d(TAG, "finishBindingItems done");
1898 mWorkspaceLoading = false;
1899 }
1900
1901 /**
1902 * Add the icons for all apps.
1903 *
1904 * Implementation of the method from LauncherModel.Callbacks.
1905 */
1906 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
1907 Log.d(LOG_TAG, "got info for " + apps.size() + " apps");
1908 mAllAppsList = apps;
1909 mAllAppsGrid.setApps(mAllAppsList);
1910 }
1911
1912 /**
1913 * A package was installed.
1914 *
1915 * Implementation of the method from LauncherModel.Callbacks.
1916 */
1917 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1918 removeDialog(DIALOG_CREATE_SHORTCUT);
1919 }
1920
1921 /**
1922 * A package was updated.
1923 *
1924 * Implementation of the method from LauncherModel.Callbacks.
1925 */
1926 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1927 removeDialog(DIALOG_CREATE_SHORTCUT);
1928 mWorkspace.updateShortcutsForPackage(packageName);
1929 }
1930
1931 /**
1932 * A package was uninstalled.
1933 *
1934 * Implementation of the method from LauncherModel.Callbacks.
1935 */
1936 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1937 removeDialog(DIALOG_CREATE_SHORTCUT);
1938 mWorkspace.removeShortcutsForPackage(packageName);
1939 }
1940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941}