blob: 2c59b4c8197844774a001417fcae6ee4d8637ad3 [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 Onoratoad72e172009-11-06 16:25:04 -050069import java.util.Map.Entry;
Joe Onorato9c1289c2009-08-17 11:03:03 -040070import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070071import java.io.DataOutputStream;
72import java.io.FileNotFoundException;
73import java.io.IOException;
74import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
76/**
77 * Default launcher application.
78 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040079public final class Launcher extends Activity
Joe Onorato7bb17492009-09-24 17:51:01 -070080 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040082 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 static final boolean LOGD = false;
84
Joe Onorato9c1289c2009-08-17 11:03:03 -040085 static final boolean PROFILE_STARTUP = false;
86 static final boolean PROFILE_ROTATE = false;
87 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070088
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 private static final int WALLPAPER_SCREENS_SPAN = 2;
90
91 private static final int MENU_GROUP_ADD = 1;
92 private static final int MENU_ADD = Menu.FIRST + 1;
93 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
94 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
95 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -070096 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
98 private static final int REQUEST_CREATE_SHORTCUT = 1;
99 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700100 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 private static final int REQUEST_PICK_APPLICATION = 6;
102 private static final int REQUEST_PICK_SHORTCUT = 7;
103 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700104 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700105 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106
107 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
108
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700109 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
110 static final String SEARCH_WIDGET = "search_widget";
111
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800112 static final int SCREEN_COUNT = 5;
113 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700115 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
Joe Onorato7c312c12009-08-13 21:36:53 -0700117 static final int DIALOG_CREATE_SHORTCUT = 1;
118 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Romain Guy98d01652009-06-30 16:21:04 -0700120 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
122 // Type: int
123 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
124 // Type: boolean
125 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
126 // Type: long
127 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
128 // Type: int
129 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
130 // Type: int
131 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
142 // Type: int[]
143 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
144 // Type: boolean
145 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
146 // Type: long
147 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
148
Joe Onorato9c1289c2009-08-17 11:03:03 -0400149 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private LayoutInflater mInflater;
155
Joe Onorato00acb122009-08-04 16:04:30 -0400156 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700158
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700159 private AppWidgetManager mAppWidgetManager;
160 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private CellLayout.CellInfo mAddItemCellInfo;
163 private CellLayout.CellInfo mMenuAddInfo;
164 private final int[] mCellCoordinates = new int[2];
165 private FolderInfo mFolderInfo;
166
Joe Onorato7c312c12009-08-13 21:36:53 -0700167 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700168 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700169 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private Bundle mSavedState;
172
173 private SpannableStringBuilder mDefaultKeySsb = null;
174
Mike LeBeau736cf282009-07-02 17:46:59 -0700175 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 private boolean mWorkspaceLoading = true;
178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private boolean mRestoring;
180 private boolean mWaitingForResult;
Joe Onoratoff3862d2009-10-07 10:35:51 -0700181 private boolean mExitingBecauseOfLaunch;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
183 private Bundle mSavedInstanceState;
184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 private LauncherModel mModel;
186
Romain Guy84f296c2009-11-04 15:00:44 -0800187 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
188 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700189
Joe Onorato080d9b62009-11-02 12:01:11 -0500190 public static long lastStartTime;
191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 @Override
193 protected void onCreate(Bundle savedInstanceState) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500194 lastStartTime = SystemClock.uptimeMillis();
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700197
Joe Onorato9c1289c2009-08-17 11:03:03 -0400198 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400199 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700201
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700202 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
205 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 if (PROFILE_STARTUP) {
208 android.os.Debug.startMethodTracing("/sdcard/launcher");
209 }
210
211 checkForLocaleChange();
212 setWallpaperDimension();
213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 setContentView(R.layout.launcher);
215 setupViews();
216
Joe Onorato7c312c12009-08-13 21:36:53 -0700217 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 mSavedState = savedInstanceState;
220 restoreState(mSavedState);
221
222 if (PROFILE_STARTUP) {
223 android.os.Debug.stopMethodTracing();
224 }
225
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700226 // We have a new AllAppsView, we need to re-bind everything, and it could have
227 // changed in our absence.
228 mModel.setAllAppsDirty();
229 mModel.setWorkspaceDirty();
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 }
234
235 // For handling default keys
236 mDefaultKeySsb = new SpannableStringBuilder();
237 Selection.setSelection(mDefaultKeySsb, 0);
238 }
Romain Guycbb89e42009-06-08 15:52:54 -0700239
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700241 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
242 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 final Configuration configuration = getResources().getConfiguration();
245
Romain Guy98d01652009-06-30 16:21:04 -0700246 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 final String locale = configuration.locale.toString();
248
Romain Guy98d01652009-06-30 16:21:04 -0700249 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 final int mcc = configuration.mcc;
251
Romain Guy98d01652009-06-30 16:21:04 -0700252 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 final int mnc = configuration.mnc;
254
Romain Guy84f296c2009-11-04 15:00:44 -0800255 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256
Romain Guy84f296c2009-11-04 15:00:44 -0800257 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700258 localeConfiguration.locale = locale;
259 localeConfiguration.mcc = mcc;
260 localeConfiguration.mnc = mnc;
261
262 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700264 }
265 }
266
267 private static class LocaleConfiguration {
268 public String locale;
269 public int mcc = -1;
270 public int mnc = -1;
271 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700272
Romain Guy98d01652009-06-30 16:21:04 -0700273 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
274 DataInputStream in = null;
275 try {
276 in = new DataInputStream(context.openFileInput(PREFERENCES));
277 configuration.locale = in.readUTF();
278 configuration.mcc = in.readInt();
279 configuration.mnc = in.readInt();
280 } catch (FileNotFoundException e) {
281 // Ignore
282 } catch (IOException e) {
283 // Ignore
284 } finally {
285 if (in != null) {
286 try {
287 in.close();
288 } catch (IOException e) {
289 // Ignore
290 }
291 }
292 }
293 }
294
295 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
296 DataOutputStream out = null;
297 try {
298 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
299 out.writeUTF(configuration.locale);
300 out.writeInt(configuration.mcc);
301 out.writeInt(configuration.mnc);
302 out.flush();
303 } catch (FileNotFoundException e) {
304 // Ignore
305 } catch (IOException e) {
306 //noinspection ResultOfMethodCallIgnored
307 context.getFileStreamPath(PREFERENCES).delete();
308 } finally {
309 if (out != null) {
310 try {
311 out.close();
312 } catch (IOException e) {
313 // Ignore
314 }
315 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 }
317 }
318
319 static int getScreen() {
320 synchronized (sLock) {
321 return sScreen;
322 }
323 }
324
325 static void setScreen(int screen) {
326 synchronized (sLock) {
327 sScreen = screen;
328 }
329 }
330
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700332 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333
334 Display display = getWindowManager().getDefaultDisplay();
335 boolean isPortrait = display.getWidth() < display.getHeight();
336
337 final int width = isPortrait ? display.getWidth() : display.getHeight();
338 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700339 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 }
341
342 @Override
343 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700344 mWaitingForResult = false;
345
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 // The pattern used here is that a user PICKs a specific application,
347 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700348
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
350 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700351
Romain Guy94dabf12009-07-21 10:55:43 -0700352 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 switch (requestCode) {
354 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400355 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 break;
357 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700358 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 break;
360 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400361 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 break;
363 case REQUEST_PICK_LIVE_FOLDER:
364 addLiveFolder(data);
365 break;
366 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700369 case REQUEST_PICK_APPWIDGET:
370 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700372 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700375 case REQUEST_PICK_WALLPAPER:
376 // We just wanted the activity result here so we can clear mWaitingForResult
377 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 }
Romain Guy18042c82009-11-06 11:44:55 -0800379 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
380 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
381 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 // Clean up the appWidgetId if we canceled
383 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
384 if (appWidgetId != -1) {
385 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 }
387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 }
389
390 @Override
391 protected void onResume() {
Joe Onorato080d9b62009-11-02 12:01:11 -0500392 if (lastStartTime == 0) {
393 lastStartTime = SystemClock.uptimeMillis();
394 }
395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 super.onResume();
397
398 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400399 mWorkspaceLoading = true;
400 mModel.startLoader(this, true);
401 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700403
Mike LeBeau736cf282009-07-02 17:46:59 -0700404 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
405 // onNewIntent), then close the search dialog if needed, because it probably
406 // came from the user pressing 'home' (rather than, for example, pressing 'back').
407 if (mIsNewIntent) {
408 // Post to a handler so that this happens after the search dialog tries to open
409 // itself again.
410 mWorkspace.post(new Runnable() {
411 public void run() {
412 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
413 ServiceManager.getService(Context.SEARCH_SERVICE));
414 try {
415 searchManagerService.stopSearch();
416 } catch (RemoteException e) {
417 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700418 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700419 }
420 });
421 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700422
Mike LeBeau736cf282009-07-02 17:46:59 -0700423 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425
426 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 protected void onPause() {
428 super.onPause();
Joe Onoratoff3862d2009-10-07 10:35:51 -0700429 if (mExitingBecauseOfLaunch) {
430 mExitingBecauseOfLaunch = false;
431 closeAllApps(false);
432 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 }
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700435 @Override
436 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400437 // Flag the loader to stop early before switching
438 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700439
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700440 if (PROFILE_ROTATE) {
441 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
442 }
443 return null;
444 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 private boolean acceptFilter() {
447 final InputMethodManager inputManager = (InputMethodManager)
448 getSystemService(Context.INPUT_METHOD_SERVICE);
449 return !inputManager.isFullscreenMode();
450 }
451
452 @Override
453 public boolean onKeyDown(int keyCode, KeyEvent event) {
454 boolean handled = super.onKeyDown(keyCode, event);
455 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
456 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
457 keyCode, event);
458 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700459 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700460 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700461 // showSearchDialog()
462 // If there are multiple keystrokes before the search dialog takes focus,
463 // onSearchRequested() will be called for every keystroke,
464 // but it is idempotent, so it's fine.
465 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 }
467 }
468
469 return handled;
470 }
471
Karl Rosaen138a0412009-04-23 19:00:21 -0700472 private String getTypedText() {
473 return mDefaultKeySsb.toString();
474 }
475
476 private void clearTypedText() {
477 mDefaultKeySsb.clear();
478 mDefaultKeySsb.clearSpans();
479 Selection.setSelection(mDefaultKeySsb, 0);
480 }
481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 /**
483 * Restores the previous state, if it exists.
484 *
485 * @param savedState The previous state.
486 */
487 private void restoreState(Bundle savedState) {
488 if (savedState == null) {
489 return;
490 }
491
492 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
493 if (currentScreen > -1) {
494 mWorkspace.setCurrentScreen(currentScreen);
495 }
496
497 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
498 if (addScreen > -1) {
499 mAddItemCellInfo = new CellLayout.CellInfo();
500 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
501 addItemCellInfo.valid = true;
502 addItemCellInfo.screen = addScreen;
503 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
504 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
505 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
506 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
507 addItemCellInfo.findVacantCellsFromOccupied(
508 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
509 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
510 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
511 mRestoring = true;
512 }
513
514 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
515 if (renameFolder) {
516 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400517 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 mRestoring = true;
519 }
520 }
521
522 /**
523 * Finds all the views we need and configure them properly.
524 */
525 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400526 DragController dragController = mDragController;
527
Romain Guyb1b69f52009-08-10 15:10:15 -0700528 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400529 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530
Joe Onorato7c312c12009-08-13 21:36:53 -0700531 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700532 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700533 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700534 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700535 // Manage focusability manually since this thing is always visible
536 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700537
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
539 final Workspace workspace = mWorkspace;
540
Joe Onorato7c312c12009-08-13 21:36:53 -0700541 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
542 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543
Joe Onorato7c312c12009-08-13 21:36:53 -0700544 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700545 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700546 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700547 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700548 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700549 handleIocon.setCrossFadeEnabled(true);
550 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400553 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555
556 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400557 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700558 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559
Joe Onorato00acb122009-08-04 16:04:30 -0400560 dragController.setDragScoller(workspace);
561 dragController.setDragListener(deleteZone);
562 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700563
Joe Onorato00acb122009-08-04 16:04:30 -0400564 // The order here is bottom to top.
565 dragController.addDropTarget(workspace);
566 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 }
568
569 /**
570 * Creates a view representing a shortcut.
571 *
572 * @param info The data structure describing the shortcut.
573 *
574 * @return A View inflated from R.layout.application.
575 */
576 View createShortcut(ApplicationInfo info) {
577 return createShortcut(R.layout.application,
578 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
579 }
580
581 /**
582 * Creates a view representing a shortcut inflated from the specified resource.
583 *
584 * @param layoutResId The id of the XML layout used to create the shortcut.
585 * @param parent The group the shortcut belongs to.
586 * @param info The data structure describing the shortcut.
587 *
588 * @return A View inflated from layoutResId.
589 */
590 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
591 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
592
Joe Onorato5162ea92009-09-03 09:39:42 -0700593 if (info.icon == null) {
594 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
595 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700597 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 info.filtered = true;
599 }
600
601 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
602 favorite.setText(info.title);
603 favorite.setTag(info);
604 favorite.setOnClickListener(this);
605
606 return favorite;
607 }
608
609 /**
610 * Add an application shortcut to the workspace.
611 *
612 * @param data The intent describing the application.
613 * @param cellInfo The position on screen where to create the shortcut.
614 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400615 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 cellInfo.screen = mWorkspace.getCurrentScreen();
617 if (!findSingleSlot(cellInfo)) return;
618
Romain Guy73b979d2009-06-09 12:57:21 -0700619 final ApplicationInfo info = infoFromApplicationIntent(context, data);
620 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700622 }
623 }
624
625 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 ComponentName component = data.getComponent();
627 PackageManager packageManager = context.getPackageManager();
628 ActivityInfo activityInfo = null;
629 try {
630 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
631 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700632 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 }
Romain Guycbb89e42009-06-08 15:52:54 -0700634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 if (activityInfo != null) {
636 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700637
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 itemInfo.title = activityInfo.loadLabel(packageManager);
639 if (itemInfo.title == null) {
640 itemInfo.title = activityInfo.name;
641 }
Romain Guycbb89e42009-06-08 15:52:54 -0700642
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
644 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
645 itemInfo.icon = activityInfo.loadIcon(packageManager);
646 itemInfo.container = ItemInfo.NO_ID;
647
Romain Guy73b979d2009-06-09 12:57:21 -0700648 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
Romain Guy73b979d2009-06-09 12:57:21 -0700650
651 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
Romain Guycbb89e42009-06-08 15:52:54 -0700653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 /**
655 * Add a shortcut to the workspace.
656 *
657 * @param data The intent describing the shortcut.
658 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 cellInfo.screen = mWorkspace.getCurrentScreen();
662 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
665
666 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400668 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
669 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
671 }
672
Romain Guycbb89e42009-06-08 15:52:54 -0700673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700675 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677 * @param data The intent describing the appWidgetId.
678 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400680 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700682 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700683
Romain Guy18042c82009-11-06 11:44:55 -0800684 if (LOGD) d(LOG_TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700685
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700687
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700690 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700691
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 // Try finding open space on Launcher screen
693 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800694 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
695 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
696 return;
697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700699 // Build Launcher-specific widget info and save to database
700 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 launcherInfo.spanX = spans[0];
702 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 LauncherModel.addItemToDatabase(this, launcherInfo,
705 LauncherSettings.Favorites.CONTAINER_DESKTOP,
706 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
707
708 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400709 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700714 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700716
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 }
720 }
Romain Guycbb89e42009-06-08 15:52:54 -0700721
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
723 mDesktopItems.remove(launcherInfo);
724 launcherInfo.hostView = null;
725 }
726
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700727 public LauncherAppWidgetHost getAppWidgetHost() {
728 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 }
Romain Guycbb89e42009-06-08 15:52:54 -0700730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 static ApplicationInfo addShortcut(Context context, Intent data,
732 CellLayout.CellInfo cellInfo, boolean notify) {
733
Romain Guy73b979d2009-06-09 12:57:21 -0700734 final ApplicationInfo info = infoFromShortcutIntent(context, data);
735 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
736 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
737
738 return info;
739 }
740
741 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
743 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
744 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
745
746 Drawable icon = null;
747 boolean filtered = false;
748 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700749 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750
751 if (bitmap != null) {
752 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
753 filtered = true;
754 customIcon = true;
755 } else {
756 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700757 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700759 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 final PackageManager packageManager = context.getPackageManager();
761 Resources resources = packageManager.getResourcesForApplication(
762 iconResource.packageName);
763 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
764 icon = resources.getDrawable(id);
765 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700766 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
768 }
769 }
770
771 if (icon == null) {
772 icon = context.getPackageManager().getDefaultActivityIcon();
773 }
774
775 final ApplicationInfo info = new ApplicationInfo();
776 info.icon = icon;
777 info.filtered = filtered;
778 info.title = name;
779 info.intent = intent;
780 info.customIcon = customIcon;
781 info.iconResource = iconResource;
782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 return info;
784 }
785
786 @Override
787 protected void onNewIntent(Intent intent) {
788 super.onNewIntent(intent);
789
790 // Close the menu
791 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
792 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700793
Joe Onorato742966b2009-10-07 10:26:17 -0700794 // Whatever we were doing is hereby canceled.
795 mWaitingForResult = false;
796
Mike LeBeau736cf282009-07-02 17:46:59 -0700797 // Set this flag so that onResume knows to close the search dialog if it's open,
798 // because this was a new intent (thus a press of 'home' or some such) rather than
799 // for example onResume being called when the user pressed the 'back' button.
800 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801
802 try {
803 dismissDialog(DIALOG_CREATE_SHORTCUT);
804 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 } catch (Exception e) {
806 // An exception is thrown if the dialog is not visible, which is fine
807 }
808
809 try {
810 dismissDialog(DIALOG_RENAME_FOLDER);
811 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 } catch (Exception e) {
813 // An exception is thrown if the dialog is not visible, which is fine
814 }
815
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
817 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700818
Romain Guy94dabf12009-07-21 10:55:43 -0700819 if (!mWorkspace.isDefaultScreenShowing()) {
820 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700822
Joe Onorato7bb17492009-09-24 17:51:01 -0700823 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700824
825 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 if (v != null && v.getWindowToken() != null) {
827 InputMethodManager imm = (InputMethodManager)getSystemService(
828 INPUT_METHOD_SERVICE);
829 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
830 }
831 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700832 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
834 }
835 }
836
837 @Override
838 protected void onRestoreInstanceState(Bundle savedInstanceState) {
839 // Do not call super here
840 mSavedInstanceState = savedInstanceState;
841 }
842
843 @Override
844 protected void onSaveInstanceState(Bundle outState) {
845 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
846
847 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
848 if (folders.size() > 0) {
849 final int count = folders.size();
850 long[] ids = new long[count];
851 for (int i = 0; i < count; i++) {
852 final FolderInfo info = folders.get(i).getInfo();
853 ids[i] = info.id;
854 }
855 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
856 } else {
857 super.onSaveInstanceState(outState);
858 }
859
Romain Guy3cf604f2009-06-16 13:12:53 -0700860 final boolean isConfigurationChange = getChangingConfigurations() != 0;
861
Romain Guy5a941392009-04-28 15:18:25 -0700862 // When the drawer is opened and we are saving the state because of a
863 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400864 // TODO should not do this if the drawer is currently closing.
865 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700867 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868
869 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
870 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
871 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
872
873 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
874 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
875 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
876 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
877 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
878 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
879 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
880 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
881 layout.getOccupiedCells());
882 }
883
884 if (mFolderInfo != null && mWaitingForResult) {
885 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
886 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
887 }
888 }
889
890 @Override
891 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700893
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700895 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700897 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
899
900 TextKeyListener.getInstance().release();
901
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904 unbindDesktopItems();
905 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 }
907
908 @Override
909 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700910 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 super.startActivityForResult(intent, requestCode);
912 }
913
914 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700915 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700917
Joe Onorato7bb17492009-09-24 17:51:01 -0700918 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700919
Karl Rosaen138a0412009-04-23 19:00:21 -0700920 // Slide the search widget to the top, if it's on the current screen,
921 // otherwise show the search dialog immediately.
922 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
923 if (searchWidget == null) {
924 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
925 } else {
926 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
927 // show the currently typed text in the search widget while sliding
928 searchWidget.setQuery(getTypedText());
929 }
930 }
Romain Guycbb89e42009-06-08 15:52:54 -0700931
Karl Rosaen138a0412009-04-23 19:00:21 -0700932 /**
933 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700934 *
935 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700936 */
Romain Guycbb89e42009-06-08 15:52:54 -0700937 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700938 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700939
Karl Rosaen138a0412009-04-23 19:00:21 -0700940 if (initialQuery == null) {
941 // Use any text typed in the launcher as the initial query
942 initialQuery = getTypedText();
943 clearTypedText();
944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 if (appSearchData == null) {
946 appSearchData = new Bundle();
947 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
948 }
Romain Guycbb89e42009-06-08 15:52:54 -0700949
Karl Rosaen138a0412009-04-23 19:00:21 -0700950 final SearchManager searchManager =
951 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
952
953 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
954 if (searchWidget != null) {
955 // This gets called when the user leaves the search dialog to go back to
956 // the Launcher.
957 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
958 public void onCancel() {
959 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700960 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700961 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 });
963 }
Romain Guycbb89e42009-06-08 15:52:54 -0700964
Karl Rosaen138a0412009-04-23 19:00:21 -0700965 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700966 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 /**
970 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 */
Romain Guy5a941392009-04-28 15:18:25 -0700972 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700973 // Close search dialog
974 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100975 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700976 // Restore search widget to its normal position
977 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
978 if (searchWidget != null) {
979 searchWidget.stopSearch(false);
980 }
981 }
Romain Guycbb89e42009-06-08 15:52:54 -0700982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 @Override
984 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 if (isWorkspaceLocked()) {
986 return false;
987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988
989 super.onCreateOptionsMenu(menu);
990 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
991 .setIcon(android.R.drawable.ic_menu_add)
992 .setAlphabeticShortcut('A');
993 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
994 .setIcon(android.R.drawable.ic_menu_gallery)
995 .setAlphabeticShortcut('W');
996 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
997 .setIcon(android.R.drawable.ic_search_category_default)
998 .setAlphabeticShortcut(SearchManager.MENU_KEY);
999 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1000 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1001 .setAlphabeticShortcut('N');
1002
1003 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001004 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1005 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006
1007 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1008 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1009 .setIntent(settings);
1010
1011 return true;
1012 }
1013
1014 @Override
1015 public boolean onPrepareOptionsMenu(Menu menu) {
1016 super.onPrepareOptionsMenu(menu);
1017
1018 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1019 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1020
1021 return true;
1022 }
1023
1024 @Override
1025 public boolean onOptionsItemSelected(MenuItem item) {
1026 switch (item.getItemId()) {
1027 case MENU_ADD:
1028 addItems();
1029 return true;
1030 case MENU_WALLPAPER_SETTINGS:
1031 startWallpaper();
1032 return true;
1033 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001034 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 return true;
1036 case MENU_NOTIFICATIONS:
1037 showNotifications();
1038 return true;
1039 }
1040
1041 return super.onOptionsItemSelected(item);
1042 }
Romain Guycbb89e42009-06-08 15:52:54 -07001043
Karl Rosaen138a0412009-04-23 19:00:21 -07001044 /**
1045 * Indicates that we want global search for this activity by setting the globalSearch
1046 * argument for {@link #startSearch} to true.
1047 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001049 @Override
1050 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001051 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001052 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001053 }
1054
Joe Onorato9c1289c2009-08-17 11:03:03 -04001055 public boolean isWorkspaceLocked() {
1056 return mWorkspaceLoading || mWaitingForResult;
1057 }
1058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001060 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 showAddDialog(mMenuAddInfo);
1062 }
1063
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001064 void addAppWidget(Intent data) {
1065 // TODO: catch bad widget exception when sent
1066 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001068 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1069 if (SEARCH_WIDGET.equals(customWidget)) {
1070 // We don't need this any more, since this isn't a real app widget.
1071 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1072 // add the search widget
1073 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001075 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1076
1077 if (appWidget.configure != null) {
1078 // Launch over to configure widget, if needed
1079 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1080 intent.setComponent(appWidget.configure);
1081 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1082
1083 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1084 } else {
1085 // Otherwise just add it
1086 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1087 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 }
1089 }
Romain Guycbb89e42009-06-08 15:52:54 -07001090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 void addSearch() {
1092 final Widget info = Widget.makeSearch();
1093 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 final int[] xy = mCellCoordinates;
1096 final int spanX = info.spanX;
1097 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1102 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 final View view = mInflater.inflate(info.layoutResource, null);
1105 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001106 Search search = (Search) view.findViewById(R.id.widget_search);
1107 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1110 }
1111
Romain Guy73b979d2009-06-09 12:57:21 -07001112 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001113 // Handle case where user selected "Applications"
1114 String applicationName = getResources().getString(R.string.group_applications);
1115 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001116
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001117 if (applicationName != null && applicationName.equals(shortcutName)) {
1118 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1119 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001120
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001121 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1122 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001123 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001124 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001125 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001126 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 }
1128
1129 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001130 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001131 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001132 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001133
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001134 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001135 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001136 } else {
1137 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1138 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 }
1140
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 UserFolderInfo folderInfo = new UserFolderInfo();
1143 folderInfo.title = getText(R.string.folder_name);
1144
1145 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1146 cellInfo.screen = mWorkspace.getCurrentScreen();
1147 if (!findSingleSlot(cellInfo)) return;
1148
1149 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001150 LauncherModel.addItemToDatabase(this, folderInfo,
1151 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001153 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154
1155 // Create the view
1156 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1157 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1158 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001159 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 }
Romain Guycbb89e42009-06-08 15:52:54 -07001161
Joe Onorato9c1289c2009-08-17 11:03:03 -04001162 void removeFolder(FolderInfo folder) {
1163 mFolders.remove(folder.id);
1164 }
1165
1166 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 cellInfo.screen = mWorkspace.getCurrentScreen();
1168 if (!findSingleSlot(cellInfo)) return;
1169
1170 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1171
1172 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1174 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1176 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 }
1178 }
1179
1180 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1181 CellLayout.CellInfo cellInfo, boolean notify) {
1182
1183 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1184 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1185
1186 Drawable icon = null;
1187 boolean filtered = false;
1188 Intent.ShortcutIconResource iconResource = null;
1189
1190 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1191 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1192 try {
1193 iconResource = (Intent.ShortcutIconResource) extra;
1194 final PackageManager packageManager = context.getPackageManager();
1195 Resources resources = packageManager.getResourcesForApplication(
1196 iconResource.packageName);
1197 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1198 icon = resources.getDrawable(id);
1199 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001200 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 }
1202 }
1203
1204 if (icon == null) {
1205 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1206 }
1207
1208 final LiveFolderInfo info = new LiveFolderInfo();
1209 info.icon = icon;
1210 info.filtered = filtered;
1211 info.title = name;
1212 info.iconResource = iconResource;
1213 info.uri = data.getData();
1214 info.baseIntent = baseIntent;
1215 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1216 LiveFolders.DISPLAY_MODE_GRID);
1217
1218 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1219 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001220 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221
1222 return info;
1223 }
1224
1225 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1226 final int[] xy = new int[2];
1227 if (findSlot(cellInfo, xy, 1, 1)) {
1228 cellInfo.cellX = xy[0];
1229 cellInfo.cellY = xy[1];
1230 return true;
1231 }
1232 return false;
1233 }
1234
1235 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1236 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1237 boolean[] occupied = mSavedState != null ?
1238 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1239 cellInfo = mWorkspace.findAllVacantCells(occupied);
1240 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1241 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1242 return false;
1243 }
1244 }
1245 return true;
1246 }
1247
1248 private void showNotifications() {
1249 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1250 if (statusBar != null) {
1251 statusBar.expand();
1252 }
1253 }
1254
1255 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001256 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001258 Intent chooser = Intent.createChooser(pickWallpaper,
1259 getText(R.string.chooser_wallpaper));
1260 WallpaperManager wm = (WallpaperManager)
1261 getSystemService(Context.WALLPAPER_SERVICE);
1262 WallpaperInfo wi = wm.getWallpaperInfo();
1263 if (wi != null && wi.getSettingsActivity() != null) {
1264 LabeledIntent li = new LabeledIntent(getPackageName(),
1265 R.string.configure_wallpaper, 0);
1266 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1267 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1268 }
Mike Clerona0618e42009-10-22 13:55:21 -07001269 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 }
1271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 @Override
1273 public boolean dispatchKeyEvent(KeyEvent event) {
1274 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1275 switch (event.getKeyCode()) {
1276 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 return true;
1278 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001279 return true;
1280 }
1281 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1282 switch (event.getKeyCode()) {
1283 case KeyEvent.KEYCODE_BACK:
1284 if (!event.isCanceled()) {
1285 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001286 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001287 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001288 } else {
1289 closeFolder();
1290 }
1291 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001292 return true;
1293 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 return true;
1295 }
1296 }
1297
1298 return super.dispatchKeyEvent(event);
1299 }
1300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 private void closeFolder() {
1302 Folder folder = mWorkspace.getOpenFolder();
1303 if (folder != null) {
1304 closeFolder(folder);
1305 }
1306 }
1307
1308 void closeFolder(Folder folder) {
1309 folder.getInfo().opened = false;
1310 ViewGroup parent = (ViewGroup) folder.getParent();
1311 if (parent != null) {
1312 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001313 if (folder instanceof DropTarget) {
1314 // Live folders aren't DropTargets.
1315 mDragController.removeDropTarget((DropTarget)folder);
1316 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
1318 folder.onClose();
1319 }
1320
1321 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001322 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1323 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325 private void unbindDesktopItems() {
1326 for (ItemInfo item: mDesktopItems) {
1327 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 }
1329 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001330
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 /**
1332 * Launches the intent referred by the clicked shortcut.
1333 *
1334 * @param v The view representing the clicked shortcut.
1335 */
1336 public void onClick(View v) {
1337 Object tag = v.getTag();
1338 if (tag instanceof ApplicationInfo) {
1339 // Open shortcut
1340 final Intent intent = ((ApplicationInfo) tag).intent;
1341 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001342 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 } else if (tag instanceof FolderInfo) {
1344 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001345 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001346 Log.d(TAG, "onClick");
1347 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001348 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001349 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001350 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001351 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 }
1353 }
1354
1355 void startActivitySafely(Intent intent) {
1356 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1357 try {
1358 startActivity(intent);
1359 } catch (ActivityNotFoundException e) {
1360 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1361 } catch (SecurityException e) {
1362 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001363 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1365 "or use the exported attribute for this activity.", e);
1366 }
1367 }
1368
1369 private void handleFolderClick(FolderInfo folderInfo) {
1370 if (!folderInfo.opened) {
1371 // Close any open folder
1372 closeFolder();
1373 // Open the requested folder
1374 openFolder(folderInfo);
1375 } else {
1376 // Find the open folder...
1377 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1378 int folderScreen;
1379 if (openFolder != null) {
1380 folderScreen = mWorkspace.getScreenForView(openFolder);
1381 // .. and close it
1382 closeFolder(openFolder);
1383 if (folderScreen != mWorkspace.getCurrentScreen()) {
1384 // Close any folder open on the current screen
1385 closeFolder();
1386 // Pull the folder onto this screen
1387 openFolder(folderInfo);
1388 }
1389 }
1390 }
1391 }
1392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 /**
1394 * Opens the user fodler described by the specified tag. The opening of the folder
1395 * is animated relative to the specified View. If the View is null, no animation
1396 * is played.
1397 *
1398 * @param folderInfo The FolderInfo describing the folder to open.
1399 */
1400 private void openFolder(FolderInfo folderInfo) {
1401 Folder openFolder;
1402
1403 if (folderInfo instanceof UserFolderInfo) {
1404 openFolder = UserFolder.fromXml(this);
1405 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001406 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 } else {
1408 return;
1409 }
1410
Joe Onorato00acb122009-08-04 16:04:30 -04001411 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 openFolder.setLauncher(this);
1413
1414 openFolder.bind(folderInfo);
1415 folderInfo.opened = true;
1416
1417 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1418 openFolder.onOpen();
1419 }
1420
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001422 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 return false;
1424 }
1425
1426 if (!(v instanceof CellLayout)) {
1427 v = (View) v.getParent();
1428 }
1429
1430 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1431
1432 // This happens when long clicking an item with the dpad/trackball
1433 if (cellInfo == null) {
1434 return true;
1435 }
1436
1437 if (mWorkspace.allowLongPress()) {
1438 if (cellInfo.cell == null) {
1439 if (cellInfo.valid) {
1440 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001441 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 showAddDialog(cellInfo);
1443 }
1444 } else {
1445 if (!(cellInfo.cell instanceof Folder)) {
1446 // User long pressed on an item
1447 mWorkspace.startDrag(cellInfo);
1448 }
1449 }
1450 }
1451 return true;
1452 }
1453
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001454 View getDrawerHandle() {
1455 return mHandleView;
1456 }
1457
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 Workspace getWorkspace() {
1459 return mWorkspace;
1460 }
1461
Joe Onorato7404ee42009-07-31 11:54:44 -07001462 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 GridView getApplicationsGrid() {
1464 return mAllAppsGrid;
1465 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001466 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467
1468 @Override
1469 protected Dialog onCreateDialog(int id) {
1470 switch (id) {
1471 case DIALOG_CREATE_SHORTCUT:
1472 return new CreateShortcut().createDialog();
1473 case DIALOG_RENAME_FOLDER:
1474 return new RenameFolder().createDialog();
1475 }
1476
1477 return super.onCreateDialog(id);
1478 }
1479
1480 @Override
1481 protected void onPrepareDialog(int id, Dialog dialog) {
1482 switch (id) {
1483 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 break;
1485 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001486 if (mFolderInfo != null) {
1487 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1488 final CharSequence text = mFolderInfo.title;
1489 input.setText(text);
1490 input.setSelection(0, text.length());
1491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 break;
1493 }
1494 }
1495
1496 void showRenameDialog(FolderInfo info) {
1497 mFolderInfo = info;
1498 mWaitingForResult = true;
1499 showDialog(DIALOG_RENAME_FOLDER);
1500 }
1501
1502 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1503 mAddItemCellInfo = cellInfo;
1504 mWaitingForResult = true;
1505 showDialog(DIALOG_CREATE_SHORTCUT);
1506 }
1507
Romain Guy73b979d2009-06-09 12:57:21 -07001508 private void pickShortcut(int requestCode, int title) {
1509 Bundle bundle = new Bundle();
1510
1511 ArrayList<String> shortcutNames = new ArrayList<String>();
1512 shortcutNames.add(getString(R.string.group_applications));
1513 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1514
1515 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1516 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1517 R.drawable.ic_launcher_application));
1518 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1519
1520 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1521 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1522 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1523 pickIntent.putExtras(bundle);
1524
1525 startActivityForResult(pickIntent, requestCode);
1526 }
1527
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 private class RenameFolder {
1529 private EditText mInput;
1530
1531 Dialog createDialog() {
1532 mWaitingForResult = true;
1533 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1534 mInput = (EditText) layout.findViewById(R.id.folder_name);
1535
1536 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1537 builder.setIcon(0);
1538 builder.setTitle(getString(R.string.rename_folder_title));
1539 builder.setCancelable(true);
1540 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1541 public void onCancel(DialogInterface dialog) {
1542 cleanup();
1543 }
1544 });
1545 builder.setNegativeButton(getString(R.string.cancel_action),
1546 new Dialog.OnClickListener() {
1547 public void onClick(DialogInterface dialog, int which) {
1548 cleanup();
1549 }
1550 }
1551 );
1552 builder.setPositiveButton(getString(R.string.rename_action),
1553 new Dialog.OnClickListener() {
1554 public void onClick(DialogInterface dialog, int which) {
1555 changeFolderName();
1556 }
1557 }
1558 );
1559 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001560
1561 final AlertDialog dialog = builder.create();
1562 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1563 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001564 mInput.requestFocus();
1565 InputMethodManager inputManager = (InputMethodManager)
1566 getSystemService(Context.INPUT_METHOD_SERVICE);
1567 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001568 }
1569 });
1570
1571 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
1573
1574 private void changeFolderName() {
1575 final String name = mInput.getText().toString();
1576 if (!TextUtils.isEmpty(name)) {
1577 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001578 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 mFolderInfo.title = name;
1580 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1581
Joe Onorato9c1289c2009-08-17 11:03:03 -04001582 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001583 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001584 mModel.setWorkspaceDirty();
1585 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 } else {
1587 final FolderIcon folderIcon = (FolderIcon)
1588 mWorkspace.getViewForTag(mFolderInfo);
1589 if (folderIcon != null) {
1590 folderIcon.setText(name);
1591 getWorkspace().requestLayout();
1592 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001593 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001594 mModel.setWorkspaceDirty();
1595 mWorkspaceLoading = true;
1596 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 }
1598 }
1599 }
1600 cleanup();
1601 }
1602
1603 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 dismissDialog(DIALOG_RENAME_FOLDER);
1605 mWaitingForResult = false;
1606 mFolderInfo = null;
1607 }
1608 }
1609
Joe Onoratofb0ca672009-09-14 17:55:46 -04001610 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001611 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001612 }
1613
Jason Samsfd22dac2009-09-20 17:24:16 -07001614 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001615 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001616 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001617
Mike Cleronb6082fa2009-10-19 17:03:36 -07001618 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001619 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001620
Joe Onorato7c312c12009-08-13 21:36:53 -07001621 // TODO: fade these two too
1622 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001623 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001624 }
1625
Joe Onorato7bb17492009-09-24 17:51:01 -07001626 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001627 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001628 mAllAppsGrid.zoom(0.0f);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001629 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001630 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001631
Mike Cleronb6082fa2009-10-19 17:03:36 -07001632
Joe Onorato7c312c12009-08-13 21:36:53 -07001633 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001634 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001635 mDeleteZone.setVisibility(View.VISIBLE);
1636 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001637 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001638 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001639 }
1640
Joe Onorato7c312c12009-08-13 21:36:53 -07001641 void lockAllApps() {
1642 // TODO
1643 }
1644
1645 void unlockAllApps() {
1646 // TODO
1647 }
1648
Joe Onorato7404ee42009-07-31 11:54:44 -07001649 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 * Displays the shortcut creation dialog and launches, if necessary, the
1651 * appropriate activity.
1652 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001653 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001654 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1655 DialogInterface.OnShowListener {
1656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 Dialog createDialog() {
1660 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1665 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001666 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001667
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 builder.setInverseBackgroundForced(true);
1669
1670 AlertDialog dialog = builder.create();
1671 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001672 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001673 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 return dialog;
1676 }
1677
1678 public void onCancel(DialogInterface dialog) {
1679 mWaitingForResult = false;
1680 cleanup();
1681 }
1682
Romain Guycbb89e42009-06-08 15:52:54 -07001683 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001684 }
1685
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 dismissDialog(DIALOG_CREATE_SHORTCUT);
1688 }
1689
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001690 /**
1691 * Handle the action clicked in the "Add to home" dialog.
1692 */
1693 public void onClick(DialogInterface dialog, int which) {
1694 Resources res = getResources();
1695 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001696
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001697 switch (which) {
1698 case AddAdapter.ITEM_SHORTCUT: {
1699 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001700 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001701 break;
1702 }
Romain Guycbb89e42009-06-08 15:52:54 -07001703
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001704 case AddAdapter.ITEM_APPWIDGET: {
1705 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001706
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001707 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1708 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1709 // add the search widget
1710 ArrayList<AppWidgetProviderInfo> customInfo =
1711 new ArrayList<AppWidgetProviderInfo>();
1712 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1713 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1714 info.label = getString(R.string.group_search);
1715 info.icon = R.drawable.ic_search_widget;
1716 customInfo.add(info);
1717 pickIntent.putParcelableArrayListExtra(
1718 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1719 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1720 Bundle b = new Bundle();
1721 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1722 customExtras.add(b);
1723 pickIntent.putParcelableArrayListExtra(
1724 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1725 // start the pick activity
1726 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1727 break;
1728 }
Romain Guycbb89e42009-06-08 15:52:54 -07001729
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001730 case AddAdapter.ITEM_LIVE_FOLDER: {
1731 // Insert extra item to handle inserting folder
1732 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001733
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001734 ArrayList<String> shortcutNames = new ArrayList<String>();
1735 shortcutNames.add(res.getString(R.string.group_folder));
1736 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001737
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001738 ArrayList<ShortcutIconResource> shortcutIcons =
1739 new ArrayList<ShortcutIconResource>();
1740 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1741 R.drawable.ic_launcher_folder));
1742 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1743
1744 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1745 pickIntent.putExtra(Intent.EXTRA_INTENT,
1746 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1747 pickIntent.putExtra(Intent.EXTRA_TITLE,
1748 getText(R.string.title_select_live_folder));
1749 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001750
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001751 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1752 break;
1753 }
1754
1755 case AddAdapter.ITEM_WALLPAPER: {
1756 startWallpaper();
1757 break;
1758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 }
1760 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001761
1762 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001763 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 }
1765
1766 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001767 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001769 public int getCurrentWorkspaceScreen() {
1770 return mWorkspace.getCurrentScreen();
1771 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001772
Joe Onorato9c1289c2009-08-17 11:03:03 -04001773 /**
1774 * Refreshes the shortcuts shown on the workspace.
1775 *
1776 * Implementation of the method from LauncherModel.Callbacks.
1777 */
1778 public void startBinding() {
1779 final Workspace workspace = mWorkspace;
1780 int count = workspace.getChildCount();
1781 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001782 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001783 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1784 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001785
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786 if (DEBUG_USER_INTERFACE) {
1787 android.widget.Button finishButton = new android.widget.Button(this);
1788 finishButton.setText("Finish");
1789 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1790
1791 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1792 public void onClick(View v) {
1793 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001795 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797 }
1798
1799 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001800 * Bind the items start-end from the list.
1801 *
1802 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1805
1806 final Workspace workspace = mWorkspace;
1807
1808 for (int i=start; i<end; i++) {
1809 final ItemInfo item = shortcuts.get(i);
1810 mDesktopItems.add(item);
1811 switch (item.itemType) {
1812 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1813 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1814 final View shortcut = createShortcut((ApplicationInfo) item);
1815 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1816 false);
1817 break;
1818 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1819 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1820 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1821 (UserFolderInfo) item);
1822 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1823 false);
1824 break;
1825 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1826 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1827 R.layout.live_folder_icon, this,
1828 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1829 (LiveFolderInfo) item);
1830 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1831 false);
1832 break;
1833 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1834 final int screen = workspace.getCurrentScreen();
1835 final View view = mInflater.inflate(R.layout.widget_search,
1836 (ViewGroup) workspace.getChildAt(screen), false);
1837
1838 Search search = (Search) view.findViewById(R.id.widget_search);
1839 search.setLauncher(this);
1840
1841 final Widget widget = (Widget) item;
1842 view.setTag(widget);
1843
1844 workspace.addWidget(view, widget, false);
1845 break;
1846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 }
1848
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 }
1851
Joe Onorato9c1289c2009-08-17 11:03:03 -04001852 /**
1853 * Implementation of the method from LauncherModel.Callbacks.
1854 */
Joe Onoratoad72e172009-11-06 16:25:04 -05001855 public void bindFolders(HashMap<Long, FolderInfo> folders) {
1856 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001857 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859
1860 /**
1861 * Add the views for a widget to the workspace.
1862 *
1863 * Implementation of the method from LauncherModel.Callbacks.
1864 */
1865 public void bindAppWidget(LauncherAppWidgetInfo item) {
1866 final Workspace workspace = mWorkspace;
1867
1868 final int appWidgetId = item.appWidgetId;
1869 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1870 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1871
Joe Onorato9c1289c2009-08-17 11:03:03 -04001872 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1873 item.hostView.setTag(item);
1874
1875 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1876 item.cellY, item.spanX, item.spanY, false);
1877
1878 workspace.requestLayout();
1879
1880 mDesktopItems.add(item);
1881 }
1882
1883 /**
1884 * Callback saying that there aren't any more items to bind.
1885 *
1886 * Implementation of the method from LauncherModel.Callbacks.
1887 */
1888 public void finishBindingItems() {
1889 if (mSavedState != null) {
1890 if (!mWorkspace.hasFocus()) {
1891 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1892 }
1893
1894 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1895 if (userFolders != null) {
1896 for (long folderId : userFolders) {
1897 final FolderInfo info = mFolders.get(folderId);
1898 if (info != null) {
1899 openFolder(info);
1900 }
1901 }
1902 final Folder openFolder = mWorkspace.getOpenFolder();
1903 if (openFolder != null) {
1904 openFolder.requestFocus();
1905 }
1906 }
1907
1908 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1909 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001910 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001911 }
1912
1913 mSavedState = null;
1914 }
1915
1916 if (mSavedInstanceState != null) {
1917 super.onRestoreInstanceState(mSavedInstanceState);
1918 mSavedInstanceState = null;
1919 }
1920
Joe Onorato9c1289c2009-08-17 11:03:03 -04001921 Log.d(TAG, "finishBindingItems done");
1922 mWorkspaceLoading = false;
1923 }
1924
1925 /**
1926 * Add the icons for all apps.
1927 *
1928 * Implementation of the method from LauncherModel.Callbacks.
1929 */
1930 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08001931 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001932 }
1933
1934 /**
1935 * A package was installed.
1936 *
1937 * Implementation of the method from LauncherModel.Callbacks.
1938 */
1939 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1940 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001941 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001942 }
1943
1944 /**
1945 * A package was updated.
1946 *
1947 * Implementation of the method from LauncherModel.Callbacks.
1948 */
1949 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1950 removeDialog(DIALOG_CREATE_SHORTCUT);
1951 mWorkspace.updateShortcutsForPackage(packageName);
1952 }
1953
1954 /**
1955 * A package was uninstalled.
1956 *
1957 * Implementation of the method from LauncherModel.Callbacks.
1958 */
1959 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1960 removeDialog(DIALOG_CREATE_SHORTCUT);
1961 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001962 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964}