blob: 36d7da16ed456cdcbcafd9755f7ef35c90bb0854 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.os.Bundle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Parcelable;
43import android.os.RemoteException;
44import android.os.ServiceManager;
Joe Onorato080d9b62009-11-02 12:01:11 -050045import android.os.SystemClock;
Karl Rosaen138a0412009-04-23 19:00:21 -070046import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.text.Selection;
48import android.text.SpannableStringBuilder;
49import android.text.TextUtils;
50import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070051import static android.util.Log.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070052import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.view.KeyEvent;
55import android.view.LayoutInflater;
56import android.view.Menu;
57import android.view.MenuItem;
58import android.view.View;
59import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.view.View.OnLongClickListener;
61import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.widget.TextView;
64import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070065import android.appwidget.AppWidgetManager;
66import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040069import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070070import java.io.DataOutputStream;
71import java.io.FileNotFoundException;
72import java.io.IOException;
73import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
75/**
76 * Default launcher application.
77 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040078public final class Launcher extends Activity
Joe Onorato7bb17492009-09-24 17:51:01 -070079 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040081 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 static final boolean LOGD = false;
83
Joe Onorato9c1289c2009-08-17 11:03:03 -040084 static final boolean PROFILE_STARTUP = false;
85 static final boolean PROFILE_ROTATE = false;
86 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070087
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 private static final int WALLPAPER_SCREENS_SPAN = 2;
89
90 private static final int MENU_GROUP_ADD = 1;
91 private static final int MENU_ADD = Menu.FIRST + 1;
92 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
93 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
94 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -070095 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096
97 private static final int REQUEST_CREATE_SHORTCUT = 1;
98 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070099 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 private static final int REQUEST_PICK_APPLICATION = 6;
101 private static final int REQUEST_PICK_SHORTCUT = 7;
102 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700103 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700104 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105
106 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
107
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700108 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
109 static final String SEARCH_WIDGET = "search_widget";
110
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800111 static final int SCREEN_COUNT = 5;
112 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700114 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Joe Onorato7c312c12009-08-13 21:36:53 -0700116 static final int DIALOG_CREATE_SHORTCUT = 1;
117 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Romain Guy98d01652009-06-30 16:21:04 -0700119 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
121 // Type: int
122 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
123 // Type: boolean
124 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
125 // Type: long
126 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
127 // Type: int
128 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
129 // Type: int
130 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
131 // Type: int
132 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
133 // Type: int
134 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
141 // Type: int[]
142 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
143 // Type: boolean
144 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
145 // Type: long
146 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
147
Joe Onorato9c1289c2009-08-17 11:03:03 -0400148 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800151 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private LayoutInflater mInflater;
154
Joe Onorato00acb122009-08-04 16:04:30 -0400155 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700157
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700158 private AppWidgetManager mAppWidgetManager;
159 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 private CellLayout.CellInfo mAddItemCellInfo;
162 private CellLayout.CellInfo mMenuAddInfo;
163 private final int[] mCellCoordinates = new int[2];
164 private FolderInfo mFolderInfo;
165
Joe Onorato7c312c12009-08-13 21:36:53 -0700166 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700168 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private Bundle mSavedState;
171
172 private SpannableStringBuilder mDefaultKeySsb = null;
173
Mike LeBeau736cf282009-07-02 17:46:59 -0700174 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 private boolean mWorkspaceLoading = true;
177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private boolean mRestoring;
179 private boolean mWaitingForResult;
Joe Onoratoff3862d2009-10-07 10:35:51 -0700180 private boolean mExitingBecauseOfLaunch;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
182 private Bundle mSavedInstanceState;
183
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 private LauncherModel mModel;
185
Romain Guy84f296c2009-11-04 15:00:44 -0800186 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
187 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700188
Joe Onorato080d9b62009-11-02 12:01:11 -0500189 public static long lastStartTime;
190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 @Override
192 protected void onCreate(Bundle savedInstanceState) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500193 lastStartTime = SystemClock.uptimeMillis();
194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400198 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700200
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700201 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700202
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
204 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 if (PROFILE_STARTUP) {
207 android.os.Debug.startMethodTracing("/sdcard/launcher");
208 }
209
210 checkForLocaleChange();
211 setWallpaperDimension();
212
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 setContentView(R.layout.launcher);
214 setupViews();
215
Joe Onorato7c312c12009-08-13 21:36:53 -0700216 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 mSavedState = savedInstanceState;
219 restoreState(mSavedState);
220
221 if (PROFILE_STARTUP) {
222 android.os.Debug.stopMethodTracing();
223 }
224
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700225 // We have a new AllAppsView, we need to re-bind everything, and it could have
226 // changed in our absence.
227 mModel.setAllAppsDirty();
228 mModel.setWorkspaceDirty();
229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 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);
Jason Samsfd22dac2009-09-20 17:24:16 -0700242
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
Romain Guy84f296c2009-11-04 15:00:44 -0800254 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255
Romain Guy84f296c2009-11-04 15:00:44 -0800256 if (localeChanged) {
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 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700271
Romain Guy98d01652009-06-30 16:21:04 -0700272 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;
Mike Clerona0618e42009-10-22 13:55:21 -0700374 case REQUEST_PICK_WALLPAPER:
375 // We just wanted the activity result here so we can clear mWaitingForResult
376 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700378 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700380 // Clean up the appWidgetId if we canceled
381 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
382 if (appWidgetId != -1) {
383 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 }
385 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 }
387
388 @Override
389 protected void onResume() {
Joe Onorato080d9b62009-11-02 12:01:11 -0500390 if (lastStartTime == 0) {
391 lastStartTime = SystemClock.uptimeMillis();
392 }
393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 super.onResume();
395
396 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400397 mWorkspaceLoading = true;
398 mModel.startLoader(this, true);
399 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700401
Mike LeBeau736cf282009-07-02 17:46:59 -0700402 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
403 // onNewIntent), then close the search dialog if needed, because it probably
404 // came from the user pressing 'home' (rather than, for example, pressing 'back').
405 if (mIsNewIntent) {
406 // Post to a handler so that this happens after the search dialog tries to open
407 // itself again.
408 mWorkspace.post(new Runnable() {
409 public void run() {
410 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
411 ServiceManager.getService(Context.SEARCH_SERVICE));
412 try {
413 searchManagerService.stopSearch();
414 } catch (RemoteException e) {
415 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700416 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700417 }
418 });
419 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700420
Mike LeBeau736cf282009-07-02 17:46:59 -0700421 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 }
423
424 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700425 protected void onPause() {
426 super.onPause();
Joe Onoratoff3862d2009-10-07 10:35:51 -0700427 if (mExitingBecauseOfLaunch) {
428 mExitingBecauseOfLaunch = false;
429 closeAllApps(false);
430 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700431 }
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700433 @Override
434 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400435 // Flag the loader to stop early before switching
436 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700438 if (PROFILE_ROTATE) {
439 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
440 }
441 return null;
442 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 private boolean acceptFilter() {
445 final InputMethodManager inputManager = (InputMethodManager)
446 getSystemService(Context.INPUT_METHOD_SERVICE);
447 return !inputManager.isFullscreenMode();
448 }
449
450 @Override
451 public boolean onKeyDown(int keyCode, KeyEvent event) {
452 boolean handled = super.onKeyDown(keyCode, event);
453 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
454 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
455 keyCode, event);
456 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700457 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700458 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700459 // showSearchDialog()
460 // If there are multiple keystrokes before the search dialog takes focus,
461 // onSearchRequested() will be called for every keystroke,
462 // but it is idempotent, so it's fine.
463 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 }
465 }
466
467 return handled;
468 }
469
Karl Rosaen138a0412009-04-23 19:00:21 -0700470 private String getTypedText() {
471 return mDefaultKeySsb.toString();
472 }
473
474 private void clearTypedText() {
475 mDefaultKeySsb.clear();
476 mDefaultKeySsb.clearSpans();
477 Selection.setSelection(mDefaultKeySsb, 0);
478 }
479
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 /**
481 * Restores the previous state, if it exists.
482 *
483 * @param savedState The previous state.
484 */
485 private void restoreState(Bundle savedState) {
486 if (savedState == null) {
487 return;
488 }
489
490 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
491 if (currentScreen > -1) {
492 mWorkspace.setCurrentScreen(currentScreen);
493 }
494
495 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
496 if (addScreen > -1) {
497 mAddItemCellInfo = new CellLayout.CellInfo();
498 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
499 addItemCellInfo.valid = true;
500 addItemCellInfo.screen = addScreen;
501 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
502 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
503 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
504 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
505 addItemCellInfo.findVacantCellsFromOccupied(
506 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
507 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
508 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
509 mRestoring = true;
510 }
511
512 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
513 if (renameFolder) {
514 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400515 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800516 mRestoring = true;
517 }
518 }
519
520 /**
521 * Finds all the views we need and configure them properly.
522 */
523 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400524 DragController dragController = mDragController;
525
Romain Guyb1b69f52009-08-10 15:10:15 -0700526 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400527 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800528
Joe Onorato7c312c12009-08-13 21:36:53 -0700529 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700530 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700531 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700532 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700533 // Manage focusability manually since this thing is always visible
534 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700535
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
537 final Workspace workspace = mWorkspace;
538
Joe Onorato7c312c12009-08-13 21:36:53 -0700539 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
540 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541
Joe Onorato7c312c12009-08-13 21:36:53 -0700542 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700543 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700544 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700545 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700546 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700547 handleIocon.setCrossFadeEnabled(true);
548 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400551 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553
554 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400555 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700556 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557
Joe Onorato00acb122009-08-04 16:04:30 -0400558 dragController.setDragScoller(workspace);
559 dragController.setDragListener(deleteZone);
560 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700561
Joe Onorato00acb122009-08-04 16:04:30 -0400562 // The order here is bottom to top.
563 dragController.addDropTarget(workspace);
564 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 }
566
567 /**
568 * Creates a view representing a shortcut.
569 *
570 * @param info The data structure describing the shortcut.
571 *
572 * @return A View inflated from R.layout.application.
573 */
574 View createShortcut(ApplicationInfo info) {
575 return createShortcut(R.layout.application,
576 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
577 }
578
579 /**
580 * Creates a view representing a shortcut inflated from the specified resource.
581 *
582 * @param layoutResId The id of the XML layout used to create the shortcut.
583 * @param parent The group the shortcut belongs to.
584 * @param info The data structure describing the shortcut.
585 *
586 * @return A View inflated from layoutResId.
587 */
588 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
589 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
590
Joe Onorato5162ea92009-09-03 09:39:42 -0700591 if (info.icon == null) {
592 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
593 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700595 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 info.filtered = true;
597 }
598
599 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
600 favorite.setText(info.title);
601 favorite.setTag(info);
602 favorite.setOnClickListener(this);
603
604 return favorite;
605 }
606
607 /**
608 * Add an application shortcut to the workspace.
609 *
610 * @param data The intent describing the application.
611 * @param cellInfo The position on screen where to create the shortcut.
612 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400613 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 cellInfo.screen = mWorkspace.getCurrentScreen();
615 if (!findSingleSlot(cellInfo)) return;
616
Romain Guy73b979d2009-06-09 12:57:21 -0700617 final ApplicationInfo info = infoFromApplicationIntent(context, data);
618 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400619 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700620 }
621 }
622
623 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 ComponentName component = data.getComponent();
625 PackageManager packageManager = context.getPackageManager();
626 ActivityInfo activityInfo = null;
627 try {
628 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
629 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700630 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 }
Romain Guycbb89e42009-06-08 15:52:54 -0700632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 if (activityInfo != null) {
634 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 itemInfo.title = activityInfo.loadLabel(packageManager);
637 if (itemInfo.title == null) {
638 itemInfo.title = activityInfo.name;
639 }
Romain Guycbb89e42009-06-08 15:52:54 -0700640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
642 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
643 itemInfo.icon = activityInfo.loadIcon(packageManager);
644 itemInfo.container = ItemInfo.NO_ID;
645
Romain Guy73b979d2009-06-09 12:57:21 -0700646 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 }
Romain Guy73b979d2009-06-09 12:57:21 -0700648
649 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
Romain Guycbb89e42009-06-08 15:52:54 -0700651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 /**
653 * Add a shortcut to the workspace.
654 *
655 * @param data The intent describing the shortcut.
656 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 cellInfo.screen = mWorkspace.getCurrentScreen();
660 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
663
664 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
667 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
669 }
670
Romain Guycbb89e42009-06-08 15:52:54 -0700671
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700673 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700675 * @param data The intent describing the appWidgetId.
676 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400678 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700680 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700681
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700682 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700683
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700684 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700685
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 // Try finding open space on Launcher screen
691 final int[] xy = mCellCoordinates;
692 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
693
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700694 // Build Launcher-specific widget info and save to database
695 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 launcherInfo.spanX = spans[0];
697 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700698
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 LauncherModel.addItemToDatabase(this, launcherInfo,
700 LauncherSettings.Favorites.CONTAINER_DESKTOP,
701 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
702
703 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400704 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700708
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700709 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400713 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 }
715 }
Romain Guycbb89e42009-06-08 15:52:54 -0700716
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
718 mDesktopItems.remove(launcherInfo);
719 launcherInfo.hostView = null;
720 }
721
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700722 public LauncherAppWidgetHost getAppWidgetHost() {
723 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 }
Romain Guycbb89e42009-06-08 15:52:54 -0700725
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 static ApplicationInfo addShortcut(Context context, Intent data,
727 CellLayout.CellInfo cellInfo, boolean notify) {
728
Romain Guy73b979d2009-06-09 12:57:21 -0700729 final ApplicationInfo info = infoFromShortcutIntent(context, data);
730 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
731 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
732
733 return info;
734 }
735
736 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
738 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
739 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
740
741 Drawable icon = null;
742 boolean filtered = false;
743 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700744 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745
746 if (bitmap != null) {
747 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
748 filtered = true;
749 customIcon = true;
750 } else {
751 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700752 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700754 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 final PackageManager packageManager = context.getPackageManager();
756 Resources resources = packageManager.getResourcesForApplication(
757 iconResource.packageName);
758 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
759 icon = resources.getDrawable(id);
760 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700761 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
763 }
764 }
765
766 if (icon == null) {
767 icon = context.getPackageManager().getDefaultActivityIcon();
768 }
769
770 final ApplicationInfo info = new ApplicationInfo();
771 info.icon = icon;
772 info.filtered = filtered;
773 info.title = name;
774 info.intent = intent;
775 info.customIcon = customIcon;
776 info.iconResource = iconResource;
777
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 return info;
779 }
780
781 @Override
782 protected void onNewIntent(Intent intent) {
783 super.onNewIntent(intent);
784
785 // Close the menu
786 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
787 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700788
Joe Onorato742966b2009-10-07 10:26:17 -0700789 // Whatever we were doing is hereby canceled.
790 mWaitingForResult = false;
791
Mike LeBeau736cf282009-07-02 17:46:59 -0700792 // Set this flag so that onResume knows to close the search dialog if it's open,
793 // because this was a new intent (thus a press of 'home' or some such) rather than
794 // for example onResume being called when the user pressed the 'back' button.
795 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796
797 try {
798 dismissDialog(DIALOG_CREATE_SHORTCUT);
799 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 } catch (Exception e) {
801 // An exception is thrown if the dialog is not visible, which is fine
802 }
803
804 try {
805 dismissDialog(DIALOG_RENAME_FOLDER);
806 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 } catch (Exception e) {
808 // An exception is thrown if the dialog is not visible, which is fine
809 }
810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
812 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700813
Romain Guy94dabf12009-07-21 10:55:43 -0700814 if (!mWorkspace.isDefaultScreenShowing()) {
815 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700817
Joe Onorato7bb17492009-09-24 17:51:01 -0700818 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700819
820 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 if (v != null && v.getWindowToken() != null) {
822 InputMethodManager imm = (InputMethodManager)getSystemService(
823 INPUT_METHOD_SERVICE);
824 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
825 }
826 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700827 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 }
829 }
830 }
831
832 @Override
833 protected void onRestoreInstanceState(Bundle savedInstanceState) {
834 // Do not call super here
835 mSavedInstanceState = savedInstanceState;
836 }
837
838 @Override
839 protected void onSaveInstanceState(Bundle outState) {
840 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
841
842 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
843 if (folders.size() > 0) {
844 final int count = folders.size();
845 long[] ids = new long[count];
846 for (int i = 0; i < count; i++) {
847 final FolderInfo info = folders.get(i).getInfo();
848 ids[i] = info.id;
849 }
850 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
851 } else {
852 super.onSaveInstanceState(outState);
853 }
854
Romain Guy3cf604f2009-06-16 13:12:53 -0700855 final boolean isConfigurationChange = getChangingConfigurations() != 0;
856
Romain Guy5a941392009-04-28 15:18:25 -0700857 // When the drawer is opened and we are saving the state because of a
858 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400859 // TODO should not do this if the drawer is currently closing.
860 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863
864 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
865 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
866 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
867
868 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
869 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
870 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
871 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
872 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
873 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
874 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
875 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
876 layout.getOccupiedCells());
877 }
878
879 if (mFolderInfo != null && mWaitingForResult) {
880 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
881 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
882 }
883 }
884
885 @Override
886 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700888
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700890 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700892 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 }
894
895 TextKeyListener.getInstance().release();
896
Joe Onorato9c1289c2009-08-17 11:03:03 -0400897 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899 unbindDesktopItems();
900 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 }
902
903 @Override
904 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700905 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 super.startActivityForResult(intent, requestCode);
907 }
908
909 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700910 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700912
Joe Onorato7bb17492009-09-24 17:51:01 -0700913 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700914
Karl Rosaen138a0412009-04-23 19:00:21 -0700915 // Slide the search widget to the top, if it's on the current screen,
916 // otherwise show the search dialog immediately.
917 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
918 if (searchWidget == null) {
919 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
920 } else {
921 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
922 // show the currently typed text in the search widget while sliding
923 searchWidget.setQuery(getTypedText());
924 }
925 }
Romain Guycbb89e42009-06-08 15:52:54 -0700926
Karl Rosaen138a0412009-04-23 19:00:21 -0700927 /**
928 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700929 *
930 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700931 */
Romain Guycbb89e42009-06-08 15:52:54 -0700932 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700933 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700934
Karl Rosaen138a0412009-04-23 19:00:21 -0700935 if (initialQuery == null) {
936 // Use any text typed in the launcher as the initial query
937 initialQuery = getTypedText();
938 clearTypedText();
939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 if (appSearchData == null) {
941 appSearchData = new Bundle();
942 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
943 }
Romain Guycbb89e42009-06-08 15:52:54 -0700944
Karl Rosaen138a0412009-04-23 19:00:21 -0700945 final SearchManager searchManager =
946 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
947
948 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
949 if (searchWidget != null) {
950 // This gets called when the user leaves the search dialog to go back to
951 // the Launcher.
952 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
953 public void onCancel() {
954 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700955 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700956 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700957 });
958 }
Romain Guycbb89e42009-06-08 15:52:54 -0700959
Karl Rosaen138a0412009-04-23 19:00:21 -0700960 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700961 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 }
963
Karl Rosaen138a0412009-04-23 19:00:21 -0700964 /**
965 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700966 */
Romain Guy5a941392009-04-28 15:18:25 -0700967 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700968 // Close search dialog
969 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100970 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 // Restore search widget to its normal position
972 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
973 if (searchWidget != null) {
974 searchWidget.stopSearch(false);
975 }
976 }
Romain Guycbb89e42009-06-08 15:52:54 -0700977
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 @Override
979 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980 if (isWorkspaceLocked()) {
981 return false;
982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983
984 super.onCreateOptionsMenu(menu);
985 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
986 .setIcon(android.R.drawable.ic_menu_add)
987 .setAlphabeticShortcut('A');
988 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
989 .setIcon(android.R.drawable.ic_menu_gallery)
990 .setAlphabeticShortcut('W');
991 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
992 .setIcon(android.R.drawable.ic_search_category_default)
993 .setAlphabeticShortcut(SearchManager.MENU_KEY);
994 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
995 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
996 .setAlphabeticShortcut('N');
997
998 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700999 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1000 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001
1002 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1003 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1004 .setIntent(settings);
1005
1006 return true;
1007 }
1008
1009 @Override
1010 public boolean onPrepareOptionsMenu(Menu menu) {
1011 super.onPrepareOptionsMenu(menu);
1012
1013 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1014 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1015
1016 return true;
1017 }
1018
1019 @Override
1020 public boolean onOptionsItemSelected(MenuItem item) {
1021 switch (item.getItemId()) {
1022 case MENU_ADD:
1023 addItems();
1024 return true;
1025 case MENU_WALLPAPER_SETTINGS:
1026 startWallpaper();
1027 return true;
1028 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001029 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 return true;
1031 case MENU_NOTIFICATIONS:
1032 showNotifications();
1033 return true;
1034 }
1035
1036 return super.onOptionsItemSelected(item);
1037 }
Romain Guycbb89e42009-06-08 15:52:54 -07001038
Karl Rosaen138a0412009-04-23 19:00:21 -07001039 /**
1040 * Indicates that we want global search for this activity by setting the globalSearch
1041 * argument for {@link #startSearch} to true.
1042 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001044 @Override
1045 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001046 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001047 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001048 }
1049
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050 public boolean isWorkspaceLocked() {
1051 return mWorkspaceLoading || mWaitingForResult;
1052 }
1053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 private void addItems() {
1055 showAddDialog(mMenuAddInfo);
1056 }
1057
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001058 void addAppWidget(Intent data) {
1059 // TODO: catch bad widget exception when sent
1060 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001062 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1063 if (SEARCH_WIDGET.equals(customWidget)) {
1064 // We don't need this any more, since this isn't a real app widget.
1065 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1066 // add the search widget
1067 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001069 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1070
1071 if (appWidget.configure != null) {
1072 // Launch over to configure widget, if needed
1073 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1074 intent.setComponent(appWidget.configure);
1075 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1076
1077 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1078 } else {
1079 // Otherwise just add it
1080 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 }
1083 }
Romain Guycbb89e42009-06-08 15:52:54 -07001084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 void addSearch() {
1086 final Widget info = Widget.makeSearch();
1087 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 final int[] xy = mCellCoordinates;
1090 final int spanX = info.spanX;
1091 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1096 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 final View view = mInflater.inflate(info.layoutResource, null);
1099 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001100 Search search = (Search) view.findViewById(R.id.widget_search);
1101 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1104 }
1105
Romain Guy73b979d2009-06-09 12:57:21 -07001106 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001107 // Handle case where user selected "Applications"
1108 String applicationName = getResources().getString(R.string.group_applications);
1109 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001110
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001111 if (applicationName != null && applicationName.equals(shortcutName)) {
1112 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1113 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001114
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001115 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1116 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001117 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001118 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001119 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
1122
1123 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001124 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001125 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001126 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001127
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001128 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001129 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001130 } else {
1131 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 }
1134
Joe Onorato9c1289c2009-08-17 11:03:03 -04001135 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 UserFolderInfo folderInfo = new UserFolderInfo();
1137 folderInfo.title = getText(R.string.folder_name);
1138
1139 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1140 cellInfo.screen = mWorkspace.getCurrentScreen();
1141 if (!findSingleSlot(cellInfo)) return;
1142
1143 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001144 LauncherModel.addItemToDatabase(this, folderInfo,
1145 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001147 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148
1149 // Create the view
1150 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1151 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1152 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001153 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
Romain Guycbb89e42009-06-08 15:52:54 -07001155
Joe Onorato9c1289c2009-08-17 11:03:03 -04001156 void removeFolder(FolderInfo folder) {
1157 mFolders.remove(folder.id);
1158 }
1159
1160 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 cellInfo.screen = mWorkspace.getCurrentScreen();
1162 if (!findSingleSlot(cellInfo)) return;
1163
1164 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1165
1166 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1168 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001169 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1170 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 }
1172 }
1173
1174 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1175 CellLayout.CellInfo cellInfo, boolean notify) {
1176
1177 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1178 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1179
1180 Drawable icon = null;
1181 boolean filtered = false;
1182 Intent.ShortcutIconResource iconResource = null;
1183
1184 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1185 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1186 try {
1187 iconResource = (Intent.ShortcutIconResource) extra;
1188 final PackageManager packageManager = context.getPackageManager();
1189 Resources resources = packageManager.getResourcesForApplication(
1190 iconResource.packageName);
1191 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1192 icon = resources.getDrawable(id);
1193 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001194 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 }
1196 }
1197
1198 if (icon == null) {
1199 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1200 }
1201
1202 final LiveFolderInfo info = new LiveFolderInfo();
1203 info.icon = icon;
1204 info.filtered = filtered;
1205 info.title = name;
1206 info.iconResource = iconResource;
1207 info.uri = data.getData();
1208 info.baseIntent = baseIntent;
1209 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1210 LiveFolders.DISPLAY_MODE_GRID);
1211
1212 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1213 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001214 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215
1216 return info;
1217 }
1218
1219 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1220 final int[] xy = new int[2];
1221 if (findSlot(cellInfo, xy, 1, 1)) {
1222 cellInfo.cellX = xy[0];
1223 cellInfo.cellY = xy[1];
1224 return true;
1225 }
1226 return false;
1227 }
1228
1229 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1230 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1231 boolean[] occupied = mSavedState != null ?
1232 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1233 cellInfo = mWorkspace.findAllVacantCells(occupied);
1234 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1235 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1236 return false;
1237 }
1238 }
1239 return true;
1240 }
1241
1242 private void showNotifications() {
1243 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1244 if (statusBar != null) {
1245 statusBar.expand();
1246 }
1247 }
1248
1249 private void startWallpaper() {
1250 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001251 Intent chooser = Intent.createChooser(pickWallpaper,
1252 getText(R.string.chooser_wallpaper));
1253 WallpaperManager wm = (WallpaperManager)
1254 getSystemService(Context.WALLPAPER_SERVICE);
1255 WallpaperInfo wi = wm.getWallpaperInfo();
1256 if (wi != null && wi.getSettingsActivity() != null) {
1257 LabeledIntent li = new LabeledIntent(getPackageName(),
1258 R.string.configure_wallpaper, 0);
1259 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1260 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1261 }
Mike Clerona0618e42009-10-22 13:55:21 -07001262 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 }
1264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 @Override
1266 public boolean dispatchKeyEvent(KeyEvent event) {
1267 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1268 switch (event.getKeyCode()) {
1269 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 return true;
1271 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001272 return true;
1273 }
1274 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1275 switch (event.getKeyCode()) {
1276 case KeyEvent.KEYCODE_BACK:
1277 if (!event.isCanceled()) {
1278 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001279 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001280 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001281 } else {
1282 closeFolder();
1283 }
1284 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001285 return true;
1286 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 return true;
1288 }
1289 }
1290
1291 return super.dispatchKeyEvent(event);
1292 }
1293
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 private void closeFolder() {
1295 Folder folder = mWorkspace.getOpenFolder();
1296 if (folder != null) {
1297 closeFolder(folder);
1298 }
1299 }
1300
1301 void closeFolder(Folder folder) {
1302 folder.getInfo().opened = false;
1303 ViewGroup parent = (ViewGroup) folder.getParent();
1304 if (parent != null) {
1305 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001306 if (folder instanceof DropTarget) {
1307 // Live folders aren't DropTargets.
1308 mDragController.removeDropTarget((DropTarget)folder);
1309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
1311 folder.onClose();
1312 }
1313
1314 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1316 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001318 private void unbindDesktopItems() {
1319 for (ItemInfo item: mDesktopItems) {
1320 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001323
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 /**
1325 * Launches the intent referred by the clicked shortcut.
1326 *
1327 * @param v The view representing the clicked shortcut.
1328 */
1329 public void onClick(View v) {
1330 Object tag = v.getTag();
1331 if (tag instanceof ApplicationInfo) {
1332 // Open shortcut
1333 final Intent intent = ((ApplicationInfo) tag).intent;
1334 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001335 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 } else if (tag instanceof FolderInfo) {
1337 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001338 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001339 Log.d(TAG, "onClick");
1340 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001341 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001342 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001343 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346 }
1347
1348 void startActivitySafely(Intent intent) {
1349 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1350 try {
1351 startActivity(intent);
1352 } catch (ActivityNotFoundException e) {
1353 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1354 } catch (SecurityException e) {
1355 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001356 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1358 "or use the exported attribute for this activity.", e);
1359 }
1360 }
1361
1362 private void handleFolderClick(FolderInfo folderInfo) {
1363 if (!folderInfo.opened) {
1364 // Close any open folder
1365 closeFolder();
1366 // Open the requested folder
1367 openFolder(folderInfo);
1368 } else {
1369 // Find the open folder...
1370 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1371 int folderScreen;
1372 if (openFolder != null) {
1373 folderScreen = mWorkspace.getScreenForView(openFolder);
1374 // .. and close it
1375 closeFolder(openFolder);
1376 if (folderScreen != mWorkspace.getCurrentScreen()) {
1377 // Close any folder open on the current screen
1378 closeFolder();
1379 // Pull the folder onto this screen
1380 openFolder(folderInfo);
1381 }
1382 }
1383 }
1384 }
1385
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 /**
1387 * Opens the user fodler described by the specified tag. The opening of the folder
1388 * is animated relative to the specified View. If the View is null, no animation
1389 * is played.
1390 *
1391 * @param folderInfo The FolderInfo describing the folder to open.
1392 */
1393 private void openFolder(FolderInfo folderInfo) {
1394 Folder openFolder;
1395
1396 if (folderInfo instanceof UserFolderInfo) {
1397 openFolder = UserFolder.fromXml(this);
1398 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001399 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 } else {
1401 return;
1402 }
1403
Joe Onorato00acb122009-08-04 16:04:30 -04001404 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 openFolder.setLauncher(this);
1406
1407 openFolder.bind(folderInfo);
1408 folderInfo.opened = true;
1409
1410 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1411 openFolder.onOpen();
1412 }
1413
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001415 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 return false;
1417 }
1418
1419 if (!(v instanceof CellLayout)) {
1420 v = (View) v.getParent();
1421 }
1422
1423 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1424
1425 // This happens when long clicking an item with the dpad/trackball
1426 if (cellInfo == null) {
1427 return true;
1428 }
1429
1430 if (mWorkspace.allowLongPress()) {
1431 if (cellInfo.cell == null) {
1432 if (cellInfo.valid) {
1433 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001434 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 showAddDialog(cellInfo);
1436 }
1437 } else {
1438 if (!(cellInfo.cell instanceof Folder)) {
1439 // User long pressed on an item
1440 mWorkspace.startDrag(cellInfo);
1441 }
1442 }
1443 }
1444 return true;
1445 }
1446
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001447 View getDrawerHandle() {
1448 return mHandleView;
1449 }
1450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 Workspace getWorkspace() {
1452 return mWorkspace;
1453 }
1454
Joe Onorato7404ee42009-07-31 11:54:44 -07001455 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 GridView getApplicationsGrid() {
1457 return mAllAppsGrid;
1458 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001459 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460
1461 @Override
1462 protected Dialog onCreateDialog(int id) {
1463 switch (id) {
1464 case DIALOG_CREATE_SHORTCUT:
1465 return new CreateShortcut().createDialog();
1466 case DIALOG_RENAME_FOLDER:
1467 return new RenameFolder().createDialog();
1468 }
1469
1470 return super.onCreateDialog(id);
1471 }
1472
1473 @Override
1474 protected void onPrepareDialog(int id, Dialog dialog) {
1475 switch (id) {
1476 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 break;
1478 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001479 if (mFolderInfo != null) {
1480 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1481 final CharSequence text = mFolderInfo.title;
1482 input.setText(text);
1483 input.setSelection(0, text.length());
1484 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 break;
1486 }
1487 }
1488
1489 void showRenameDialog(FolderInfo info) {
1490 mFolderInfo = info;
1491 mWaitingForResult = true;
1492 showDialog(DIALOG_RENAME_FOLDER);
1493 }
1494
1495 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1496 mAddItemCellInfo = cellInfo;
1497 mWaitingForResult = true;
1498 showDialog(DIALOG_CREATE_SHORTCUT);
1499 }
1500
Romain Guy73b979d2009-06-09 12:57:21 -07001501 private void pickShortcut(int requestCode, int title) {
1502 Bundle bundle = new Bundle();
1503
1504 ArrayList<String> shortcutNames = new ArrayList<String>();
1505 shortcutNames.add(getString(R.string.group_applications));
1506 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1507
1508 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1509 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1510 R.drawable.ic_launcher_application));
1511 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1512
1513 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1514 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1515 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1516 pickIntent.putExtras(bundle);
1517
1518 startActivityForResult(pickIntent, requestCode);
1519 }
1520
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 private class RenameFolder {
1522 private EditText mInput;
1523
1524 Dialog createDialog() {
1525 mWaitingForResult = true;
1526 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1527 mInput = (EditText) layout.findViewById(R.id.folder_name);
1528
1529 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1530 builder.setIcon(0);
1531 builder.setTitle(getString(R.string.rename_folder_title));
1532 builder.setCancelable(true);
1533 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1534 public void onCancel(DialogInterface dialog) {
1535 cleanup();
1536 }
1537 });
1538 builder.setNegativeButton(getString(R.string.cancel_action),
1539 new Dialog.OnClickListener() {
1540 public void onClick(DialogInterface dialog, int which) {
1541 cleanup();
1542 }
1543 }
1544 );
1545 builder.setPositiveButton(getString(R.string.rename_action),
1546 new Dialog.OnClickListener() {
1547 public void onClick(DialogInterface dialog, int which) {
1548 changeFolderName();
1549 }
1550 }
1551 );
1552 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001553
1554 final AlertDialog dialog = builder.create();
1555 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1556 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001557 }
1558 });
1559
1560 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
1562
1563 private void changeFolderName() {
1564 final String name = mInput.getText().toString();
1565 if (!TextUtils.isEmpty(name)) {
1566 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001567 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 mFolderInfo.title = name;
1569 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1570
Joe Onorato9c1289c2009-08-17 11:03:03 -04001571 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001572 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001573 mModel.setWorkspaceDirty();
1574 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 } else {
1576 final FolderIcon folderIcon = (FolderIcon)
1577 mWorkspace.getViewForTag(mFolderInfo);
1578 if (folderIcon != null) {
1579 folderIcon.setText(name);
1580 getWorkspace().requestLayout();
1581 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001582 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001583 mModel.setWorkspaceDirty();
1584 mWorkspaceLoading = true;
1585 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
1587 }
1588 }
1589 cleanup();
1590 }
1591
1592 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 dismissDialog(DIALOG_RENAME_FOLDER);
1594 mWaitingForResult = false;
1595 mFolderInfo = null;
1596 }
1597 }
1598
Joe Onoratofb0ca672009-09-14 17:55:46 -04001599 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001600 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001601 }
1602
Jason Samsfd22dac2009-09-20 17:24:16 -07001603 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001604 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001605 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001606
Mike Cleronb6082fa02009-10-19 17:03:36 -07001607 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001608 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001609
Joe Onorato7c312c12009-08-13 21:36:53 -07001610 // TODO: fade these two too
1611 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001612 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001613 }
1614
Joe Onorato7bb17492009-09-24 17:51:01 -07001615 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001616 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001617 mAllAppsGrid.zoom(0.0f);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001618 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001619 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001620
Mike Cleronb6082fa02009-10-19 17:03:36 -07001621
Joe Onorato7c312c12009-08-13 21:36:53 -07001622 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001623 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001624 mDeleteZone.setVisibility(View.VISIBLE);
1625 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001626 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001627 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001628 }
1629
Joe Onorato7c312c12009-08-13 21:36:53 -07001630 void lockAllApps() {
1631 // TODO
1632 }
1633
1634 void unlockAllApps() {
1635 // TODO
1636 }
1637
Joe Onorato7404ee42009-07-31 11:54:44 -07001638 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 * Displays the shortcut creation dialog and launches, if necessary, the
1640 * appropriate activity.
1641 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001642 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001643 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1644 DialogInterface.OnShowListener {
1645
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 Dialog createDialog() {
1649 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001650
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1654 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001655 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 builder.setInverseBackgroundForced(true);
1658
1659 AlertDialog dialog = builder.create();
1660 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001661 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001662 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 return dialog;
1665 }
1666
1667 public void onCancel(DialogInterface dialog) {
1668 mWaitingForResult = false;
1669 cleanup();
1670 }
1671
Romain Guycbb89e42009-06-08 15:52:54 -07001672 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001673 }
1674
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 dismissDialog(DIALOG_CREATE_SHORTCUT);
1677 }
1678
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001679 /**
1680 * Handle the action clicked in the "Add to home" dialog.
1681 */
1682 public void onClick(DialogInterface dialog, int which) {
1683 Resources res = getResources();
1684 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001685
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001686 switch (which) {
1687 case AddAdapter.ITEM_SHORTCUT: {
1688 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001689 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001690 break;
1691 }
Romain Guycbb89e42009-06-08 15:52:54 -07001692
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001693 case AddAdapter.ITEM_APPWIDGET: {
1694 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001695
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001696 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1697 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1698 // add the search widget
1699 ArrayList<AppWidgetProviderInfo> customInfo =
1700 new ArrayList<AppWidgetProviderInfo>();
1701 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1702 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1703 info.label = getString(R.string.group_search);
1704 info.icon = R.drawable.ic_search_widget;
1705 customInfo.add(info);
1706 pickIntent.putParcelableArrayListExtra(
1707 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1708 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1709 Bundle b = new Bundle();
1710 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1711 customExtras.add(b);
1712 pickIntent.putParcelableArrayListExtra(
1713 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1714 // start the pick activity
1715 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1716 break;
1717 }
Romain Guycbb89e42009-06-08 15:52:54 -07001718
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001719 case AddAdapter.ITEM_LIVE_FOLDER: {
1720 // Insert extra item to handle inserting folder
1721 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001722
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001723 ArrayList<String> shortcutNames = new ArrayList<String>();
1724 shortcutNames.add(res.getString(R.string.group_folder));
1725 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001726
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001727 ArrayList<ShortcutIconResource> shortcutIcons =
1728 new ArrayList<ShortcutIconResource>();
1729 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1730 R.drawable.ic_launcher_folder));
1731 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1732
1733 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1734 pickIntent.putExtra(Intent.EXTRA_INTENT,
1735 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1736 pickIntent.putExtra(Intent.EXTRA_TITLE,
1737 getText(R.string.title_select_live_folder));
1738 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001739
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001740 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1741 break;
1742 }
1743
1744 case AddAdapter.ITEM_WALLPAPER: {
1745 startWallpaper();
1746 break;
1747 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 }
1749 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001750
1751 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001752 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 }
1754
1755 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001756 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001758 public int getCurrentWorkspaceScreen() {
1759 return mWorkspace.getCurrentScreen();
1760 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001761
Joe Onorato9c1289c2009-08-17 11:03:03 -04001762 /**
1763 * Refreshes the shortcuts shown on the workspace.
1764 *
1765 * Implementation of the method from LauncherModel.Callbacks.
1766 */
1767 public void startBinding() {
1768 final Workspace workspace = mWorkspace;
1769 int count = workspace.getChildCount();
1770 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001771 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001772 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1773 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001774
Joe Onorato9c1289c2009-08-17 11:03:03 -04001775 if (DEBUG_USER_INTERFACE) {
1776 android.widget.Button finishButton = new android.widget.Button(this);
1777 finishButton.setText("Finish");
1778 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1779
1780 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1781 public void onClick(View v) {
1782 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001783 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001784 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 }
1786 }
1787
1788 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789 * Bind the items start-end from the list.
1790 *
1791 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1794
1795 final Workspace workspace = mWorkspace;
1796
1797 for (int i=start; i<end; i++) {
1798 final ItemInfo item = shortcuts.get(i);
1799 mDesktopItems.add(item);
1800 switch (item.itemType) {
1801 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1802 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1803 final View shortcut = createShortcut((ApplicationInfo) item);
1804 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1805 false);
1806 break;
1807 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1808 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1809 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1810 (UserFolderInfo) item);
1811 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1812 false);
1813 break;
1814 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1815 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1816 R.layout.live_folder_icon, this,
1817 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1818 (LiveFolderInfo) item);
1819 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1820 false);
1821 break;
1822 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1823 final int screen = workspace.getCurrentScreen();
1824 final View view = mInflater.inflate(R.layout.widget_search,
1825 (ViewGroup) workspace.getChildAt(screen), false);
1826
1827 Search search = (Search) view.findViewById(R.id.widget_search);
1828 search.setLauncher(this);
1829
1830 final Widget widget = (Widget) item;
1831 view.setTag(widget);
1832
1833 workspace.addWidget(view, widget, false);
1834 break;
1835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
1837
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 }
1840
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841 /**
1842 * Implementation of the method from LauncherModel.Callbacks.
1843 */
1844 void bindFolders(HashMap<Long, FolderInfo> folders) {
1845 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001847
1848 /**
1849 * Add the views for a widget to the workspace.
1850 *
1851 * Implementation of the method from LauncherModel.Callbacks.
1852 */
1853 public void bindAppWidget(LauncherAppWidgetInfo item) {
1854 final Workspace workspace = mWorkspace;
1855
1856 final int appWidgetId = item.appWidgetId;
1857 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1858 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1859
Joe Onorato9c1289c2009-08-17 11:03:03 -04001860 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1861 item.hostView.setTag(item);
1862
1863 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1864 item.cellY, item.spanX, item.spanY, false);
1865
1866 workspace.requestLayout();
1867
1868 mDesktopItems.add(item);
1869 }
1870
1871 /**
1872 * Callback saying that there aren't any more items to bind.
1873 *
1874 * Implementation of the method from LauncherModel.Callbacks.
1875 */
1876 public void finishBindingItems() {
1877 if (mSavedState != null) {
1878 if (!mWorkspace.hasFocus()) {
1879 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1880 }
1881
1882 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1883 if (userFolders != null) {
1884 for (long folderId : userFolders) {
1885 final FolderInfo info = mFolders.get(folderId);
1886 if (info != null) {
1887 openFolder(info);
1888 }
1889 }
1890 final Folder openFolder = mWorkspace.getOpenFolder();
1891 if (openFolder != null) {
1892 openFolder.requestFocus();
1893 }
1894 }
1895
1896 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1897 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001898 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001899 }
1900
1901 mSavedState = null;
1902 }
1903
1904 if (mSavedInstanceState != null) {
1905 super.onRestoreInstanceState(mSavedInstanceState);
1906 mSavedInstanceState = null;
1907 }
1908
Joe Onorato9c1289c2009-08-17 11:03:03 -04001909 Log.d(TAG, "finishBindingItems done");
1910 mWorkspaceLoading = false;
1911 }
1912
1913 /**
1914 * Add the icons for all apps.
1915 *
1916 * Implementation of the method from LauncherModel.Callbacks.
1917 */
1918 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08001919 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001920 }
1921
1922 /**
1923 * A package was installed.
1924 *
1925 * Implementation of the method from LauncherModel.Callbacks.
1926 */
1927 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1928 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001929 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001930 }
1931
1932 /**
1933 * A package was updated.
1934 *
1935 * Implementation of the method from LauncherModel.Callbacks.
1936 */
1937 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1938 removeDialog(DIALOG_CREATE_SHORTCUT);
1939 mWorkspace.updateShortcutsForPackage(packageName);
1940 }
1941
1942 /**
1943 * A package was uninstalled.
1944 *
1945 * Implementation of the method from LauncherModel.Callbacks.
1946 */
1947 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1948 removeDialog(DIALOG_CREATE_SHORTCUT);
1949 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001950 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952}