blob: 86914b0cd2d3763b43203e949a401c4e082b8176 [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
Joe Onorato9c1289c2009-08-17 11:03:03 -0400396 Log.d(TAG, "onResume mRestoring=" + mRestoring);
397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400399 mWorkspaceLoading = true;
400 mModel.startLoader(this, true);
401 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700403
404 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
405 // onNewIntent), then close the search dialog if needed, because it probably
406 // came from the user pressing 'home' (rather than, for example, pressing 'back').
407 if (mIsNewIntent) {
408 // Post to a handler so that this happens after the search dialog tries to open
409 // itself again.
410 mWorkspace.post(new Runnable() {
411 public void run() {
412 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
413 ServiceManager.getService(Context.SEARCH_SERVICE));
414 try {
415 searchManagerService.stopSearch();
416 } catch (RemoteException e) {
417 e(LOG_TAG, "error stopping search", e);
418 }
419 }
420 });
421 }
422
423 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425
426 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 protected void onPause() {
428 super.onPause();
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700429 closeAllAppsDialog(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 }
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700432 @Override
433 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400434 // Flag the loader to stop early before switching
435 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700436
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700437 if (PROFILE_ROTATE) {
438 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
439 }
440 return null;
441 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 private boolean acceptFilter() {
444 final InputMethodManager inputManager = (InputMethodManager)
445 getSystemService(Context.INPUT_METHOD_SERVICE);
446 return !inputManager.isFullscreenMode();
447 }
448
449 @Override
450 public boolean onKeyDown(int keyCode, KeyEvent event) {
451 boolean handled = super.onKeyDown(keyCode, event);
452 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
453 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
454 keyCode, event);
455 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700456 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700457 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700458 // showSearchDialog()
459 // If there are multiple keystrokes before the search dialog takes focus,
460 // onSearchRequested() will be called for every keystroke,
461 // but it is idempotent, so it's fine.
462 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 }
464 }
465
466 return handled;
467 }
468
Karl Rosaen138a0412009-04-23 19:00:21 -0700469 private String getTypedText() {
470 return mDefaultKeySsb.toString();
471 }
472
473 private void clearTypedText() {
474 mDefaultKeySsb.clear();
475 mDefaultKeySsb.clearSpans();
476 Selection.setSelection(mDefaultKeySsb, 0);
477 }
478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 /**
480 * Restores the previous state, if it exists.
481 *
482 * @param savedState The previous state.
483 */
484 private void restoreState(Bundle savedState) {
485 if (savedState == null) {
486 return;
487 }
488
489 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
490 if (currentScreen > -1) {
491 mWorkspace.setCurrentScreen(currentScreen);
492 }
493
494 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
495 if (addScreen > -1) {
496 mAddItemCellInfo = new CellLayout.CellInfo();
497 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
498 addItemCellInfo.valid = true;
499 addItemCellInfo.screen = addScreen;
500 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
501 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
502 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
503 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
504 addItemCellInfo.findVacantCellsFromOccupied(
505 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
506 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
507 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
508 mRestoring = true;
509 }
510
511 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
512 if (renameFolder) {
513 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400514 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 mRestoring = true;
516 }
517 }
518
519 /**
520 * Finds all the views we need and configure them properly.
521 */
522 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400523 mDragController = new DragController(this);
524 DragController dragController = mDragController;
Joe Onorato85a02a82009-09-08 12:34:22 -0700525 mSwipeController = new SwipeController(this, this);
526 SwipeController swipeController = mSwipeController;
527 swipeController.setRange(-1, 0);
Joe Onorato00acb122009-08-04 16:04:30 -0400528
Romain Guyb1b69f52009-08-10 15:10:15 -0700529 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400530 dragLayer.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700531 dragLayer.setSwipeController(swipeController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532
Joe Onorato7c312c12009-08-13 21:36:53 -0700533 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700534 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700535 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700536 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Joe Onorato7c312c12009-08-13 21:36:53 -0700537
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
539 final Workspace workspace = mWorkspace;
540
Joe Onorato7c312c12009-08-13 21:36:53 -0700541 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
542 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543
Joe Onorato7c312c12009-08-13 21:36:53 -0700544 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700545 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700546 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700547 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700548 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700549 handleIocon.setCrossFadeEnabled(true);
550 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400553 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555
556 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400557 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700558 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559
Joe Onorato00acb122009-08-04 16:04:30 -0400560 dragController.setDragScoller(workspace);
561 dragController.setDragListener(deleteZone);
562 dragController.setScrollView(dragLayer);
563
564 // The order here is bottom to top.
565 dragController.addDropTarget(workspace);
566 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 }
568
569 /**
570 * Creates a view representing a shortcut.
571 *
572 * @param info The data structure describing the shortcut.
573 *
574 * @return A View inflated from R.layout.application.
575 */
576 View createShortcut(ApplicationInfo info) {
577 return createShortcut(R.layout.application,
578 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
579 }
580
581 /**
582 * Creates a view representing a shortcut inflated from the specified resource.
583 *
584 * @param layoutResId The id of the XML layout used to create the shortcut.
585 * @param parent The group the shortcut belongs to.
586 * @param info The data structure describing the shortcut.
587 *
588 * @return A View inflated from layoutResId.
589 */
590 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
591 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
592
Joe Onorato5162ea92009-09-03 09:39:42 -0700593 if (info.icon == null) {
594 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
595 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700597 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 info.filtered = true;
599 }
600
601 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
602 favorite.setText(info.title);
603 favorite.setTag(info);
604 favorite.setOnClickListener(this);
605
606 return favorite;
607 }
608
609 /**
610 * Add an application shortcut to the workspace.
611 *
612 * @param data The intent describing the application.
613 * @param cellInfo The position on screen where to create the shortcut.
614 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400615 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 cellInfo.screen = mWorkspace.getCurrentScreen();
617 if (!findSingleSlot(cellInfo)) return;
618
Romain Guy73b979d2009-06-09 12:57:21 -0700619 final ApplicationInfo info = infoFromApplicationIntent(context, data);
620 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700622 }
623 }
624
625 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 ComponentName component = data.getComponent();
627 PackageManager packageManager = context.getPackageManager();
628 ActivityInfo activityInfo = null;
629 try {
630 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
631 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700632 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 }
Romain Guycbb89e42009-06-08 15:52:54 -0700634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 if (activityInfo != null) {
636 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700637
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 itemInfo.title = activityInfo.loadLabel(packageManager);
639 if (itemInfo.title == null) {
640 itemInfo.title = activityInfo.name;
641 }
Romain Guycbb89e42009-06-08 15:52:54 -0700642
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
644 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
645 itemInfo.icon = activityInfo.loadIcon(packageManager);
646 itemInfo.container = ItemInfo.NO_ID;
647
Romain Guy73b979d2009-06-09 12:57:21 -0700648 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
Romain Guy73b979d2009-06-09 12:57:21 -0700650
651 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
Romain Guycbb89e42009-06-08 15:52:54 -0700653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 /**
655 * Add a shortcut to the workspace.
656 *
657 * @param data The intent describing the shortcut.
658 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 cellInfo.screen = mWorkspace.getCurrentScreen();
662 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
665
666 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400668 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
669 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
671 }
672
Romain Guycbb89e42009-06-08 15:52:54 -0700673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700675 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677 * @param data The intent describing the appWidgetId.
678 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400680 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700682 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700683
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700684 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700685
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700687
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700690 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700691
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 // Try finding open space on Launcher screen
693 final int[] xy = mCellCoordinates;
694 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
695
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700696 // Build Launcher-specific widget info and save to database
697 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 launcherInfo.spanX = spans[0];
699 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700700
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 LauncherModel.addItemToDatabase(this, launcherInfo,
702 LauncherSettings.Favorites.CONTAINER_DESKTOP,
703 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
704
705 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400706 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700709 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700711 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400715 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
717 }
Romain Guycbb89e42009-06-08 15:52:54 -0700718
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
720 mDesktopItems.remove(launcherInfo);
721 launcherInfo.hostView = null;
722 }
723
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700724 public LauncherAppWidgetHost getAppWidgetHost() {
725 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 }
Romain Guycbb89e42009-06-08 15:52:54 -0700727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 static ApplicationInfo addShortcut(Context context, Intent data,
729 CellLayout.CellInfo cellInfo, boolean notify) {
730
Romain Guy73b979d2009-06-09 12:57:21 -0700731 final ApplicationInfo info = infoFromShortcutIntent(context, data);
732 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
733 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
734
735 return info;
736 }
737
738 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
740 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
741 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
742
743 Drawable icon = null;
744 boolean filtered = false;
745 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700746 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747
748 if (bitmap != null) {
749 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
750 filtered = true;
751 customIcon = true;
752 } else {
753 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700754 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700756 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 final PackageManager packageManager = context.getPackageManager();
758 Resources resources = packageManager.getResourcesForApplication(
759 iconResource.packageName);
760 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
761 icon = resources.getDrawable(id);
762 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700763 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 }
765 }
766 }
767
768 if (icon == null) {
769 icon = context.getPackageManager().getDefaultActivityIcon();
770 }
771
772 final ApplicationInfo info = new ApplicationInfo();
773 info.icon = icon;
774 info.filtered = filtered;
775 info.title = name;
776 info.intent = intent;
777 info.customIcon = customIcon;
778 info.iconResource = iconResource;
779
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 return info;
781 }
782
783 @Override
784 protected void onNewIntent(Intent intent) {
785 super.onNewIntent(intent);
786
787 // Close the menu
788 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
789 getWindow().closeAllPanels();
Mike LeBeau736cf282009-07-02 17:46:59 -0700790
791 // Set this flag so that onResume knows to close the search dialog if it's open,
792 // because this was a new intent (thus a press of 'home' or some such) rather than
793 // for example onResume being called when the user pressed the 'back' button.
794 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795
796 try {
797 dismissDialog(DIALOG_CREATE_SHORTCUT);
798 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 } catch (Exception e) {
800 // An exception is thrown if the dialog is not visible, which is fine
801 }
802
803 try {
804 dismissDialog(DIALOG_RENAME_FOLDER);
805 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 } catch (Exception e) {
807 // An exception is thrown if the dialog is not visible, which is fine
808 }
809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
811 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700812
Romain Guy94dabf12009-07-21 10:55:43 -0700813 if (!mWorkspace.isDefaultScreenShowing()) {
814 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700816
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700817 closeAllAppsDialog(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700818
819 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 if (v != null && v.getWindowToken() != null) {
821 InputMethodManager imm = (InputMethodManager)getSystemService(
822 INPUT_METHOD_SERVICE);
823 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
824 }
825 } else {
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700826 closeAllAppsDialog(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 }
828 }
829 }
830
831 @Override
832 protected void onRestoreInstanceState(Bundle savedInstanceState) {
833 // Do not call super here
834 mSavedInstanceState = savedInstanceState;
835 }
836
837 @Override
838 protected void onSaveInstanceState(Bundle outState) {
839 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
840
841 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
842 if (folders.size() > 0) {
843 final int count = folders.size();
844 long[] ids = new long[count];
845 for (int i = 0; i < count; i++) {
846 final FolderInfo info = folders.get(i).getInfo();
847 ids[i] = info.id;
848 }
849 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
850 } else {
851 super.onSaveInstanceState(outState);
852 }
853
Romain Guy3cf604f2009-06-16 13:12:53 -0700854 final boolean isConfigurationChange = getChangingConfigurations() != 0;
855
Romain Guy5a941392009-04-28 15:18:25 -0700856 // When the drawer is opened and we are saving the state because of a
857 // configuration change
Joe Onorato7c312c12009-08-13 21:36:53 -0700858 if (mAllAppsVisible && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861
862 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
863 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
864 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
865
866 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
867 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
868 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
869 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
870 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
871 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
872 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
873 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
874 layout.getOccupiedCells());
875 }
876
877 if (mFolderInfo != null && mWaitingForResult) {
878 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
879 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
880 }
881 }
882
883 @Override
884 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700888 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700890 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
892
893 TextKeyListener.getInstance().release();
894
Joe Onorato9c1289c2009-08-17 11:03:03 -0400895 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896
Joe Onorato9c1289c2009-08-17 11:03:03 -0400897 unbindDesktopItems();
898 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 }
900
901 @Override
902 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700903 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 super.startActivityForResult(intent, requestCode);
905 }
906
907 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700908 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700910
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700911 closeAllAppsDialog(false);
Romain Guycbb89e42009-06-08 15:52:54 -0700912
Karl Rosaen138a0412009-04-23 19:00:21 -0700913 // Slide the search widget to the top, if it's on the current screen,
914 // otherwise show the search dialog immediately.
915 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
916 if (searchWidget == null) {
917 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
918 } else {
919 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
920 // show the currently typed text in the search widget while sliding
921 searchWidget.setQuery(getTypedText());
922 }
923 }
Romain Guycbb89e42009-06-08 15:52:54 -0700924
Karl Rosaen138a0412009-04-23 19:00:21 -0700925 /**
926 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700927 *
928 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700929 */
Romain Guycbb89e42009-06-08 15:52:54 -0700930 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700931 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700932
Karl Rosaen138a0412009-04-23 19:00:21 -0700933 if (initialQuery == null) {
934 // Use any text typed in the launcher as the initial query
935 initialQuery = getTypedText();
936 clearTypedText();
937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 if (appSearchData == null) {
939 appSearchData = new Bundle();
940 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
941 }
Romain Guycbb89e42009-06-08 15:52:54 -0700942
Karl Rosaen138a0412009-04-23 19:00:21 -0700943 final SearchManager searchManager =
944 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
945
946 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
947 if (searchWidget != null) {
948 // This gets called when the user leaves the search dialog to go back to
949 // the Launcher.
950 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
951 public void onCancel() {
952 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700953 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700954 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700955 });
956 }
Romain Guycbb89e42009-06-08 15:52:54 -0700957
Karl Rosaen138a0412009-04-23 19:00:21 -0700958 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700959 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 }
961
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 /**
963 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700964 */
Romain Guy5a941392009-04-28 15:18:25 -0700965 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700966 // Close search dialog
967 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100968 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 // Restore search widget to its normal position
970 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
971 if (searchWidget != null) {
972 searchWidget.stopSearch(false);
973 }
974 }
Romain Guycbb89e42009-06-08 15:52:54 -0700975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 @Override
977 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 if (isWorkspaceLocked()) {
979 return false;
980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981
982 super.onCreateOptionsMenu(menu);
983 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
984 .setIcon(android.R.drawable.ic_menu_add)
985 .setAlphabeticShortcut('A');
986 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
987 .setIcon(android.R.drawable.ic_menu_gallery)
988 .setAlphabeticShortcut('W');
989 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
990 .setIcon(android.R.drawable.ic_search_category_default)
991 .setAlphabeticShortcut(SearchManager.MENU_KEY);
992 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
993 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
994 .setAlphabeticShortcut('N');
995
996 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700997 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
998 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999
1000 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1001 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1002 .setIntent(settings);
1003
1004 return true;
1005 }
1006
1007 @Override
1008 public boolean onPrepareOptionsMenu(Menu menu) {
1009 super.onPrepareOptionsMenu(menu);
1010
1011 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1012 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1013
1014 return true;
1015 }
1016
1017 @Override
1018 public boolean onOptionsItemSelected(MenuItem item) {
1019 switch (item.getItemId()) {
1020 case MENU_ADD:
1021 addItems();
1022 return true;
1023 case MENU_WALLPAPER_SETTINGS:
1024 startWallpaper();
1025 return true;
1026 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001027 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 return true;
1029 case MENU_NOTIFICATIONS:
1030 showNotifications();
1031 return true;
1032 }
1033
1034 return super.onOptionsItemSelected(item);
1035 }
Romain Guycbb89e42009-06-08 15:52:54 -07001036
Karl Rosaen138a0412009-04-23 19:00:21 -07001037 /**
1038 * Indicates that we want global search for this activity by setting the globalSearch
1039 * argument for {@link #startSearch} to true.
1040 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001042 @Override
1043 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001044 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001045 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001046 }
1047
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 public boolean isWorkspaceLocked() {
1049 return mWorkspaceLoading || mWaitingForResult;
1050 }
1051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 private void addItems() {
1053 showAddDialog(mMenuAddInfo);
1054 }
1055
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001056 void addAppWidget(Intent data) {
1057 // TODO: catch bad widget exception when sent
1058 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001060 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1061 if (SEARCH_WIDGET.equals(customWidget)) {
1062 // We don't need this any more, since this isn't a real app widget.
1063 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1064 // add the search widget
1065 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001067 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1068
1069 if (appWidget.configure != null) {
1070 // Launch over to configure widget, if needed
1071 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1072 intent.setComponent(appWidget.configure);
1073 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1074
1075 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1076 } else {
1077 // Otherwise just add it
1078 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 }
1081 }
Romain Guycbb89e42009-06-08 15:52:54 -07001082
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 void addSearch() {
1084 final Widget info = Widget.makeSearch();
1085 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 final int[] xy = mCellCoordinates;
1088 final int spanX = info.spanX;
1089 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1094 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 final View view = mInflater.inflate(info.layoutResource, null);
1097 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001098 Search search = (Search) view.findViewById(R.id.widget_search);
1099 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1102 }
1103
Romain Guy73b979d2009-06-09 12:57:21 -07001104 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001105 // Handle case where user selected "Applications"
1106 String applicationName = getResources().getString(R.string.group_applications);
1107 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001108
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001109 if (applicationName != null && applicationName.equals(shortcutName)) {
1110 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1111 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001112
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001113 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1114 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001115 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001116 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001117 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001118 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 }
1120
1121 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001122 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001123 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001124 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001125
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001126 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001128 } else {
1129 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 }
1132
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 UserFolderInfo folderInfo = new UserFolderInfo();
1135 folderInfo.title = getText(R.string.folder_name);
1136
1137 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1138 cellInfo.screen = mWorkspace.getCurrentScreen();
1139 if (!findSingleSlot(cellInfo)) return;
1140
1141 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001142 LauncherModel.addItemToDatabase(this, folderInfo,
1143 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001145 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146
1147 // Create the view
1148 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1149 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1150 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001151 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 }
Romain Guycbb89e42009-06-08 15:52:54 -07001153
Joe Onorato9c1289c2009-08-17 11:03:03 -04001154 void removeFolder(FolderInfo folder) {
1155 mFolders.remove(folder.id);
1156 }
1157
1158 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 cellInfo.screen = mWorkspace.getCurrentScreen();
1160 if (!findSingleSlot(cellInfo)) return;
1161
1162 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1163
1164 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1166 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001167 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1168 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170 }
1171
1172 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1173 CellLayout.CellInfo cellInfo, boolean notify) {
1174
1175 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1176 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1177
1178 Drawable icon = null;
1179 boolean filtered = false;
1180 Intent.ShortcutIconResource iconResource = null;
1181
1182 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1183 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1184 try {
1185 iconResource = (Intent.ShortcutIconResource) extra;
1186 final PackageManager packageManager = context.getPackageManager();
1187 Resources resources = packageManager.getResourcesForApplication(
1188 iconResource.packageName);
1189 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1190 icon = resources.getDrawable(id);
1191 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001192 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 }
1194 }
1195
1196 if (icon == null) {
1197 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1198 }
1199
1200 final LiveFolderInfo info = new LiveFolderInfo();
1201 info.icon = icon;
1202 info.filtered = filtered;
1203 info.title = name;
1204 info.iconResource = iconResource;
1205 info.uri = data.getData();
1206 info.baseIntent = baseIntent;
1207 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1208 LiveFolders.DISPLAY_MODE_GRID);
1209
1210 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1211 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213
1214 return info;
1215 }
1216
1217 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1218 final int[] xy = new int[2];
1219 if (findSlot(cellInfo, xy, 1, 1)) {
1220 cellInfo.cellX = xy[0];
1221 cellInfo.cellY = xy[1];
1222 return true;
1223 }
1224 return false;
1225 }
1226
1227 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1228 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1229 boolean[] occupied = mSavedState != null ?
1230 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1231 cellInfo = mWorkspace.findAllVacantCells(occupied);
1232 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1233 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1234 return false;
1235 }
1236 }
1237 return true;
1238 }
1239
1240 private void showNotifications() {
1241 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1242 if (statusBar != null) {
1243 statusBar.expand();
1244 }
1245 }
1246
1247 private void startWallpaper() {
1248 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001249 Intent chooser = Intent.createChooser(pickWallpaper,
1250 getText(R.string.chooser_wallpaper));
1251 WallpaperManager wm = (WallpaperManager)
1252 getSystemService(Context.WALLPAPER_SERVICE);
1253 WallpaperInfo wi = wm.getWallpaperInfo();
1254 if (wi != null && wi.getSettingsActivity() != null) {
1255 LabeledIntent li = new LabeledIntent(getPackageName(),
1256 R.string.configure_wallpaper, 0);
1257 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1258 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1259 }
1260 startActivity(chooser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 }
1262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001264 public void onWindowFocusChanged(boolean hasFocus) {
1265 super.onWindowFocusChanged(hasFocus);
1266 if (!hasFocus) {
1267 mBackDown = mHomeDown = false;
1268 }
1269 }
1270
1271 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 public boolean dispatchKeyEvent(KeyEvent event) {
1273 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1274 switch (event.getKeyCode()) {
1275 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001276 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 return true;
1278 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001279 mHomeDown = true;
1280 return true;
1281 }
1282 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1283 switch (event.getKeyCode()) {
1284 case KeyEvent.KEYCODE_BACK:
1285 if (!event.isCanceled()) {
1286 mWorkspace.dispatchKeyEvent(event);
Joe Onorato7c312c12009-08-13 21:36:53 -07001287 if (mAllAppsVisible) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001288 closeAllAppsDialog(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001289 } else {
1290 closeFolder();
1291 }
1292 }
1293 mBackDown = false;
1294 return true;
1295 case KeyEvent.KEYCODE_HOME:
1296 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 return true;
1298 }
1299 }
1300
1301 return super.dispatchKeyEvent(event);
1302 }
1303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 private void closeFolder() {
1305 Folder folder = mWorkspace.getOpenFolder();
1306 if (folder != null) {
1307 closeFolder(folder);
1308 }
1309 }
1310
1311 void closeFolder(Folder folder) {
1312 folder.getInfo().opened = false;
1313 ViewGroup parent = (ViewGroup) folder.getParent();
1314 if (parent != null) {
1315 parent.removeView(folder);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316 // TODO: this line crashes.
1317 //mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 }
1319 folder.onClose();
1320 }
1321
1322 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001323 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1324 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001326 private void unbindDesktopItems() {
1327 for (ItemInfo item: mDesktopItems) {
1328 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 }
1330 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 /**
1333 * Launches the intent referred by the clicked shortcut.
1334 *
1335 * @param v The view representing the clicked shortcut.
1336 */
1337 public void onClick(View v) {
1338 Object tag = v.getTag();
1339 if (tag instanceof ApplicationInfo) {
1340 // Open shortcut
1341 final Intent intent = ((ApplicationInfo) tag).intent;
1342 startActivitySafely(intent);
1343 } else if (tag instanceof FolderInfo) {
1344 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001345 } else if (v == mHandleView) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001346 if (mAllAppsVisible) {
Joe Onorato7404ee42009-07-31 11:54:44 -07001347 // TODO how can we be here?
1348 } else {
Joe Onorato45d43462009-07-31 14:36:51 -07001349 showAllAppsDialog();
Joe Onorato7404ee42009-07-31 11:54:44 -07001350 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
1352 }
1353
1354 void startActivitySafely(Intent intent) {
1355 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1356 try {
1357 startActivity(intent);
1358 } catch (ActivityNotFoundException e) {
1359 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1360 } catch (SecurityException e) {
1361 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001362 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1364 "or use the exported attribute for this activity.", e);
1365 }
1366 }
1367
1368 private void handleFolderClick(FolderInfo folderInfo) {
1369 if (!folderInfo.opened) {
1370 // Close any open folder
1371 closeFolder();
1372 // Open the requested folder
1373 openFolder(folderInfo);
1374 } else {
1375 // Find the open folder...
1376 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1377 int folderScreen;
1378 if (openFolder != null) {
1379 folderScreen = mWorkspace.getScreenForView(openFolder);
1380 // .. and close it
1381 closeFolder(openFolder);
1382 if (folderScreen != mWorkspace.getCurrentScreen()) {
1383 // Close any folder open on the current screen
1384 closeFolder();
1385 // Pull the folder onto this screen
1386 openFolder(folderInfo);
1387 }
1388 }
1389 }
1390 }
1391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 /**
1393 * Opens the user fodler described by the specified tag. The opening of the folder
1394 * is animated relative to the specified View. If the View is null, no animation
1395 * is played.
1396 *
1397 * @param folderInfo The FolderInfo describing the folder to open.
1398 */
1399 private void openFolder(FolderInfo folderInfo) {
1400 Folder openFolder;
1401
1402 if (folderInfo instanceof UserFolderInfo) {
1403 openFolder = UserFolder.fromXml(this);
1404 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001405 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 } else {
1407 return;
1408 }
1409
Joe Onorato00acb122009-08-04 16:04:30 -04001410 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 openFolder.setLauncher(this);
1412
1413 openFolder.bind(folderInfo);
1414 folderInfo.opened = true;
1415
1416 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1417 openFolder.onOpen();
1418 }
1419
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001421 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 return false;
1423 }
1424
1425 if (!(v instanceof CellLayout)) {
1426 v = (View) v.getParent();
1427 }
1428
1429 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1430
1431 // This happens when long clicking an item with the dpad/trackball
1432 if (cellInfo == null) {
1433 return true;
1434 }
1435
1436 if (mWorkspace.allowLongPress()) {
Joe Onorato85a02a82009-09-08 12:34:22 -07001437 mSwipeController.cancelSwipe();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 if (cellInfo.cell == null) {
1439 if (cellInfo.valid) {
1440 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001441 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 showAddDialog(cellInfo);
1443 }
1444 } else {
1445 if (!(cellInfo.cell instanceof Folder)) {
1446 // User long pressed on an item
1447 mWorkspace.startDrag(cellInfo);
1448 }
1449 }
1450 }
1451 return true;
1452 }
1453
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001454 View getDrawerHandle() {
1455 return mHandleView;
1456 }
1457
Joe Onorato7c312c12009-08-13 21:36:53 -07001458 boolean isDrawerDown() { // TODO rename to isAllAppsVisible()
1459 return /* TODO !mDrawer.isMoving() && */ !mAllAppsVisible;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 }
1461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 Workspace getWorkspace() {
1463 return mWorkspace;
1464 }
1465
Joe Onorato7404ee42009-07-31 11:54:44 -07001466 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 GridView getApplicationsGrid() {
1468 return mAllAppsGrid;
1469 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001470 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471
1472 @Override
1473 protected Dialog onCreateDialog(int id) {
1474 switch (id) {
1475 case DIALOG_CREATE_SHORTCUT:
1476 return new CreateShortcut().createDialog();
1477 case DIALOG_RENAME_FOLDER:
1478 return new RenameFolder().createDialog();
1479 }
1480
1481 return super.onCreateDialog(id);
1482 }
1483
1484 @Override
1485 protected void onPrepareDialog(int id, Dialog dialog) {
1486 switch (id) {
1487 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 break;
1489 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001490 if (mFolderInfo != null) {
1491 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1492 final CharSequence text = mFolderInfo.title;
1493 input.setText(text);
1494 input.setSelection(0, text.length());
1495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 break;
1497 }
1498 }
1499
1500 void showRenameDialog(FolderInfo info) {
1501 mFolderInfo = info;
1502 mWaitingForResult = true;
1503 showDialog(DIALOG_RENAME_FOLDER);
1504 }
1505
1506 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1507 mAddItemCellInfo = cellInfo;
1508 mWaitingForResult = true;
1509 showDialog(DIALOG_CREATE_SHORTCUT);
1510 }
1511
Romain Guy73b979d2009-06-09 12:57:21 -07001512 private void pickShortcut(int requestCode, int title) {
1513 Bundle bundle = new Bundle();
1514
1515 ArrayList<String> shortcutNames = new ArrayList<String>();
1516 shortcutNames.add(getString(R.string.group_applications));
1517 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1518
1519 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1520 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1521 R.drawable.ic_launcher_application));
1522 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1523
1524 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1525 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1526 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1527 pickIntent.putExtras(bundle);
1528
1529 startActivityForResult(pickIntent, requestCode);
1530 }
1531
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 private class RenameFolder {
1533 private EditText mInput;
1534
1535 Dialog createDialog() {
1536 mWaitingForResult = true;
1537 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1538 mInput = (EditText) layout.findViewById(R.id.folder_name);
1539
1540 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1541 builder.setIcon(0);
1542 builder.setTitle(getString(R.string.rename_folder_title));
1543 builder.setCancelable(true);
1544 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1545 public void onCancel(DialogInterface dialog) {
1546 cleanup();
1547 }
1548 });
1549 builder.setNegativeButton(getString(R.string.cancel_action),
1550 new Dialog.OnClickListener() {
1551 public void onClick(DialogInterface dialog, int which) {
1552 cleanup();
1553 }
1554 }
1555 );
1556 builder.setPositiveButton(getString(R.string.rename_action),
1557 new Dialog.OnClickListener() {
1558 public void onClick(DialogInterface dialog, int which) {
1559 changeFolderName();
1560 }
1561 }
1562 );
1563 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001564
1565 final AlertDialog dialog = builder.create();
1566 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1567 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001568 }
1569 });
1570
1571 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
1573
1574 private void changeFolderName() {
1575 final String name = mInput.getText().toString();
1576 if (!TextUtils.isEmpty(name)) {
1577 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001578 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 mFolderInfo.title = name;
1580 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1581
Joe Onorato9c1289c2009-08-17 11:03:03 -04001582 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001583 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001584 mModel.setWorkspaceDirty();
1585 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 } else {
1587 final FolderIcon folderIcon = (FolderIcon)
1588 mWorkspace.getViewForTag(mFolderInfo);
1589 if (folderIcon != null) {
1590 folderIcon.setText(name);
1591 getWorkspace().requestLayout();
1592 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001593 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001594 mModel.setWorkspaceDirty();
1595 mWorkspaceLoading = true;
1596 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 }
1598 }
1599 }
1600 cleanup();
1601 }
1602
1603 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 dismissDialog(DIALOG_RENAME_FOLDER);
1605 mWaitingForResult = false;
1606 mFolderInfo = null;
1607 }
1608 }
1609
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001610 void showAllAppsDialog() {
Joe Onorato7c312c12009-08-13 21:36:53 -07001611 mAllAppsVisible = true;
Joe Onorato006b25f2009-09-03 11:38:43 -07001612 mAllAppsGrid.show();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001613 mWorkspace.hide();
Joe Onorato7c312c12009-08-13 21:36:53 -07001614
1615 // TODO: fade these two too
1616 mDeleteZone.setVisibility(View.GONE);
1617 mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001618 }
1619
1620 void closeAllAppsDialog(boolean animated) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001621 if (mAllAppsVisible) {
1622 Log.d(LOG_TAG, "closing all apps");
Joe Onorato85a02a82009-09-08 12:34:22 -07001623 mAllAppsGrid.hide();
Joe Onorato7c312c12009-08-13 21:36:53 -07001624 mAllAppsVisible = false;
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001625 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1626 mWorkspace.show();
Joe Onorato7c312c12009-08-13 21:36:53 -07001627
1628 // TODO: fade these two too
1629 mDeleteZone.setVisibility(View.VISIBLE);
1630 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001631 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001632 }
1633
Joe Onorato7c312c12009-08-13 21:36:53 -07001634 void lockAllApps() {
1635 // TODO
1636 }
1637
1638 void unlockAllApps() {
1639 // TODO
1640 }
1641
Joe Onorato7404ee42009-07-31 11:54:44 -07001642 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 * Displays the shortcut creation dialog and launches, if necessary, the
1644 * appropriate activity.
1645 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001646 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001647 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1648 DialogInterface.OnShowListener {
1649
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 Dialog createDialog() {
1653 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1658 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001659 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 builder.setInverseBackgroundForced(true);
1662
1663 AlertDialog dialog = builder.create();
1664 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001665 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001666 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 return dialog;
1669 }
1670
1671 public void onCancel(DialogInterface dialog) {
1672 mWaitingForResult = false;
1673 cleanup();
1674 }
1675
Romain Guycbb89e42009-06-08 15:52:54 -07001676 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001677 }
1678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 dismissDialog(DIALOG_CREATE_SHORTCUT);
1681 }
1682
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001683 /**
1684 * Handle the action clicked in the "Add to home" dialog.
1685 */
1686 public void onClick(DialogInterface dialog, int which) {
1687 Resources res = getResources();
1688 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001689
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001690 switch (which) {
1691 case AddAdapter.ITEM_SHORTCUT: {
1692 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001693 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001694 break;
1695 }
Romain Guycbb89e42009-06-08 15:52:54 -07001696
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001697 case AddAdapter.ITEM_APPWIDGET: {
1698 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001699
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001700 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1701 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1702 // add the search widget
1703 ArrayList<AppWidgetProviderInfo> customInfo =
1704 new ArrayList<AppWidgetProviderInfo>();
1705 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1706 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1707 info.label = getString(R.string.group_search);
1708 info.icon = R.drawable.ic_search_widget;
1709 customInfo.add(info);
1710 pickIntent.putParcelableArrayListExtra(
1711 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1712 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1713 Bundle b = new Bundle();
1714 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1715 customExtras.add(b);
1716 pickIntent.putParcelableArrayListExtra(
1717 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1718 // start the pick activity
1719 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1720 break;
1721 }
Romain Guycbb89e42009-06-08 15:52:54 -07001722
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001723 case AddAdapter.ITEM_LIVE_FOLDER: {
1724 // Insert extra item to handle inserting folder
1725 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001726
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001727 ArrayList<String> shortcutNames = new ArrayList<String>();
1728 shortcutNames.add(res.getString(R.string.group_folder));
1729 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001730
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001731 ArrayList<ShortcutIconResource> shortcutIcons =
1732 new ArrayList<ShortcutIconResource>();
1733 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1734 R.drawable.ic_launcher_folder));
1735 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1736
1737 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1738 pickIntent.putExtra(Intent.EXTRA_INTENT,
1739 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1740 pickIntent.putExtra(Intent.EXTRA_TITLE,
1741 getText(R.string.title_select_live_folder));
1742 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001743
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001744 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1745 break;
1746 }
1747
1748 case AddAdapter.ITEM_WALLPAPER: {
1749 startWallpaper();
1750 break;
1751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 }
1753 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001754
1755 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001756 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 }
1758
1759 /**
Joe Onorato85a02a82009-09-08 12:34:22 -07001760 * Implementation of the method from SwipeController.SwipeListener.
1761 */
1762 public void onStartSwipe() {
1763 switch (mMode) {
1764 case MODE_WORKSPACE:
1765 mWorkspace.enableChildrenCache();
1766 break;
1767 case MODE_ALL_APPS:
1768 break;
1769 case MODE_ALL_APPS_ZOOMED:
1770 break;
1771 }
1772 }
1773
1774 /**
1775 * Implementation of the method from SwipeController.SwipeListener.
1776 *
1777 * @param amount The final value of the swipe (-1, 0 or 1)
1778 */
1779 public void onFinishSwipe(int amount) {
1780 switch (mMode) {
1781 case MODE_WORKSPACE:
1782 if (amount == -1) {
1783 mWorkspace.clearChildrenCache();
1784 mMode = MODE_ALL_APPS;
1785 mSwipeController.setRange(0, 1);
1786 }
1787 break;
1788 case MODE_ALL_APPS:
1789 if (amount == 1) {
1790 mWorkspace.clearChildrenCache();
1791 mMode = MODE_WORKSPACE;
1792 mSwipeController.setRange(-1, 0);
1793 }
1794 break;
1795 case MODE_ALL_APPS_ZOOMED:
1796 break;
1797 }
1798 }
1799
1800 /**
1801 * Implementation of the method from SwipeController.SwipeListener.
1802 */
1803 public void onSwipe(float amount) {
1804 switch (mMode) {
1805 case MODE_WORKSPACE:
1806 // We can open the all apps view.
1807 // 0 == workspace is showing
1808 // -1 == all apps is showing
1809 setWorkspaceAndAllAppsScale(-amount);
1810 break;
1811 case MODE_ALL_APPS:
1812 // We can close it, or (someday) zoom it further
1813 // 0 == all apps showing
1814 // 1 == workspace is showing
1815 setWorkspaceAndAllAppsScale(1-amount);
1816 break;
1817 }
1818 }
1819
1820 /**
1821 * Set the scale factor for the workspace and the all apps grid.
1822 *
1823 * @param amount A float between 0 and 1, where:
1824 * 0 == workspace is showing and
1825 * 1 == the all apps grid is showing.
1826 */
1827 private void setWorkspaceAndAllAppsScale(float amount) {
1828 //Log.d("setWorkspaceAndAllAppsScale", "setWorkspaceAndAllAppsScale amount=" + amount);
1829 if (amount < 0.001f) {
1830 amount = 0.0f;
1831 }
1832 if (amount > 0.999f) {
1833 amount = 1.0f;
1834 mWorkspace.setVisibility(View.INVISIBLE);
1835 } else {
1836 mWorkspace.setVisibility(View.VISIBLE);
1837 }
1838 mWorkspace.setScale(1-amount);
1839 mAllAppsGrid.setScale(amount);
1840 }
1841
1842 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001845 public int getCurrentWorkspaceScreen() {
1846 return mWorkspace.getCurrentScreen();
1847 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001848
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849 /**
1850 * Refreshes the shortcuts shown on the workspace.
1851 *
1852 * Implementation of the method from LauncherModel.Callbacks.
1853 */
1854 public void startBinding() {
1855 final Workspace workspace = mWorkspace;
1856 int count = workspace.getChildCount();
1857 for (int i = 0; i < count; i++) {
1858 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1859 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001860
Joe Onorato9c1289c2009-08-17 11:03:03 -04001861 if (DEBUG_USER_INTERFACE) {
1862 android.widget.Button finishButton = new android.widget.Button(this);
1863 finishButton.setText("Finish");
1864 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1865
1866 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1867 public void onClick(View v) {
1868 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001870 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 }
1872 }
1873
1874 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001875 * Bind the items start-end from the list.
1876 *
1877 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001879 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1880
1881 final Workspace workspace = mWorkspace;
1882
1883 for (int i=start; i<end; i++) {
1884 final ItemInfo item = shortcuts.get(i);
1885 mDesktopItems.add(item);
1886 switch (item.itemType) {
1887 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1888 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1889 final View shortcut = createShortcut((ApplicationInfo) item);
1890 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1891 false);
1892 break;
1893 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1894 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1895 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1896 (UserFolderInfo) item);
1897 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1898 false);
1899 break;
1900 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1901 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1902 R.layout.live_folder_icon, this,
1903 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1904 (LiveFolderInfo) item);
1905 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1906 false);
1907 break;
1908 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1909 final int screen = workspace.getCurrentScreen();
1910 final View view = mInflater.inflate(R.layout.widget_search,
1911 (ViewGroup) workspace.getChildAt(screen), false);
1912
1913 Search search = (Search) view.findViewById(R.id.widget_search);
1914 search.setLauncher(this);
1915
1916 final Widget widget = (Widget) item;
1917 view.setTag(widget);
1918
1919 workspace.addWidget(view, widget, false);
1920 break;
1921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 }
1923
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 }
1926
Joe Onorato9c1289c2009-08-17 11:03:03 -04001927 /**
1928 * Implementation of the method from LauncherModel.Callbacks.
1929 */
1930 void bindFolders(HashMap<Long, FolderInfo> folders) {
1931 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001933
1934 /**
1935 * Add the views for a widget to the workspace.
1936 *
1937 * Implementation of the method from LauncherModel.Callbacks.
1938 */
1939 public void bindAppWidget(LauncherAppWidgetInfo item) {
1940 final Workspace workspace = mWorkspace;
1941
1942 final int appWidgetId = item.appWidgetId;
1943 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1944 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1945
1946 if (true) {
1947 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1948 appWidgetId, appWidgetInfo));
1949 }
1950
1951 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1952 item.hostView.setTag(item);
1953
1954 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1955 item.cellY, item.spanX, item.spanY, false);
1956
1957 workspace.requestLayout();
1958
1959 mDesktopItems.add(item);
1960 }
1961
1962 /**
1963 * Callback saying that there aren't any more items to bind.
1964 *
1965 * Implementation of the method from LauncherModel.Callbacks.
1966 */
1967 public void finishBindingItems() {
1968 if (mSavedState != null) {
1969 if (!mWorkspace.hasFocus()) {
1970 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1971 }
1972
1973 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1974 if (userFolders != null) {
1975 for (long folderId : userFolders) {
1976 final FolderInfo info = mFolders.get(folderId);
1977 if (info != null) {
1978 openFolder(info);
1979 }
1980 }
1981 final Folder openFolder = mWorkspace.getOpenFolder();
1982 if (openFolder != null) {
1983 openFolder.requestFocus();
1984 }
1985 }
1986
1987 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1988 if (allApps) {
1989 showAllAppsDialog();
1990 }
1991
1992 mSavedState = null;
1993 }
1994
1995 if (mSavedInstanceState != null) {
1996 super.onRestoreInstanceState(mSavedInstanceState);
1997 mSavedInstanceState = null;
1998 }
1999
2000 /* TODO
2001 if (mAllAppsVisible && !mDrawer.hasFocus()) {
2002 mDrawer.requestFocus();
2003 }
2004 */
2005
2006 Log.d(TAG, "finishBindingItems done");
2007 mWorkspaceLoading = false;
2008 }
2009
2010 /**
2011 * Add the icons for all apps.
2012 *
2013 * Implementation of the method from LauncherModel.Callbacks.
2014 */
2015 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
2016 Log.d(LOG_TAG, "got info for " + apps.size() + " apps");
2017 mAllAppsList = apps;
2018 mAllAppsGrid.setApps(mAllAppsList);
2019 }
2020
2021 /**
2022 * A package was installed.
2023 *
2024 * Implementation of the method from LauncherModel.Callbacks.
2025 */
2026 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2027 removeDialog(DIALOG_CREATE_SHORTCUT);
2028 }
2029
2030 /**
2031 * A package was updated.
2032 *
2033 * Implementation of the method from LauncherModel.Callbacks.
2034 */
2035 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2036 removeDialog(DIALOG_CREATE_SHORTCUT);
2037 mWorkspace.updateShortcutsForPackage(packageName);
2038 }
2039
2040 /**
2041 * A package was uninstalled.
2042 *
2043 * Implementation of the method from LauncherModel.Callbacks.
2044 */
2045 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2046 removeDialog(DIALOG_CREATE_SHORTCUT);
2047 mWorkspace.removeShortcutsForPackage(packageName);
2048 }
2049
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050}