blob: 738793b6d8a6c18a4f8a71b6f86e84b8eda98e61 [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;
Romain Guy8a73c512009-11-09 19:19:59 -080068import android.widget.ImageView;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070069import android.appwidget.AppWidgetManager;
70import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import java.util.ArrayList;
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);
Romain Guy8a73c512009-11-09 19:19:59 -0800553
554 Drawable previous = ((ImageView) dragLayer.findViewById(R.id.previous_screen)).getDrawable();
555 Drawable next = ((ImageView) dragLayer.findViewById(R.id.next_screen)).getDrawable();
556 mWorkspace.setIndicators(previous, next);
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
Romain Guy8a73c512009-11-09 19:19:59 -0800575 @SuppressWarnings({"UnusedDeclaration"})
576 public void previousScreen(View v) {
577 mWorkspace.scrollLeft();
578 }
579
580 @SuppressWarnings({"UnusedDeclaration"})
581 public void nextScreen(View v) {
582 mWorkspace.scrollRight();
583 }
584
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 /**
586 * Creates a view representing a shortcut.
587 *
588 * @param info The data structure describing the shortcut.
589 *
590 * @return A View inflated from R.layout.application.
591 */
592 View createShortcut(ApplicationInfo info) {
593 return createShortcut(R.layout.application,
594 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
595 }
596
597 /**
598 * Creates a view representing a shortcut inflated from the specified resource.
599 *
600 * @param layoutResId The id of the XML layout used to create the shortcut.
601 * @param parent The group the shortcut belongs to.
602 * @param info The data structure describing the shortcut.
603 *
604 * @return A View inflated from layoutResId.
605 */
606 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
607 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
608
Joe Onorato5162ea92009-09-03 09:39:42 -0700609 if (info.icon == null) {
610 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
611 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700613 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 info.filtered = true;
615 }
616
617 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
618 favorite.setText(info.title);
619 favorite.setTag(info);
620 favorite.setOnClickListener(this);
621
622 return favorite;
623 }
624
625 /**
626 * Add an application shortcut to the workspace.
627 *
628 * @param data The intent describing the application.
629 * @param cellInfo The position on screen where to create the shortcut.
630 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 cellInfo.screen = mWorkspace.getCurrentScreen();
633 if (!findSingleSlot(cellInfo)) return;
634
Romain Guy73b979d2009-06-09 12:57:21 -0700635 final ApplicationInfo info = infoFromApplicationIntent(context, data);
636 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700638 }
639 }
640
641 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 ComponentName component = data.getComponent();
643 PackageManager packageManager = context.getPackageManager();
644 ActivityInfo activityInfo = null;
645 try {
646 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
647 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700648 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
Romain Guycbb89e42009-06-08 15:52:54 -0700650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 if (activityInfo != null) {
652 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 itemInfo.title = activityInfo.loadLabel(packageManager);
655 if (itemInfo.title == null) {
656 itemInfo.title = activityInfo.name;
657 }
Romain Guycbb89e42009-06-08 15:52:54 -0700658
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
660 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
661 itemInfo.icon = activityInfo.loadIcon(packageManager);
662 itemInfo.container = ItemInfo.NO_ID;
663
Romain Guy73b979d2009-06-09 12:57:21 -0700664 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
Romain Guy73b979d2009-06-09 12:57:21 -0700666
667 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
Romain Guycbb89e42009-06-08 15:52:54 -0700669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 /**
671 * Add a shortcut to the workspace.
672 *
673 * @param data The intent describing the shortcut.
674 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400676 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 cellInfo.screen = mWorkspace.getCurrentScreen();
678 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700679
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
681
682 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
685 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 }
687 }
688
Romain Guycbb89e42009-06-08 15:52:54 -0700689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700693 * @param data The intent describing the appWidgetId.
694 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700698 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700699
Romain Guy18042c82009-11-06 11:44:55 -0800700 if (LOGD) d(LOG_TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700702 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700703
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700704 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 // Try finding open space on Launcher screen
709 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800710 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
711 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
712 return;
713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700715 // Build Launcher-specific widget info and save to database
716 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 launcherInfo.spanX = spans[0];
718 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 LauncherModel.addItemToDatabase(this, launcherInfo,
721 LauncherSettings.Favorites.CONTAINER_DESKTOP,
722 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
723
724 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400725 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700726
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700729
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700730 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 }
736 }
Romain Guycbb89e42009-06-08 15:52:54 -0700737
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
739 mDesktopItems.remove(launcherInfo);
740 launcherInfo.hostView = null;
741 }
742
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700743 public LauncherAppWidgetHost getAppWidgetHost() {
744 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 }
Romain Guycbb89e42009-06-08 15:52:54 -0700746
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 static ApplicationInfo addShortcut(Context context, Intent data,
748 CellLayout.CellInfo cellInfo, boolean notify) {
749
Romain Guy73b979d2009-06-09 12:57:21 -0700750 final ApplicationInfo info = infoFromShortcutIntent(context, data);
751 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
752 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
753
754 return info;
755 }
756
757 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
759 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
760 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
761
762 Drawable icon = null;
763 boolean filtered = false;
764 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700765 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766
767 if (bitmap != null) {
768 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
769 filtered = true;
770 customIcon = true;
771 } else {
772 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700773 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700775 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 final PackageManager packageManager = context.getPackageManager();
777 Resources resources = packageManager.getResourcesForApplication(
778 iconResource.packageName);
779 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
780 icon = resources.getDrawable(id);
781 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700782 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 }
784 }
785 }
786
787 if (icon == null) {
788 icon = context.getPackageManager().getDefaultActivityIcon();
789 }
790
791 final ApplicationInfo info = new ApplicationInfo();
792 info.icon = icon;
793 info.filtered = filtered;
794 info.title = name;
795 info.intent = intent;
796 info.customIcon = customIcon;
797 info.iconResource = iconResource;
798
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 return info;
800 }
801
802 @Override
803 protected void onNewIntent(Intent intent) {
804 super.onNewIntent(intent);
805
806 // Close the menu
807 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
808 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700809
Joe Onorato742966b2009-10-07 10:26:17 -0700810 // Whatever we were doing is hereby canceled.
811 mWaitingForResult = false;
812
Mike LeBeau736cf282009-07-02 17:46:59 -0700813 // Set this flag so that onResume knows to close the search dialog if it's open,
814 // because this was a new intent (thus a press of 'home' or some such) rather than
815 // for example onResume being called when the user pressed the 'back' button.
816 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817
818 try {
819 dismissDialog(DIALOG_CREATE_SHORTCUT);
820 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 } catch (Exception e) {
822 // An exception is thrown if the dialog is not visible, which is fine
823 }
824
825 try {
826 dismissDialog(DIALOG_RENAME_FOLDER);
827 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 } catch (Exception e) {
829 // An exception is thrown if the dialog is not visible, which is fine
830 }
831
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
833 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700834
Romain Guy94dabf12009-07-21 10:55:43 -0700835 if (!mWorkspace.isDefaultScreenShowing()) {
836 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700838
Joe Onorato7bb17492009-09-24 17:51:01 -0700839 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700840
841 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 if (v != null && v.getWindowToken() != null) {
843 InputMethodManager imm = (InputMethodManager)getSystemService(
844 INPUT_METHOD_SERVICE);
845 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
846 }
847 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700848 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 }
850 }
851 }
852
853 @Override
854 protected void onRestoreInstanceState(Bundle savedInstanceState) {
855 // Do not call super here
856 mSavedInstanceState = savedInstanceState;
857 }
858
859 @Override
860 protected void onSaveInstanceState(Bundle outState) {
861 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
862
863 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
864 if (folders.size() > 0) {
865 final int count = folders.size();
866 long[] ids = new long[count];
867 for (int i = 0; i < count; i++) {
868 final FolderInfo info = folders.get(i).getInfo();
869 ids[i] = info.id;
870 }
871 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
872 } else {
873 super.onSaveInstanceState(outState);
874 }
875
Romain Guy3cf604f2009-06-16 13:12:53 -0700876 final boolean isConfigurationChange = getChangingConfigurations() != 0;
877
Romain Guy5a941392009-04-28 15:18:25 -0700878 // When the drawer is opened and we are saving the state because of a
879 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400880 // TODO should not do this if the drawer is currently closing.
881 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700883 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884
885 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
886 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
887 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
888
889 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
890 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
891 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
892 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
893 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
894 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
895 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
896 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
897 layout.getOccupiedCells());
898 }
899
900 if (mFolderInfo != null && mWaitingForResult) {
901 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
902 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
903 }
904 }
905
906 @Override
907 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700909
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700911 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700913 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 }
915
916 TextKeyListener.getInstance().release();
917
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920 unbindDesktopItems();
921 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800922
923 getContentResolver().unregisterContentObserver(mWidgetObserver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 }
925
926 @Override
927 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700928 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 super.startActivityForResult(intent, requestCode);
930 }
931
932 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700933 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700935
Joe Onorato7bb17492009-09-24 17:51:01 -0700936 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700937
Karl Rosaen138a0412009-04-23 19:00:21 -0700938 // Slide the search widget to the top, if it's on the current screen,
939 // otherwise show the search dialog immediately.
940 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
941 if (searchWidget == null) {
942 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
943 } else {
944 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
945 // show the currently typed text in the search widget while sliding
946 searchWidget.setQuery(getTypedText());
947 }
948 }
Romain Guycbb89e42009-06-08 15:52:54 -0700949
Karl Rosaen138a0412009-04-23 19:00:21 -0700950 /**
951 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700952 *
953 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700954 */
Romain Guycbb89e42009-06-08 15:52:54 -0700955 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700956 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700957
Karl Rosaen138a0412009-04-23 19:00:21 -0700958 if (initialQuery == null) {
959 // Use any text typed in the launcher as the initial query
960 initialQuery = getTypedText();
961 clearTypedText();
962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 if (appSearchData == null) {
964 appSearchData = new Bundle();
965 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
966 }
Romain Guycbb89e42009-06-08 15:52:54 -0700967
Karl Rosaen138a0412009-04-23 19:00:21 -0700968 final SearchManager searchManager =
969 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
970
971 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
972 if (searchWidget != null) {
973 // This gets called when the user leaves the search dialog to go back to
974 // the Launcher.
975 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
976 public void onCancel() {
977 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700978 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700979 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700980 });
981 }
Romain Guycbb89e42009-06-08 15:52:54 -0700982
Karl Rosaen138a0412009-04-23 19:00:21 -0700983 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700984 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 }
986
Karl Rosaen138a0412009-04-23 19:00:21 -0700987 /**
988 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700989 */
Romain Guy5a941392009-04-28 15:18:25 -0700990 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700991 // Close search dialog
992 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100993 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700994 // Restore search widget to its normal position
995 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
996 if (searchWidget != null) {
997 searchWidget.stopSearch(false);
998 }
999 }
Romain Guycbb89e42009-06-08 15:52:54 -07001000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 @Override
1002 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 if (isWorkspaceLocked()) {
1004 return false;
1005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006
1007 super.onCreateOptionsMenu(menu);
1008 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1009 .setIcon(android.R.drawable.ic_menu_add)
1010 .setAlphabeticShortcut('A');
1011 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1012 .setIcon(android.R.drawable.ic_menu_gallery)
1013 .setAlphabeticShortcut('W');
1014 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1015 .setIcon(android.R.drawable.ic_search_category_default)
1016 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1017 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1018 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1019 .setAlphabeticShortcut('N');
1020
1021 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001022 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1023 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024
1025 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1026 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1027 .setIntent(settings);
1028
1029 return true;
1030 }
1031
1032 @Override
1033 public boolean onPrepareOptionsMenu(Menu menu) {
1034 super.onPrepareOptionsMenu(menu);
1035
1036 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1037 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1038
1039 return true;
1040 }
1041
1042 @Override
1043 public boolean onOptionsItemSelected(MenuItem item) {
1044 switch (item.getItemId()) {
1045 case MENU_ADD:
1046 addItems();
1047 return true;
1048 case MENU_WALLPAPER_SETTINGS:
1049 startWallpaper();
1050 return true;
1051 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001052 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 return true;
1054 case MENU_NOTIFICATIONS:
1055 showNotifications();
1056 return true;
1057 }
1058
1059 return super.onOptionsItemSelected(item);
1060 }
Romain Guycbb89e42009-06-08 15:52:54 -07001061
Karl Rosaen138a0412009-04-23 19:00:21 -07001062 /**
1063 * Indicates that we want global search for this activity by setting the globalSearch
1064 * argument for {@link #startSearch} to true.
1065 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001067 @Override
1068 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001069 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001070 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001071 }
1072
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073 public boolean isWorkspaceLocked() {
1074 return mWorkspaceLoading || mWaitingForResult;
1075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001078 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 showAddDialog(mMenuAddInfo);
1080 }
1081
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 void addAppWidget(Intent data) {
1083 // TODO: catch bad widget exception when sent
1084 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001086 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1087 if (SEARCH_WIDGET.equals(customWidget)) {
1088 // We don't need this any more, since this isn't a real app widget.
1089 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1090 // add the search widget
1091 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001093 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1094
1095 if (appWidget.configure != null) {
1096 // Launch over to configure widget, if needed
1097 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1098 intent.setComponent(appWidget.configure);
1099 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1100
1101 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1102 } else {
1103 // Otherwise just add it
1104 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 }
1107 }
Romain Guycbb89e42009-06-08 15:52:54 -07001108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 void addSearch() {
1110 final Widget info = Widget.makeSearch();
1111 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 final int[] xy = mCellCoordinates;
1114 final int spanX = info.spanX;
1115 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001118
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1120 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001121
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 final View view = mInflater.inflate(info.layoutResource, null);
1123 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001124 Search search = (Search) view.findViewById(R.id.widget_search);
1125 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001126
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1128 }
1129
Romain Guy73b979d2009-06-09 12:57:21 -07001130 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001131 // Handle case where user selected "Applications"
1132 String applicationName = getResources().getString(R.string.group_applications);
1133 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001134
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001135 if (applicationName != null && applicationName.equals(shortcutName)) {
1136 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1137 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001138
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001139 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1140 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001141 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001142 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001143 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 }
1146
1147 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001148 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001149 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001150 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001152 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001153 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001154 } else {
1155 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 }
1158
Joe Onorato9c1289c2009-08-17 11:03:03 -04001159 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 UserFolderInfo folderInfo = new UserFolderInfo();
1161 folderInfo.title = getText(R.string.folder_name);
1162
1163 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1164 cellInfo.screen = mWorkspace.getCurrentScreen();
1165 if (!findSingleSlot(cellInfo)) return;
1166
1167 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001168 LauncherModel.addItemToDatabase(this, folderInfo,
1169 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001171 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172
1173 // Create the view
1174 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1175 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1176 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001177 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 }
Romain Guycbb89e42009-06-08 15:52:54 -07001179
Joe Onorato9c1289c2009-08-17 11:03:03 -04001180 void removeFolder(FolderInfo folder) {
1181 mFolders.remove(folder.id);
1182 }
1183
1184 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 cellInfo.screen = mWorkspace.getCurrentScreen();
1186 if (!findSingleSlot(cellInfo)) return;
1187
1188 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1189
1190 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1192 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1194 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 }
1196 }
1197
1198 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1199 CellLayout.CellInfo cellInfo, boolean notify) {
1200
1201 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1202 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1203
1204 Drawable icon = null;
1205 boolean filtered = false;
1206 Intent.ShortcutIconResource iconResource = null;
1207
1208 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1209 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1210 try {
1211 iconResource = (Intent.ShortcutIconResource) extra;
1212 final PackageManager packageManager = context.getPackageManager();
1213 Resources resources = packageManager.getResourcesForApplication(
1214 iconResource.packageName);
1215 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1216 icon = resources.getDrawable(id);
1217 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001218 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 }
1220 }
1221
1222 if (icon == null) {
1223 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1224 }
1225
1226 final LiveFolderInfo info = new LiveFolderInfo();
1227 info.icon = icon;
1228 info.filtered = filtered;
1229 info.title = name;
1230 info.iconResource = iconResource;
1231 info.uri = data.getData();
1232 info.baseIntent = baseIntent;
1233 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1234 LiveFolders.DISPLAY_MODE_GRID);
1235
1236 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1237 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239
1240 return info;
1241 }
1242
1243 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1244 final int[] xy = new int[2];
1245 if (findSlot(cellInfo, xy, 1, 1)) {
1246 cellInfo.cellX = xy[0];
1247 cellInfo.cellY = xy[1];
1248 return true;
1249 }
1250 return false;
1251 }
1252
1253 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1254 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1255 boolean[] occupied = mSavedState != null ?
1256 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1257 cellInfo = mWorkspace.findAllVacantCells(occupied);
1258 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1259 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1260 return false;
1261 }
1262 }
1263 return true;
1264 }
1265
1266 private void showNotifications() {
1267 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1268 if (statusBar != null) {
1269 statusBar.expand();
1270 }
1271 }
1272
1273 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001274 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001276 Intent chooser = Intent.createChooser(pickWallpaper,
1277 getText(R.string.chooser_wallpaper));
1278 WallpaperManager wm = (WallpaperManager)
1279 getSystemService(Context.WALLPAPER_SERVICE);
1280 WallpaperInfo wi = wm.getWallpaperInfo();
1281 if (wi != null && wi.getSettingsActivity() != null) {
1282 LabeledIntent li = new LabeledIntent(getPackageName(),
1283 R.string.configure_wallpaper, 0);
1284 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1285 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1286 }
Mike Clerona0618e42009-10-22 13:55:21 -07001287 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 }
1289
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001290 private void registerContentObservers() {
1291 ContentResolver resolver = getContentResolver();
1292 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1293 true, mWidgetObserver);
1294 }
1295
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 @Override
1297 public boolean dispatchKeyEvent(KeyEvent event) {
1298 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1299 switch (event.getKeyCode()) {
1300 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 return true;
1302 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001303 return true;
1304 }
1305 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1306 switch (event.getKeyCode()) {
1307 case KeyEvent.KEYCODE_BACK:
1308 if (!event.isCanceled()) {
1309 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001310 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001311 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001312 } else {
1313 closeFolder();
1314 }
1315 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001316 return true;
1317 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 return true;
1319 }
1320 }
1321
1322 return super.dispatchKeyEvent(event);
1323 }
1324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 private void closeFolder() {
1326 Folder folder = mWorkspace.getOpenFolder();
1327 if (folder != null) {
1328 closeFolder(folder);
1329 }
1330 }
1331
1332 void closeFolder(Folder folder) {
1333 folder.getInfo().opened = false;
1334 ViewGroup parent = (ViewGroup) folder.getParent();
1335 if (parent != null) {
1336 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001337 if (folder instanceof DropTarget) {
1338 // Live folders aren't DropTargets.
1339 mDragController.removeDropTarget((DropTarget)folder);
1340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 }
1342 folder.onClose();
1343 }
1344
1345 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001346 * Re-listen when widgets are reset.
1347 */
1348 private void onAppWidgetReset() {
1349 mAppWidgetHost.startListening();
1350 }
1351
1352 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001353 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1354 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356 private void unbindDesktopItems() {
1357 for (ItemInfo item: mDesktopItems) {
1358 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
1360 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001361
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 /**
1363 * Launches the intent referred by the clicked shortcut.
1364 *
1365 * @param v The view representing the clicked shortcut.
1366 */
1367 public void onClick(View v) {
1368 Object tag = v.getTag();
1369 if (tag instanceof ApplicationInfo) {
1370 // Open shortcut
1371 final Intent intent = ((ApplicationInfo) tag).intent;
1372 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001373 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 } else if (tag instanceof FolderInfo) {
1375 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001376 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001377 Log.d(TAG, "onClick");
1378 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001379 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001380 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001381 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001382 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 }
1384 }
1385
1386 void startActivitySafely(Intent intent) {
1387 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1388 try {
1389 startActivity(intent);
1390 } catch (ActivityNotFoundException e) {
1391 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1392 } catch (SecurityException e) {
1393 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001394 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1396 "or use the exported attribute for this activity.", e);
1397 }
1398 }
1399
1400 private void handleFolderClick(FolderInfo folderInfo) {
1401 if (!folderInfo.opened) {
1402 // Close any open folder
1403 closeFolder();
1404 // Open the requested folder
1405 openFolder(folderInfo);
1406 } else {
1407 // Find the open folder...
1408 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1409 int folderScreen;
1410 if (openFolder != null) {
1411 folderScreen = mWorkspace.getScreenForView(openFolder);
1412 // .. and close it
1413 closeFolder(openFolder);
1414 if (folderScreen != mWorkspace.getCurrentScreen()) {
1415 // Close any folder open on the current screen
1416 closeFolder();
1417 // Pull the folder onto this screen
1418 openFolder(folderInfo);
1419 }
1420 }
1421 }
1422 }
1423
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 /**
1425 * Opens the user fodler described by the specified tag. The opening of the folder
1426 * is animated relative to the specified View. If the View is null, no animation
1427 * is played.
1428 *
1429 * @param folderInfo The FolderInfo describing the folder to open.
1430 */
1431 private void openFolder(FolderInfo folderInfo) {
1432 Folder openFolder;
1433
1434 if (folderInfo instanceof UserFolderInfo) {
1435 openFolder = UserFolder.fromXml(this);
1436 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001437 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 } else {
1439 return;
1440 }
1441
Joe Onorato00acb122009-08-04 16:04:30 -04001442 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 openFolder.setLauncher(this);
1444
1445 openFolder.bind(folderInfo);
1446 folderInfo.opened = true;
1447
1448 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1449 openFolder.onOpen();
1450 }
1451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 return false;
1455 }
1456
1457 if (!(v instanceof CellLayout)) {
1458 v = (View) v.getParent();
1459 }
1460
1461 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1462
1463 // This happens when long clicking an item with the dpad/trackball
1464 if (cellInfo == null) {
1465 return true;
1466 }
1467
1468 if (mWorkspace.allowLongPress()) {
1469 if (cellInfo.cell == null) {
1470 if (cellInfo.valid) {
1471 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001472 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 showAddDialog(cellInfo);
1474 }
1475 } else {
1476 if (!(cellInfo.cell instanceof Folder)) {
1477 // User long pressed on an item
1478 mWorkspace.startDrag(cellInfo);
1479 }
1480 }
1481 }
1482 return true;
1483 }
1484
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001485 View getDrawerHandle() {
1486 return mHandleView;
1487 }
1488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 Workspace getWorkspace() {
1490 return mWorkspace;
1491 }
1492
Joe Onorato7404ee42009-07-31 11:54:44 -07001493 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 GridView getApplicationsGrid() {
1495 return mAllAppsGrid;
1496 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001497 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498
1499 @Override
1500 protected Dialog onCreateDialog(int id) {
1501 switch (id) {
1502 case DIALOG_CREATE_SHORTCUT:
1503 return new CreateShortcut().createDialog();
1504 case DIALOG_RENAME_FOLDER:
1505 return new RenameFolder().createDialog();
1506 }
1507
1508 return super.onCreateDialog(id);
1509 }
1510
1511 @Override
1512 protected void onPrepareDialog(int id, Dialog dialog) {
1513 switch (id) {
1514 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 break;
1516 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001517 if (mFolderInfo != null) {
1518 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1519 final CharSequence text = mFolderInfo.title;
1520 input.setText(text);
1521 input.setSelection(0, text.length());
1522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 break;
1524 }
1525 }
1526
1527 void showRenameDialog(FolderInfo info) {
1528 mFolderInfo = info;
1529 mWaitingForResult = true;
1530 showDialog(DIALOG_RENAME_FOLDER);
1531 }
1532
1533 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1534 mAddItemCellInfo = cellInfo;
1535 mWaitingForResult = true;
1536 showDialog(DIALOG_CREATE_SHORTCUT);
1537 }
1538
Romain Guy73b979d2009-06-09 12:57:21 -07001539 private void pickShortcut(int requestCode, int title) {
1540 Bundle bundle = new Bundle();
1541
1542 ArrayList<String> shortcutNames = new ArrayList<String>();
1543 shortcutNames.add(getString(R.string.group_applications));
1544 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1545
1546 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1547 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1548 R.drawable.ic_launcher_application));
1549 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1550
1551 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1552 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1553 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1554 pickIntent.putExtras(bundle);
1555
1556 startActivityForResult(pickIntent, requestCode);
1557 }
1558
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 private class RenameFolder {
1560 private EditText mInput;
1561
1562 Dialog createDialog() {
1563 mWaitingForResult = true;
1564 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1565 mInput = (EditText) layout.findViewById(R.id.folder_name);
1566
1567 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1568 builder.setIcon(0);
1569 builder.setTitle(getString(R.string.rename_folder_title));
1570 builder.setCancelable(true);
1571 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1572 public void onCancel(DialogInterface dialog) {
1573 cleanup();
1574 }
1575 });
1576 builder.setNegativeButton(getString(R.string.cancel_action),
1577 new Dialog.OnClickListener() {
1578 public void onClick(DialogInterface dialog, int which) {
1579 cleanup();
1580 }
1581 }
1582 );
1583 builder.setPositiveButton(getString(R.string.rename_action),
1584 new Dialog.OnClickListener() {
1585 public void onClick(DialogInterface dialog, int which) {
1586 changeFolderName();
1587 }
1588 }
1589 );
1590 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001591
1592 final AlertDialog dialog = builder.create();
1593 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1594 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001595 mInput.requestFocus();
1596 InputMethodManager inputManager = (InputMethodManager)
1597 getSystemService(Context.INPUT_METHOD_SERVICE);
1598 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001599 }
1600 });
1601
1602 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 }
1604
1605 private void changeFolderName() {
1606 final String name = mInput.getText().toString();
1607 if (!TextUtils.isEmpty(name)) {
1608 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001609 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 mFolderInfo.title = name;
1611 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1612
Joe Onorato9c1289c2009-08-17 11:03:03 -04001613 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001614 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001615 mModel.setWorkspaceDirty();
1616 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 } else {
1618 final FolderIcon folderIcon = (FolderIcon)
1619 mWorkspace.getViewForTag(mFolderInfo);
1620 if (folderIcon != null) {
1621 folderIcon.setText(name);
1622 getWorkspace().requestLayout();
1623 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001624 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001625 mModel.setWorkspaceDirty();
1626 mWorkspaceLoading = true;
1627 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 }
1629 }
1630 }
1631 cleanup();
1632 }
1633
1634 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 dismissDialog(DIALOG_RENAME_FOLDER);
1636 mWaitingForResult = false;
1637 mFolderInfo = null;
1638 }
1639 }
1640
Joe Onoratofb0ca672009-09-14 17:55:46 -04001641 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001642 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001643 }
1644
Jason Samsfd22dac2009-09-20 17:24:16 -07001645 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001646 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001647 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001648
Mike Cleronb6082fa02009-10-19 17:03:36 -07001649 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001650 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001651
Joe Onorato7c312c12009-08-13 21:36:53 -07001652 // TODO: fade these two too
1653 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001654 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001655 }
1656
Joe Onorato7bb17492009-09-24 17:51:01 -07001657 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001658 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001659 mAllAppsGrid.zoom(0.0f);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001660 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001661 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001662
Mike Cleronb6082fa02009-10-19 17:03:36 -07001663
Joe Onorato7c312c12009-08-13 21:36:53 -07001664 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001665 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001666 mDeleteZone.setVisibility(View.VISIBLE);
1667 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001668 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001669 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001670 }
1671
Joe Onorato7c312c12009-08-13 21:36:53 -07001672 void lockAllApps() {
1673 // TODO
1674 }
1675
1676 void unlockAllApps() {
1677 // TODO
1678 }
1679
Joe Onorato7404ee42009-07-31 11:54:44 -07001680 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 * Displays the shortcut creation dialog and launches, if necessary, the
1682 * appropriate activity.
1683 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001684 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001685 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1686 DialogInterface.OnShowListener {
1687
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001689
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 Dialog createDialog() {
1691 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001692
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1696 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001697 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001698
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 builder.setInverseBackgroundForced(true);
1700
1701 AlertDialog dialog = builder.create();
1702 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001703 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001704 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 return dialog;
1707 }
1708
1709 public void onCancel(DialogInterface dialog) {
1710 mWaitingForResult = false;
1711 cleanup();
1712 }
1713
Romain Guycbb89e42009-06-08 15:52:54 -07001714 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001715 }
1716
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 dismissDialog(DIALOG_CREATE_SHORTCUT);
1719 }
1720
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001721 /**
1722 * Handle the action clicked in the "Add to home" dialog.
1723 */
1724 public void onClick(DialogInterface dialog, int which) {
1725 Resources res = getResources();
1726 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001727
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001728 switch (which) {
1729 case AddAdapter.ITEM_SHORTCUT: {
1730 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001731 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001732 break;
1733 }
Romain Guycbb89e42009-06-08 15:52:54 -07001734
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001735 case AddAdapter.ITEM_APPWIDGET: {
1736 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001737
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001738 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1739 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1740 // add the search widget
1741 ArrayList<AppWidgetProviderInfo> customInfo =
1742 new ArrayList<AppWidgetProviderInfo>();
1743 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1744 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1745 info.label = getString(R.string.group_search);
1746 info.icon = R.drawable.ic_search_widget;
1747 customInfo.add(info);
1748 pickIntent.putParcelableArrayListExtra(
1749 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1750 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1751 Bundle b = new Bundle();
1752 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1753 customExtras.add(b);
1754 pickIntent.putParcelableArrayListExtra(
1755 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1756 // start the pick activity
1757 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1758 break;
1759 }
Romain Guycbb89e42009-06-08 15:52:54 -07001760
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001761 case AddAdapter.ITEM_LIVE_FOLDER: {
1762 // Insert extra item to handle inserting folder
1763 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001764
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001765 ArrayList<String> shortcutNames = new ArrayList<String>();
1766 shortcutNames.add(res.getString(R.string.group_folder));
1767 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001768
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001769 ArrayList<ShortcutIconResource> shortcutIcons =
1770 new ArrayList<ShortcutIconResource>();
1771 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1772 R.drawable.ic_launcher_folder));
1773 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1774
1775 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1776 pickIntent.putExtra(Intent.EXTRA_INTENT,
1777 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1778 pickIntent.putExtra(Intent.EXTRA_TITLE,
1779 getText(R.string.title_select_live_folder));
1780 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001781
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001782 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1783 break;
1784 }
1785
1786 case AddAdapter.ITEM_WALLPAPER: {
1787 startWallpaper();
1788 break;
1789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
1791 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001792
1793 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001794 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 }
1796
1797 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001798 * Receives notifications whenever the appwidgets are reset.
1799 */
1800 private class AppWidgetResetObserver extends ContentObserver {
1801 public AppWidgetResetObserver() {
1802 super(new Handler());
1803 }
1804
1805 @Override
1806 public void onChange(boolean selfChange) {
1807 onAppWidgetReset();
1808 }
1809 }
1810
1811 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001812 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001814 public int getCurrentWorkspaceScreen() {
1815 return mWorkspace.getCurrentScreen();
1816 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001817
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818 /**
1819 * Refreshes the shortcuts shown on the workspace.
1820 *
1821 * Implementation of the method from LauncherModel.Callbacks.
1822 */
1823 public void startBinding() {
1824 final Workspace workspace = mWorkspace;
1825 int count = workspace.getChildCount();
1826 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001827 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001828 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1829 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001830
Joe Onorato9c1289c2009-08-17 11:03:03 -04001831 if (DEBUG_USER_INTERFACE) {
1832 android.widget.Button finishButton = new android.widget.Button(this);
1833 finishButton.setText("Finish");
1834 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1835
1836 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1837 public void onClick(View v) {
1838 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001839 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 }
1842 }
1843
1844 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001845 * Bind the items start-end from the list.
1846 *
1847 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1850
1851 final Workspace workspace = mWorkspace;
1852
1853 for (int i=start; i<end; i++) {
1854 final ItemInfo item = shortcuts.get(i);
1855 mDesktopItems.add(item);
1856 switch (item.itemType) {
1857 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1858 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1859 final View shortcut = createShortcut((ApplicationInfo) item);
1860 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1861 false);
1862 break;
1863 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1864 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1865 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1866 (UserFolderInfo) item);
1867 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1868 false);
1869 break;
1870 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1871 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1872 R.layout.live_folder_icon, this,
1873 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1874 (LiveFolderInfo) item);
1875 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1876 false);
1877 break;
1878 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1879 final int screen = workspace.getCurrentScreen();
1880 final View view = mInflater.inflate(R.layout.widget_search,
1881 (ViewGroup) workspace.getChildAt(screen), false);
1882
1883 Search search = (Search) view.findViewById(R.id.widget_search);
1884 search.setLauncher(this);
1885
1886 final Widget widget = (Widget) item;
1887 view.setTag(widget);
1888
1889 workspace.addWidget(view, widget, false);
1890 break;
1891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 }
1893
Joe Onorato9c1289c2009-08-17 11:03:03 -04001894 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 }
1896
Joe Onorato9c1289c2009-08-17 11:03:03 -04001897 /**
1898 * Implementation of the method from LauncherModel.Callbacks.
1899 */
Joe Onoratoad72e172009-11-06 16:25:04 -05001900 public void bindFolders(HashMap<Long, FolderInfo> folders) {
1901 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001902 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001904
1905 /**
1906 * Add the views for a widget to the workspace.
1907 *
1908 * Implementation of the method from LauncherModel.Callbacks.
1909 */
1910 public void bindAppWidget(LauncherAppWidgetInfo item) {
1911 final Workspace workspace = mWorkspace;
1912
1913 final int appWidgetId = item.appWidgetId;
1914 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1915 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1916
Joe Onorato9c1289c2009-08-17 11:03:03 -04001917 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1918 item.hostView.setTag(item);
1919
1920 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1921 item.cellY, item.spanX, item.spanY, false);
1922
1923 workspace.requestLayout();
1924
1925 mDesktopItems.add(item);
1926 }
1927
1928 /**
1929 * Callback saying that there aren't any more items to bind.
1930 *
1931 * Implementation of the method from LauncherModel.Callbacks.
1932 */
1933 public void finishBindingItems() {
1934 if (mSavedState != null) {
1935 if (!mWorkspace.hasFocus()) {
1936 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1937 }
1938
1939 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1940 if (userFolders != null) {
1941 for (long folderId : userFolders) {
1942 final FolderInfo info = mFolders.get(folderId);
1943 if (info != null) {
1944 openFolder(info);
1945 }
1946 }
1947 final Folder openFolder = mWorkspace.getOpenFolder();
1948 if (openFolder != null) {
1949 openFolder.requestFocus();
1950 }
1951 }
1952
1953 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1954 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001955 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001956 }
1957
1958 mSavedState = null;
1959 }
1960
1961 if (mSavedInstanceState != null) {
1962 super.onRestoreInstanceState(mSavedInstanceState);
1963 mSavedInstanceState = null;
1964 }
1965
Joe Onorato9c1289c2009-08-17 11:03:03 -04001966 Log.d(TAG, "finishBindingItems done");
1967 mWorkspaceLoading = false;
1968 }
1969
1970 /**
1971 * Add the icons for all apps.
1972 *
1973 * Implementation of the method from LauncherModel.Callbacks.
1974 */
1975 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08001976 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977 }
1978
1979 /**
1980 * A package was installed.
1981 *
1982 * Implementation of the method from LauncherModel.Callbacks.
1983 */
1984 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1985 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001986 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001987 }
1988
1989 /**
1990 * A package was updated.
1991 *
1992 * Implementation of the method from LauncherModel.Callbacks.
1993 */
1994 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1995 removeDialog(DIALOG_CREATE_SHORTCUT);
1996 mWorkspace.updateShortcutsForPackage(packageName);
1997 }
1998
1999 /**
2000 * A package was uninstalled.
2001 *
2002 * Implementation of the method from LauncherModel.Callbacks.
2003 */
2004 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2005 removeDialog(DIALOG_CREATE_SHORTCUT);
2006 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002007 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009}