blob: ec3622c61fbfda11e1b7d0c27e8eb71c0ac5f34c [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 Onorato85a02a82009-09-08 12:34:22 -070085 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
86 SwipeController.SwipeListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040088 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 static final boolean LOGD = false;
90
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 static final boolean PROFILE_STARTUP = false;
92 static final boolean PROFILE_ROTATE = false;
93 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070094
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 private static final int WALLPAPER_SCREENS_SPAN = 2;
96
97 private static final int MENU_GROUP_ADD = 1;
98 private static final int MENU_ADD = Menu.FIRST + 1;
99 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
100 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
101 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700102 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103
104 private static final int REQUEST_CREATE_SHORTCUT = 1;
105 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700106 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 private static final int REQUEST_PICK_APPLICATION = 6;
108 private static final int REQUEST_PICK_SHORTCUT = 7;
109 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700110 private static final int REQUEST_PICK_APPWIDGET = 9;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Joe Onorato85a02a82009-09-08 12:34:22 -0700112 private static final int MODE_WORKSPACE = 0;
113 private static final int MODE_ALL_APPS = 1;
114 private static final int MODE_ALL_APPS_ZOOMED = 2;
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
117
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700118 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
119 static final String SEARCH_WIDGET = "search_widget";
120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 static final int SCREEN_COUNT = 3;
122 static final int DEFAULT_SCREN = 1;
123 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700124 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Joe Onorato7c312c12009-08-13 21:36:53 -0700126 static final int DIALOG_CREATE_SHORTCUT = 1;
127 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Romain Guy98d01652009-06-30 16:21:04 -0700129 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
131 // Type: int
132 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
133 // Type: boolean
134 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
135 // Type: long
136 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
149 // Type: int
150 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
151 // Type: int[]
152 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
153 // Type: boolean
154 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
155 // Type: long
156 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
157
Joe Onorato9c1289c2009-08-17 11:03:03 -0400158 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 private static final Object sLock = new Object();
161 private static int sScreen = DEFAULT_SCREN;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private LayoutInflater mInflater;
164
Joe Onorato00acb122009-08-04 16:04:30 -0400165 private DragController mDragController;
Joe Onorato85a02a82009-09-08 12:34:22 -0700166 private SwipeController mSwipeController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private AppWidgetManager mAppWidgetManager;
170 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private CellLayout.CellInfo mAddItemCellInfo;
173 private CellLayout.CellInfo mMenuAddInfo;
174 private final int[] mCellCoordinates = new int[2];
175 private FolderInfo mFolderInfo;
176
Joe Onorato7c312c12009-08-13 21:36:53 -0700177 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700179 private AllAppsView mAllAppsGrid;
Joe Onorato85a02a82009-09-08 12:34:22 -0700180 private boolean mAllAppsVisible; // if it's visible at all
181 private int mMode = MODE_WORKSPACE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private Bundle mSavedState;
184
185 private SpannableStringBuilder mDefaultKeySsb = null;
186
Mike LeBeau736cf282009-07-02 17:46:59 -0700187 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 private boolean mWorkspaceLoading = true;
190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private boolean mRestoring;
192 private boolean mWaitingForResult;
193 private boolean mLocaleChanged;
194
Dianne Hackborn67800862009-07-24 17:15:20 -0700195 private boolean mHomeDown;
196 private boolean mBackDown;
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private Bundle mSavedInstanceState;
199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 private LauncherModel mModel;
201
202 private ArrayList<ItemInfo> mDesktopItems = new ArrayList();
203 private static HashMap<Long, FolderInfo> mFolders = new HashMap();
204 private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList();
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 @Override
207 protected void onCreate(Bundle savedInstanceState) {
208 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700209
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700212
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700214
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
216 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 if (PROFILE_STARTUP) {
219 android.os.Debug.startMethodTracing("/sdcard/launcher");
220 }
221
222 checkForLocaleChange();
223 setWallpaperDimension();
224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 setContentView(R.layout.launcher);
226 setupViews();
227
Joe Onorato7c312c12009-08-13 21:36:53 -0700228 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 mSavedState = savedInstanceState;
231 restoreState(mSavedState);
232
233 if (PROFILE_STARTUP) {
234 android.os.Debug.stopMethodTracing();
235 }
236
237 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);
249
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 }
278
279 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;
381 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700384 // Clean up the appWidgetId if we canceled
385 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
386 if (appWidgetId != -1) {
387 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 }
389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
391
392 @Override
393 protected void onResume() {
394 super.onResume();
395
396 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400397 mWorkspaceLoading = true;
398 mModel.startLoader(this, true);
399 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700401
402 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
403 // onNewIntent), then close the search dialog if needed, because it probably
404 // came from the user pressing 'home' (rather than, for example, pressing 'back').
405 if (mIsNewIntent) {
406 // Post to a handler so that this happens after the search dialog tries to open
407 // itself again.
408 mWorkspace.post(new Runnable() {
409 public void run() {
410 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
411 ServiceManager.getService(Context.SEARCH_SERVICE));
412 try {
413 searchManagerService.stopSearch();
414 } catch (RemoteException e) {
415 e(LOG_TAG, "error stopping search", e);
416 }
417 }
418 });
419 }
420
421 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 }
423
424 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700425 protected void onPause() {
426 super.onPause();
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700427 closeAllAppsDialog(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700428 }
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700430 @Override
431 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400432 // Flag the loader to stop early before switching
433 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700435 if (PROFILE_ROTATE) {
436 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
437 }
438 return null;
439 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 private boolean acceptFilter() {
442 final InputMethodManager inputManager = (InputMethodManager)
443 getSystemService(Context.INPUT_METHOD_SERVICE);
444 return !inputManager.isFullscreenMode();
445 }
446
447 @Override
448 public boolean onKeyDown(int keyCode, KeyEvent event) {
449 boolean handled = super.onKeyDown(keyCode, event);
450 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
451 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
452 keyCode, event);
453 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700454 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700455 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700456 // showSearchDialog()
457 // If there are multiple keystrokes before the search dialog takes focus,
458 // onSearchRequested() will be called for every keystroke,
459 // but it is idempotent, so it's fine.
460 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
462 }
463
464 return handled;
465 }
466
Karl Rosaen138a0412009-04-23 19:00:21 -0700467 private String getTypedText() {
468 return mDefaultKeySsb.toString();
469 }
470
471 private void clearTypedText() {
472 mDefaultKeySsb.clear();
473 mDefaultKeySsb.clearSpans();
474 Selection.setSelection(mDefaultKeySsb, 0);
475 }
476
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 /**
478 * Restores the previous state, if it exists.
479 *
480 * @param savedState The previous state.
481 */
482 private void restoreState(Bundle savedState) {
483 if (savedState == null) {
484 return;
485 }
486
487 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
488 if (currentScreen > -1) {
489 mWorkspace.setCurrentScreen(currentScreen);
490 }
491
492 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
493 if (addScreen > -1) {
494 mAddItemCellInfo = new CellLayout.CellInfo();
495 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
496 addItemCellInfo.valid = true;
497 addItemCellInfo.screen = addScreen;
498 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
499 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
500 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
501 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
502 addItemCellInfo.findVacantCellsFromOccupied(
503 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
504 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
505 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
506 mRestoring = true;
507 }
508
509 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
510 if (renameFolder) {
511 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800513 mRestoring = true;
514 }
515 }
516
517 /**
518 * Finds all the views we need and configure them properly.
519 */
520 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400521 mDragController = new DragController(this);
522 DragController dragController = mDragController;
Joe Onorato85a02a82009-09-08 12:34:22 -0700523 mSwipeController = new SwipeController(this, this);
524 SwipeController swipeController = mSwipeController;
525 swipeController.setRange(-1, 0);
Joe Onorato00acb122009-08-04 16:04:30 -0400526
Romain Guyb1b69f52009-08-10 15:10:15 -0700527 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400528 dragLayer.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700529 dragLayer.setSwipeController(swipeController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530
Joe Onorato7c312c12009-08-13 21:36:53 -0700531 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700532 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700533 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700534 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Joe Onorato7c312c12009-08-13 21:36:53 -0700535
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
537 final Workspace workspace = mWorkspace;
538
Joe Onorato7c312c12009-08-13 21:36:53 -0700539 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
540 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541
Joe Onorato7c312c12009-08-13 21:36:53 -0700542 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700543 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700544 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700545 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700546 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700547 handleIocon.setCrossFadeEnabled(true);
548 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400551 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553
554 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400555 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700556 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557
Joe Onorato00acb122009-08-04 16:04:30 -0400558 dragController.setDragScoller(workspace);
559 dragController.setDragListener(deleteZone);
560 dragController.setScrollView(dragLayer);
561
562 // The order here is bottom to top.
563 dragController.addDropTarget(workspace);
564 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 }
566
567 /**
568 * Creates a view representing a shortcut.
569 *
570 * @param info The data structure describing the shortcut.
571 *
572 * @return A View inflated from R.layout.application.
573 */
574 View createShortcut(ApplicationInfo info) {
575 return createShortcut(R.layout.application,
576 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
577 }
578
579 /**
580 * Creates a view representing a shortcut inflated from the specified resource.
581 *
582 * @param layoutResId The id of the XML layout used to create the shortcut.
583 * @param parent The group the shortcut belongs to.
584 * @param info The data structure describing the shortcut.
585 *
586 * @return A View inflated from layoutResId.
587 */
588 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
589 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
590
Joe Onorato5162ea92009-09-03 09:39:42 -0700591 if (info.icon == null) {
592 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
593 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700595 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 info.filtered = true;
597 }
598
599 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
600 favorite.setText(info.title);
601 favorite.setTag(info);
602 favorite.setOnClickListener(this);
603
604 return favorite;
605 }
606
607 /**
608 * Add an application shortcut to the workspace.
609 *
610 * @param data The intent describing the application.
611 * @param cellInfo The position on screen where to create the shortcut.
612 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400613 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 cellInfo.screen = mWorkspace.getCurrentScreen();
615 if (!findSingleSlot(cellInfo)) return;
616
Romain Guy73b979d2009-06-09 12:57:21 -0700617 final ApplicationInfo info = infoFromApplicationIntent(context, data);
618 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400619 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700620 }
621 }
622
623 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 ComponentName component = data.getComponent();
625 PackageManager packageManager = context.getPackageManager();
626 ActivityInfo activityInfo = null;
627 try {
628 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
629 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700630 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 }
Romain Guycbb89e42009-06-08 15:52:54 -0700632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 if (activityInfo != null) {
634 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 itemInfo.title = activityInfo.loadLabel(packageManager);
637 if (itemInfo.title == null) {
638 itemInfo.title = activityInfo.name;
639 }
Romain Guycbb89e42009-06-08 15:52:54 -0700640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
642 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
643 itemInfo.icon = activityInfo.loadIcon(packageManager);
644 itemInfo.container = ItemInfo.NO_ID;
645
Romain Guy73b979d2009-06-09 12:57:21 -0700646 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 }
Romain Guy73b979d2009-06-09 12:57:21 -0700648
649 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
Romain Guycbb89e42009-06-08 15:52:54 -0700651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 /**
653 * Add a shortcut to the workspace.
654 *
655 * @param data The intent describing the shortcut.
656 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 cellInfo.screen = mWorkspace.getCurrentScreen();
660 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
663
664 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
667 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
669 }
670
Romain Guycbb89e42009-06-08 15:52:54 -0700671
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700673 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700675 * @param data The intent describing the appWidgetId.
676 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400678 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700680 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700681
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700682 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700683
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700684 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700685
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 // Try finding open space on Launcher screen
691 final int[] xy = mCellCoordinates;
692 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
693
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700694 // Build Launcher-specific widget info and save to database
695 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 launcherInfo.spanX = spans[0];
697 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700698
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 LauncherModel.addItemToDatabase(this, launcherInfo,
700 LauncherSettings.Favorites.CONTAINER_DESKTOP,
701 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
702
703 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400704 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700708
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700709 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400713 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 }
715 }
Romain Guycbb89e42009-06-08 15:52:54 -0700716
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
718 mDesktopItems.remove(launcherInfo);
719 launcherInfo.hostView = null;
720 }
721
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700722 public LauncherAppWidgetHost getAppWidgetHost() {
723 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 }
Romain Guycbb89e42009-06-08 15:52:54 -0700725
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 static ApplicationInfo addShortcut(Context context, Intent data,
727 CellLayout.CellInfo cellInfo, boolean notify) {
728
Romain Guy73b979d2009-06-09 12:57:21 -0700729 final ApplicationInfo info = infoFromShortcutIntent(context, data);
730 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
731 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
732
733 return info;
734 }
735
736 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
738 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
739 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
740
741 Drawable icon = null;
742 boolean filtered = false;
743 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700744 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745
746 if (bitmap != null) {
747 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
748 filtered = true;
749 customIcon = true;
750 } else {
751 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700752 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700754 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 final PackageManager packageManager = context.getPackageManager();
756 Resources resources = packageManager.getResourcesForApplication(
757 iconResource.packageName);
758 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
759 icon = resources.getDrawable(id);
760 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700761 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
763 }
764 }
765
766 if (icon == null) {
767 icon = context.getPackageManager().getDefaultActivityIcon();
768 }
769
770 final ApplicationInfo info = new ApplicationInfo();
771 info.icon = icon;
772 info.filtered = filtered;
773 info.title = name;
774 info.intent = intent;
775 info.customIcon = customIcon;
776 info.iconResource = iconResource;
777
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 return info;
779 }
780
781 @Override
782 protected void onNewIntent(Intent intent) {
783 super.onNewIntent(intent);
784
785 // Close the menu
786 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
787 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700788
789 // Set this flag so that onResume knows to close the search dialog if it's open,
790 // because this was a new intent (thus a press of 'home' or some such) rather than
791 // for example onResume being called when the user pressed the 'back' button.
792 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793
794 try {
795 dismissDialog(DIALOG_CREATE_SHORTCUT);
796 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 } catch (Exception e) {
798 // An exception is thrown if the dialog is not visible, which is fine
799 }
800
801 try {
802 dismissDialog(DIALOG_RENAME_FOLDER);
803 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 } catch (Exception e) {
805 // An exception is thrown if the dialog is not visible, which is fine
806 }
807
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
809 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700810
Romain Guy94dabf12009-07-21 10:55:43 -0700811 if (!mWorkspace.isDefaultScreenShowing()) {
812 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700814
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700815 closeAllAppsDialog(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700816
817 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 if (v != null && v.getWindowToken() != null) {
819 InputMethodManager imm = (InputMethodManager)getSystemService(
820 INPUT_METHOD_SERVICE);
821 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
822 }
823 } else {
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700824 closeAllAppsDialog(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 }
826 }
827 }
828
829 @Override
830 protected void onRestoreInstanceState(Bundle savedInstanceState) {
831 // Do not call super here
832 mSavedInstanceState = savedInstanceState;
833 }
834
835 @Override
836 protected void onSaveInstanceState(Bundle outState) {
837 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
838
839 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
840 if (folders.size() > 0) {
841 final int count = folders.size();
842 long[] ids = new long[count];
843 for (int i = 0; i < count; i++) {
844 final FolderInfo info = folders.get(i).getInfo();
845 ids[i] = info.id;
846 }
847 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
848 } else {
849 super.onSaveInstanceState(outState);
850 }
851
Romain Guy3cf604f2009-06-16 13:12:53 -0700852 final boolean isConfigurationChange = getChangingConfigurations() != 0;
853
Romain Guy5a941392009-04-28 15:18:25 -0700854 // When the drawer is opened and we are saving the state because of a
855 // configuration change
Joe Onorato7c312c12009-08-13 21:36:53 -0700856 if (mAllAppsVisible && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700858 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859
860 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
861 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
862 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
863
864 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
865 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
866 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
867 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
868 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
869 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
870 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
871 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
872 layout.getOccupiedCells());
873 }
874
875 if (mFolderInfo != null && mWaitingForResult) {
876 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
877 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
878 }
879 }
880
881 @Override
882 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700884
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700886 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700888 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
890
891 TextKeyListener.getInstance().release();
892
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894
Joe Onorato9c1289c2009-08-17 11:03:03 -0400895 unbindDesktopItems();
896 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 }
898
899 @Override
900 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700901 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 super.startActivityForResult(intent, requestCode);
903 }
904
905 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700906 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700908
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700909 closeAllAppsDialog(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700910
Karl Rosaen138a0412009-04-23 19:00:21 -0700911 // Slide the search widget to the top, if it's on the current screen,
912 // otherwise show the search dialog immediately.
913 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
914 if (searchWidget == null) {
915 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
916 } else {
917 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
918 // show the currently typed text in the search widget while sliding
919 searchWidget.setQuery(getTypedText());
920 }
921 }
Romain Guycbb89e42009-06-08 15:52:54 -0700922
Karl Rosaen138a0412009-04-23 19:00:21 -0700923 /**
924 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700925 *
926 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700927 */
Romain Guycbb89e42009-06-08 15:52:54 -0700928 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700929 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700930
Karl Rosaen138a0412009-04-23 19:00:21 -0700931 if (initialQuery == null) {
932 // Use any text typed in the launcher as the initial query
933 initialQuery = getTypedText();
934 clearTypedText();
935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 if (appSearchData == null) {
937 appSearchData = new Bundle();
938 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
939 }
Romain Guycbb89e42009-06-08 15:52:54 -0700940
Karl Rosaen138a0412009-04-23 19:00:21 -0700941 final SearchManager searchManager =
942 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
943
944 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
945 if (searchWidget != null) {
946 // This gets called when the user leaves the search dialog to go back to
947 // the Launcher.
948 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
949 public void onCancel() {
950 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700951 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700952 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700953 });
954 }
Romain Guycbb89e42009-06-08 15:52:54 -0700955
Karl Rosaen138a0412009-04-23 19:00:21 -0700956 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700957 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
959
Karl Rosaen138a0412009-04-23 19:00:21 -0700960 /**
961 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 */
Romain Guy5a941392009-04-28 15:18:25 -0700963 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700964 // Close search dialog
965 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100966 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 // Restore search widget to its normal position
968 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
969 if (searchWidget != null) {
970 searchWidget.stopSearch(false);
971 }
972 }
Romain Guycbb89e42009-06-08 15:52:54 -0700973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 @Override
975 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 if (isWorkspaceLocked()) {
977 return false;
978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979
980 super.onCreateOptionsMenu(menu);
981 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
982 .setIcon(android.R.drawable.ic_menu_add)
983 .setAlphabeticShortcut('A');
984 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
985 .setIcon(android.R.drawable.ic_menu_gallery)
986 .setAlphabeticShortcut('W');
987 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
988 .setIcon(android.R.drawable.ic_search_category_default)
989 .setAlphabeticShortcut(SearchManager.MENU_KEY);
990 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
991 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
992 .setAlphabeticShortcut('N');
993
994 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700995 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
996 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997
998 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
999 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1000 .setIntent(settings);
1001
1002 return true;
1003 }
1004
1005 @Override
1006 public boolean onPrepareOptionsMenu(Menu menu) {
1007 super.onPrepareOptionsMenu(menu);
1008
1009 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1010 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1011
1012 return true;
1013 }
1014
1015 @Override
1016 public boolean onOptionsItemSelected(MenuItem item) {
1017 switch (item.getItemId()) {
1018 case MENU_ADD:
1019 addItems();
1020 return true;
1021 case MENU_WALLPAPER_SETTINGS:
1022 startWallpaper();
1023 return true;
1024 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001025 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 return true;
1027 case MENU_NOTIFICATIONS:
1028 showNotifications();
1029 return true;
1030 }
1031
1032 return super.onOptionsItemSelected(item);
1033 }
Romain Guycbb89e42009-06-08 15:52:54 -07001034
Karl Rosaen138a0412009-04-23 19:00:21 -07001035 /**
1036 * Indicates that we want global search for this activity by setting the globalSearch
1037 * argument for {@link #startSearch} to true.
1038 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001040 @Override
1041 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001042 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001043 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001044 }
1045
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046 public boolean isWorkspaceLocked() {
1047 return mWorkspaceLoading || mWaitingForResult;
1048 }
1049
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 private void addItems() {
1051 showAddDialog(mMenuAddInfo);
1052 }
1053
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001054 void addAppWidget(Intent data) {
1055 // TODO: catch bad widget exception when sent
1056 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001058 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1059 if (SEARCH_WIDGET.equals(customWidget)) {
1060 // We don't need this any more, since this isn't a real app widget.
1061 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1062 // add the search widget
1063 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001065 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1066
1067 if (appWidget.configure != null) {
1068 // Launch over to configure widget, if needed
1069 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1070 intent.setComponent(appWidget.configure);
1071 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1072
1073 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1074 } else {
1075 // Otherwise just add it
1076 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 }
1079 }
Romain Guycbb89e42009-06-08 15:52:54 -07001080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 void addSearch() {
1082 final Widget info = Widget.makeSearch();
1083 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 final int[] xy = mCellCoordinates;
1086 final int spanX = info.spanX;
1087 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1092 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 final View view = mInflater.inflate(info.layoutResource, null);
1095 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001096 Search search = (Search) view.findViewById(R.id.widget_search);
1097 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1100 }
1101
Romain Guy73b979d2009-06-09 12:57:21 -07001102 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001103 // Handle case where user selected "Applications"
1104 String applicationName = getResources().getString(R.string.group_applications);
1105 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001106
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001107 if (applicationName != null && applicationName.equals(shortcutName)) {
1108 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1109 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001110
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001111 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1112 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001113 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001114 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001115 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
1118
1119 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001120 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001121 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001122 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001123
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001124 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001125 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001126 } else {
1127 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 }
1130
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 UserFolderInfo folderInfo = new UserFolderInfo();
1133 folderInfo.title = getText(R.string.folder_name);
1134
1135 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1136 cellInfo.screen = mWorkspace.getCurrentScreen();
1137 if (!findSingleSlot(cellInfo)) return;
1138
1139 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140 LauncherModel.addItemToDatabase(this, folderInfo,
1141 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144
1145 // Create the view
1146 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1147 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1148 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001149 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 }
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Joe Onorato9c1289c2009-08-17 11:03:03 -04001152 void removeFolder(FolderInfo folder) {
1153 mFolders.remove(folder.id);
1154 }
1155
1156 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 cellInfo.screen = mWorkspace.getCurrentScreen();
1158 if (!findSingleSlot(cellInfo)) return;
1159
1160 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1161
1162 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1164 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001165 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1166 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 }
1168 }
1169
1170 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1171 CellLayout.CellInfo cellInfo, boolean notify) {
1172
1173 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1174 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1175
1176 Drawable icon = null;
1177 boolean filtered = false;
1178 Intent.ShortcutIconResource iconResource = null;
1179
1180 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1181 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1182 try {
1183 iconResource = (Intent.ShortcutIconResource) extra;
1184 final PackageManager packageManager = context.getPackageManager();
1185 Resources resources = packageManager.getResourcesForApplication(
1186 iconResource.packageName);
1187 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1188 icon = resources.getDrawable(id);
1189 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001190 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 }
1192 }
1193
1194 if (icon == null) {
1195 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1196 }
1197
1198 final LiveFolderInfo info = new LiveFolderInfo();
1199 info.icon = icon;
1200 info.filtered = filtered;
1201 info.title = name;
1202 info.iconResource = iconResource;
1203 info.uri = data.getData();
1204 info.baseIntent = baseIntent;
1205 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1206 LiveFolders.DISPLAY_MODE_GRID);
1207
1208 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1209 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211
1212 return info;
1213 }
1214
1215 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1216 final int[] xy = new int[2];
1217 if (findSlot(cellInfo, xy, 1, 1)) {
1218 cellInfo.cellX = xy[0];
1219 cellInfo.cellY = xy[1];
1220 return true;
1221 }
1222 return false;
1223 }
1224
1225 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1226 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1227 boolean[] occupied = mSavedState != null ?
1228 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1229 cellInfo = mWorkspace.findAllVacantCells(occupied);
1230 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1231 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1232 return false;
1233 }
1234 }
1235 return true;
1236 }
1237
1238 private void showNotifications() {
1239 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1240 if (statusBar != null) {
1241 statusBar.expand();
1242 }
1243 }
1244
1245 private void startWallpaper() {
1246 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001247 Intent chooser = Intent.createChooser(pickWallpaper,
1248 getText(R.string.chooser_wallpaper));
1249 WallpaperManager wm = (WallpaperManager)
1250 getSystemService(Context.WALLPAPER_SERVICE);
1251 WallpaperInfo wi = wm.getWallpaperInfo();
1252 if (wi != null && wi.getSettingsActivity() != null) {
1253 LabeledIntent li = new LabeledIntent(getPackageName(),
1254 R.string.configure_wallpaper, 0);
1255 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1256 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1257 }
1258 startActivity(chooser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 }
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001262 public void onWindowFocusChanged(boolean hasFocus) {
1263 super.onWindowFocusChanged(hasFocus);
1264 if (!hasFocus) {
1265 mBackDown = mHomeDown = false;
1266 }
1267 }
1268
1269 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 public boolean dispatchKeyEvent(KeyEvent event) {
1271 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1272 switch (event.getKeyCode()) {
1273 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001274 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 return true;
1276 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001277 mHomeDown = true;
1278 return true;
1279 }
1280 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1281 switch (event.getKeyCode()) {
1282 case KeyEvent.KEYCODE_BACK:
1283 if (!event.isCanceled()) {
1284 mWorkspace.dispatchKeyEvent(event);
Joe Onorato7c312c12009-08-13 21:36:53 -07001285 if (mAllAppsVisible) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001286 closeAllAppsDialog(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001287 } else {
1288 closeFolder();
1289 }
1290 }
1291 mBackDown = false;
1292 return true;
1293 case KeyEvent.KEYCODE_HOME:
1294 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 return true;
1296 }
1297 }
1298
1299 return super.dispatchKeyEvent(event);
1300 }
1301
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 private void closeFolder() {
1303 Folder folder = mWorkspace.getOpenFolder();
1304 if (folder != null) {
1305 closeFolder(folder);
1306 }
1307 }
1308
1309 void closeFolder(Folder folder) {
1310 folder.getInfo().opened = false;
1311 ViewGroup parent = (ViewGroup) folder.getParent();
1312 if (parent != null) {
1313 parent.removeView(folder);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314 // TODO: this line crashes.
1315 //mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
1317 folder.onClose();
1318 }
1319
1320 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001321 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1322 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001324 private void unbindDesktopItems() {
1325 for (ItemInfo item: mDesktopItems) {
1326 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 /**
1331 * Launches the intent referred by the clicked shortcut.
1332 *
1333 * @param v The view representing the clicked shortcut.
1334 */
1335 public void onClick(View v) {
1336 Object tag = v.getTag();
1337 if (tag instanceof ApplicationInfo) {
1338 // Open shortcut
1339 final Intent intent = ((ApplicationInfo) tag).intent;
1340 startActivitySafely(intent);
1341 } else if (tag instanceof FolderInfo) {
1342 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001343 } else if (v == mHandleView) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001344 if (mAllAppsVisible) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001345 // TODO how can we be here?
1346 } else {
Joe Onorato45d43462009-07-31 14:36:51 -07001347 showAllAppsDialog();
Joe Onorato7404ee42009-07-31 11:54:44 -07001348 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 }
1350 }
1351
1352 void startActivitySafely(Intent intent) {
1353 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1354 try {
1355 startActivity(intent);
1356 } catch (ActivityNotFoundException e) {
1357 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1358 } catch (SecurityException e) {
1359 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001360 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1362 "or use the exported attribute for this activity.", e);
1363 }
1364 }
1365
1366 private void handleFolderClick(FolderInfo folderInfo) {
1367 if (!folderInfo.opened) {
1368 // Close any open folder
1369 closeFolder();
1370 // Open the requested folder
1371 openFolder(folderInfo);
1372 } else {
1373 // Find the open folder...
1374 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1375 int folderScreen;
1376 if (openFolder != null) {
1377 folderScreen = mWorkspace.getScreenForView(openFolder);
1378 // .. and close it
1379 closeFolder(openFolder);
1380 if (folderScreen != mWorkspace.getCurrentScreen()) {
1381 // Close any folder open on the current screen
1382 closeFolder();
1383 // Pull the folder onto this screen
1384 openFolder(folderInfo);
1385 }
1386 }
1387 }
1388 }
1389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 /**
1391 * Opens the user fodler described by the specified tag. The opening of the folder
1392 * is animated relative to the specified View. If the View is null, no animation
1393 * is played.
1394 *
1395 * @param folderInfo The FolderInfo describing the folder to open.
1396 */
1397 private void openFolder(FolderInfo folderInfo) {
1398 Folder openFolder;
1399
1400 if (folderInfo instanceof UserFolderInfo) {
1401 openFolder = UserFolder.fromXml(this);
1402 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001403 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 } else {
1405 return;
1406 }
1407
Joe Onorato00acb122009-08-04 16:04:30 -04001408 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 openFolder.setLauncher(this);
1410
1411 openFolder.bind(folderInfo);
1412 folderInfo.opened = true;
1413
1414 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1415 openFolder.onOpen();
1416 }
1417
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001419 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 return false;
1421 }
1422
1423 if (!(v instanceof CellLayout)) {
1424 v = (View) v.getParent();
1425 }
1426
1427 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1428
1429 // This happens when long clicking an item with the dpad/trackball
1430 if (cellInfo == null) {
1431 return true;
1432 }
1433
1434 if (mWorkspace.allowLongPress()) {
Joe Onorato85a02a82009-09-08 12:34:22 -07001435 mSwipeController.cancelSwipe();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 if (cellInfo.cell == null) {
1437 if (cellInfo.valid) {
1438 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001439 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 showAddDialog(cellInfo);
1441 }
1442 } else {
1443 if (!(cellInfo.cell instanceof Folder)) {
1444 // User long pressed on an item
1445 mWorkspace.startDrag(cellInfo);
1446 }
1447 }
1448 }
1449 return true;
1450 }
1451
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001452 View getDrawerHandle() {
1453 return mHandleView;
1454 }
1455
Joe Onorato7c312c12009-08-13 21:36:53 -07001456 boolean isDrawerDown() { // TODO rename to isAllAppsVisible()
1457 return /* TODO !mDrawer.isMoving() && */ !mAllAppsVisible;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
1459
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 Workspace getWorkspace() {
1461 return mWorkspace;
1462 }
1463
Joe Onorato7404ee42009-07-31 11:54:44 -07001464 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 GridView getApplicationsGrid() {
1466 return mAllAppsGrid;
1467 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001468 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469
1470 @Override
1471 protected Dialog onCreateDialog(int id) {
1472 switch (id) {
1473 case DIALOG_CREATE_SHORTCUT:
1474 return new CreateShortcut().createDialog();
1475 case DIALOG_RENAME_FOLDER:
1476 return new RenameFolder().createDialog();
1477 }
1478
1479 return super.onCreateDialog(id);
1480 }
1481
1482 @Override
1483 protected void onPrepareDialog(int id, Dialog dialog) {
1484 switch (id) {
1485 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 break;
1487 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001488 if (mFolderInfo != null) {
1489 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1490 final CharSequence text = mFolderInfo.title;
1491 input.setText(text);
1492 input.setSelection(0, text.length());
1493 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 break;
1495 }
1496 }
1497
1498 void showRenameDialog(FolderInfo info) {
1499 mFolderInfo = info;
1500 mWaitingForResult = true;
1501 showDialog(DIALOG_RENAME_FOLDER);
1502 }
1503
1504 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1505 mAddItemCellInfo = cellInfo;
1506 mWaitingForResult = true;
1507 showDialog(DIALOG_CREATE_SHORTCUT);
1508 }
1509
Romain Guy73b979d2009-06-09 12:57:21 -07001510 private void pickShortcut(int requestCode, int title) {
1511 Bundle bundle = new Bundle();
1512
1513 ArrayList<String> shortcutNames = new ArrayList<String>();
1514 shortcutNames.add(getString(R.string.group_applications));
1515 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1516
1517 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1518 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1519 R.drawable.ic_launcher_application));
1520 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1521
1522 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1523 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1524 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1525 pickIntent.putExtras(bundle);
1526
1527 startActivityForResult(pickIntent, requestCode);
1528 }
1529
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 private class RenameFolder {
1531 private EditText mInput;
1532
1533 Dialog createDialog() {
1534 mWaitingForResult = true;
1535 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1536 mInput = (EditText) layout.findViewById(R.id.folder_name);
1537
1538 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1539 builder.setIcon(0);
1540 builder.setTitle(getString(R.string.rename_folder_title));
1541 builder.setCancelable(true);
1542 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1543 public void onCancel(DialogInterface dialog) {
1544 cleanup();
1545 }
1546 });
1547 builder.setNegativeButton(getString(R.string.cancel_action),
1548 new Dialog.OnClickListener() {
1549 public void onClick(DialogInterface dialog, int which) {
1550 cleanup();
1551 }
1552 }
1553 );
1554 builder.setPositiveButton(getString(R.string.rename_action),
1555 new Dialog.OnClickListener() {
1556 public void onClick(DialogInterface dialog, int which) {
1557 changeFolderName();
1558 }
1559 }
1560 );
1561 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001562
1563 final AlertDialog dialog = builder.create();
1564 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1565 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001566 }
1567 });
1568
1569 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
1571
1572 private void changeFolderName() {
1573 final String name = mInput.getText().toString();
1574 if (!TextUtils.isEmpty(name)) {
1575 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001576 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 mFolderInfo.title = name;
1578 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1579
Joe Onorato9c1289c2009-08-17 11:03:03 -04001580 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001581 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001582 mModel.setWorkspaceDirty();
1583 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 } else {
1585 final FolderIcon folderIcon = (FolderIcon)
1586 mWorkspace.getViewForTag(mFolderInfo);
1587 if (folderIcon != null) {
1588 folderIcon.setText(name);
1589 getWorkspace().requestLayout();
1590 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001591 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001592 mModel.setWorkspaceDirty();
1593 mWorkspaceLoading = true;
1594 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
1596 }
1597 }
1598 cleanup();
1599 }
1600
1601 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 dismissDialog(DIALOG_RENAME_FOLDER);
1603 mWaitingForResult = false;
1604 mFolderInfo = null;
1605 }
1606 }
1607
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001608 void showAllAppsDialog() {
Joe Onorato7c312c12009-08-13 21:36:53 -07001609 mAllAppsVisible = true;
Joe Onorato006b25f2009-09-03 11:38:43 -07001610 mAllAppsGrid.show();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001611 mWorkspace.hide();
Joe Onorato7c312c12009-08-13 21:36:53 -07001612
1613 // TODO: fade these two too
1614 mDeleteZone.setVisibility(View.GONE);
1615 mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001616 }
1617
1618 void closeAllAppsDialog(boolean animated) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001619 if (mAllAppsVisible) {
Joe Onorato85a02a82009-09-08 12:34:22 -07001620 mAllAppsGrid.hide();
Joe Onorato7c312c12009-08-13 21:36:53 -07001621 mAllAppsVisible = false;
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001622 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1623 mWorkspace.show();
Joe Onorato7c312c12009-08-13 21:36:53 -07001624
1625 // TODO: fade these two too
1626 mDeleteZone.setVisibility(View.VISIBLE);
1627 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001628 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001629 }
1630
Joe Onorato7c312c12009-08-13 21:36:53 -07001631 void lockAllApps() {
1632 // TODO
1633 }
1634
1635 void unlockAllApps() {
1636 // TODO
1637 }
1638
Joe Onorato7404ee42009-07-31 11:54:44 -07001639 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 * Displays the shortcut creation dialog and launches, if necessary, the
1641 * appropriate activity.
1642 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001643 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001644 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1645 DialogInterface.OnShowListener {
1646
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 Dialog createDialog() {
1650 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001653
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1655 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001656 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 builder.setInverseBackgroundForced(true);
1659
1660 AlertDialog dialog = builder.create();
1661 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001662 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001663 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 return dialog;
1666 }
1667
1668 public void onCancel(DialogInterface dialog) {
1669 mWaitingForResult = false;
1670 cleanup();
1671 }
1672
Romain Guycbb89e42009-06-08 15:52:54 -07001673 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001674 }
1675
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 dismissDialog(DIALOG_CREATE_SHORTCUT);
1678 }
1679
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001680 /**
1681 * Handle the action clicked in the "Add to home" dialog.
1682 */
1683 public void onClick(DialogInterface dialog, int which) {
1684 Resources res = getResources();
1685 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001686
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001687 switch (which) {
1688 case AddAdapter.ITEM_SHORTCUT: {
1689 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001690 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001691 break;
1692 }
Romain Guycbb89e42009-06-08 15:52:54 -07001693
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001694 case AddAdapter.ITEM_APPWIDGET: {
1695 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001696
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001697 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1698 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1699 // add the search widget
1700 ArrayList<AppWidgetProviderInfo> customInfo =
1701 new ArrayList<AppWidgetProviderInfo>();
1702 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1703 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1704 info.label = getString(R.string.group_search);
1705 info.icon = R.drawable.ic_search_widget;
1706 customInfo.add(info);
1707 pickIntent.putParcelableArrayListExtra(
1708 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1709 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1710 Bundle b = new Bundle();
1711 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1712 customExtras.add(b);
1713 pickIntent.putParcelableArrayListExtra(
1714 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1715 // start the pick activity
1716 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1717 break;
1718 }
Romain Guycbb89e42009-06-08 15:52:54 -07001719
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001720 case AddAdapter.ITEM_LIVE_FOLDER: {
1721 // Insert extra item to handle inserting folder
1722 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001723
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001724 ArrayList<String> shortcutNames = new ArrayList<String>();
1725 shortcutNames.add(res.getString(R.string.group_folder));
1726 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001727
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001728 ArrayList<ShortcutIconResource> shortcutIcons =
1729 new ArrayList<ShortcutIconResource>();
1730 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1731 R.drawable.ic_launcher_folder));
1732 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1733
1734 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1735 pickIntent.putExtra(Intent.EXTRA_INTENT,
1736 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1737 pickIntent.putExtra(Intent.EXTRA_TITLE,
1738 getText(R.string.title_select_live_folder));
1739 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001740
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001741 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1742 break;
1743 }
1744
1745 case AddAdapter.ITEM_WALLPAPER: {
1746 startWallpaper();
1747 break;
1748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 }
1750 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001751
1752 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001753 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
1755
1756 /**
Joe Onorato85a02a82009-09-08 12:34:22 -07001757 * Implementation of the method from SwipeController.SwipeListener.
1758 */
1759 public void onStartSwipe() {
1760 switch (mMode) {
1761 case MODE_WORKSPACE:
1762 mWorkspace.enableChildrenCache();
1763 break;
1764 case MODE_ALL_APPS:
1765 break;
1766 case MODE_ALL_APPS_ZOOMED:
1767 break;
1768 }
1769 }
1770
1771 /**
1772 * Implementation of the method from SwipeController.SwipeListener.
1773 *
1774 * @param amount The final value of the swipe (-1, 0 or 1)
1775 */
1776 public void onFinishSwipe(int amount) {
1777 switch (mMode) {
1778 case MODE_WORKSPACE:
1779 if (amount == -1) {
1780 mWorkspace.clearChildrenCache();
1781 mMode = MODE_ALL_APPS;
1782 mSwipeController.setRange(0, 1);
1783 }
1784 break;
1785 case MODE_ALL_APPS:
1786 if (amount == 1) {
1787 mWorkspace.clearChildrenCache();
1788 mMode = MODE_WORKSPACE;
1789 mSwipeController.setRange(-1, 0);
1790 }
1791 break;
1792 case MODE_ALL_APPS_ZOOMED:
1793 break;
1794 }
1795 }
1796
1797 /**
1798 * Implementation of the method from SwipeController.SwipeListener.
1799 */
1800 public void onSwipe(float amount) {
1801 switch (mMode) {
1802 case MODE_WORKSPACE:
1803 // We can open the all apps view.
1804 // 0 == workspace is showing
1805 // -1 == all apps is showing
1806 setWorkspaceAndAllAppsScale(-amount);
1807 break;
1808 case MODE_ALL_APPS:
1809 // We can close it, or (someday) zoom it further
1810 // 0 == all apps showing
1811 // 1 == workspace is showing
1812 setWorkspaceAndAllAppsScale(1-amount);
1813 break;
1814 }
1815 }
1816
1817 /**
1818 * Set the scale factor for the workspace and the all apps grid.
1819 *
1820 * @param amount A float between 0 and 1, where:
1821 * 0 == workspace is showing and
1822 * 1 == the all apps grid is showing.
1823 */
1824 private void setWorkspaceAndAllAppsScale(float amount) {
1825 //Log.d("setWorkspaceAndAllAppsScale", "setWorkspaceAndAllAppsScale amount=" + amount);
1826 if (amount < 0.001f) {
1827 amount = 0.0f;
1828 }
1829 if (amount > 0.999f) {
1830 amount = 1.0f;
1831 mWorkspace.setVisibility(View.INVISIBLE);
1832 } else {
1833 mWorkspace.setVisibility(View.VISIBLE);
1834 }
1835 mWorkspace.setScale(1-amount);
1836 mAllAppsGrid.setScale(amount);
1837 }
1838
1839 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 public int getCurrentWorkspaceScreen() {
1843 return mWorkspace.getCurrentScreen();
1844 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001845
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 /**
1847 * Refreshes the shortcuts shown on the workspace.
1848 *
1849 * Implementation of the method from LauncherModel.Callbacks.
1850 */
1851 public void startBinding() {
1852 final Workspace workspace = mWorkspace;
1853 int count = workspace.getChildCount();
1854 for (int i = 0; i < count; i++) {
1855 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1856 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001857
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858 if (DEBUG_USER_INTERFACE) {
1859 android.widget.Button finishButton = new android.widget.Button(this);
1860 finishButton.setText("Finish");
1861 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1862
1863 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1864 public void onClick(View v) {
1865 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001866 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001867 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 }
1869 }
1870
1871 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001872 * Bind the items start-end from the list.
1873 *
1874 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1877
1878 final Workspace workspace = mWorkspace;
1879
1880 for (int i=start; i<end; i++) {
1881 final ItemInfo item = shortcuts.get(i);
1882 mDesktopItems.add(item);
1883 switch (item.itemType) {
1884 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1885 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1886 final View shortcut = createShortcut((ApplicationInfo) item);
1887 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1888 false);
1889 break;
1890 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1891 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1892 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1893 (UserFolderInfo) item);
1894 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1895 false);
1896 break;
1897 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1898 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1899 R.layout.live_folder_icon, this,
1900 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1901 (LiveFolderInfo) item);
1902 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1903 false);
1904 break;
1905 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1906 final int screen = workspace.getCurrentScreen();
1907 final View view = mInflater.inflate(R.layout.widget_search,
1908 (ViewGroup) workspace.getChildAt(screen), false);
1909
1910 Search search = (Search) view.findViewById(R.id.widget_search);
1911 search.setLauncher(this);
1912
1913 final Widget widget = (Widget) item;
1914 view.setTag(widget);
1915
1916 workspace.addWidget(view, widget, false);
1917 break;
1918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
1920
Joe Onorato9c1289c2009-08-17 11:03:03 -04001921 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 }
1923
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 /**
1925 * Implementation of the method from LauncherModel.Callbacks.
1926 */
1927 void bindFolders(HashMap<Long, FolderInfo> folders) {
1928 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001930
1931 /**
1932 * Add the views for a widget to the workspace.
1933 *
1934 * Implementation of the method from LauncherModel.Callbacks.
1935 */
1936 public void bindAppWidget(LauncherAppWidgetInfo item) {
1937 final Workspace workspace = mWorkspace;
1938
1939 final int appWidgetId = item.appWidgetId;
1940 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1941 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1942
1943 if (true) {
1944 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1945 appWidgetId, appWidgetInfo));
1946 }
1947
1948 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1949 item.hostView.setTag(item);
1950
1951 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1952 item.cellY, item.spanX, item.spanY, false);
1953
1954 workspace.requestLayout();
1955
1956 mDesktopItems.add(item);
1957 }
1958
1959 /**
1960 * Callback saying that there aren't any more items to bind.
1961 *
1962 * Implementation of the method from LauncherModel.Callbacks.
1963 */
1964 public void finishBindingItems() {
1965 if (mSavedState != null) {
1966 if (!mWorkspace.hasFocus()) {
1967 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1968 }
1969
1970 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1971 if (userFolders != null) {
1972 for (long folderId : userFolders) {
1973 final FolderInfo info = mFolders.get(folderId);
1974 if (info != null) {
1975 openFolder(info);
1976 }
1977 }
1978 final Folder openFolder = mWorkspace.getOpenFolder();
1979 if (openFolder != null) {
1980 openFolder.requestFocus();
1981 }
1982 }
1983
1984 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1985 if (allApps) {
1986 showAllAppsDialog();
1987 }
1988
1989 mSavedState = null;
1990 }
1991
1992 if (mSavedInstanceState != null) {
1993 super.onRestoreInstanceState(mSavedInstanceState);
1994 mSavedInstanceState = null;
1995 }
1996
1997 /* TODO
1998 if (mAllAppsVisible && !mDrawer.hasFocus()) {
1999 mDrawer.requestFocus();
2000 }
2001 */
2002
2003 Log.d(TAG, "finishBindingItems done");
2004 mWorkspaceLoading = false;
2005 }
2006
2007 /**
2008 * Add the icons for all apps.
2009 *
2010 * Implementation of the method from LauncherModel.Callbacks.
2011 */
2012 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
2013 Log.d(LOG_TAG, "got info for " + apps.size() + " apps");
2014 mAllAppsList = apps;
2015 mAllAppsGrid.setApps(mAllAppsList);
2016 }
2017
2018 /**
2019 * A package was installed.
2020 *
2021 * Implementation of the method from LauncherModel.Callbacks.
2022 */
2023 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2024 removeDialog(DIALOG_CREATE_SHORTCUT);
2025 }
2026
2027 /**
2028 * A package was updated.
2029 *
2030 * Implementation of the method from LauncherModel.Callbacks.
2031 */
2032 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2033 removeDialog(DIALOG_CREATE_SHORTCUT);
2034 mWorkspace.updateShortcutsForPackage(packageName);
2035 }
2036
2037 /**
2038 * A package was uninstalled.
2039 *
2040 * Implementation of the method from LauncherModel.Callbacks.
2041 */
2042 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2043 removeDialog(DIALOG_CREATE_SHORTCUT);
2044 mWorkspace.removeShortcutsForPackage(packageName);
2045 }
2046
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047}