blob: be1ee67bc494ab72903236be7425b8b6a565c835 [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;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080029import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070033import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ActivityInfo;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070035import android.content.pm.LabeledIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070039import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080044import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Parcelable;
46import android.os.RemoteException;
47import android.os.ServiceManager;
Joe Onorato080d9b62009-11-02 12:01:11 -050048import android.os.SystemClock;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.text.Selection;
51import android.text.SpannableStringBuilder;
52import android.text.TextUtils;
53import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070054import static android.util.Log.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070055import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.KeyEvent;
58import android.view.LayoutInflater;
59import android.view.Menu;
60import android.view.MenuItem;
61import android.view.View;
62import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.View.OnLongClickListener;
64import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.widget.TextView;
67import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070068import android.appwidget.AppWidgetManager;
69import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import java.util.ArrayList;
Joe Onoratoad72e172009-11-06 16:25:04 -050072import java.util.Map.Entry;
Joe Onorato9c1289c2009-08-17 11:03:03 -040073import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070074import java.io.DataOutputStream;
75import java.io.FileNotFoundException;
76import java.io.IOException;
77import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
79/**
80 * Default launcher application.
81 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040082public final class Launcher extends Activity
Joe Onorato7bb17492009-09-24 17:51:01 -070083 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040085 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 static final boolean LOGD = false;
87
Joe Onorato9c1289c2009-08-17 11:03:03 -040088 static final boolean PROFILE_STARTUP = false;
89 static final boolean PROFILE_ROTATE = false;
90 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070091
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 private static final int WALLPAPER_SCREENS_SPAN = 2;
93
94 private static final int MENU_GROUP_ADD = 1;
95 private static final int MENU_ADD = Menu.FIRST + 1;
96 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
97 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
98 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -070099 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100
101 private static final int REQUEST_CREATE_SHORTCUT = 1;
102 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700103 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 private static final int REQUEST_PICK_APPLICATION = 6;
105 private static final int REQUEST_PICK_SHORTCUT = 7;
106 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700107 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700108 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
110 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
111
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700112 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
113 static final String SEARCH_WIDGET = "search_widget";
114
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800115 static final int SCREEN_COUNT = 5;
116 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700118 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato7c312c12009-08-13 21:36:53 -0700120 static final int DIALOG_CREATE_SHORTCUT = 1;
121 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Romain Guy98d01652009-06-30 16:21:04 -0700123 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
125 // Type: int
126 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
127 // Type: boolean
128 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
129 // Type: long
130 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
131 // Type: int
132 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
133 // Type: int
134 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
145 // Type: int[]
146 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
147 // Type: boolean
148 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
149 // Type: long
150 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
151
Joe Onorato9c1289c2009-08-17 11:03:03 -0400152 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800155 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800157 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 private LayoutInflater mInflater;
160
Joe Onorato00acb122009-08-04 16:04:30 -0400161 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700163
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700164 private AppWidgetManager mAppWidgetManager;
165 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private CellLayout.CellInfo mAddItemCellInfo;
168 private CellLayout.CellInfo mMenuAddInfo;
169 private final int[] mCellCoordinates = new int[2];
170 private FolderInfo mFolderInfo;
171
Joe Onorato7c312c12009-08-13 21:36:53 -0700172 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700173 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700174 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private Bundle mSavedState;
177
178 private SpannableStringBuilder mDefaultKeySsb = null;
179
Mike LeBeau736cf282009-07-02 17:46:59 -0700180 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 private boolean mWorkspaceLoading = true;
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private boolean mRestoring;
185 private boolean mWaitingForResult;
Joe Onoratoff3862d2009-10-07 10:35:51 -0700186 private boolean mExitingBecauseOfLaunch;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
188 private Bundle mSavedInstanceState;
189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 private LauncherModel mModel;
191
Romain Guy84f296c2009-11-04 15:00:44 -0800192 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
193 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700194
Joe Onorato080d9b62009-11-02 12:01:11 -0500195 public static long lastStartTime;
196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 @Override
198 protected void onCreate(Bundle savedInstanceState) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500199 lastStartTime = SystemClock.uptimeMillis();
200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400204 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700206
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700207 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
209 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 if (PROFILE_STARTUP) {
212 android.os.Debug.startMethodTracing("/sdcard/launcher");
213 }
214
215 checkForLocaleChange();
216 setWallpaperDimension();
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 setContentView(R.layout.launcher);
219 setupViews();
220
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800221 registerContentObservers();
222
Joe Onorato7c312c12009-08-13 21:36:53 -0700223 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 mSavedState = savedInstanceState;
226 restoreState(mSavedState);
227
228 if (PROFILE_STARTUP) {
229 android.os.Debug.stopMethodTracing();
230 }
231
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700232 // We have a new AllAppsView, we need to re-bind everything, and it could have
233 // changed in our absence.
234 mModel.setAllAppsDirty();
235 mModel.setWorkspaceDirty();
236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400238 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 }
240
241 // For handling default keys
242 mDefaultKeySsb = new SpannableStringBuilder();
243 Selection.setSelection(mDefaultKeySsb, 0);
244 }
Romain Guycbb89e42009-06-08 15:52:54 -0700245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700247 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
248 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 final Configuration configuration = getResources().getConfiguration();
251
Romain Guy98d01652009-06-30 16:21:04 -0700252 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 final String locale = configuration.locale.toString();
254
Romain Guy98d01652009-06-30 16:21:04 -0700255 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 final int mcc = configuration.mcc;
257
Romain Guy98d01652009-06-30 16:21:04 -0700258 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 final int mnc = configuration.mnc;
260
Romain Guy84f296c2009-11-04 15:00:44 -0800261 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
Romain Guy84f296c2009-11-04 15:00:44 -0800263 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700264 localeConfiguration.locale = locale;
265 localeConfiguration.mcc = mcc;
266 localeConfiguration.mnc = mnc;
267
268 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700270 }
271 }
272
273 private static class LocaleConfiguration {
274 public String locale;
275 public int mcc = -1;
276 public int mnc = -1;
277 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700278
Romain Guy98d01652009-06-30 16:21:04 -0700279 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
280 DataInputStream in = null;
281 try {
282 in = new DataInputStream(context.openFileInput(PREFERENCES));
283 configuration.locale = in.readUTF();
284 configuration.mcc = in.readInt();
285 configuration.mnc = in.readInt();
286 } catch (FileNotFoundException e) {
287 // Ignore
288 } catch (IOException e) {
289 // Ignore
290 } finally {
291 if (in != null) {
292 try {
293 in.close();
294 } catch (IOException e) {
295 // Ignore
296 }
297 }
298 }
299 }
300
301 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
302 DataOutputStream out = null;
303 try {
304 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
305 out.writeUTF(configuration.locale);
306 out.writeInt(configuration.mcc);
307 out.writeInt(configuration.mnc);
308 out.flush();
309 } catch (FileNotFoundException e) {
310 // Ignore
311 } catch (IOException e) {
312 //noinspection ResultOfMethodCallIgnored
313 context.getFileStreamPath(PREFERENCES).delete();
314 } finally {
315 if (out != null) {
316 try {
317 out.close();
318 } catch (IOException e) {
319 // Ignore
320 }
321 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
323 }
324
325 static int getScreen() {
326 synchronized (sLock) {
327 return sScreen;
328 }
329 }
330
331 static void setScreen(int screen) {
332 synchronized (sLock) {
333 sScreen = screen;
334 }
335 }
336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700338 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339
340 Display display = getWindowManager().getDefaultDisplay();
341 boolean isPortrait = display.getWidth() < display.getHeight();
342
343 final int width = isPortrait ? display.getWidth() : display.getHeight();
344 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700345 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 }
347
348 @Override
349 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700350 mWaitingForResult = false;
351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 // The pattern used here is that a user PICKs a specific application,
353 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
356 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700357
Romain Guy94dabf12009-07-21 10:55:43 -0700358 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 switch (requestCode) {
360 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400361 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 break;
363 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700364 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 break;
366 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 break;
369 case REQUEST_PICK_LIVE_FOLDER:
370 addLiveFolder(data);
371 break;
372 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700375 case REQUEST_PICK_APPWIDGET:
376 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700378 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700381 case REQUEST_PICK_WALLPAPER:
382 // We just wanted the activity result here so we can clear mWaitingForResult
383 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 }
Romain Guy18042c82009-11-06 11:44:55 -0800385 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
386 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
387 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700388 // Clean up the appWidgetId if we canceled
389 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
390 if (appWidgetId != -1) {
391 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
393 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 }
395
396 @Override
397 protected void onResume() {
Joe Onorato080d9b62009-11-02 12:01:11 -0500398 if (lastStartTime == 0) {
399 lastStartTime = SystemClock.uptimeMillis();
400 }
401
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 super.onResume();
403
404 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400405 mWorkspaceLoading = true;
406 mModel.startLoader(this, true);
407 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700409
Mike LeBeau736cf282009-07-02 17:46:59 -0700410 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
411 // onNewIntent), then close the search dialog if needed, because it probably
412 // came from the user pressing 'home' (rather than, for example, pressing 'back').
413 if (mIsNewIntent) {
414 // Post to a handler so that this happens after the search dialog tries to open
415 // itself again.
416 mWorkspace.post(new Runnable() {
417 public void run() {
418 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
419 ServiceManager.getService(Context.SEARCH_SERVICE));
420 try {
421 searchManagerService.stopSearch();
422 } catch (RemoteException e) {
423 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700424 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700425 }
426 });
427 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700428
Mike LeBeau736cf282009-07-02 17:46:59 -0700429 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 }
431
432 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 protected void onPause() {
434 super.onPause();
Joe Onoratoff3862d2009-10-07 10:35:51 -0700435 if (mExitingBecauseOfLaunch) {
436 mExitingBecauseOfLaunch = false;
437 closeAllApps(false);
438 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700439 }
Romain Guycbb89e42009-06-08 15:52:54 -0700440
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700441 @Override
442 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400443 // Flag the loader to stop early before switching
444 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700445
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700446 if (PROFILE_ROTATE) {
447 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
448 }
449 return null;
450 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 private boolean acceptFilter() {
453 final InputMethodManager inputManager = (InputMethodManager)
454 getSystemService(Context.INPUT_METHOD_SERVICE);
455 return !inputManager.isFullscreenMode();
456 }
457
458 @Override
459 public boolean onKeyDown(int keyCode, KeyEvent event) {
460 boolean handled = super.onKeyDown(keyCode, event);
461 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
462 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
463 keyCode, event);
464 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700465 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700466 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700467 // showSearchDialog()
468 // If there are multiple keystrokes before the search dialog takes focus,
469 // onSearchRequested() will be called for every keystroke,
470 // but it is idempotent, so it's fine.
471 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
473 }
474
475 return handled;
476 }
477
Karl Rosaen138a0412009-04-23 19:00:21 -0700478 private String getTypedText() {
479 return mDefaultKeySsb.toString();
480 }
481
482 private void clearTypedText() {
483 mDefaultKeySsb.clear();
484 mDefaultKeySsb.clearSpans();
485 Selection.setSelection(mDefaultKeySsb, 0);
486 }
487
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 /**
489 * Restores the previous state, if it exists.
490 *
491 * @param savedState The previous state.
492 */
493 private void restoreState(Bundle savedState) {
494 if (savedState == null) {
495 return;
496 }
497
498 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
499 if (currentScreen > -1) {
500 mWorkspace.setCurrentScreen(currentScreen);
501 }
502
503 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
504 if (addScreen > -1) {
505 mAddItemCellInfo = new CellLayout.CellInfo();
506 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
507 addItemCellInfo.valid = true;
508 addItemCellInfo.screen = addScreen;
509 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
510 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
511 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
512 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
513 addItemCellInfo.findVacantCellsFromOccupied(
514 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
515 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
516 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
517 mRestoring = true;
518 }
519
520 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
521 if (renameFolder) {
522 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400523 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 mRestoring = true;
525 }
526 }
527
528 /**
529 * Finds all the views we need and configure them properly.
530 */
531 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400532 DragController dragController = mDragController;
533
Romain Guyb1b69f52009-08-10 15:10:15 -0700534 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400535 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536
Joe Onorato7c312c12009-08-13 21:36:53 -0700537 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700538 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700539 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700540 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700541 // Manage focusability manually since this thing is always visible
542 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700543
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
545 final Workspace workspace = mWorkspace;
546
Joe Onorato7c312c12009-08-13 21:36:53 -0700547 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
548 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549
Joe Onorato7c312c12009-08-13 21:36:53 -0700550 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700551 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700552 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700553 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700554 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700555 handleIocon.setCrossFadeEnabled(true);
556 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400559 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561
562 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400563 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700564 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565
Joe Onorato00acb122009-08-04 16:04:30 -0400566 dragController.setDragScoller(workspace);
567 dragController.setDragListener(deleteZone);
568 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700569
Joe Onorato00acb122009-08-04 16:04:30 -0400570 // The order here is bottom to top.
571 dragController.addDropTarget(workspace);
572 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 }
574
575 /**
576 * Creates a view representing a shortcut.
577 *
578 * @param info The data structure describing the shortcut.
579 *
580 * @return A View inflated from R.layout.application.
581 */
582 View createShortcut(ApplicationInfo info) {
583 return createShortcut(R.layout.application,
584 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
585 }
586
587 /**
588 * Creates a view representing a shortcut inflated from the specified resource.
589 *
590 * @param layoutResId The id of the XML layout used to create the shortcut.
591 * @param parent The group the shortcut belongs to.
592 * @param info The data structure describing the shortcut.
593 *
594 * @return A View inflated from layoutResId.
595 */
596 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
597 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
598
Joe Onorato5162ea92009-09-03 09:39:42 -0700599 if (info.icon == null) {
600 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
601 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700603 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 info.filtered = true;
605 }
606
607 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
608 favorite.setText(info.title);
609 favorite.setTag(info);
610 favorite.setOnClickListener(this);
611
612 return favorite;
613 }
614
615 /**
616 * Add an application shortcut to the workspace.
617 *
618 * @param data The intent describing the application.
619 * @param cellInfo The position on screen where to create the shortcut.
620 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 cellInfo.screen = mWorkspace.getCurrentScreen();
623 if (!findSingleSlot(cellInfo)) return;
624
Romain Guy73b979d2009-06-09 12:57:21 -0700625 final ApplicationInfo info = infoFromApplicationIntent(context, data);
626 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400627 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700628 }
629 }
630
631 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 ComponentName component = data.getComponent();
633 PackageManager packageManager = context.getPackageManager();
634 ActivityInfo activityInfo = null;
635 try {
636 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
637 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700638 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 }
Romain Guycbb89e42009-06-08 15:52:54 -0700640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 if (activityInfo != null) {
642 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 itemInfo.title = activityInfo.loadLabel(packageManager);
645 if (itemInfo.title == null) {
646 itemInfo.title = activityInfo.name;
647 }
Romain Guycbb89e42009-06-08 15:52:54 -0700648
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
650 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
651 itemInfo.icon = activityInfo.loadIcon(packageManager);
652 itemInfo.container = ItemInfo.NO_ID;
653
Romain Guy73b979d2009-06-09 12:57:21 -0700654 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 }
Romain Guy73b979d2009-06-09 12:57:21 -0700656
657 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 }
Romain Guycbb89e42009-06-08 15:52:54 -0700659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 /**
661 * Add a shortcut to the workspace.
662 *
663 * @param data The intent describing the shortcut.
664 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 cellInfo.screen = mWorkspace.getCurrentScreen();
668 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
671
672 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400674 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
675 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
677 }
678
Romain Guycbb89e42009-06-08 15:52:54 -0700679
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700681 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 * @param data The intent describing the appWidgetId.
684 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400686 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700689
Romain Guy18042c82009-11-06 11:44:55 -0800690 if (LOGD) d(LOG_TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700691
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700692 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700693
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700694 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700696 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 // Try finding open space on Launcher screen
699 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800700 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
701 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
702 return;
703 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700705 // Build Launcher-specific widget info and save to database
706 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 launcherInfo.spanX = spans[0];
708 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 LauncherModel.addItemToDatabase(this, launcherInfo,
711 LauncherSettings.Favorites.CONTAINER_DESKTOP,
712 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
713
714 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400715 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700716
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700719
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700720 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700722
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 }
726 }
Romain Guycbb89e42009-06-08 15:52:54 -0700727
Joe Onorato9c1289c2009-08-17 11:03:03 -0400728 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
729 mDesktopItems.remove(launcherInfo);
730 launcherInfo.hostView = null;
731 }
732
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700733 public LauncherAppWidgetHost getAppWidgetHost() {
734 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 }
Romain Guycbb89e42009-06-08 15:52:54 -0700736
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 static ApplicationInfo addShortcut(Context context, Intent data,
738 CellLayout.CellInfo cellInfo, boolean notify) {
739
Romain Guy73b979d2009-06-09 12:57:21 -0700740 final ApplicationInfo info = infoFromShortcutIntent(context, data);
741 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
742 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
743
744 return info;
745 }
746
747 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
749 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
750 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
751
752 Drawable icon = null;
753 boolean filtered = false;
754 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700755 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756
757 if (bitmap != null) {
758 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
759 filtered = true;
760 customIcon = true;
761 } else {
762 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700763 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700765 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 final PackageManager packageManager = context.getPackageManager();
767 Resources resources = packageManager.getResourcesForApplication(
768 iconResource.packageName);
769 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
770 icon = resources.getDrawable(id);
771 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700772 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 }
774 }
775 }
776
777 if (icon == null) {
778 icon = context.getPackageManager().getDefaultActivityIcon();
779 }
780
781 final ApplicationInfo info = new ApplicationInfo();
782 info.icon = icon;
783 info.filtered = filtered;
784 info.title = name;
785 info.intent = intent;
786 info.customIcon = customIcon;
787 info.iconResource = iconResource;
788
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 return info;
790 }
791
792 @Override
793 protected void onNewIntent(Intent intent) {
794 super.onNewIntent(intent);
795
796 // Close the menu
797 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
798 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700799
Joe Onorato742966b2009-10-07 10:26:17 -0700800 // Whatever we were doing is hereby canceled.
801 mWaitingForResult = false;
802
Mike LeBeau736cf282009-07-02 17:46:59 -0700803 // Set this flag so that onResume knows to close the search dialog if it's open,
804 // because this was a new intent (thus a press of 'home' or some such) rather than
805 // for example onResume being called when the user pressed the 'back' button.
806 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807
808 try {
809 dismissDialog(DIALOG_CREATE_SHORTCUT);
810 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 } catch (Exception e) {
812 // An exception is thrown if the dialog is not visible, which is fine
813 }
814
815 try {
816 dismissDialog(DIALOG_RENAME_FOLDER);
817 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 } catch (Exception e) {
819 // An exception is thrown if the dialog is not visible, which is fine
820 }
821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
823 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700824
Romain Guy94dabf12009-07-21 10:55:43 -0700825 if (!mWorkspace.isDefaultScreenShowing()) {
826 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700828
Joe Onorato7bb17492009-09-24 17:51:01 -0700829 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700830
831 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 if (v != null && v.getWindowToken() != null) {
833 InputMethodManager imm = (InputMethodManager)getSystemService(
834 INPUT_METHOD_SERVICE);
835 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
836 }
837 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700838 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
840 }
841 }
842
843 @Override
844 protected void onRestoreInstanceState(Bundle savedInstanceState) {
845 // Do not call super here
846 mSavedInstanceState = savedInstanceState;
847 }
848
849 @Override
850 protected void onSaveInstanceState(Bundle outState) {
851 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
852
853 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
854 if (folders.size() > 0) {
855 final int count = folders.size();
856 long[] ids = new long[count];
857 for (int i = 0; i < count; i++) {
858 final FolderInfo info = folders.get(i).getInfo();
859 ids[i] = info.id;
860 }
861 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
862 } else {
863 super.onSaveInstanceState(outState);
864 }
865
Romain Guy3cf604f2009-06-16 13:12:53 -0700866 final boolean isConfigurationChange = getChangingConfigurations() != 0;
867
Romain Guy5a941392009-04-28 15:18:25 -0700868 // When the drawer is opened and we are saving the state because of a
869 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400870 // TODO should not do this if the drawer is currently closing.
871 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874
875 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
876 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
877 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
878
879 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
880 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
881 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
882 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
883 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
884 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
885 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
886 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
887 layout.getOccupiedCells());
888 }
889
890 if (mFolderInfo != null && mWaitingForResult) {
891 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
892 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
893 }
894 }
895
896 @Override
897 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700899
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700901 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700903 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905
906 TextKeyListener.getInstance().release();
907
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909
Joe Onorato9c1289c2009-08-17 11:03:03 -0400910 unbindDesktopItems();
911 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800912
913 getContentResolver().unregisterContentObserver(mWidgetObserver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 }
915
916 @Override
917 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700918 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 super.startActivityForResult(intent, requestCode);
920 }
921
922 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700923 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700925
Joe Onorato7bb17492009-09-24 17:51:01 -0700926 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700927
Karl Rosaen138a0412009-04-23 19:00:21 -0700928 // Slide the search widget to the top, if it's on the current screen,
929 // otherwise show the search dialog immediately.
930 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
931 if (searchWidget == null) {
932 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
933 } else {
934 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
935 // show the currently typed text in the search widget while sliding
936 searchWidget.setQuery(getTypedText());
937 }
938 }
Romain Guycbb89e42009-06-08 15:52:54 -0700939
Karl Rosaen138a0412009-04-23 19:00:21 -0700940 /**
941 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700942 *
943 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700944 */
Romain Guycbb89e42009-06-08 15:52:54 -0700945 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700946 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700947
Karl Rosaen138a0412009-04-23 19:00:21 -0700948 if (initialQuery == null) {
949 // Use any text typed in the launcher as the initial query
950 initialQuery = getTypedText();
951 clearTypedText();
952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 if (appSearchData == null) {
954 appSearchData = new Bundle();
955 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
956 }
Romain Guycbb89e42009-06-08 15:52:54 -0700957
Karl Rosaen138a0412009-04-23 19:00:21 -0700958 final SearchManager searchManager =
959 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
960
961 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
962 if (searchWidget != null) {
963 // This gets called when the user leaves the search dialog to go back to
964 // the Launcher.
965 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
966 public void onCancel() {
967 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700968 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700969 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 });
971 }
Romain Guycbb89e42009-06-08 15:52:54 -0700972
Karl Rosaen138a0412009-04-23 19:00:21 -0700973 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700974 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
976
Karl Rosaen138a0412009-04-23 19:00:21 -0700977 /**
978 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700979 */
Romain Guy5a941392009-04-28 15:18:25 -0700980 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700981 // Close search dialog
982 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100983 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700984 // Restore search widget to its normal position
985 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
986 if (searchWidget != null) {
987 searchWidget.stopSearch(false);
988 }
989 }
Romain Guycbb89e42009-06-08 15:52:54 -0700990
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 @Override
992 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993 if (isWorkspaceLocked()) {
994 return false;
995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996
997 super.onCreateOptionsMenu(menu);
998 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
999 .setIcon(android.R.drawable.ic_menu_add)
1000 .setAlphabeticShortcut('A');
1001 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1002 .setIcon(android.R.drawable.ic_menu_gallery)
1003 .setAlphabeticShortcut('W');
1004 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1005 .setIcon(android.R.drawable.ic_search_category_default)
1006 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1007 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1008 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1009 .setAlphabeticShortcut('N');
1010
1011 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001012 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1013 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014
1015 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1016 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1017 .setIntent(settings);
1018
1019 return true;
1020 }
1021
1022 @Override
1023 public boolean onPrepareOptionsMenu(Menu menu) {
1024 super.onPrepareOptionsMenu(menu);
1025
1026 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1027 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1028
1029 return true;
1030 }
1031
1032 @Override
1033 public boolean onOptionsItemSelected(MenuItem item) {
1034 switch (item.getItemId()) {
1035 case MENU_ADD:
1036 addItems();
1037 return true;
1038 case MENU_WALLPAPER_SETTINGS:
1039 startWallpaper();
1040 return true;
1041 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001042 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 return true;
1044 case MENU_NOTIFICATIONS:
1045 showNotifications();
1046 return true;
1047 }
1048
1049 return super.onOptionsItemSelected(item);
1050 }
Romain Guycbb89e42009-06-08 15:52:54 -07001051
Karl Rosaen138a0412009-04-23 19:00:21 -07001052 /**
1053 * Indicates that we want global search for this activity by setting the globalSearch
1054 * argument for {@link #startSearch} to true.
1055 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001057 @Override
1058 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001059 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001060 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001061 }
1062
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063 public boolean isWorkspaceLocked() {
1064 return mWorkspaceLoading || mWaitingForResult;
1065 }
1066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001068 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 showAddDialog(mMenuAddInfo);
1070 }
1071
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001072 void addAppWidget(Intent data) {
1073 // TODO: catch bad widget exception when sent
1074 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001076 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1077 if (SEARCH_WIDGET.equals(customWidget)) {
1078 // We don't need this any more, since this isn't a real app widget.
1079 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1080 // add the search widget
1081 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001083 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1084
1085 if (appWidget.configure != null) {
1086 // Launch over to configure widget, if needed
1087 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1088 intent.setComponent(appWidget.configure);
1089 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1090
1091 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1092 } else {
1093 // Otherwise just add it
1094 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1095 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 }
1097 }
Romain Guycbb89e42009-06-08 15:52:54 -07001098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 void addSearch() {
1100 final Widget info = Widget.makeSearch();
1101 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 final int[] xy = mCellCoordinates;
1104 final int spanX = info.spanX;
1105 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1110 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001111
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 final View view = mInflater.inflate(info.layoutResource, null);
1113 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001114 Search search = (Search) view.findViewById(R.id.widget_search);
1115 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1118 }
1119
Romain Guy73b979d2009-06-09 12:57:21 -07001120 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001121 // Handle case where user selected "Applications"
1122 String applicationName = getResources().getString(R.string.group_applications);
1123 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001124
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001125 if (applicationName != null && applicationName.equals(shortcutName)) {
1126 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1127 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001128
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001129 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1130 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001131 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001132 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001133 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 }
1136
1137 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001138 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001139 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001140 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001141
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001142 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001144 } else {
1145 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1146 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 }
1148
Joe Onorato9c1289c2009-08-17 11:03:03 -04001149 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 UserFolderInfo folderInfo = new UserFolderInfo();
1151 folderInfo.title = getText(R.string.folder_name);
1152
1153 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1154 cellInfo.screen = mWorkspace.getCurrentScreen();
1155 if (!findSingleSlot(cellInfo)) return;
1156
1157 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001158 LauncherModel.addItemToDatabase(this, folderInfo,
1159 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001161 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162
1163 // Create the view
1164 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1165 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1166 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001167 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 }
Romain Guycbb89e42009-06-08 15:52:54 -07001169
Joe Onorato9c1289c2009-08-17 11:03:03 -04001170 void removeFolder(FolderInfo folder) {
1171 mFolders.remove(folder.id);
1172 }
1173
1174 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 cellInfo.screen = mWorkspace.getCurrentScreen();
1176 if (!findSingleSlot(cellInfo)) return;
1177
1178 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1179
1180 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1182 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001183 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1184 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 }
1186 }
1187
1188 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1189 CellLayout.CellInfo cellInfo, boolean notify) {
1190
1191 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1192 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1193
1194 Drawable icon = null;
1195 boolean filtered = false;
1196 Intent.ShortcutIconResource iconResource = null;
1197
1198 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1199 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1200 try {
1201 iconResource = (Intent.ShortcutIconResource) extra;
1202 final PackageManager packageManager = context.getPackageManager();
1203 Resources resources = packageManager.getResourcesForApplication(
1204 iconResource.packageName);
1205 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1206 icon = resources.getDrawable(id);
1207 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001208 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
1210 }
1211
1212 if (icon == null) {
1213 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1214 }
1215
1216 final LiveFolderInfo info = new LiveFolderInfo();
1217 info.icon = icon;
1218 info.filtered = filtered;
1219 info.title = name;
1220 info.iconResource = iconResource;
1221 info.uri = data.getData();
1222 info.baseIntent = baseIntent;
1223 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1224 LiveFolders.DISPLAY_MODE_GRID);
1225
1226 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1227 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001228 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229
1230 return info;
1231 }
1232
1233 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1234 final int[] xy = new int[2];
1235 if (findSlot(cellInfo, xy, 1, 1)) {
1236 cellInfo.cellX = xy[0];
1237 cellInfo.cellY = xy[1];
1238 return true;
1239 }
1240 return false;
1241 }
1242
1243 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1244 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1245 boolean[] occupied = mSavedState != null ?
1246 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1247 cellInfo = mWorkspace.findAllVacantCells(occupied);
1248 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1249 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1250 return false;
1251 }
1252 }
1253 return true;
1254 }
1255
1256 private void showNotifications() {
1257 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1258 if (statusBar != null) {
1259 statusBar.expand();
1260 }
1261 }
1262
1263 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001264 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001266 Intent chooser = Intent.createChooser(pickWallpaper,
1267 getText(R.string.chooser_wallpaper));
1268 WallpaperManager wm = (WallpaperManager)
1269 getSystemService(Context.WALLPAPER_SERVICE);
1270 WallpaperInfo wi = wm.getWallpaperInfo();
1271 if (wi != null && wi.getSettingsActivity() != null) {
1272 LabeledIntent li = new LabeledIntent(getPackageName(),
1273 R.string.configure_wallpaper, 0);
1274 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1275 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1276 }
Mike Clerona0618e42009-10-22 13:55:21 -07001277 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
1279
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001280 private void registerContentObservers() {
1281 ContentResolver resolver = getContentResolver();
1282 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1283 true, mWidgetObserver);
1284 }
1285
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 @Override
1287 public boolean dispatchKeyEvent(KeyEvent event) {
1288 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1289 switch (event.getKeyCode()) {
1290 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 return true;
1292 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001293 return true;
1294 }
1295 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1296 switch (event.getKeyCode()) {
1297 case KeyEvent.KEYCODE_BACK:
1298 if (!event.isCanceled()) {
1299 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001300 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001301 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001302 } else {
1303 closeFolder();
1304 }
1305 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001306 return true;
1307 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 return true;
1309 }
1310 }
1311
1312 return super.dispatchKeyEvent(event);
1313 }
1314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 private void closeFolder() {
1316 Folder folder = mWorkspace.getOpenFolder();
1317 if (folder != null) {
1318 closeFolder(folder);
1319 }
1320 }
1321
1322 void closeFolder(Folder folder) {
1323 folder.getInfo().opened = false;
1324 ViewGroup parent = (ViewGroup) folder.getParent();
1325 if (parent != null) {
1326 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001327 if (folder instanceof DropTarget) {
1328 // Live folders aren't DropTargets.
1329 mDragController.removeDropTarget((DropTarget)folder);
1330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332 folder.onClose();
1333 }
1334
1335 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001336 * Re-listen when widgets are reset.
1337 */
1338 private void onAppWidgetReset() {
1339 mAppWidgetHost.startListening();
1340 }
1341
1342 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001343 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1344 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001346 private void unbindDesktopItems() {
1347 for (ItemInfo item: mDesktopItems) {
1348 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 }
1350 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001351
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 /**
1353 * Launches the intent referred by the clicked shortcut.
1354 *
1355 * @param v The view representing the clicked shortcut.
1356 */
1357 public void onClick(View v) {
1358 Object tag = v.getTag();
1359 if (tag instanceof ApplicationInfo) {
1360 // Open shortcut
1361 final Intent intent = ((ApplicationInfo) tag).intent;
1362 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001363 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 } else if (tag instanceof FolderInfo) {
1365 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001366 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001367 Log.d(TAG, "onClick");
1368 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001369 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001370 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001371 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001372 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 }
1374 }
1375
1376 void startActivitySafely(Intent intent) {
1377 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1378 try {
1379 startActivity(intent);
1380 } catch (ActivityNotFoundException e) {
1381 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1382 } catch (SecurityException e) {
1383 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001384 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1386 "or use the exported attribute for this activity.", e);
1387 }
1388 }
1389
1390 private void handleFolderClick(FolderInfo folderInfo) {
1391 if (!folderInfo.opened) {
1392 // Close any open folder
1393 closeFolder();
1394 // Open the requested folder
1395 openFolder(folderInfo);
1396 } else {
1397 // Find the open folder...
1398 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1399 int folderScreen;
1400 if (openFolder != null) {
1401 folderScreen = mWorkspace.getScreenForView(openFolder);
1402 // .. and close it
1403 closeFolder(openFolder);
1404 if (folderScreen != mWorkspace.getCurrentScreen()) {
1405 // Close any folder open on the current screen
1406 closeFolder();
1407 // Pull the folder onto this screen
1408 openFolder(folderInfo);
1409 }
1410 }
1411 }
1412 }
1413
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 /**
1415 * Opens the user fodler described by the specified tag. The opening of the folder
1416 * is animated relative to the specified View. If the View is null, no animation
1417 * is played.
1418 *
1419 * @param folderInfo The FolderInfo describing the folder to open.
1420 */
1421 private void openFolder(FolderInfo folderInfo) {
1422 Folder openFolder;
1423
1424 if (folderInfo instanceof UserFolderInfo) {
1425 openFolder = UserFolder.fromXml(this);
1426 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001427 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 } else {
1429 return;
1430 }
1431
Joe Onorato00acb122009-08-04 16:04:30 -04001432 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 openFolder.setLauncher(this);
1434
1435 openFolder.bind(folderInfo);
1436 folderInfo.opened = true;
1437
1438 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1439 openFolder.onOpen();
1440 }
1441
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001443 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 return false;
1445 }
1446
1447 if (!(v instanceof CellLayout)) {
1448 v = (View) v.getParent();
1449 }
1450
1451 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1452
1453 // This happens when long clicking an item with the dpad/trackball
1454 if (cellInfo == null) {
1455 return true;
1456 }
1457
1458 if (mWorkspace.allowLongPress()) {
1459 if (cellInfo.cell == null) {
1460 if (cellInfo.valid) {
1461 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001462 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 showAddDialog(cellInfo);
1464 }
1465 } else {
1466 if (!(cellInfo.cell instanceof Folder)) {
1467 // User long pressed on an item
1468 mWorkspace.startDrag(cellInfo);
1469 }
1470 }
1471 }
1472 return true;
1473 }
1474
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001475 View getDrawerHandle() {
1476 return mHandleView;
1477 }
1478
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 Workspace getWorkspace() {
1480 return mWorkspace;
1481 }
1482
Joe Onorato7404ee42009-07-31 11:54:44 -07001483 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 GridView getApplicationsGrid() {
1485 return mAllAppsGrid;
1486 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001487 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488
1489 @Override
1490 protected Dialog onCreateDialog(int id) {
1491 switch (id) {
1492 case DIALOG_CREATE_SHORTCUT:
1493 return new CreateShortcut().createDialog();
1494 case DIALOG_RENAME_FOLDER:
1495 return new RenameFolder().createDialog();
1496 }
1497
1498 return super.onCreateDialog(id);
1499 }
1500
1501 @Override
1502 protected void onPrepareDialog(int id, Dialog dialog) {
1503 switch (id) {
1504 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 break;
1506 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001507 if (mFolderInfo != null) {
1508 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1509 final CharSequence text = mFolderInfo.title;
1510 input.setText(text);
1511 input.setSelection(0, text.length());
1512 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 break;
1514 }
1515 }
1516
1517 void showRenameDialog(FolderInfo info) {
1518 mFolderInfo = info;
1519 mWaitingForResult = true;
1520 showDialog(DIALOG_RENAME_FOLDER);
1521 }
1522
1523 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1524 mAddItemCellInfo = cellInfo;
1525 mWaitingForResult = true;
1526 showDialog(DIALOG_CREATE_SHORTCUT);
1527 }
1528
Romain Guy73b979d2009-06-09 12:57:21 -07001529 private void pickShortcut(int requestCode, int title) {
1530 Bundle bundle = new Bundle();
1531
1532 ArrayList<String> shortcutNames = new ArrayList<String>();
1533 shortcutNames.add(getString(R.string.group_applications));
1534 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1535
1536 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1537 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1538 R.drawable.ic_launcher_application));
1539 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1540
1541 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1542 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1543 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1544 pickIntent.putExtras(bundle);
1545
1546 startActivityForResult(pickIntent, requestCode);
1547 }
1548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 private class RenameFolder {
1550 private EditText mInput;
1551
1552 Dialog createDialog() {
1553 mWaitingForResult = true;
1554 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1555 mInput = (EditText) layout.findViewById(R.id.folder_name);
1556
1557 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1558 builder.setIcon(0);
1559 builder.setTitle(getString(R.string.rename_folder_title));
1560 builder.setCancelable(true);
1561 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1562 public void onCancel(DialogInterface dialog) {
1563 cleanup();
1564 }
1565 });
1566 builder.setNegativeButton(getString(R.string.cancel_action),
1567 new Dialog.OnClickListener() {
1568 public void onClick(DialogInterface dialog, int which) {
1569 cleanup();
1570 }
1571 }
1572 );
1573 builder.setPositiveButton(getString(R.string.rename_action),
1574 new Dialog.OnClickListener() {
1575 public void onClick(DialogInterface dialog, int which) {
1576 changeFolderName();
1577 }
1578 }
1579 );
1580 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001581
1582 final AlertDialog dialog = builder.create();
1583 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1584 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001585 mInput.requestFocus();
1586 InputMethodManager inputManager = (InputMethodManager)
1587 getSystemService(Context.INPUT_METHOD_SERVICE);
1588 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001589 }
1590 });
1591
1592 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
1594
1595 private void changeFolderName() {
1596 final String name = mInput.getText().toString();
1597 if (!TextUtils.isEmpty(name)) {
1598 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001599 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 mFolderInfo.title = name;
1601 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1602
Joe Onorato9c1289c2009-08-17 11:03:03 -04001603 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001604 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001605 mModel.setWorkspaceDirty();
1606 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 } else {
1608 final FolderIcon folderIcon = (FolderIcon)
1609 mWorkspace.getViewForTag(mFolderInfo);
1610 if (folderIcon != null) {
1611 folderIcon.setText(name);
1612 getWorkspace().requestLayout();
1613 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001614 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001615 mModel.setWorkspaceDirty();
1616 mWorkspaceLoading = true;
1617 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 }
1619 }
1620 }
1621 cleanup();
1622 }
1623
1624 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 dismissDialog(DIALOG_RENAME_FOLDER);
1626 mWaitingForResult = false;
1627 mFolderInfo = null;
1628 }
1629 }
1630
Joe Onoratofb0ca672009-09-14 17:55:46 -04001631 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001632 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001633 }
1634
Jason Samsfd22dac2009-09-20 17:24:16 -07001635 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001636 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001637 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001638
Mike Cleronb6082fa02009-10-19 17:03:36 -07001639 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001640 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001641
Joe Onorato7c312c12009-08-13 21:36:53 -07001642 // TODO: fade these two too
1643 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001644 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001645 }
1646
Joe Onorato7bb17492009-09-24 17:51:01 -07001647 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001648 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001649 mAllAppsGrid.zoom(0.0f);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001650 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001651 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001652
Mike Cleronb6082fa02009-10-19 17:03:36 -07001653
Joe Onorato7c312c12009-08-13 21:36:53 -07001654 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001655 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001656 mDeleteZone.setVisibility(View.VISIBLE);
1657 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001658 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001659 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001660 }
1661
Joe Onorato7c312c12009-08-13 21:36:53 -07001662 void lockAllApps() {
1663 // TODO
1664 }
1665
1666 void unlockAllApps() {
1667 // TODO
1668 }
1669
Joe Onorato7404ee42009-07-31 11:54:44 -07001670 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 * Displays the shortcut creation dialog and launches, if necessary, the
1672 * appropriate activity.
1673 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001674 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001675 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1676 DialogInterface.OnShowListener {
1677
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 Dialog createDialog() {
1681 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001684
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1686 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001687 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001688
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 builder.setInverseBackgroundForced(true);
1690
1691 AlertDialog dialog = builder.create();
1692 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001693 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001694 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 return dialog;
1697 }
1698
1699 public void onCancel(DialogInterface dialog) {
1700 mWaitingForResult = false;
1701 cleanup();
1702 }
1703
Romain Guycbb89e42009-06-08 15:52:54 -07001704 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001705 }
1706
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 dismissDialog(DIALOG_CREATE_SHORTCUT);
1709 }
1710
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001711 /**
1712 * Handle the action clicked in the "Add to home" dialog.
1713 */
1714 public void onClick(DialogInterface dialog, int which) {
1715 Resources res = getResources();
1716 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001717
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001718 switch (which) {
1719 case AddAdapter.ITEM_SHORTCUT: {
1720 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001721 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001722 break;
1723 }
Romain Guycbb89e42009-06-08 15:52:54 -07001724
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001725 case AddAdapter.ITEM_APPWIDGET: {
1726 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001727
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001728 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1729 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1730 // add the search widget
1731 ArrayList<AppWidgetProviderInfo> customInfo =
1732 new ArrayList<AppWidgetProviderInfo>();
1733 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1734 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1735 info.label = getString(R.string.group_search);
1736 info.icon = R.drawable.ic_search_widget;
1737 customInfo.add(info);
1738 pickIntent.putParcelableArrayListExtra(
1739 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1740 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1741 Bundle b = new Bundle();
1742 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1743 customExtras.add(b);
1744 pickIntent.putParcelableArrayListExtra(
1745 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1746 // start the pick activity
1747 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1748 break;
1749 }
Romain Guycbb89e42009-06-08 15:52:54 -07001750
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001751 case AddAdapter.ITEM_LIVE_FOLDER: {
1752 // Insert extra item to handle inserting folder
1753 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001754
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001755 ArrayList<String> shortcutNames = new ArrayList<String>();
1756 shortcutNames.add(res.getString(R.string.group_folder));
1757 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001758
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001759 ArrayList<ShortcutIconResource> shortcutIcons =
1760 new ArrayList<ShortcutIconResource>();
1761 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1762 R.drawable.ic_launcher_folder));
1763 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1764
1765 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1766 pickIntent.putExtra(Intent.EXTRA_INTENT,
1767 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1768 pickIntent.putExtra(Intent.EXTRA_TITLE,
1769 getText(R.string.title_select_live_folder));
1770 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001771
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001772 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1773 break;
1774 }
1775
1776 case AddAdapter.ITEM_WALLPAPER: {
1777 startWallpaper();
1778 break;
1779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 }
1781 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001782
1783 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001784 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 }
1786
1787 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001788 * Receives notifications whenever the appwidgets are reset.
1789 */
1790 private class AppWidgetResetObserver extends ContentObserver {
1791 public AppWidgetResetObserver() {
1792 super(new Handler());
1793 }
1794
1795 @Override
1796 public void onChange(boolean selfChange) {
1797 onAppWidgetReset();
1798 }
1799 }
1800
1801 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001802 * 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 int getCurrentWorkspaceScreen() {
1805 return mWorkspace.getCurrentScreen();
1806 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001807
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808 /**
1809 * Refreshes the shortcuts shown on the workspace.
1810 *
1811 * Implementation of the method from LauncherModel.Callbacks.
1812 */
1813 public void startBinding() {
1814 final Workspace workspace = mWorkspace;
1815 int count = workspace.getChildCount();
1816 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001817 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1819 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001820
Joe Onorato9c1289c2009-08-17 11:03:03 -04001821 if (DEBUG_USER_INTERFACE) {
1822 android.widget.Button finishButton = new android.widget.Button(this);
1823 finishButton.setText("Finish");
1824 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1825
1826 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1827 public void onClick(View v) {
1828 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001829 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001830 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 }
1832 }
1833
1834 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 * Bind the items start-end from the list.
1836 *
1837 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001839 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1840
1841 final Workspace workspace = mWorkspace;
1842
1843 for (int i=start; i<end; i++) {
1844 final ItemInfo item = shortcuts.get(i);
1845 mDesktopItems.add(item);
1846 switch (item.itemType) {
1847 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1848 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1849 final View shortcut = createShortcut((ApplicationInfo) item);
1850 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1851 false);
1852 break;
1853 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1854 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1855 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1856 (UserFolderInfo) item);
1857 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1858 false);
1859 break;
1860 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1861 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1862 R.layout.live_folder_icon, this,
1863 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1864 (LiveFolderInfo) item);
1865 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1866 false);
1867 break;
1868 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1869 final int screen = workspace.getCurrentScreen();
1870 final View view = mInflater.inflate(R.layout.widget_search,
1871 (ViewGroup) workspace.getChildAt(screen), false);
1872
1873 Search search = (Search) view.findViewById(R.id.widget_search);
1874 search.setLauncher(this);
1875
1876 final Widget widget = (Widget) item;
1877 view.setTag(widget);
1878
1879 workspace.addWidget(view, widget, false);
1880 break;
1881 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 }
1883
Joe Onorato9c1289c2009-08-17 11:03:03 -04001884 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
1886
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887 /**
1888 * Implementation of the method from LauncherModel.Callbacks.
1889 */
Joe Onoratoad72e172009-11-06 16:25:04 -05001890 public void bindFolders(HashMap<Long, FolderInfo> folders) {
1891 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001892 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001894
1895 /**
1896 * Add the views for a widget to the workspace.
1897 *
1898 * Implementation of the method from LauncherModel.Callbacks.
1899 */
1900 public void bindAppWidget(LauncherAppWidgetInfo item) {
1901 final Workspace workspace = mWorkspace;
1902
1903 final int appWidgetId = item.appWidgetId;
1904 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1905 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1906
Joe Onorato9c1289c2009-08-17 11:03:03 -04001907 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1908 item.hostView.setTag(item);
1909
1910 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1911 item.cellY, item.spanX, item.spanY, false);
1912
1913 workspace.requestLayout();
1914
1915 mDesktopItems.add(item);
1916 }
1917
1918 /**
1919 * Callback saying that there aren't any more items to bind.
1920 *
1921 * Implementation of the method from LauncherModel.Callbacks.
1922 */
1923 public void finishBindingItems() {
1924 if (mSavedState != null) {
1925 if (!mWorkspace.hasFocus()) {
1926 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1927 }
1928
1929 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1930 if (userFolders != null) {
1931 for (long folderId : userFolders) {
1932 final FolderInfo info = mFolders.get(folderId);
1933 if (info != null) {
1934 openFolder(info);
1935 }
1936 }
1937 final Folder openFolder = mWorkspace.getOpenFolder();
1938 if (openFolder != null) {
1939 openFolder.requestFocus();
1940 }
1941 }
1942
1943 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1944 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001945 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001946 }
1947
1948 mSavedState = null;
1949 }
1950
1951 if (mSavedInstanceState != null) {
1952 super.onRestoreInstanceState(mSavedInstanceState);
1953 mSavedInstanceState = null;
1954 }
1955
Joe Onorato9c1289c2009-08-17 11:03:03 -04001956 Log.d(TAG, "finishBindingItems done");
1957 mWorkspaceLoading = false;
1958 }
1959
1960 /**
1961 * Add the icons for all apps.
1962 *
1963 * Implementation of the method from LauncherModel.Callbacks.
1964 */
1965 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08001966 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001967 }
1968
1969 /**
1970 * A package was installed.
1971 *
1972 * Implementation of the method from LauncherModel.Callbacks.
1973 */
1974 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1975 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001976 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977 }
1978
1979 /**
1980 * A package was updated.
1981 *
1982 * Implementation of the method from LauncherModel.Callbacks.
1983 */
1984 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1985 removeDialog(DIALOG_CREATE_SHORTCUT);
1986 mWorkspace.updateShortcutsForPackage(packageName);
1987 }
1988
1989 /**
1990 * A package was uninstalled.
1991 *
1992 * Implementation of the method from LauncherModel.Callbacks.
1993 */
1994 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1995 removeDialog(DIALOG_CREATE_SHORTCUT);
1996 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001997 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001998 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999}