blob: 533222d1048905f7525bbfaf6b5d0b05abe28df0 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
20import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070022import android.app.ISearchManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.SearchManager;
24import android.app.StatusBarManager;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070025import android.app.WallpaperInfo;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
30import android.content.DialogInterface;
31import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070032import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ActivityInfo;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070034import android.content.pm.LabeledIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.PackageManager;
36import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070038import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.graphics.drawable.Drawable;
41import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Bundle;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070043import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070044import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070045import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.text.Selection;
51import android.text.SpannableStringBuilder;
52import android.text.TextUtils;
53import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070054import static android.util.Log.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070055import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.KeyEvent;
58import android.view.LayoutInflater;
59import android.view.Menu;
60import android.view.MenuItem;
61import android.view.View;
62import android.view.ViewGroup;
Romain Guyb1b69f52009-08-10 15:10:15 -070063import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.view.View.OnLongClickListener;
65import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.widget.TextView;
68import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070069import android.appwidget.AppWidgetManager;
70import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071
72import java.lang.ref.WeakReference;
73import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074import java.util.HashMap;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070075import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070076import java.io.DataOutputStream;
77import java.io.FileNotFoundException;
78import java.io.IOException;
79import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
81/**
82 * Default launcher application.
83 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040084public final class Launcher extends Activity
Joe Onorato7bb17492009-09-24 17:51:01 -070085 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040087 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 static final boolean LOGD = false;
89
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 static final boolean PROFILE_STARTUP = false;
91 static final boolean PROFILE_ROTATE = false;
92 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 private static final int WALLPAPER_SCREENS_SPAN = 2;
95
96 private static final int MENU_GROUP_ADD = 1;
97 private static final int MENU_ADD = Menu.FIRST + 1;
98 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
99 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
100 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700101 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
103 private static final int REQUEST_CREATE_SHORTCUT = 1;
104 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700105 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 private static final int REQUEST_PICK_APPLICATION = 6;
107 private static final int REQUEST_PICK_SHORTCUT = 7;
108 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700109 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700110 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
112 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
113
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700114 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
115 static final String SEARCH_WIDGET = "search_widget";
116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 static final int SCREEN_COUNT = 3;
118 static final int DEFAULT_SCREN = 1;
119 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700120 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Joe Onorato7c312c12009-08-13 21:36:53 -0700122 static final int DIALOG_CREATE_SHORTCUT = 1;
123 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Romain Guy98d01652009-06-30 16:21:04 -0700125 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 // Type: int
128 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
129 // Type: boolean
130 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
131 // Type: long
132 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
133 // Type: int
134 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
147 // Type: int[]
148 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
149 // Type: boolean
150 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
151 // Type: long
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
153
Joe Onorato9c1289c2009-08-17 11:03:03 -0400154 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 private static final Object sLock = new Object();
157 private static int sScreen = DEFAULT_SCREN;
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;
186 private boolean mLocaleChanged;
Joe Onoratoff3862d2009-10-07 10:35:51 -0700187 private boolean mExitingBecauseOfLaunch;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
Dianne Hackborn67800862009-07-24 17:15:20 -0700189 private boolean mHomeDown;
190 private boolean mBackDown;
Jason Samsfd22dac2009-09-20 17:24:16 -0700191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private Bundle mSavedInstanceState;
193
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 private LauncherModel mModel;
195
196 private ArrayList<ItemInfo> mDesktopItems = new ArrayList();
197 private static HashMap<Long, FolderInfo> mFolders = new HashMap();
198 private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList();
Romain Guycbb89e42009-06-08 15:52:54 -0700199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 @Override
201 protected void onCreate(Bundle savedInstanceState) {
202 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400205 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700207
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700209
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
211 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700212
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 if (PROFILE_STARTUP) {
214 android.os.Debug.startMethodTracing("/sdcard/launcher");
215 }
216
217 checkForLocaleChange();
218 setWallpaperDimension();
219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 setContentView(R.layout.launcher);
221 setupViews();
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
261 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
262
263 if (mLocaleChanged) {
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 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700385 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700387 // Clean up the appWidgetId if we canceled
388 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
389 if (appWidgetId != -1) {
390 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 }
392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
394
395 @Override
396 protected void onResume() {
397 super.onResume();
398
399 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400400 mWorkspaceLoading = true;
401 mModel.startLoader(this, true);
402 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700404
Mike LeBeau736cf282009-07-02 17:46:59 -0700405 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
406 // onNewIntent), then close the search dialog if needed, because it probably
407 // came from the user pressing 'home' (rather than, for example, pressing 'back').
408 if (mIsNewIntent) {
409 // Post to a handler so that this happens after the search dialog tries to open
410 // itself again.
411 mWorkspace.post(new Runnable() {
412 public void run() {
413 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
414 ServiceManager.getService(Context.SEARCH_SERVICE));
415 try {
416 searchManagerService.stopSearch();
417 } catch (RemoteException e) {
418 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700419 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700420 }
421 });
422 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700423
Mike LeBeau736cf282009-07-02 17:46:59 -0700424 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 }
426
427 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700428 protected void onPause() {
429 super.onPause();
Joe Onoratoff3862d2009-10-07 10:35:51 -0700430 if (mExitingBecauseOfLaunch) {
431 mExitingBecauseOfLaunch = false;
432 closeAllApps(false);
433 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700434 }
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700436 @Override
437 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400438 // Flag the loader to stop early before switching
439 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700440
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700441 if (PROFILE_ROTATE) {
442 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
443 }
444 return null;
445 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 private boolean acceptFilter() {
448 final InputMethodManager inputManager = (InputMethodManager)
449 getSystemService(Context.INPUT_METHOD_SERVICE);
450 return !inputManager.isFullscreenMode();
451 }
452
453 @Override
454 public boolean onKeyDown(int keyCode, KeyEvent event) {
455 boolean handled = super.onKeyDown(keyCode, event);
456 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
457 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
458 keyCode, event);
459 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700460 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700461 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700462 // showSearchDialog()
463 // If there are multiple keystrokes before the search dialog takes focus,
464 // onSearchRequested() will be called for every keystroke,
465 // but it is idempotent, so it's fine.
466 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468 }
469
470 return handled;
471 }
472
Karl Rosaen138a0412009-04-23 19:00:21 -0700473 private String getTypedText() {
474 return mDefaultKeySsb.toString();
475 }
476
477 private void clearTypedText() {
478 mDefaultKeySsb.clear();
479 mDefaultKeySsb.clearSpans();
480 Selection.setSelection(mDefaultKeySsb, 0);
481 }
482
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 /**
484 * Restores the previous state, if it exists.
485 *
486 * @param savedState The previous state.
487 */
488 private void restoreState(Bundle savedState) {
489 if (savedState == null) {
490 return;
491 }
492
493 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
494 if (currentScreen > -1) {
495 mWorkspace.setCurrentScreen(currentScreen);
496 }
497
498 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
499 if (addScreen > -1) {
500 mAddItemCellInfo = new CellLayout.CellInfo();
501 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
502 addItemCellInfo.valid = true;
503 addItemCellInfo.screen = addScreen;
504 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
505 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
506 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
507 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
508 addItemCellInfo.findVacantCellsFromOccupied(
509 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
510 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
511 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
512 mRestoring = true;
513 }
514
515 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
516 if (renameFolder) {
517 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400518 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800519 mRestoring = true;
520 }
521 }
522
523 /**
524 * Finds all the views we need and configure them properly.
525 */
526 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400527 DragController dragController = mDragController;
528
Romain Guyb1b69f52009-08-10 15:10:15 -0700529 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400530 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531
Joe Onorato7c312c12009-08-13 21:36:53 -0700532 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700533 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700534 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700535 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700536 // Manage focusability manually since this thing is always visible
537 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700538
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
540 final Workspace workspace = mWorkspace;
541
Joe Onorato7c312c12009-08-13 21:36:53 -0700542 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
543 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544
Joe Onorato7c312c12009-08-13 21:36:53 -0700545 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700546 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700547 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700548 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700549 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700550 handleIocon.setCrossFadeEnabled(true);
551 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400554 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556
557 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400558 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700559 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560
Joe Onorato00acb122009-08-04 16:04:30 -0400561 dragController.setDragScoller(workspace);
562 dragController.setDragListener(deleteZone);
563 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700564
Joe Onorato00acb122009-08-04 16:04:30 -0400565 // The order here is bottom to top.
566 dragController.addDropTarget(workspace);
567 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 }
569
570 /**
571 * Creates a view representing a shortcut.
572 *
573 * @param info The data structure describing the shortcut.
574 *
575 * @return A View inflated from R.layout.application.
576 */
577 View createShortcut(ApplicationInfo info) {
578 return createShortcut(R.layout.application,
579 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
580 }
581
582 /**
583 * Creates a view representing a shortcut inflated from the specified resource.
584 *
585 * @param layoutResId The id of the XML layout used to create the shortcut.
586 * @param parent The group the shortcut belongs to.
587 * @param info The data structure describing the shortcut.
588 *
589 * @return A View inflated from layoutResId.
590 */
591 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
592 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
593
Joe Onorato5162ea92009-09-03 09:39:42 -0700594 if (info.icon == null) {
595 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700598 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 info.filtered = true;
600 }
601
602 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
603 favorite.setText(info.title);
604 favorite.setTag(info);
605 favorite.setOnClickListener(this);
606
607 return favorite;
608 }
609
610 /**
611 * Add an application shortcut to the workspace.
612 *
613 * @param data The intent describing the application.
614 * @param cellInfo The position on screen where to create the shortcut.
615 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 cellInfo.screen = mWorkspace.getCurrentScreen();
618 if (!findSingleSlot(cellInfo)) return;
619
Romain Guy73b979d2009-06-09 12:57:21 -0700620 final ApplicationInfo info = infoFromApplicationIntent(context, data);
621 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400622 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700623 }
624 }
625
626 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 ComponentName component = data.getComponent();
628 PackageManager packageManager = context.getPackageManager();
629 ActivityInfo activityInfo = null;
630 try {
631 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
632 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700633 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 }
Romain Guycbb89e42009-06-08 15:52:54 -0700635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 if (activityInfo != null) {
637 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700638
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 itemInfo.title = activityInfo.loadLabel(packageManager);
640 if (itemInfo.title == null) {
641 itemInfo.title = activityInfo.name;
642 }
Romain Guycbb89e42009-06-08 15:52:54 -0700643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
645 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
646 itemInfo.icon = activityInfo.loadIcon(packageManager);
647 itemInfo.container = ItemInfo.NO_ID;
648
Romain Guy73b979d2009-06-09 12:57:21 -0700649 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
Romain Guy73b979d2009-06-09 12:57:21 -0700651
652 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 }
Romain Guycbb89e42009-06-08 15:52:54 -0700654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 /**
656 * Add a shortcut to the workspace.
657 *
658 * @param data The intent describing the shortcut.
659 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 cellInfo.screen = mWorkspace.getCurrentScreen();
663 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
666
667 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
670 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
672 }
673
Romain Guycbb89e42009-06-08 15:52:54 -0700674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700678 * @param data The intent describing the appWidgetId.
679 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400681 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700684
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700685 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700686
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700688
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700689 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 // Try finding open space on Launcher screen
694 final int[] xy = mCellCoordinates;
695 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
696
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 // Build Launcher-specific widget info and save to database
698 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 launcherInfo.spanX = spans[0];
700 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 LauncherModel.addItemToDatabase(this, launcherInfo,
703 LauncherSettings.Favorites.CONTAINER_DESKTOP,
704 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
705
706 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700710 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700711
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 }
718 }
Romain Guycbb89e42009-06-08 15:52:54 -0700719
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
721 mDesktopItems.remove(launcherInfo);
722 launcherInfo.hostView = null;
723 }
724
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700725 public LauncherAppWidgetHost getAppWidgetHost() {
726 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 }
Romain Guycbb89e42009-06-08 15:52:54 -0700728
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 static ApplicationInfo addShortcut(Context context, Intent data,
730 CellLayout.CellInfo cellInfo, boolean notify) {
731
Romain Guy73b979d2009-06-09 12:57:21 -0700732 final ApplicationInfo info = infoFromShortcutIntent(context, data);
733 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
734 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
735
736 return info;
737 }
738
739 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
741 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
742 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
743
744 Drawable icon = null;
745 boolean filtered = false;
746 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700747 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748
749 if (bitmap != null) {
750 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
751 filtered = true;
752 customIcon = true;
753 } else {
754 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700755 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700757 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 final PackageManager packageManager = context.getPackageManager();
759 Resources resources = packageManager.getResourcesForApplication(
760 iconResource.packageName);
761 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
762 icon = resources.getDrawable(id);
763 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700764 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 }
766 }
767 }
768
769 if (icon == null) {
770 icon = context.getPackageManager().getDefaultActivityIcon();
771 }
772
773 final ApplicationInfo info = new ApplicationInfo();
774 info.icon = icon;
775 info.filtered = filtered;
776 info.title = name;
777 info.intent = intent;
778 info.customIcon = customIcon;
779 info.iconResource = iconResource;
780
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 return info;
782 }
783
784 @Override
785 protected void onNewIntent(Intent intent) {
786 super.onNewIntent(intent);
787
788 // Close the menu
789 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
790 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700791
Joe Onorato742966b2009-10-07 10:26:17 -0700792 // Whatever we were doing is hereby canceled.
793 mWaitingForResult = false;
794
Mike LeBeau736cf282009-07-02 17:46:59 -0700795 // Set this flag so that onResume knows to close the search dialog if it's open,
796 // because this was a new intent (thus a press of 'home' or some such) rather than
797 // for example onResume being called when the user pressed the 'back' button.
798 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799
800 try {
801 dismissDialog(DIALOG_CREATE_SHORTCUT);
802 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 } catch (Exception e) {
804 // An exception is thrown if the dialog is not visible, which is fine
805 }
806
807 try {
808 dismissDialog(DIALOG_RENAME_FOLDER);
809 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 } catch (Exception e) {
811 // An exception is thrown if the dialog is not visible, which is fine
812 }
813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
815 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700816
Romain Guy94dabf12009-07-21 10:55:43 -0700817 if (!mWorkspace.isDefaultScreenShowing()) {
818 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700820
Joe Onorato7bb17492009-09-24 17:51:01 -0700821 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700822
823 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 if (v != null && v.getWindowToken() != null) {
825 InputMethodManager imm = (InputMethodManager)getSystemService(
826 INPUT_METHOD_SERVICE);
827 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
828 }
829 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700830 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
832 }
833 }
834
835 @Override
836 protected void onRestoreInstanceState(Bundle savedInstanceState) {
837 // Do not call super here
838 mSavedInstanceState = savedInstanceState;
839 }
840
841 @Override
842 protected void onSaveInstanceState(Bundle outState) {
843 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
844
845 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
846 if (folders.size() > 0) {
847 final int count = folders.size();
848 long[] ids = new long[count];
849 for (int i = 0; i < count; i++) {
850 final FolderInfo info = folders.get(i).getInfo();
851 ids[i] = info.id;
852 }
853 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
854 } else {
855 super.onSaveInstanceState(outState);
856 }
857
Romain Guy3cf604f2009-06-16 13:12:53 -0700858 final boolean isConfigurationChange = getChangingConfigurations() != 0;
859
Romain Guy5a941392009-04-28 15:18:25 -0700860 // When the drawer is opened and we are saving the state because of a
861 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400862 // TODO should not do this if the drawer is currently closing.
863 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700865 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866
867 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
868 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
869 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
870
871 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
872 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
873 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
874 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
875 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
876 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
877 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
878 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
879 layout.getOccupiedCells());
880 }
881
882 if (mFolderInfo != null && mWaitingForResult) {
883 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
884 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
885 }
886 }
887
888 @Override
889 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700891
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700893 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700895 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 }
897
898 TextKeyListener.getInstance().release();
899
Joe Onorato9c1289c2009-08-17 11:03:03 -0400900 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902 unbindDesktopItems();
903 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905
906 @Override
907 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700908 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 super.startActivityForResult(intent, requestCode);
910 }
911
912 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700913 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700915
Joe Onorato7bb17492009-09-24 17:51:01 -0700916 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700917
Karl Rosaen138a0412009-04-23 19:00:21 -0700918 // Slide the search widget to the top, if it's on the current screen,
919 // otherwise show the search dialog immediately.
920 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
921 if (searchWidget == null) {
922 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
923 } else {
924 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
925 // show the currently typed text in the search widget while sliding
926 searchWidget.setQuery(getTypedText());
927 }
928 }
Romain Guycbb89e42009-06-08 15:52:54 -0700929
Karl Rosaen138a0412009-04-23 19:00:21 -0700930 /**
931 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700932 *
933 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700934 */
Romain Guycbb89e42009-06-08 15:52:54 -0700935 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700936 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700937
Karl Rosaen138a0412009-04-23 19:00:21 -0700938 if (initialQuery == null) {
939 // Use any text typed in the launcher as the initial query
940 initialQuery = getTypedText();
941 clearTypedText();
942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 if (appSearchData == null) {
944 appSearchData = new Bundle();
945 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
946 }
Romain Guycbb89e42009-06-08 15:52:54 -0700947
Karl Rosaen138a0412009-04-23 19:00:21 -0700948 final SearchManager searchManager =
949 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
950
951 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
952 if (searchWidget != null) {
953 // This gets called when the user leaves the search dialog to go back to
954 // the Launcher.
955 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
956 public void onCancel() {
957 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700958 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700959 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700960 });
961 }
Romain Guycbb89e42009-06-08 15:52:54 -0700962
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700964 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 }
966
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 /**
968 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 */
Romain Guy5a941392009-04-28 15:18:25 -0700970 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 // Close search dialog
972 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100973 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700974 // Restore search widget to its normal position
975 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
976 if (searchWidget != null) {
977 searchWidget.stopSearch(false);
978 }
979 }
Romain Guycbb89e42009-06-08 15:52:54 -0700980
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 @Override
982 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 if (isWorkspaceLocked()) {
984 return false;
985 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986
987 super.onCreateOptionsMenu(menu);
988 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
989 .setIcon(android.R.drawable.ic_menu_add)
990 .setAlphabeticShortcut('A');
991 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
992 .setIcon(android.R.drawable.ic_menu_gallery)
993 .setAlphabeticShortcut('W');
994 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
995 .setIcon(android.R.drawable.ic_search_category_default)
996 .setAlphabeticShortcut(SearchManager.MENU_KEY);
997 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
998 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
999 .setAlphabeticShortcut('N');
1000
1001 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001002 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1003 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004
1005 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1006 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1007 .setIntent(settings);
1008
1009 return true;
1010 }
1011
1012 @Override
1013 public boolean onPrepareOptionsMenu(Menu menu) {
1014 super.onPrepareOptionsMenu(menu);
1015
1016 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1017 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1018
1019 return true;
1020 }
1021
1022 @Override
1023 public boolean onOptionsItemSelected(MenuItem item) {
1024 switch (item.getItemId()) {
1025 case MENU_ADD:
1026 addItems();
1027 return true;
1028 case MENU_WALLPAPER_SETTINGS:
1029 startWallpaper();
1030 return true;
1031 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001032 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 return true;
1034 case MENU_NOTIFICATIONS:
1035 showNotifications();
1036 return true;
1037 }
1038
1039 return super.onOptionsItemSelected(item);
1040 }
Romain Guycbb89e42009-06-08 15:52:54 -07001041
Karl Rosaen138a0412009-04-23 19:00:21 -07001042 /**
1043 * Indicates that we want global search for this activity by setting the globalSearch
1044 * argument for {@link #startSearch} to true.
1045 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001047 @Override
1048 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001049 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001050 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001051 }
1052
Joe Onorato9c1289c2009-08-17 11:03:03 -04001053 public boolean isWorkspaceLocked() {
1054 return mWorkspaceLoading || mWaitingForResult;
1055 }
1056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 private void addItems() {
1058 showAddDialog(mMenuAddInfo);
1059 }
1060
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001061 void addAppWidget(Intent data) {
1062 // TODO: catch bad widget exception when sent
1063 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001065 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1066 if (SEARCH_WIDGET.equals(customWidget)) {
1067 // We don't need this any more, since this isn't a real app widget.
1068 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1069 // add the search widget
1070 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001072 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1073
1074 if (appWidget.configure != null) {
1075 // Launch over to configure widget, if needed
1076 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1077 intent.setComponent(appWidget.configure);
1078 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1079
1080 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1081 } else {
1082 // Otherwise just add it
1083 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 }
1086 }
Romain Guycbb89e42009-06-08 15:52:54 -07001087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 void addSearch() {
1089 final Widget info = Widget.makeSearch();
1090 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 final int[] xy = mCellCoordinates;
1093 final int spanX = info.spanX;
1094 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1099 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 final View view = mInflater.inflate(info.layoutResource, null);
1102 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001103 Search search = (Search) view.findViewById(R.id.widget_search);
1104 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1107 }
1108
Romain Guy73b979d2009-06-09 12:57:21 -07001109 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001110 // Handle case where user selected "Applications"
1111 String applicationName = getResources().getString(R.string.group_applications);
1112 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001113
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001114 if (applicationName != null && applicationName.equals(shortcutName)) {
1115 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1116 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001117
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001118 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1119 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001120 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001121 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001122 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 }
1125
1126 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001127 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001128 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001129 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001130
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001131 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001133 } else {
1134 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 }
1137
Joe Onorato9c1289c2009-08-17 11:03:03 -04001138 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 UserFolderInfo folderInfo = new UserFolderInfo();
1140 folderInfo.title = getText(R.string.folder_name);
1141
1142 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1143 cellInfo.screen = mWorkspace.getCurrentScreen();
1144 if (!findSingleSlot(cellInfo)) return;
1145
1146 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001147 LauncherModel.addItemToDatabase(this, folderInfo,
1148 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001150 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151
1152 // Create the view
1153 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1154 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1155 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001156 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 }
Romain Guycbb89e42009-06-08 15:52:54 -07001158
Joe Onorato9c1289c2009-08-17 11:03:03 -04001159 void removeFolder(FolderInfo folder) {
1160 mFolders.remove(folder.id);
1161 }
1162
1163 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 cellInfo.screen = mWorkspace.getCurrentScreen();
1165 if (!findSingleSlot(cellInfo)) return;
1166
1167 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1168
1169 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1171 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001172 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1173 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
1175 }
1176
1177 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1178 CellLayout.CellInfo cellInfo, boolean notify) {
1179
1180 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1181 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1182
1183 Drawable icon = null;
1184 boolean filtered = false;
1185 Intent.ShortcutIconResource iconResource = null;
1186
1187 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1188 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1189 try {
1190 iconResource = (Intent.ShortcutIconResource) extra;
1191 final PackageManager packageManager = context.getPackageManager();
1192 Resources resources = packageManager.getResourcesForApplication(
1193 iconResource.packageName);
1194 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1195 icon = resources.getDrawable(id);
1196 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001197 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 }
1199 }
1200
1201 if (icon == null) {
1202 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1203 }
1204
1205 final LiveFolderInfo info = new LiveFolderInfo();
1206 info.icon = icon;
1207 info.filtered = filtered;
1208 info.title = name;
1209 info.iconResource = iconResource;
1210 info.uri = data.getData();
1211 info.baseIntent = baseIntent;
1212 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1213 LiveFolders.DISPLAY_MODE_GRID);
1214
1215 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1216 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218
1219 return info;
1220 }
1221
1222 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1223 final int[] xy = new int[2];
1224 if (findSlot(cellInfo, xy, 1, 1)) {
1225 cellInfo.cellX = xy[0];
1226 cellInfo.cellY = xy[1];
1227 return true;
1228 }
1229 return false;
1230 }
1231
1232 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1233 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1234 boolean[] occupied = mSavedState != null ?
1235 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1236 cellInfo = mWorkspace.findAllVacantCells(occupied);
1237 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1238 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1239 return false;
1240 }
1241 }
1242 return true;
1243 }
1244
1245 private void showNotifications() {
1246 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1247 if (statusBar != null) {
1248 statusBar.expand();
1249 }
1250 }
1251
1252 private void startWallpaper() {
1253 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001254 Intent chooser = Intent.createChooser(pickWallpaper,
1255 getText(R.string.chooser_wallpaper));
1256 WallpaperManager wm = (WallpaperManager)
1257 getSystemService(Context.WALLPAPER_SERVICE);
1258 WallpaperInfo wi = wm.getWallpaperInfo();
1259 if (wi != null && wi.getSettingsActivity() != null) {
1260 LabeledIntent li = new LabeledIntent(getPackageName(),
1261 R.string.configure_wallpaper, 0);
1262 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1263 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1264 }
Mike Clerona0618e42009-10-22 13:55:21 -07001265 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001269 public void onWindowFocusChanged(boolean hasFocus) {
1270 super.onWindowFocusChanged(hasFocus);
1271 if (!hasFocus) {
1272 mBackDown = mHomeDown = false;
1273 }
1274 }
1275
1276 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 public boolean dispatchKeyEvent(KeyEvent event) {
1278 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1279 switch (event.getKeyCode()) {
1280 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001281 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 return true;
1283 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001284 mHomeDown = true;
1285 return true;
1286 }
1287 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1288 switch (event.getKeyCode()) {
1289 case KeyEvent.KEYCODE_BACK:
1290 if (!event.isCanceled()) {
1291 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001292 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001293 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001294 } else {
1295 closeFolder();
1296 }
1297 }
1298 mBackDown = false;
1299 return true;
1300 case KeyEvent.KEYCODE_HOME:
1301 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 return true;
1303 }
1304 }
1305
1306 return super.dispatchKeyEvent(event);
1307 }
1308
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 private void closeFolder() {
1310 Folder folder = mWorkspace.getOpenFolder();
1311 if (folder != null) {
1312 closeFolder(folder);
1313 }
1314 }
1315
1316 void closeFolder(Folder folder) {
1317 folder.getInfo().opened = false;
1318 ViewGroup parent = (ViewGroup) folder.getParent();
1319 if (parent != null) {
1320 parent.removeView(folder);
Joe Onorato41a12d22009-10-31 18:30:00 -04001321 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
1323 folder.onClose();
1324 }
1325
1326 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001327 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1328 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001330 private void unbindDesktopItems() {
1331 for (ItemInfo item: mDesktopItems) {
1332 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 /**
1337 * Launches the intent referred by the clicked shortcut.
1338 *
1339 * @param v The view representing the clicked shortcut.
1340 */
1341 public void onClick(View v) {
1342 Object tag = v.getTag();
1343 if (tag instanceof ApplicationInfo) {
1344 // Open shortcut
1345 final Intent intent = ((ApplicationInfo) tag).intent;
1346 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001347 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 } else if (tag instanceof FolderInfo) {
1349 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001350 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001351 Log.d(TAG, "onClick");
1352 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001353 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001354 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001355 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001356 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
1358 }
1359
1360 void startActivitySafely(Intent intent) {
1361 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1362 try {
1363 startActivity(intent);
1364 } catch (ActivityNotFoundException e) {
1365 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1366 } catch (SecurityException e) {
1367 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001368 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1370 "or use the exported attribute for this activity.", e);
1371 }
1372 }
1373
1374 private void handleFolderClick(FolderInfo folderInfo) {
1375 if (!folderInfo.opened) {
1376 // Close any open folder
1377 closeFolder();
1378 // Open the requested folder
1379 openFolder(folderInfo);
1380 } else {
1381 // Find the open folder...
1382 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1383 int folderScreen;
1384 if (openFolder != null) {
1385 folderScreen = mWorkspace.getScreenForView(openFolder);
1386 // .. and close it
1387 closeFolder(openFolder);
1388 if (folderScreen != mWorkspace.getCurrentScreen()) {
1389 // Close any folder open on the current screen
1390 closeFolder();
1391 // Pull the folder onto this screen
1392 openFolder(folderInfo);
1393 }
1394 }
1395 }
1396 }
1397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 /**
1399 * Opens the user fodler described by the specified tag. The opening of the folder
1400 * is animated relative to the specified View. If the View is null, no animation
1401 * is played.
1402 *
1403 * @param folderInfo The FolderInfo describing the folder to open.
1404 */
1405 private void openFolder(FolderInfo folderInfo) {
1406 Folder openFolder;
1407
1408 if (folderInfo instanceof UserFolderInfo) {
1409 openFolder = UserFolder.fromXml(this);
1410 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001411 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 } else {
1413 return;
1414 }
1415
Joe Onorato00acb122009-08-04 16:04:30 -04001416 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 openFolder.setLauncher(this);
1418
1419 openFolder.bind(folderInfo);
1420 folderInfo.opened = true;
1421
1422 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1423 openFolder.onOpen();
1424 }
1425
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 return false;
1429 }
1430
1431 if (!(v instanceof CellLayout)) {
1432 v = (View) v.getParent();
1433 }
1434
1435 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1436
1437 // This happens when long clicking an item with the dpad/trackball
1438 if (cellInfo == null) {
1439 return true;
1440 }
1441
1442 if (mWorkspace.allowLongPress()) {
1443 if (cellInfo.cell == null) {
1444 if (cellInfo.valid) {
1445 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001446 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 showAddDialog(cellInfo);
1448 }
1449 } else {
1450 if (!(cellInfo.cell instanceof Folder)) {
1451 // User long pressed on an item
1452 mWorkspace.startDrag(cellInfo);
1453 }
1454 }
1455 }
1456 return true;
1457 }
1458
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001459 View getDrawerHandle() {
1460 return mHandleView;
1461 }
1462
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 Workspace getWorkspace() {
1464 return mWorkspace;
1465 }
1466
Joe Onorato7404ee42009-07-31 11:54:44 -07001467 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 GridView getApplicationsGrid() {
1469 return mAllAppsGrid;
1470 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001471 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472
1473 @Override
1474 protected Dialog onCreateDialog(int id) {
1475 switch (id) {
1476 case DIALOG_CREATE_SHORTCUT:
1477 return new CreateShortcut().createDialog();
1478 case DIALOG_RENAME_FOLDER:
1479 return new RenameFolder().createDialog();
1480 }
1481
1482 return super.onCreateDialog(id);
1483 }
1484
1485 @Override
1486 protected void onPrepareDialog(int id, Dialog dialog) {
1487 switch (id) {
1488 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 break;
1490 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001491 if (mFolderInfo != null) {
1492 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1493 final CharSequence text = mFolderInfo.title;
1494 input.setText(text);
1495 input.setSelection(0, text.length());
1496 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 break;
1498 }
1499 }
1500
1501 void showRenameDialog(FolderInfo info) {
1502 mFolderInfo = info;
1503 mWaitingForResult = true;
1504 showDialog(DIALOG_RENAME_FOLDER);
1505 }
1506
1507 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1508 mAddItemCellInfo = cellInfo;
1509 mWaitingForResult = true;
1510 showDialog(DIALOG_CREATE_SHORTCUT);
1511 }
1512
Romain Guy73b979d2009-06-09 12:57:21 -07001513 private void pickShortcut(int requestCode, int title) {
1514 Bundle bundle = new Bundle();
1515
1516 ArrayList<String> shortcutNames = new ArrayList<String>();
1517 shortcutNames.add(getString(R.string.group_applications));
1518 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1519
1520 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1521 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1522 R.drawable.ic_launcher_application));
1523 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1524
1525 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1526 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1527 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1528 pickIntent.putExtras(bundle);
1529
1530 startActivityForResult(pickIntent, requestCode);
1531 }
1532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 private class RenameFolder {
1534 private EditText mInput;
1535
1536 Dialog createDialog() {
1537 mWaitingForResult = true;
1538 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1539 mInput = (EditText) layout.findViewById(R.id.folder_name);
1540
1541 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1542 builder.setIcon(0);
1543 builder.setTitle(getString(R.string.rename_folder_title));
1544 builder.setCancelable(true);
1545 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1546 public void onCancel(DialogInterface dialog) {
1547 cleanup();
1548 }
1549 });
1550 builder.setNegativeButton(getString(R.string.cancel_action),
1551 new Dialog.OnClickListener() {
1552 public void onClick(DialogInterface dialog, int which) {
1553 cleanup();
1554 }
1555 }
1556 );
1557 builder.setPositiveButton(getString(R.string.rename_action),
1558 new Dialog.OnClickListener() {
1559 public void onClick(DialogInterface dialog, int which) {
1560 changeFolderName();
1561 }
1562 }
1563 );
1564 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001565
1566 final AlertDialog dialog = builder.create();
1567 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1568 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001569 }
1570 });
1571
1572 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574
1575 private void changeFolderName() {
1576 final String name = mInput.getText().toString();
1577 if (!TextUtils.isEmpty(name)) {
1578 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001579 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 mFolderInfo.title = name;
1581 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1582
Joe Onorato9c1289c2009-08-17 11:03:03 -04001583 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001584 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001585 mModel.setWorkspaceDirty();
1586 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 } else {
1588 final FolderIcon folderIcon = (FolderIcon)
1589 mWorkspace.getViewForTag(mFolderInfo);
1590 if (folderIcon != null) {
1591 folderIcon.setText(name);
1592 getWorkspace().requestLayout();
1593 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001594 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001595 mModel.setWorkspaceDirty();
1596 mWorkspaceLoading = true;
1597 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
1599 }
1600 }
1601 cleanup();
1602 }
1603
1604 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 dismissDialog(DIALOG_RENAME_FOLDER);
1606 mWaitingForResult = false;
1607 mFolderInfo = null;
1608 }
1609 }
1610
Joe Onoratofb0ca672009-09-14 17:55:46 -04001611 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001612 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001613 }
1614
Jason Samsfd22dac2009-09-20 17:24:16 -07001615 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001616 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001617 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001618
Mike Cleronb6082fa2009-10-19 17:03:36 -07001619 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001620 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001621
Joe Onorato7c312c12009-08-13 21:36:53 -07001622 // TODO: fade these two too
1623 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001624 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001625 }
1626
Joe Onorato7bb17492009-09-24 17:51:01 -07001627 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001628 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001629 mAllAppsGrid.zoom(0.0f);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001630 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001631 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001632
Mike Cleronb6082fa2009-10-19 17:03:36 -07001633
Joe Onorato7c312c12009-08-13 21:36:53 -07001634 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001635 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001636 mDeleteZone.setVisibility(View.VISIBLE);
1637 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001638 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001639 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001640 }
1641
Joe Onorato7c312c12009-08-13 21:36:53 -07001642 void lockAllApps() {
1643 // TODO
1644 }
1645
1646 void unlockAllApps() {
1647 // TODO
1648 }
1649
Joe Onorato7404ee42009-07-31 11:54:44 -07001650 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 * Displays the shortcut creation dialog and launches, if necessary, the
1652 * appropriate activity.
1653 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001654 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001655 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1656 DialogInterface.OnShowListener {
1657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001659
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 Dialog createDialog() {
1661 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001664
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1666 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001667 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 builder.setInverseBackgroundForced(true);
1670
1671 AlertDialog dialog = builder.create();
1672 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001673 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001674 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 return dialog;
1677 }
1678
1679 public void onCancel(DialogInterface dialog) {
1680 mWaitingForResult = false;
1681 cleanup();
1682 }
1683
Romain Guycbb89e42009-06-08 15:52:54 -07001684 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001685 }
1686
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 dismissDialog(DIALOG_CREATE_SHORTCUT);
1689 }
1690
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001691 /**
1692 * Handle the action clicked in the "Add to home" dialog.
1693 */
1694 public void onClick(DialogInterface dialog, int which) {
1695 Resources res = getResources();
1696 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001697
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001698 switch (which) {
1699 case AddAdapter.ITEM_SHORTCUT: {
1700 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001701 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001702 break;
1703 }
Romain Guycbb89e42009-06-08 15:52:54 -07001704
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001705 case AddAdapter.ITEM_APPWIDGET: {
1706 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001707
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001708 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1709 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1710 // add the search widget
1711 ArrayList<AppWidgetProviderInfo> customInfo =
1712 new ArrayList<AppWidgetProviderInfo>();
1713 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1714 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1715 info.label = getString(R.string.group_search);
1716 info.icon = R.drawable.ic_search_widget;
1717 customInfo.add(info);
1718 pickIntent.putParcelableArrayListExtra(
1719 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1720 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1721 Bundle b = new Bundle();
1722 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1723 customExtras.add(b);
1724 pickIntent.putParcelableArrayListExtra(
1725 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1726 // start the pick activity
1727 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1728 break;
1729 }
Romain Guycbb89e42009-06-08 15:52:54 -07001730
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001731 case AddAdapter.ITEM_LIVE_FOLDER: {
1732 // Insert extra item to handle inserting folder
1733 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001734
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001735 ArrayList<String> shortcutNames = new ArrayList<String>();
1736 shortcutNames.add(res.getString(R.string.group_folder));
1737 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001738
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001739 ArrayList<ShortcutIconResource> shortcutIcons =
1740 new ArrayList<ShortcutIconResource>();
1741 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1742 R.drawable.ic_launcher_folder));
1743 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1744
1745 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1746 pickIntent.putExtra(Intent.EXTRA_INTENT,
1747 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1748 pickIntent.putExtra(Intent.EXTRA_TITLE,
1749 getText(R.string.title_select_live_folder));
1750 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001751
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001752 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1753 break;
1754 }
1755
1756 case AddAdapter.ITEM_WALLPAPER: {
1757 startWallpaper();
1758 break;
1759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 }
1761 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001762
1763 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001764 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 }
1766
1767 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001768 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001770 public int getCurrentWorkspaceScreen() {
1771 return mWorkspace.getCurrentScreen();
1772 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001773
Joe Onorato9c1289c2009-08-17 11:03:03 -04001774 /**
1775 * Refreshes the shortcuts shown on the workspace.
1776 *
1777 * Implementation of the method from LauncherModel.Callbacks.
1778 */
1779 public void startBinding() {
1780 final Workspace workspace = mWorkspace;
1781 int count = workspace.getChildCount();
1782 for (int i = 0; i < count; i++) {
1783 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1784 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001785
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786 if (DEBUG_USER_INTERFACE) {
1787 android.widget.Button finishButton = new android.widget.Button(this);
1788 finishButton.setText("Finish");
1789 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1790
1791 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1792 public void onClick(View v) {
1793 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001795 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797 }
1798
1799 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001800 * Bind the items start-end from the list.
1801 *
1802 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1805
1806 final Workspace workspace = mWorkspace;
1807
1808 for (int i=start; i<end; i++) {
1809 final ItemInfo item = shortcuts.get(i);
1810 mDesktopItems.add(item);
1811 switch (item.itemType) {
1812 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1813 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1814 final View shortcut = createShortcut((ApplicationInfo) item);
1815 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1816 false);
1817 break;
1818 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1819 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1820 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1821 (UserFolderInfo) item);
1822 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1823 false);
1824 break;
1825 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1826 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1827 R.layout.live_folder_icon, this,
1828 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1829 (LiveFolderInfo) item);
1830 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1831 false);
1832 break;
1833 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1834 final int screen = workspace.getCurrentScreen();
1835 final View view = mInflater.inflate(R.layout.widget_search,
1836 (ViewGroup) workspace.getChildAt(screen), false);
1837
1838 Search search = (Search) view.findViewById(R.id.widget_search);
1839 search.setLauncher(this);
1840
1841 final Widget widget = (Widget) item;
1842 view.setTag(widget);
1843
1844 workspace.addWidget(view, widget, false);
1845 break;
1846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 }
1848
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 }
1851
Joe Onorato9c1289c2009-08-17 11:03:03 -04001852 /**
1853 * Implementation of the method from LauncherModel.Callbacks.
1854 */
1855 void bindFolders(HashMap<Long, FolderInfo> folders) {
1856 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858
1859 /**
1860 * Add the views for a widget to the workspace.
1861 *
1862 * Implementation of the method from LauncherModel.Callbacks.
1863 */
1864 public void bindAppWidget(LauncherAppWidgetInfo item) {
1865 final Workspace workspace = mWorkspace;
1866
1867 final int appWidgetId = item.appWidgetId;
1868 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1869 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1870
1871 if (true) {
1872 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1873 appWidgetId, appWidgetInfo));
1874 }
1875
1876 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1877 item.hostView.setTag(item);
1878
1879 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1880 item.cellY, item.spanX, item.spanY, false);
1881
1882 workspace.requestLayout();
1883
1884 mDesktopItems.add(item);
1885 }
1886
1887 /**
1888 * Callback saying that there aren't any more items to bind.
1889 *
1890 * Implementation of the method from LauncherModel.Callbacks.
1891 */
1892 public void finishBindingItems() {
1893 if (mSavedState != null) {
1894 if (!mWorkspace.hasFocus()) {
1895 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1896 }
1897
1898 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1899 if (userFolders != null) {
1900 for (long folderId : userFolders) {
1901 final FolderInfo info = mFolders.get(folderId);
1902 if (info != null) {
1903 openFolder(info);
1904 }
1905 }
1906 final Folder openFolder = mWorkspace.getOpenFolder();
1907 if (openFolder != null) {
1908 openFolder.requestFocus();
1909 }
1910 }
1911
1912 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1913 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001914 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001915 }
1916
1917 mSavedState = null;
1918 }
1919
1920 if (mSavedInstanceState != null) {
1921 super.onRestoreInstanceState(mSavedInstanceState);
1922 mSavedInstanceState = null;
1923 }
1924
1925 /* TODO
1926 if (mAllAppsVisible && !mDrawer.hasFocus()) {
1927 mDrawer.requestFocus();
1928 }
1929 */
1930
1931 Log.d(TAG, "finishBindingItems done");
1932 mWorkspaceLoading = false;
1933 }
1934
1935 /**
1936 * Add the icons for all apps.
1937 *
1938 * Implementation of the method from LauncherModel.Callbacks.
1939 */
1940 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001941 mAllAppsList = apps;
1942 mAllAppsGrid.setApps(mAllAppsList);
1943 }
1944
1945 /**
1946 * A package was installed.
1947 *
1948 * Implementation of the method from LauncherModel.Callbacks.
1949 */
1950 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1951 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001952 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953 }
1954
1955 /**
1956 * A package was updated.
1957 *
1958 * Implementation of the method from LauncherModel.Callbacks.
1959 */
1960 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1961 removeDialog(DIALOG_CREATE_SHORTCUT);
1962 mWorkspace.updateShortcutsForPackage(packageName);
1963 }
1964
1965 /**
1966 * A package was uninstalled.
1967 *
1968 * Implementation of the method from LauncherModel.Callbacks.
1969 */
1970 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1971 removeDialog(DIALOG_CREATE_SHORTCUT);
1972 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001973 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975}