blob: b53fee26cad48b200a5252f9afc9f31f21b606f3 [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 Hackborn8355ae32009-09-07 21:47:51 -070025import android.app.WallpaperInfo;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
30import android.content.DialogInterface;
31import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070032import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ActivityInfo;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070034import android.content.pm.LabeledIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.PackageManager;
36import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070038import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.graphics.drawable.Drawable;
41import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Bundle;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070043import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070044import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070045import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.text.Selection;
51import android.text.SpannableStringBuilder;
52import android.text.TextUtils;
53import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070054import static android.util.Log.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070055import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.KeyEvent;
58import android.view.LayoutInflater;
59import android.view.Menu;
60import android.view.MenuItem;
61import android.view.View;
62import android.view.ViewGroup;
Romain Guyb1b69f52009-08-10 15:10:15 -070063import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.view.View.OnLongClickListener;
65import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.widget.TextView;
68import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070069import android.appwidget.AppWidgetManager;
70import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071
72import java.lang.ref.WeakReference;
73import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074import java.util.HashMap;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070075import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070076import java.io.DataOutputStream;
77import java.io.FileNotFoundException;
78import java.io.IOException;
79import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
81/**
82 * Default launcher application.
83 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040084public final class Launcher extends Activity
85 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040087 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 static final boolean LOGD = false;
89
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 static final boolean PROFILE_STARTUP = false;
91 static final boolean PROFILE_ROTATE = false;
92 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 private static final int WALLPAPER_SCREENS_SPAN = 2;
95
96 private static final int MENU_GROUP_ADD = 1;
97 private static final int MENU_ADD = Menu.FIRST + 1;
98 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
99 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
100 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700101 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
103 private static final int REQUEST_CREATE_SHORTCUT = 1;
104 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700105 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 private static final int REQUEST_PICK_APPLICATION = 6;
107 private static final int REQUEST_PICK_SHORTCUT = 7;
108 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700109 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
111 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
112
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700113 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
114 static final String SEARCH_WIDGET = "search_widget";
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 static final int SCREEN_COUNT = 3;
117 static final int DEFAULT_SCREN = 1;
118 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700119 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Joe Onorato7c312c12009-08-13 21:36:53 -0700121 static final int DIALOG_CREATE_SHORTCUT = 1;
122 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Romain Guy98d01652009-06-30 16:21:04 -0700124 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 // Type: int
127 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
128 // Type: boolean
129 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
130 // Type: long
131 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
146 // Type: int[]
147 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
148 // Type: boolean
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
150 // Type: long
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
152
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final Object sLock = new Object();
156 private static int sScreen = DEFAULT_SCREN;
157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private LayoutInflater mInflater;
159
Joe Onorato00acb122009-08-04 16:04:30 -0400160 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700162
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700163 private AppWidgetManager mAppWidgetManager;
164 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private CellLayout.CellInfo mAddItemCellInfo;
167 private CellLayout.CellInfo mMenuAddInfo;
168 private final int[] mCellCoordinates = new int[2];
169 private FolderInfo mFolderInfo;
170
Joe Onorato7c312c12009-08-13 21:36:53 -0700171 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700172 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700173 private AllAppsView mAllAppsGrid;
174 private boolean mAllAppsVisible;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private Bundle mSavedState;
177
178 private SpannableStringBuilder mDefaultKeySsb = null;
179
Mike LeBeau736cf282009-07-02 17:46:59 -0700180 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 private boolean mWorkspaceLoading = true;
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private boolean mRestoring;
185 private boolean mWaitingForResult;
186 private boolean mLocaleChanged;
187
Dianne Hackborn67800862009-07-24 17:15:20 -0700188 private boolean mHomeDown;
189 private boolean mBackDown;
190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private Bundle mSavedInstanceState;
192
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 private LauncherModel mModel;
194
195 private ArrayList<ItemInfo> mDesktopItems = new ArrayList();
196 private static HashMap<Long, FolderInfo> mFolders = new HashMap();
197 private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList();
Romain Guycbb89e42009-06-08 15:52:54 -0700198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 @Override
200 protected void onCreate(Bundle savedInstanceState) {
201 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700206 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700207
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
209 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 if (PROFILE_STARTUP) {
212 android.os.Debug.startMethodTracing("/sdcard/launcher");
213 }
214
215 checkForLocaleChange();
216 setWallpaperDimension();
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 setContentView(R.layout.launcher);
219 setupViews();
220
Joe Onorato7c312c12009-08-13 21:36:53 -0700221 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 mSavedState = savedInstanceState;
224 restoreState(mSavedState);
225
226 if (PROFILE_STARTUP) {
227 android.os.Debug.stopMethodTracing();
228 }
229
230 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 }
233
234 // For handling default keys
235 mDefaultKeySsb = new SpannableStringBuilder();
236 Selection.setSelection(mDefaultKeySsb, 0);
237 }
Romain Guycbb89e42009-06-08 15:52:54 -0700238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700240 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
241 readConfiguration(this, localeConfiguration);
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 final Configuration configuration = getResources().getConfiguration();
244
Romain Guy98d01652009-06-30 16:21:04 -0700245 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 final String locale = configuration.locale.toString();
247
Romain Guy98d01652009-06-30 16:21:04 -0700248 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 final int mcc = configuration.mcc;
250
Romain Guy98d01652009-06-30 16:21:04 -0700251 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 final int mnc = configuration.mnc;
253
254 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
255
256 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700257 localeConfiguration.locale = locale;
258 localeConfiguration.mcc = mcc;
259 localeConfiguration.mnc = mnc;
260
261 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700263 }
264 }
265
266 private static class LocaleConfiguration {
267 public String locale;
268 public int mcc = -1;
269 public int mnc = -1;
270 }
271
272 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
273 DataInputStream in = null;
274 try {
275 in = new DataInputStream(context.openFileInput(PREFERENCES));
276 configuration.locale = in.readUTF();
277 configuration.mcc = in.readInt();
278 configuration.mnc = in.readInt();
279 } catch (FileNotFoundException e) {
280 // Ignore
281 } catch (IOException e) {
282 // Ignore
283 } finally {
284 if (in != null) {
285 try {
286 in.close();
287 } catch (IOException e) {
288 // Ignore
289 }
290 }
291 }
292 }
293
294 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
295 DataOutputStream out = null;
296 try {
297 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
298 out.writeUTF(configuration.locale);
299 out.writeInt(configuration.mcc);
300 out.writeInt(configuration.mnc);
301 out.flush();
302 } catch (FileNotFoundException e) {
303 // Ignore
304 } catch (IOException e) {
305 //noinspection ResultOfMethodCallIgnored
306 context.getFileStreamPath(PREFERENCES).delete();
307 } finally {
308 if (out != null) {
309 try {
310 out.close();
311 } catch (IOException e) {
312 // Ignore
313 }
314 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 }
316 }
317
318 static int getScreen() {
319 synchronized (sLock) {
320 return sScreen;
321 }
322 }
323
324 static void setScreen(int screen) {
325 synchronized (sLock) {
326 sScreen = screen;
327 }
328 }
329
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700331 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332
333 Display display = getWindowManager().getDefaultDisplay();
334 boolean isPortrait = display.getWidth() < display.getHeight();
335
336 final int width = isPortrait ? display.getWidth() : display.getHeight();
337 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700338 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 }
340
341 @Override
342 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700343 mWaitingForResult = false;
344
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 // The pattern used here is that a user PICKs a specific application,
346 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
349 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700350
Romain Guy94dabf12009-07-21 10:55:43 -0700351 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 switch (requestCode) {
353 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400354 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 break;
356 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700357 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 break;
359 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400360 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 break;
362 case REQUEST_PICK_LIVE_FOLDER:
363 addLiveFolder(data);
364 break;
365 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700368 case REQUEST_PICK_APPWIDGET:
369 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700371 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400372 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 break;
374 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700375 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700377 // Clean up the appWidgetId if we canceled
378 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
379 if (appWidgetId != -1) {
380 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 }
382 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
384
385 @Override
386 protected void onResume() {
387 super.onResume();
388
Joe Onorato9c1289c2009-08-17 11:03:03 -0400389 Log.d(TAG, "onResume mRestoring=" + mRestoring);
390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400392 mWorkspaceLoading = true;
393 mModel.startLoader(this, true);
394 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700396
397 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
398 // onNewIntent), then close the search dialog if needed, because it probably
399 // came from the user pressing 'home' (rather than, for example, pressing 'back').
400 if (mIsNewIntent) {
401 // Post to a handler so that this happens after the search dialog tries to open
402 // itself again.
403 mWorkspace.post(new Runnable() {
404 public void run() {
405 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
406 ServiceManager.getService(Context.SEARCH_SERVICE));
407 try {
408 searchManagerService.stopSearch();
409 } catch (RemoteException e) {
410 e(LOG_TAG, "error stopping search", e);
411 }
412 }
413 });
414 }
415
416 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 }
418
419 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700420 protected void onPause() {
421 super.onPause();
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700422 closeAllAppsDialog(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700423 }
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700425 @Override
426 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400427 // Flag the loader to stop early before switching
428 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700430 if (PROFILE_ROTATE) {
431 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
432 }
433 return null;
434 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 private boolean acceptFilter() {
437 final InputMethodManager inputManager = (InputMethodManager)
438 getSystemService(Context.INPUT_METHOD_SERVICE);
439 return !inputManager.isFullscreenMode();
440 }
441
442 @Override
443 public boolean onKeyDown(int keyCode, KeyEvent event) {
444 boolean handled = super.onKeyDown(keyCode, event);
445 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
446 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
447 keyCode, event);
448 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700449 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700450 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700451 // showSearchDialog()
452 // If there are multiple keystrokes before the search dialog takes focus,
453 // onSearchRequested() will be called for every keystroke,
454 // but it is idempotent, so it's fine.
455 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 }
457 }
458
459 return handled;
460 }
461
Karl Rosaen138a0412009-04-23 19:00:21 -0700462 private String getTypedText() {
463 return mDefaultKeySsb.toString();
464 }
465
466 private void clearTypedText() {
467 mDefaultKeySsb.clear();
468 mDefaultKeySsb.clearSpans();
469 Selection.setSelection(mDefaultKeySsb, 0);
470 }
471
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 /**
473 * Restores the previous state, if it exists.
474 *
475 * @param savedState The previous state.
476 */
477 private void restoreState(Bundle savedState) {
478 if (savedState == null) {
479 return;
480 }
481
482 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
483 if (currentScreen > -1) {
484 mWorkspace.setCurrentScreen(currentScreen);
485 }
486
487 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
488 if (addScreen > -1) {
489 mAddItemCellInfo = new CellLayout.CellInfo();
490 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
491 addItemCellInfo.valid = true;
492 addItemCellInfo.screen = addScreen;
493 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
494 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
495 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
496 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
497 addItemCellInfo.findVacantCellsFromOccupied(
498 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
499 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
500 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
501 mRestoring = true;
502 }
503
504 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
505 if (renameFolder) {
506 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400507 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800508 mRestoring = true;
509 }
510 }
511
512 /**
513 * Finds all the views we need and configure them properly.
514 */
515 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400516 mDragController = new DragController(this);
517 DragController dragController = mDragController;
518
Romain Guyb1b69f52009-08-10 15:10:15 -0700519 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400520 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800521
Joe Onorato7c312c12009-08-13 21:36:53 -0700522 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700523 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700524 mAllAppsGrid.setDragController(dragController);
Joe Onorato7c312c12009-08-13 21:36:53 -0700525
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
527 final Workspace workspace = mWorkspace;
528
Joe Onorato7c312c12009-08-13 21:36:53 -0700529 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
530 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531
Joe Onorato7c312c12009-08-13 21:36:53 -0700532 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700533 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700534 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700535 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700536 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700537 handleIocon.setCrossFadeEnabled(true);
538 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400541 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543
544 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400545 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700546 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547
Joe Onorato00acb122009-08-04 16:04:30 -0400548 dragController.setDragScoller(workspace);
549 dragController.setDragListener(deleteZone);
550 dragController.setScrollView(dragLayer);
551
552 // The order here is bottom to top.
553 dragController.addDropTarget(workspace);
554 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 }
556
557 /**
558 * Creates a view representing a shortcut.
559 *
560 * @param info The data structure describing the shortcut.
561 *
562 * @return A View inflated from R.layout.application.
563 */
564 View createShortcut(ApplicationInfo info) {
565 return createShortcut(R.layout.application,
566 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
567 }
568
569 /**
570 * Creates a view representing a shortcut inflated from the specified resource.
571 *
572 * @param layoutResId The id of the XML layout used to create the shortcut.
573 * @param parent The group the shortcut belongs to.
574 * @param info The data structure describing the shortcut.
575 *
576 * @return A View inflated from layoutResId.
577 */
578 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
579 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
580
Joe Onorato5162ea92009-09-03 09:39:42 -0700581 if (info.icon == null) {
582 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
583 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700585 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 info.filtered = true;
587 }
588
589 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
590 favorite.setText(info.title);
591 favorite.setTag(info);
592 favorite.setOnClickListener(this);
593
594 return favorite;
595 }
596
597 /**
598 * Add an application shortcut to the workspace.
599 *
600 * @param data The intent describing the application.
601 * @param cellInfo The position on screen where to create the shortcut.
602 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400603 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 cellInfo.screen = mWorkspace.getCurrentScreen();
605 if (!findSingleSlot(cellInfo)) return;
606
Romain Guy73b979d2009-06-09 12:57:21 -0700607 final ApplicationInfo info = infoFromApplicationIntent(context, data);
608 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400609 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700610 }
611 }
612
613 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 ComponentName component = data.getComponent();
615 PackageManager packageManager = context.getPackageManager();
616 ActivityInfo activityInfo = null;
617 try {
618 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
619 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700620 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 }
Romain Guycbb89e42009-06-08 15:52:54 -0700622
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 if (activityInfo != null) {
624 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 itemInfo.title = activityInfo.loadLabel(packageManager);
627 if (itemInfo.title == null) {
628 itemInfo.title = activityInfo.name;
629 }
Romain Guycbb89e42009-06-08 15:52:54 -0700630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
632 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
633 itemInfo.icon = activityInfo.loadIcon(packageManager);
634 itemInfo.container = ItemInfo.NO_ID;
635
Romain Guy73b979d2009-06-09 12:57:21 -0700636 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
Romain Guy73b979d2009-06-09 12:57:21 -0700638
639 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
Romain Guycbb89e42009-06-08 15:52:54 -0700641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 /**
643 * Add a shortcut to the workspace.
644 *
645 * @param data The intent describing the shortcut.
646 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 cellInfo.screen = mWorkspace.getCurrentScreen();
650 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
653
654 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400656 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
657 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 }
659 }
660
Romain Guycbb89e42009-06-08 15:52:54 -0700661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700663 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700665 * @param data The intent describing the appWidgetId.
666 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400668 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700670 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700671
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700672 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700673
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700674 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700675
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700678 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700679
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 // Try finding open space on Launcher screen
681 final int[] xy = mCellCoordinates;
682 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
683
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700684 // Build Launcher-specific widget info and save to database
685 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 launcherInfo.spanX = spans[0];
687 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700688
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 LauncherModel.addItemToDatabase(this, launcherInfo,
690 LauncherSettings.Favorites.CONTAINER_DESKTOP,
691 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
692
693 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400694 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700695
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700698
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700699 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 }
705 }
Romain Guycbb89e42009-06-08 15:52:54 -0700706
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
708 mDesktopItems.remove(launcherInfo);
709 launcherInfo.hostView = null;
710 }
711
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 public LauncherAppWidgetHost getAppWidgetHost() {
713 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 }
Romain Guycbb89e42009-06-08 15:52:54 -0700715
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 static ApplicationInfo addShortcut(Context context, Intent data,
717 CellLayout.CellInfo cellInfo, boolean notify) {
718
Romain Guy73b979d2009-06-09 12:57:21 -0700719 final ApplicationInfo info = infoFromShortcutIntent(context, data);
720 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
721 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
722
723 return info;
724 }
725
726 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
728 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
729 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
730
731 Drawable icon = null;
732 boolean filtered = false;
733 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700734 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735
736 if (bitmap != null) {
737 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
738 filtered = true;
739 customIcon = true;
740 } else {
741 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700742 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700744 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 final PackageManager packageManager = context.getPackageManager();
746 Resources resources = packageManager.getResourcesForApplication(
747 iconResource.packageName);
748 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
749 icon = resources.getDrawable(id);
750 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700751 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753 }
754 }
755
756 if (icon == null) {
757 icon = context.getPackageManager().getDefaultActivityIcon();
758 }
759
760 final ApplicationInfo info = new ApplicationInfo();
761 info.icon = icon;
762 info.filtered = filtered;
763 info.title = name;
764 info.intent = intent;
765 info.customIcon = customIcon;
766 info.iconResource = iconResource;
767
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 return info;
769 }
770
771 @Override
772 protected void onNewIntent(Intent intent) {
773 super.onNewIntent(intent);
774
775 // Close the menu
776 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
777 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700778
779 // Set this flag so that onResume knows to close the search dialog if it's open,
780 // because this was a new intent (thus a press of 'home' or some such) rather than
781 // for example onResume being called when the user pressed the 'back' button.
782 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783
784 try {
785 dismissDialog(DIALOG_CREATE_SHORTCUT);
786 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 } catch (Exception e) {
788 // An exception is thrown if the dialog is not visible, which is fine
789 }
790
791 try {
792 dismissDialog(DIALOG_RENAME_FOLDER);
793 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 } catch (Exception e) {
795 // An exception is thrown if the dialog is not visible, which is fine
796 }
797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
799 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700800
Romain Guy94dabf12009-07-21 10:55:43 -0700801 if (!mWorkspace.isDefaultScreenShowing()) {
802 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700804
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700805 closeAllAppsDialog(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700806
807 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 if (v != null && v.getWindowToken() != null) {
809 InputMethodManager imm = (InputMethodManager)getSystemService(
810 INPUT_METHOD_SERVICE);
811 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
812 }
813 } else {
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700814 closeAllAppsDialog(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 }
816 }
817 }
818
819 @Override
820 protected void onRestoreInstanceState(Bundle savedInstanceState) {
821 // Do not call super here
822 mSavedInstanceState = savedInstanceState;
823 }
824
825 @Override
826 protected void onSaveInstanceState(Bundle outState) {
827 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
828
829 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
830 if (folders.size() > 0) {
831 final int count = folders.size();
832 long[] ids = new long[count];
833 for (int i = 0; i < count; i++) {
834 final FolderInfo info = folders.get(i).getInfo();
835 ids[i] = info.id;
836 }
837 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
838 } else {
839 super.onSaveInstanceState(outState);
840 }
841
Romain Guy3cf604f2009-06-16 13:12:53 -0700842 final boolean isConfigurationChange = getChangingConfigurations() != 0;
843
Romain Guy5a941392009-04-28 15:18:25 -0700844 // When the drawer is opened and we are saving the state because of a
845 // configuration change
Joe Onorato7c312c12009-08-13 21:36:53 -0700846 if (mAllAppsVisible && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849
850 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
851 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
852 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
853
854 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
855 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
856 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
857 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
858 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
859 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
860 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
861 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
862 layout.getOccupiedCells());
863 }
864
865 if (mFolderInfo != null && mWaitingForResult) {
866 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
867 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
868 }
869 }
870
871 @Override
872 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700874
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700876 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700878 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 }
880
881 TextKeyListener.getInstance().release();
882
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885 unbindDesktopItems();
886 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 }
888
889 @Override
890 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700891 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 super.startActivityForResult(intent, requestCode);
893 }
894
895 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700896 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700898
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700899 closeAllAppsDialog(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700900
Karl Rosaen138a0412009-04-23 19:00:21 -0700901 // Slide the search widget to the top, if it's on the current screen,
902 // otherwise show the search dialog immediately.
903 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
904 if (searchWidget == null) {
905 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
906 } else {
907 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
908 // show the currently typed text in the search widget while sliding
909 searchWidget.setQuery(getTypedText());
910 }
911 }
Romain Guycbb89e42009-06-08 15:52:54 -0700912
Karl Rosaen138a0412009-04-23 19:00:21 -0700913 /**
914 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700915 *
916 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700917 */
Romain Guycbb89e42009-06-08 15:52:54 -0700918 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700919 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700920
Karl Rosaen138a0412009-04-23 19:00:21 -0700921 if (initialQuery == null) {
922 // Use any text typed in the launcher as the initial query
923 initialQuery = getTypedText();
924 clearTypedText();
925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 if (appSearchData == null) {
927 appSearchData = new Bundle();
928 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
929 }
Romain Guycbb89e42009-06-08 15:52:54 -0700930
Karl Rosaen138a0412009-04-23 19:00:21 -0700931 final SearchManager searchManager =
932 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
933
934 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
935 if (searchWidget != null) {
936 // This gets called when the user leaves the search dialog to go back to
937 // the Launcher.
938 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
939 public void onCancel() {
940 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700941 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700942 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700943 });
944 }
Romain Guycbb89e42009-06-08 15:52:54 -0700945
Karl Rosaen138a0412009-04-23 19:00:21 -0700946 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700947 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
949
Karl Rosaen138a0412009-04-23 19:00:21 -0700950 /**
951 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700952 */
Romain Guy5a941392009-04-28 15:18:25 -0700953 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700954 // Close search dialog
955 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100956 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700957 // Restore search widget to its normal position
958 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
959 if (searchWidget != null) {
960 searchWidget.stopSearch(false);
961 }
962 }
Romain Guycbb89e42009-06-08 15:52:54 -0700963
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 @Override
965 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966 if (isWorkspaceLocked()) {
967 return false;
968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969
970 super.onCreateOptionsMenu(menu);
971 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
972 .setIcon(android.R.drawable.ic_menu_add)
973 .setAlphabeticShortcut('A');
974 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
975 .setIcon(android.R.drawable.ic_menu_gallery)
976 .setAlphabeticShortcut('W');
977 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
978 .setIcon(android.R.drawable.ic_search_category_default)
979 .setAlphabeticShortcut(SearchManager.MENU_KEY);
980 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
981 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
982 .setAlphabeticShortcut('N');
983
984 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700985 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
986 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987
988 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
989 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
990 .setIntent(settings);
991
992 return true;
993 }
994
995 @Override
996 public boolean onPrepareOptionsMenu(Menu menu) {
997 super.onPrepareOptionsMenu(menu);
998
999 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1000 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1001
1002 return true;
1003 }
1004
1005 @Override
1006 public boolean onOptionsItemSelected(MenuItem item) {
1007 switch (item.getItemId()) {
1008 case MENU_ADD:
1009 addItems();
1010 return true;
1011 case MENU_WALLPAPER_SETTINGS:
1012 startWallpaper();
1013 return true;
1014 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001015 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 return true;
1017 case MENU_NOTIFICATIONS:
1018 showNotifications();
1019 return true;
1020 }
1021
1022 return super.onOptionsItemSelected(item);
1023 }
Romain Guycbb89e42009-06-08 15:52:54 -07001024
Karl Rosaen138a0412009-04-23 19:00:21 -07001025 /**
1026 * Indicates that we want global search for this activity by setting the globalSearch
1027 * argument for {@link #startSearch} to true.
1028 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001030 @Override
1031 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001032 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001033 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001034 }
1035
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036 public boolean isWorkspaceLocked() {
1037 return mWorkspaceLoading || mWaitingForResult;
1038 }
1039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 private void addItems() {
1041 showAddDialog(mMenuAddInfo);
1042 }
1043
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001044 void addAppWidget(Intent data) {
1045 // TODO: catch bad widget exception when sent
1046 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001048 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1049 if (SEARCH_WIDGET.equals(customWidget)) {
1050 // We don't need this any more, since this isn't a real app widget.
1051 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1052 // add the search widget
1053 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001055 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1056
1057 if (appWidget.configure != null) {
1058 // Launch over to configure widget, if needed
1059 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1060 intent.setComponent(appWidget.configure);
1061 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1062
1063 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1064 } else {
1065 // Otherwise just add it
1066 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 }
1069 }
Romain Guycbb89e42009-06-08 15:52:54 -07001070
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 void addSearch() {
1072 final Widget info = Widget.makeSearch();
1073 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001074
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 final int[] xy = mCellCoordinates;
1076 final int spanX = info.spanX;
1077 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1082 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 final View view = mInflater.inflate(info.layoutResource, null);
1085 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001086 Search search = (Search) view.findViewById(R.id.widget_search);
1087 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1090 }
1091
Romain Guy73b979d2009-06-09 12:57:21 -07001092 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001093 // Handle case where user selected "Applications"
1094 String applicationName = getResources().getString(R.string.group_applications);
1095 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001096
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001097 if (applicationName != null && applicationName.equals(shortcutName)) {
1098 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1099 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001100
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001101 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1102 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001103 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001104 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001105 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001106 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 }
1108
1109 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001110 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001111 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001112 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001113
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001114 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001115 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001116 } else {
1117 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1118 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 }
1120
Joe Onorato9c1289c2009-08-17 11:03:03 -04001121 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 UserFolderInfo folderInfo = new UserFolderInfo();
1123 folderInfo.title = getText(R.string.folder_name);
1124
1125 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1126 cellInfo.screen = mWorkspace.getCurrentScreen();
1127 if (!findSingleSlot(cellInfo)) return;
1128
1129 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 LauncherModel.addItemToDatabase(this, folderInfo,
1131 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134
1135 // Create the view
1136 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1137 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1138 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
Romain Guycbb89e42009-06-08 15:52:54 -07001141
Joe Onorato9c1289c2009-08-17 11:03:03 -04001142 void removeFolder(FolderInfo folder) {
1143 mFolders.remove(folder.id);
1144 }
1145
1146 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 cellInfo.screen = mWorkspace.getCurrentScreen();
1148 if (!findSingleSlot(cellInfo)) return;
1149
1150 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1151
1152 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1154 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001155 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1156 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 }
1158 }
1159
1160 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1161 CellLayout.CellInfo cellInfo, boolean notify) {
1162
1163 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1164 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1165
1166 Drawable icon = null;
1167 boolean filtered = false;
1168 Intent.ShortcutIconResource iconResource = null;
1169
1170 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1171 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1172 try {
1173 iconResource = (Intent.ShortcutIconResource) extra;
1174 final PackageManager packageManager = context.getPackageManager();
1175 Resources resources = packageManager.getResourcesForApplication(
1176 iconResource.packageName);
1177 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1178 icon = resources.getDrawable(id);
1179 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001180 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 }
1182 }
1183
1184 if (icon == null) {
1185 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1186 }
1187
1188 final LiveFolderInfo info = new LiveFolderInfo();
1189 info.icon = icon;
1190 info.filtered = filtered;
1191 info.title = name;
1192 info.iconResource = iconResource;
1193 info.uri = data.getData();
1194 info.baseIntent = baseIntent;
1195 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1196 LiveFolders.DISPLAY_MODE_GRID);
1197
1198 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1199 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201
1202 return info;
1203 }
1204
1205 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1206 final int[] xy = new int[2];
1207 if (findSlot(cellInfo, xy, 1, 1)) {
1208 cellInfo.cellX = xy[0];
1209 cellInfo.cellY = xy[1];
1210 return true;
1211 }
1212 return false;
1213 }
1214
1215 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1216 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1217 boolean[] occupied = mSavedState != null ?
1218 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1219 cellInfo = mWorkspace.findAllVacantCells(occupied);
1220 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1221 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1222 return false;
1223 }
1224 }
1225 return true;
1226 }
1227
1228 private void showNotifications() {
1229 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1230 if (statusBar != null) {
1231 statusBar.expand();
1232 }
1233 }
1234
1235 private void startWallpaper() {
1236 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001237 Intent chooser = Intent.createChooser(pickWallpaper,
1238 getText(R.string.chooser_wallpaper));
1239 WallpaperManager wm = (WallpaperManager)
1240 getSystemService(Context.WALLPAPER_SERVICE);
1241 WallpaperInfo wi = wm.getWallpaperInfo();
1242 if (wi != null && wi.getSettingsActivity() != null) {
1243 LabeledIntent li = new LabeledIntent(getPackageName(),
1244 R.string.configure_wallpaper, 0);
1245 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1246 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1247 }
1248 startActivity(chooser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
1250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001252 public void onWindowFocusChanged(boolean hasFocus) {
1253 super.onWindowFocusChanged(hasFocus);
1254 if (!hasFocus) {
1255 mBackDown = mHomeDown = false;
1256 }
1257 }
1258
1259 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 public boolean dispatchKeyEvent(KeyEvent event) {
1261 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1262 switch (event.getKeyCode()) {
1263 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001264 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 return true;
1266 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001267 mHomeDown = true;
1268 return true;
1269 }
1270 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1271 switch (event.getKeyCode()) {
1272 case KeyEvent.KEYCODE_BACK:
1273 if (!event.isCanceled()) {
1274 mWorkspace.dispatchKeyEvent(event);
Joe Onorato7c312c12009-08-13 21:36:53 -07001275 if (mAllAppsVisible) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001276 closeAllAppsDialog(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001277 } else {
1278 closeFolder();
1279 }
1280 }
1281 mBackDown = false;
1282 return true;
1283 case KeyEvent.KEYCODE_HOME:
1284 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 return true;
1286 }
1287 }
1288
1289 return super.dispatchKeyEvent(event);
1290 }
1291
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 private void closeFolder() {
1293 Folder folder = mWorkspace.getOpenFolder();
1294 if (folder != null) {
1295 closeFolder(folder);
1296 }
1297 }
1298
1299 void closeFolder(Folder folder) {
1300 folder.getInfo().opened = false;
1301 ViewGroup parent = (ViewGroup) folder.getParent();
1302 if (parent != null) {
1303 parent.removeView(folder);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001304 // TODO: this line crashes.
1305 //mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
1307 folder.onClose();
1308 }
1309
1310 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001311 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1312 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314 private void unbindDesktopItems() {
1315 for (ItemInfo item: mDesktopItems) {
1316 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
1318 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001319
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 /**
1321 * Launches the intent referred by the clicked shortcut.
1322 *
1323 * @param v The view representing the clicked shortcut.
1324 */
1325 public void onClick(View v) {
1326 Object tag = v.getTag();
1327 if (tag instanceof ApplicationInfo) {
1328 // Open shortcut
1329 final Intent intent = ((ApplicationInfo) tag).intent;
1330 startActivitySafely(intent);
1331 } else if (tag instanceof FolderInfo) {
1332 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001333 } else if (v == mHandleView) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001334 if (mAllAppsVisible) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001335 // TODO how can we be here?
1336 } else {
Joe Onorato45d43462009-07-31 14:36:51 -07001337 showAllAppsDialog();
Joe Onorato7404ee42009-07-31 11:54:44 -07001338 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 }
1340 }
1341
1342 void startActivitySafely(Intent intent) {
1343 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1344 try {
1345 startActivity(intent);
1346 } catch (ActivityNotFoundException e) {
1347 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1348 } catch (SecurityException e) {
1349 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001350 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1352 "or use the exported attribute for this activity.", e);
1353 }
1354 }
1355
1356 private void handleFolderClick(FolderInfo folderInfo) {
1357 if (!folderInfo.opened) {
1358 // Close any open folder
1359 closeFolder();
1360 // Open the requested folder
1361 openFolder(folderInfo);
1362 } else {
1363 // Find the open folder...
1364 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1365 int folderScreen;
1366 if (openFolder != null) {
1367 folderScreen = mWorkspace.getScreenForView(openFolder);
1368 // .. and close it
1369 closeFolder(openFolder);
1370 if (folderScreen != mWorkspace.getCurrentScreen()) {
1371 // Close any folder open on the current screen
1372 closeFolder();
1373 // Pull the folder onto this screen
1374 openFolder(folderInfo);
1375 }
1376 }
1377 }
1378 }
1379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 /**
1381 * Opens the user fodler described by the specified tag. The opening of the folder
1382 * is animated relative to the specified View. If the View is null, no animation
1383 * is played.
1384 *
1385 * @param folderInfo The FolderInfo describing the folder to open.
1386 */
1387 private void openFolder(FolderInfo folderInfo) {
1388 Folder openFolder;
1389
1390 if (folderInfo instanceof UserFolderInfo) {
1391 openFolder = UserFolder.fromXml(this);
1392 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001393 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 } else {
1395 return;
1396 }
1397
Joe Onorato00acb122009-08-04 16:04:30 -04001398 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 openFolder.setLauncher(this);
1400
1401 openFolder.bind(folderInfo);
1402 folderInfo.opened = true;
1403
1404 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1405 openFolder.onOpen();
1406 }
1407
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001409 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 return false;
1411 }
1412
1413 if (!(v instanceof CellLayout)) {
1414 v = (View) v.getParent();
1415 }
1416
1417 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1418
1419 // This happens when long clicking an item with the dpad/trackball
1420 if (cellInfo == null) {
1421 return true;
1422 }
1423
1424 if (mWorkspace.allowLongPress()) {
1425 if (cellInfo.cell == null) {
1426 if (cellInfo.valid) {
1427 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001428 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 showAddDialog(cellInfo);
1430 }
1431 } else {
1432 if (!(cellInfo.cell instanceof Folder)) {
1433 // User long pressed on an item
1434 mWorkspace.startDrag(cellInfo);
1435 }
1436 }
1437 }
1438 return true;
1439 }
1440
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001441 View getDrawerHandle() {
1442 return mHandleView;
1443 }
1444
Joe Onorato7c312c12009-08-13 21:36:53 -07001445 boolean isDrawerDown() { // TODO rename to isAllAppsVisible()
1446 return /* TODO !mDrawer.isMoving() && */ !mAllAppsVisible;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
1448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 Workspace getWorkspace() {
1450 return mWorkspace;
1451 }
1452
Joe Onorato7404ee42009-07-31 11:54:44 -07001453 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 GridView getApplicationsGrid() {
1455 return mAllAppsGrid;
1456 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001457 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458
1459 @Override
1460 protected Dialog onCreateDialog(int id) {
1461 switch (id) {
1462 case DIALOG_CREATE_SHORTCUT:
1463 return new CreateShortcut().createDialog();
1464 case DIALOG_RENAME_FOLDER:
1465 return new RenameFolder().createDialog();
1466 }
1467
1468 return super.onCreateDialog(id);
1469 }
1470
1471 @Override
1472 protected void onPrepareDialog(int id, Dialog dialog) {
1473 switch (id) {
1474 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 break;
1476 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001477 if (mFolderInfo != null) {
1478 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1479 final CharSequence text = mFolderInfo.title;
1480 input.setText(text);
1481 input.setSelection(0, text.length());
1482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 break;
1484 }
1485 }
1486
1487 void showRenameDialog(FolderInfo info) {
1488 mFolderInfo = info;
1489 mWaitingForResult = true;
1490 showDialog(DIALOG_RENAME_FOLDER);
1491 }
1492
1493 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1494 mAddItemCellInfo = cellInfo;
1495 mWaitingForResult = true;
1496 showDialog(DIALOG_CREATE_SHORTCUT);
1497 }
1498
Romain Guy73b979d2009-06-09 12:57:21 -07001499 private void pickShortcut(int requestCode, int title) {
1500 Bundle bundle = new Bundle();
1501
1502 ArrayList<String> shortcutNames = new ArrayList<String>();
1503 shortcutNames.add(getString(R.string.group_applications));
1504 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1505
1506 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1507 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1508 R.drawable.ic_launcher_application));
1509 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1510
1511 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1512 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1513 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1514 pickIntent.putExtras(bundle);
1515
1516 startActivityForResult(pickIntent, requestCode);
1517 }
1518
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 private class RenameFolder {
1520 private EditText mInput;
1521
1522 Dialog createDialog() {
1523 mWaitingForResult = true;
1524 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1525 mInput = (EditText) layout.findViewById(R.id.folder_name);
1526
1527 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1528 builder.setIcon(0);
1529 builder.setTitle(getString(R.string.rename_folder_title));
1530 builder.setCancelable(true);
1531 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1532 public void onCancel(DialogInterface dialog) {
1533 cleanup();
1534 }
1535 });
1536 builder.setNegativeButton(getString(R.string.cancel_action),
1537 new Dialog.OnClickListener() {
1538 public void onClick(DialogInterface dialog, int which) {
1539 cleanup();
1540 }
1541 }
1542 );
1543 builder.setPositiveButton(getString(R.string.rename_action),
1544 new Dialog.OnClickListener() {
1545 public void onClick(DialogInterface dialog, int which) {
1546 changeFolderName();
1547 }
1548 }
1549 );
1550 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001551
1552 final AlertDialog dialog = builder.create();
1553 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1554 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001555 }
1556 });
1557
1558 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 }
1560
1561 private void changeFolderName() {
1562 final String name = mInput.getText().toString();
1563 if (!TextUtils.isEmpty(name)) {
1564 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 mFolderInfo.title = name;
1567 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1568
Joe Onorato9c1289c2009-08-17 11:03:03 -04001569 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001570 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001571 mModel.setWorkspaceDirty();
1572 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 } else {
1574 final FolderIcon folderIcon = (FolderIcon)
1575 mWorkspace.getViewForTag(mFolderInfo);
1576 if (folderIcon != null) {
1577 folderIcon.setText(name);
1578 getWorkspace().requestLayout();
1579 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001580 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001581 mModel.setWorkspaceDirty();
1582 mWorkspaceLoading = true;
1583 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
1585 }
1586 }
1587 cleanup();
1588 }
1589
1590 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 dismissDialog(DIALOG_RENAME_FOLDER);
1592 mWaitingForResult = false;
1593 mFolderInfo = null;
1594 }
1595 }
1596
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001597 void showAllAppsDialog() {
Joe Onorato7c312c12009-08-13 21:36:53 -07001598 mAllAppsVisible = true;
Joe Onorato006b25f2009-09-03 11:38:43 -07001599 mAllAppsGrid.show();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001600 mWorkspace.hide();
Joe Onorato7c312c12009-08-13 21:36:53 -07001601
1602 // TODO: fade these two too
1603 mDeleteZone.setVisibility(View.GONE);
1604 mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001605 }
1606
1607 void closeAllAppsDialog(boolean animated) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001608 if (mAllAppsVisible) {
1609 Log.d(LOG_TAG, "closing all apps");
Joe Onorato006b25f2009-09-03 11:38:43 -07001610 mAllAppsGrid.hide(animated);
Joe Onorato7c312c12009-08-13 21:36:53 -07001611 mAllAppsVisible = false;
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001612 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1613 mWorkspace.show();
Joe Onorato7c312c12009-08-13 21:36:53 -07001614
1615 // TODO: fade these two too
1616 mDeleteZone.setVisibility(View.VISIBLE);
1617 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001618 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001619 }
1620
Joe Onorato7c312c12009-08-13 21:36:53 -07001621 void lockAllApps() {
1622 // TODO
1623 }
1624
1625 void unlockAllApps() {
1626 // TODO
1627 }
1628
Joe Onorato7404ee42009-07-31 11:54:44 -07001629 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 * Displays the shortcut creation dialog and launches, if necessary, the
1631 * appropriate activity.
1632 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001633 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001634 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1635 DialogInterface.OnShowListener {
1636
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001638
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 Dialog createDialog() {
1640 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001641
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001643
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1645 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001646 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 builder.setInverseBackgroundForced(true);
1649
1650 AlertDialog dialog = builder.create();
1651 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001652 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001653 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 return dialog;
1656 }
1657
1658 public void onCancel(DialogInterface dialog) {
1659 mWaitingForResult = false;
1660 cleanup();
1661 }
1662
Romain Guycbb89e42009-06-08 15:52:54 -07001663 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001664 }
1665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 dismissDialog(DIALOG_CREATE_SHORTCUT);
1668 }
1669
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001670 /**
1671 * Handle the action clicked in the "Add to home" dialog.
1672 */
1673 public void onClick(DialogInterface dialog, int which) {
1674 Resources res = getResources();
1675 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001676
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001677 switch (which) {
1678 case AddAdapter.ITEM_SHORTCUT: {
1679 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001680 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001681 break;
1682 }
Romain Guycbb89e42009-06-08 15:52:54 -07001683
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001684 case AddAdapter.ITEM_APPWIDGET: {
1685 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001686
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001687 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1688 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1689 // add the search widget
1690 ArrayList<AppWidgetProviderInfo> customInfo =
1691 new ArrayList<AppWidgetProviderInfo>();
1692 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1693 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1694 info.label = getString(R.string.group_search);
1695 info.icon = R.drawable.ic_search_widget;
1696 customInfo.add(info);
1697 pickIntent.putParcelableArrayListExtra(
1698 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1699 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1700 Bundle b = new Bundle();
1701 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1702 customExtras.add(b);
1703 pickIntent.putParcelableArrayListExtra(
1704 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1705 // start the pick activity
1706 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1707 break;
1708 }
Romain Guycbb89e42009-06-08 15:52:54 -07001709
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001710 case AddAdapter.ITEM_LIVE_FOLDER: {
1711 // Insert extra item to handle inserting folder
1712 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001713
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001714 ArrayList<String> shortcutNames = new ArrayList<String>();
1715 shortcutNames.add(res.getString(R.string.group_folder));
1716 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001717
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001718 ArrayList<ShortcutIconResource> shortcutIcons =
1719 new ArrayList<ShortcutIconResource>();
1720 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1721 R.drawable.ic_launcher_folder));
1722 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1723
1724 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1725 pickIntent.putExtra(Intent.EXTRA_INTENT,
1726 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1727 pickIntent.putExtra(Intent.EXTRA_TITLE,
1728 getText(R.string.title_select_live_folder));
1729 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001730
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001731 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1732 break;
1733 }
1734
1735 case AddAdapter.ITEM_WALLPAPER: {
1736 startWallpaper();
1737 break;
1738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 }
1740 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001741
1742 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001743 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744 }
1745
1746 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001749 public int getCurrentWorkspaceScreen() {
1750 return mWorkspace.getCurrentScreen();
1751 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001752
Joe Onorato9c1289c2009-08-17 11:03:03 -04001753 /**
1754 * Refreshes the shortcuts shown on the workspace.
1755 *
1756 * Implementation of the method from LauncherModel.Callbacks.
1757 */
1758 public void startBinding() {
1759 final Workspace workspace = mWorkspace;
1760 int count = workspace.getChildCount();
1761 for (int i = 0; i < count; i++) {
1762 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1763 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001764
Joe Onorato9c1289c2009-08-17 11:03:03 -04001765 if (DEBUG_USER_INTERFACE) {
1766 android.widget.Button finishButton = new android.widget.Button(this);
1767 finishButton.setText("Finish");
1768 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1769
1770 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1771 public void onClick(View v) {
1772 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001773 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001774 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 }
1776 }
1777
1778 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001779 * Bind the items start-end from the list.
1780 *
1781 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001783 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1784
1785 final Workspace workspace = mWorkspace;
1786
1787 for (int i=start; i<end; i++) {
1788 final ItemInfo item = shortcuts.get(i);
1789 mDesktopItems.add(item);
1790 switch (item.itemType) {
1791 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1792 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1793 final View shortcut = createShortcut((ApplicationInfo) item);
1794 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1795 false);
1796 break;
1797 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1798 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1799 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1800 (UserFolderInfo) item);
1801 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1802 false);
1803 break;
1804 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1805 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1806 R.layout.live_folder_icon, this,
1807 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1808 (LiveFolderInfo) item);
1809 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1810 false);
1811 break;
1812 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1813 final int screen = workspace.getCurrentScreen();
1814 final View view = mInflater.inflate(R.layout.widget_search,
1815 (ViewGroup) workspace.getChildAt(screen), false);
1816
1817 Search search = (Search) view.findViewById(R.id.widget_search);
1818 search.setLauncher(this);
1819
1820 final Widget widget = (Widget) item;
1821 view.setTag(widget);
1822
1823 workspace.addWidget(view, widget, false);
1824 break;
1825 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 }
1827
Joe Onorato9c1289c2009-08-17 11:03:03 -04001828 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 }
1830
Joe Onorato9c1289c2009-08-17 11:03:03 -04001831 /**
1832 * Implementation of the method from LauncherModel.Callbacks.
1833 */
1834 void bindFolders(HashMap<Long, FolderInfo> folders) {
1835 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001837
1838 /**
1839 * Add the views for a widget to the workspace.
1840 *
1841 * Implementation of the method from LauncherModel.Callbacks.
1842 */
1843 public void bindAppWidget(LauncherAppWidgetInfo item) {
1844 final Workspace workspace = mWorkspace;
1845
1846 final int appWidgetId = item.appWidgetId;
1847 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1848 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1849
1850 if (true) {
1851 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1852 appWidgetId, appWidgetInfo));
1853 }
1854
1855 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1856 item.hostView.setTag(item);
1857
1858 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1859 item.cellY, item.spanX, item.spanY, false);
1860
1861 workspace.requestLayout();
1862
1863 mDesktopItems.add(item);
1864 }
1865
1866 /**
1867 * Callback saying that there aren't any more items to bind.
1868 *
1869 * Implementation of the method from LauncherModel.Callbacks.
1870 */
1871 public void finishBindingItems() {
1872 if (mSavedState != null) {
1873 if (!mWorkspace.hasFocus()) {
1874 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1875 }
1876
1877 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1878 if (userFolders != null) {
1879 for (long folderId : userFolders) {
1880 final FolderInfo info = mFolders.get(folderId);
1881 if (info != null) {
1882 openFolder(info);
1883 }
1884 }
1885 final Folder openFolder = mWorkspace.getOpenFolder();
1886 if (openFolder != null) {
1887 openFolder.requestFocus();
1888 }
1889 }
1890
1891 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1892 if (allApps) {
1893 showAllAppsDialog();
1894 }
1895
1896 mSavedState = null;
1897 }
1898
1899 if (mSavedInstanceState != null) {
1900 super.onRestoreInstanceState(mSavedInstanceState);
1901 mSavedInstanceState = null;
1902 }
1903
1904 /* TODO
1905 if (mAllAppsVisible && !mDrawer.hasFocus()) {
1906 mDrawer.requestFocus();
1907 }
1908 */
1909
1910 Log.d(TAG, "finishBindingItems done");
1911 mWorkspaceLoading = false;
1912 }
1913
1914 /**
1915 * Add the icons for all apps.
1916 *
1917 * Implementation of the method from LauncherModel.Callbacks.
1918 */
1919 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
1920 Log.d(LOG_TAG, "got info for " + apps.size() + " apps");
1921 mAllAppsList = apps;
1922 mAllAppsGrid.setApps(mAllAppsList);
1923 }
1924
1925 /**
1926 * A package was installed.
1927 *
1928 * Implementation of the method from LauncherModel.Callbacks.
1929 */
1930 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1931 removeDialog(DIALOG_CREATE_SHORTCUT);
1932 }
1933
1934 /**
1935 * A package was updated.
1936 *
1937 * Implementation of the method from LauncherModel.Callbacks.
1938 */
1939 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1940 removeDialog(DIALOG_CREATE_SHORTCUT);
1941 mWorkspace.updateShortcutsForPackage(packageName);
1942 }
1943
1944 /**
1945 * A package was uninstalled.
1946 *
1947 * Implementation of the method from LauncherModel.Callbacks.
1948 */
1949 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1950 removeDialog(DIALOG_CREATE_SHORTCUT);
1951 mWorkspace.removeShortcutsForPackage(packageName);
1952 }
1953
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954}