blob: acfccb17a6ebd65e65433b0719149d389b89bc78 [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 Hackborn107f8392009-08-05 21:32:16 -070025import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080027import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080029import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070033import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.ActivityInfo;
36import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070039import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080042import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080043import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080045import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080047import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.os.Parcelable;
49import android.os.RemoteException;
50import android.os.ServiceManager;
Joe Onoratobe386092009-11-17 17:32:16 -080051import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070052import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.text.Selection;
54import android.text.SpannableStringBuilder;
55import android.text.TextUtils;
56import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070057import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080059import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.view.KeyEvent;
61import android.view.LayoutInflater;
62import android.view.Menu;
63import android.view.MenuItem;
64import android.view.View;
65import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.view.View.OnLongClickListener;
67import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.widget.TextView;
70import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080071import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080072import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080073import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070074import android.appwidget.AppWidgetManager;
75import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040078import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070079import java.io.DataOutputStream;
80import java.io.FileNotFoundException;
81import java.io.IOException;
82import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
84/**
85 * Default launcher application.
86 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040087public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080088 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080089 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 static final boolean LOGD = false;
91
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 static final boolean PROFILE_STARTUP = false;
93 static final boolean PROFILE_ROTATE = false;
94 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070095
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 private static final int WALLPAPER_SCREENS_SPAN = 2;
97
98 private static final int MENU_GROUP_ADD = 1;
99 private static final int MENU_ADD = Menu.FIRST + 1;
100 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
101 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
102 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700103 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104
105 private static final int REQUEST_CREATE_SHORTCUT = 1;
106 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700107 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 private static final int REQUEST_PICK_APPLICATION = 6;
109 private static final int REQUEST_PICK_SHORTCUT = 7;
110 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700111 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700112 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
114 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
115
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700116 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
117 static final String SEARCH_WIDGET = "search_widget";
118
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800119 static final int SCREEN_COUNT = 5;
120 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700122 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato7c312c12009-08-13 21:36:53 -0700124 static final int DIALOG_CREATE_SHORTCUT = 1;
125 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Romain Guy98d01652009-06-30 16:21:04 -0700127 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 // Type: int
130 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
131 // Type: boolean
132 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
133 // Type: long
134 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
149 // Type: int[]
150 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
151 // Type: boolean
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
153 // Type: long
154 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
155
Joe Onorato9c1289c2009-08-17 11:03:03 -0400156 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800159 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800161 private final BroadcastReceiver mCloseSystemDialogsReceiver
162 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800163 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private LayoutInflater mInflater;
166
Joe Onorato00acb122009-08-04 16:04:30 -0400167 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700169
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private AppWidgetManager mAppWidgetManager;
171 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private CellLayout.CellInfo mAddItemCellInfo;
174 private CellLayout.CellInfo mMenuAddInfo;
175 private final int[] mCellCoordinates = new int[2];
176 private FolderInfo mFolderInfo;
177
Joe Onorato7c312c12009-08-13 21:36:53 -0700178 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700179 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700180 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private Bundle mSavedState;
183
184 private SpannableStringBuilder mDefaultKeySsb = null;
185
Mike LeBeau736cf282009-07-02 17:46:59 -0700186 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 private boolean mWorkspaceLoading = true;
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private boolean mRestoring;
191 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
193 private Bundle mSavedInstanceState;
194
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 private LauncherModel mModel;
196
Romain Guy84f296c2009-11-04 15:00:44 -0800197 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
198 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700199
Romain Guy1fbc1c82009-11-09 20:43:08 -0800200 private ImageView mPreviousView;
201 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 @Override
204 protected void onCreate(Bundle savedInstanceState) {
205 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700206
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400208 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700211 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
213 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700214
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 if (PROFILE_STARTUP) {
216 android.os.Debug.startMethodTracing("/sdcard/launcher");
217 }
218
219 checkForLocaleChange();
220 setWallpaperDimension();
221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 setContentView(R.layout.launcher);
223 setupViews();
224
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800225 registerContentObservers();
226
Joe Onorato7c312c12009-08-13 21:36:53 -0700227 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 mSavedState = savedInstanceState;
230 restoreState(mSavedState);
231
232 if (PROFILE_STARTUP) {
233 android.os.Debug.stopMethodTracing();
234 }
235
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700236 // We have a new AllAppsView, we need to re-bind everything, and it could have
237 // changed in our absence.
238 mModel.setAllAppsDirty();
239 mModel.setWorkspaceDirty();
240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 }
244
245 // For handling default keys
246 mDefaultKeySsb = new SpannableStringBuilder();
247 Selection.setSelection(mDefaultKeySsb, 0);
248 }
Romain Guycbb89e42009-06-08 15:52:54 -0700249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700251 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
252 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 final Configuration configuration = getResources().getConfiguration();
255
Romain Guy98d01652009-06-30 16:21:04 -0700256 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 final String locale = configuration.locale.toString();
258
Romain Guy98d01652009-06-30 16:21:04 -0700259 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 final int mcc = configuration.mcc;
261
Romain Guy98d01652009-06-30 16:21:04 -0700262 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 final int mnc = configuration.mnc;
264
Romain Guy84f296c2009-11-04 15:00:44 -0800265 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266
Romain Guy84f296c2009-11-04 15:00:44 -0800267 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700268 localeConfiguration.locale = locale;
269 localeConfiguration.mcc = mcc;
270 localeConfiguration.mnc = mnc;
271
272 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700274 }
275 }
276
277 private static class LocaleConfiguration {
278 public String locale;
279 public int mcc = -1;
280 public int mnc = -1;
281 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700282
Romain Guy98d01652009-06-30 16:21:04 -0700283 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
284 DataInputStream in = null;
285 try {
286 in = new DataInputStream(context.openFileInput(PREFERENCES));
287 configuration.locale = in.readUTF();
288 configuration.mcc = in.readInt();
289 configuration.mnc = in.readInt();
290 } catch (FileNotFoundException e) {
291 // Ignore
292 } catch (IOException e) {
293 // Ignore
294 } finally {
295 if (in != null) {
296 try {
297 in.close();
298 } catch (IOException e) {
299 // Ignore
300 }
301 }
302 }
303 }
304
305 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
306 DataOutputStream out = null;
307 try {
308 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
309 out.writeUTF(configuration.locale);
310 out.writeInt(configuration.mcc);
311 out.writeInt(configuration.mnc);
312 out.flush();
313 } catch (FileNotFoundException e) {
314 // Ignore
315 } catch (IOException e) {
316 //noinspection ResultOfMethodCallIgnored
317 context.getFileStreamPath(PREFERENCES).delete();
318 } finally {
319 if (out != null) {
320 try {
321 out.close();
322 } catch (IOException e) {
323 // Ignore
324 }
325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 }
327 }
328
329 static int getScreen() {
330 synchronized (sLock) {
331 return sScreen;
332 }
333 }
334
335 static void setScreen(int screen) {
336 synchronized (sLock) {
337 sScreen = screen;
338 }
339 }
340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700342 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343
344 Display display = getWindowManager().getDefaultDisplay();
345 boolean isPortrait = display.getWidth() < display.getHeight();
346
347 final int width = isPortrait ? display.getWidth() : display.getHeight();
348 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700349 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 }
351
352 @Override
353 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700354 mWaitingForResult = false;
355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 // The pattern used here is that a user PICKs a specific application,
357 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
360 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700361
Romain Guy94dabf12009-07-21 10:55:43 -0700362 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 switch (requestCode) {
364 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400365 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 break;
367 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700368 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 break;
370 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400371 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 break;
373 case REQUEST_PICK_LIVE_FOLDER:
374 addLiveFolder(data);
375 break;
376 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400377 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700379 case REQUEST_PICK_APPWIDGET:
380 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400383 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700385 case REQUEST_PICK_WALLPAPER:
386 // We just wanted the activity result here so we can clear mWaitingForResult
387 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 }
Romain Guy18042c82009-11-06 11:44:55 -0800389 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
390 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
391 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700392 // Clean up the appWidgetId if we canceled
393 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
394 if (appWidgetId != -1) {
395 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 }
397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
399
400 @Override
401 protected void onResume() {
402 super.onResume();
403
Joe Onorato7e4ed992009-12-03 13:10:49 -0800404 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
405 registerReceiver(mCloseSystemDialogsReceiver, filter);
406
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400408 mWorkspaceLoading = true;
409 mModel.startLoader(this, true);
410 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700412
Mike LeBeau736cf282009-07-02 17:46:59 -0700413 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
414 // onNewIntent), then close the search dialog if needed, because it probably
415 // came from the user pressing 'home' (rather than, for example, pressing 'back').
416 if (mIsNewIntent) {
417 // Post to a handler so that this happens after the search dialog tries to open
418 // itself again.
419 mWorkspace.post(new Runnable() {
420 public void run() {
Bjorn Bringert98047dc2009-11-21 22:08:04 +0000421 stopSearch();
Mike LeBeau736cf282009-07-02 17:46:59 -0700422 }
423 });
424 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700425
Mike LeBeau736cf282009-07-02 17:46:59 -0700426 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 }
428
429 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 protected void onPause() {
431 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800432 dismissPreview(mPreviousView);
433 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800434 mDragController.cancelDrag();
Joe Onorato7e4ed992009-12-03 13:10:49 -0800435 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700436 }
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700438 @Override
439 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400440 // Flag the loader to stop early before switching
441 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700442
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700443 if (PROFILE_ROTATE) {
444 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
445 }
446 return null;
447 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 private boolean acceptFilter() {
450 final InputMethodManager inputManager = (InputMethodManager)
451 getSystemService(Context.INPUT_METHOD_SERVICE);
452 return !inputManager.isFullscreenMode();
453 }
454
455 @Override
456 public boolean onKeyDown(int keyCode, KeyEvent event) {
457 boolean handled = super.onKeyDown(keyCode, event);
458 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
459 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
460 keyCode, event);
461 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700462 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700463 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700464 // showSearchDialog()
465 // If there are multiple keystrokes before the search dialog takes focus,
466 // onSearchRequested() will be called for every keystroke,
467 // but it is idempotent, so it's fine.
468 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470 }
471
472 return handled;
473 }
474
Karl Rosaen138a0412009-04-23 19:00:21 -0700475 private String getTypedText() {
476 return mDefaultKeySsb.toString();
477 }
478
479 private void clearTypedText() {
480 mDefaultKeySsb.clear();
481 mDefaultKeySsb.clearSpans();
482 Selection.setSelection(mDefaultKeySsb, 0);
483 }
484
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 /**
486 * Restores the previous state, if it exists.
487 *
488 * @param savedState The previous state.
489 */
490 private void restoreState(Bundle savedState) {
491 if (savedState == null) {
492 return;
493 }
494
Joe Onorato3a8820b2009-11-10 15:06:42 -0800495 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
496 if (allApps) {
497 showAllApps(false);
498 }
499
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800500 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
501 if (currentScreen > -1) {
502 mWorkspace.setCurrentScreen(currentScreen);
503 }
504
505 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
506 if (addScreen > -1) {
507 mAddItemCellInfo = new CellLayout.CellInfo();
508 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
509 addItemCellInfo.valid = true;
510 addItemCellInfo.screen = addScreen;
511 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
512 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
513 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
514 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
515 addItemCellInfo.findVacantCellsFromOccupied(
516 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
517 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
518 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
519 mRestoring = true;
520 }
521
522 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
523 if (renameFolder) {
524 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400525 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 mRestoring = true;
527 }
528 }
529
530 /**
531 * Finds all the views we need and configure them properly.
532 */
533 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400534 DragController dragController = mDragController;
535
Romain Guyb1b69f52009-08-10 15:10:15 -0700536 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400537 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538
Joe Onorato7c312c12009-08-13 21:36:53 -0700539 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700540 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700541 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700542 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700543 // Manage focusability manually since this thing is always visible
544 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700545
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
547 final Workspace workspace = mWorkspace;
548
Joe Onorato7c312c12009-08-13 21:36:53 -0700549 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
550 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551
Joe Onorato7c312c12009-08-13 21:36:53 -0700552 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700553 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700554 mHandleView.setOnClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800555
556 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
557 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
558
559 Drawable previous = mPreviousView.getDrawable();
560 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800561 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562
Joe Onorato0d44e942009-11-16 18:20:51 -0800563 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800564 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800565 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800566 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800567
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400569 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571
572 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400573 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700574 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575
Joe Onorato00acb122009-08-04 16:04:30 -0400576 dragController.setDragScoller(workspace);
577 dragController.setDragListener(deleteZone);
578 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700579
Joe Onorato00acb122009-08-04 16:04:30 -0400580 // The order here is bottom to top.
581 dragController.addDropTarget(workspace);
582 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 }
584
Romain Guy8a73c512009-11-09 19:19:59 -0800585 @SuppressWarnings({"UnusedDeclaration"})
586 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800587 if (!isAllAppsVisible()) {
588 mWorkspace.scrollLeft();
589 }
Romain Guy8a73c512009-11-09 19:19:59 -0800590 }
591
592 @SuppressWarnings({"UnusedDeclaration"})
593 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800594 if (!isAllAppsVisible()) {
595 mWorkspace.scrollRight();
596 }
Romain Guy8a73c512009-11-09 19:19:59 -0800597 }
598
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 /**
600 * Creates a view representing a shortcut.
601 *
602 * @param info The data structure describing the shortcut.
603 *
604 * @return A View inflated from R.layout.application.
605 */
606 View createShortcut(ApplicationInfo info) {
607 return createShortcut(R.layout.application,
608 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
609 }
610
611 /**
612 * Creates a view representing a shortcut inflated from the specified resource.
613 *
614 * @param layoutResId The id of the XML layout used to create the shortcut.
615 * @param parent The group the shortcut belongs to.
616 * @param info The data structure describing the shortcut.
617 *
618 * @return A View inflated from layoutResId.
619 */
620 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
621 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
622
Joe Onorato5162ea92009-09-03 09:39:42 -0700623 if (info.icon == null) {
624 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
625 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700627 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 info.filtered = true;
629 }
630
631 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
632 favorite.setText(info.title);
633 favorite.setTag(info);
634 favorite.setOnClickListener(this);
635
636 return favorite;
637 }
638
639 /**
640 * Add an application shortcut to the workspace.
641 *
642 * @param data The intent describing the application.
643 * @param cellInfo The position on screen where to create the shortcut.
644 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400645 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 cellInfo.screen = mWorkspace.getCurrentScreen();
647 if (!findSingleSlot(cellInfo)) return;
648
Romain Guy73b979d2009-06-09 12:57:21 -0700649 final ApplicationInfo info = infoFromApplicationIntent(context, data);
650 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700652 }
653 }
654
655 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 ComponentName component = data.getComponent();
657 PackageManager packageManager = context.getPackageManager();
658 ActivityInfo activityInfo = null;
659 try {
660 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
661 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800662 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
Romain Guycbb89e42009-06-08 15:52:54 -0700664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 if (activityInfo != null) {
666 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 itemInfo.title = activityInfo.loadLabel(packageManager);
669 if (itemInfo.title == null) {
670 itemInfo.title = activityInfo.name;
671 }
Romain Guycbb89e42009-06-08 15:52:54 -0700672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
674 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
675 itemInfo.icon = activityInfo.loadIcon(packageManager);
676 itemInfo.container = ItemInfo.NO_ID;
677
Romain Guy73b979d2009-06-09 12:57:21 -0700678 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
Romain Guy73b979d2009-06-09 12:57:21 -0700680
681 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
Romain Guycbb89e42009-06-08 15:52:54 -0700683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 /**
685 * Add a shortcut to the workspace.
686 *
687 * @param data The intent describing the shortcut.
688 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 cellInfo.screen = mWorkspace.getCurrentScreen();
692 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
695
696 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
699 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 }
701 }
702
Romain Guycbb89e42009-06-08 15:52:54 -0700703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700705 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 * @param data The intent describing the appWidgetId.
708 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700713
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800714 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700715
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700717
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700720 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 // Try finding open space on Launcher screen
723 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800724 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
725 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
726 return;
727 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700729 // Build Launcher-specific widget info and save to database
730 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 launcherInfo.spanX = spans[0];
732 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 LauncherModel.addItemToDatabase(this, launcherInfo,
735 LauncherSettings.Favorites.CONTAINER_DESKTOP,
736 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
737
738 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700740
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700742 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700743
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700744 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700746
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400748 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 }
750 }
Romain Guycbb89e42009-06-08 15:52:54 -0700751
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
753 mDesktopItems.remove(launcherInfo);
754 launcherInfo.hostView = null;
755 }
756
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700757 public LauncherAppWidgetHost getAppWidgetHost() {
758 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
Romain Guycbb89e42009-06-08 15:52:54 -0700760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 static ApplicationInfo addShortcut(Context context, Intent data,
762 CellLayout.CellInfo cellInfo, boolean notify) {
763
Romain Guy73b979d2009-06-09 12:57:21 -0700764 final ApplicationInfo info = infoFromShortcutIntent(context, data);
765 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
766 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
767
768 return info;
769 }
770
771 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
773 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
774 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
775
776 Drawable icon = null;
777 boolean filtered = false;
778 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700779 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780
781 if (bitmap != null) {
782 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
783 filtered = true;
784 customIcon = true;
785 } else {
786 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700787 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700789 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 final PackageManager packageManager = context.getPackageManager();
791 Resources resources = packageManager.getResourcesForApplication(
792 iconResource.packageName);
793 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
794 icon = resources.getDrawable(id);
795 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800796 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 }
798 }
799 }
800
801 if (icon == null) {
802 icon = context.getPackageManager().getDefaultActivityIcon();
803 }
804
805 final ApplicationInfo info = new ApplicationInfo();
806 info.icon = icon;
807 info.filtered = filtered;
808 info.title = name;
809 info.intent = intent;
810 info.customIcon = customIcon;
811 info.iconResource = iconResource;
812
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 return info;
814 }
815
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800816 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800817 getWindow().closeAllPanels();
818
819 try {
820 dismissDialog(DIALOG_CREATE_SHORTCUT);
821 // Unlock the workspace if the dialog was showing
822 } catch (Exception e) {
823 // An exception is thrown if the dialog is not visible, which is fine
824 }
825
826 try {
827 dismissDialog(DIALOG_RENAME_FOLDER);
828 // Unlock the workspace if the dialog was showing
829 } catch (Exception e) {
830 // An exception is thrown if the dialog is not visible, which is fine
831 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800832
833 // Whatever we were doing is hereby canceled.
834 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800835 }
836
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 @Override
838 protected void onNewIntent(Intent intent) {
839 super.onNewIntent(intent);
840
841 // Close the menu
842 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800843 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800844 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700845
Mike LeBeau736cf282009-07-02 17:46:59 -0700846 // Set this flag so that onResume knows to close the search dialog if it's open,
847 // because this was a new intent (thus a press of 'home' or some such) rather than
848 // for example onResume being called when the user pressed the 'back' button.
849 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850
Joe Onorato14f122b2009-11-19 14:06:36 -0800851 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
852 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
853 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800854 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800855 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800856 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800857 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700858
Joe Onorato3a8820b2009-11-10 15:06:42 -0800859 final View v = getWindow().peekDecorView();
860 if (v != null && v.getWindowToken() != null) {
861 InputMethodManager imm = (InputMethodManager)getSystemService(
862 INPUT_METHOD_SERVICE);
863 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
865 }
866 }
867
868 @Override
869 protected void onRestoreInstanceState(Bundle savedInstanceState) {
870 // Do not call super here
871 mSavedInstanceState = savedInstanceState;
872 }
873
874 @Override
875 protected void onSaveInstanceState(Bundle outState) {
876 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
877
878 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
879 if (folders.size() > 0) {
880 final int count = folders.size();
881 long[] ids = new long[count];
882 for (int i = 0; i < count; i++) {
883 final FolderInfo info = folders.get(i).getInfo();
884 ids[i] = info.id;
885 }
886 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
887 } else {
888 super.onSaveInstanceState(outState);
889 }
890
Joe Onoratofb0ca672009-09-14 17:55:46 -0400891 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800892 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700894 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895
896 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
897 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
898 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
899
900 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
901 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
902 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
903 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
904 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
905 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
906 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
907 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
908 layout.getOccupiedCells());
909 }
910
911 if (mFolderInfo != null && mWaitingForResult) {
912 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
913 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
914 }
915 }
916
917 @Override
918 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700920
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700922 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800924 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 }
926
927 TextKeyListener.getInstance().release();
928
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 unbindDesktopItems();
932 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800933
934 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800935
936 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800937 dismissPreview(mNextView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
940 @Override
941 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700942 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 super.startActivityForResult(intent, requestCode);
944 }
945
946 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700947 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700949
Joe Onorato7bb17492009-09-24 17:51:01 -0700950 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700951
Karl Rosaen138a0412009-04-23 19:00:21 -0700952 // Slide the search widget to the top, if it's on the current screen,
953 // otherwise show the search dialog immediately.
954 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
955 if (searchWidget == null) {
956 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
957 } else {
958 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
959 // show the currently typed text in the search widget while sliding
960 searchWidget.setQuery(getTypedText());
961 }
962 }
Romain Guycbb89e42009-06-08 15:52:54 -0700963
Karl Rosaen138a0412009-04-23 19:00:21 -0700964 /**
965 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700966 *
967 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700968 */
Romain Guycbb89e42009-06-08 15:52:54 -0700969 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700971
Karl Rosaen138a0412009-04-23 19:00:21 -0700972 if (initialQuery == null) {
973 // Use any text typed in the launcher as the initial query
974 initialQuery = getTypedText();
975 clearTypedText();
976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 if (appSearchData == null) {
978 appSearchData = new Bundle();
979 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
980 }
Romain Guycbb89e42009-06-08 15:52:54 -0700981
Karl Rosaen138a0412009-04-23 19:00:21 -0700982 final SearchManager searchManager =
983 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
984
985 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
986 if (searchWidget != null) {
987 // This gets called when the user leaves the search dialog to go back to
988 // the Launcher.
989 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
990 public void onCancel() {
991 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700992 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700993 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700994 });
995 }
Romain Guycbb89e42009-06-08 15:52:54 -0700996
Karl Rosaen138a0412009-04-23 19:00:21 -0700997 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700998 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
1000
Karl Rosaen138a0412009-04-23 19:00:21 -07001001 /**
1002 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001003 */
Romain Guy5a941392009-04-28 15:18:25 -07001004 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001005 // Close search dialog
1006 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001007 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001008 // Restore search widget to its normal position
1009 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1010 if (searchWidget != null) {
1011 searchWidget.stopSearch(false);
1012 }
1013 }
Romain Guycbb89e42009-06-08 15:52:54 -07001014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 @Override
1016 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017 if (isWorkspaceLocked()) {
1018 return false;
1019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020
1021 super.onCreateOptionsMenu(menu);
1022 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1023 .setIcon(android.R.drawable.ic_menu_add)
1024 .setAlphabeticShortcut('A');
1025 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1026 .setIcon(android.R.drawable.ic_menu_gallery)
1027 .setAlphabeticShortcut('W');
1028 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1029 .setIcon(android.R.drawable.ic_search_category_default)
1030 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1031 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1032 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1033 .setAlphabeticShortcut('N');
1034
1035 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001036 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1037 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038
1039 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1040 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1041 .setIntent(settings);
1042
1043 return true;
1044 }
1045
1046 @Override
1047 public boolean onPrepareOptionsMenu(Menu menu) {
1048 super.onPrepareOptionsMenu(menu);
1049
1050 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1051 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1052
1053 return true;
1054 }
1055
1056 @Override
1057 public boolean onOptionsItemSelected(MenuItem item) {
1058 switch (item.getItemId()) {
1059 case MENU_ADD:
1060 addItems();
1061 return true;
1062 case MENU_WALLPAPER_SETTINGS:
1063 startWallpaper();
1064 return true;
1065 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001066 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 return true;
1068 case MENU_NOTIFICATIONS:
1069 showNotifications();
1070 return true;
1071 }
1072
1073 return super.onOptionsItemSelected(item);
1074 }
Romain Guycbb89e42009-06-08 15:52:54 -07001075
Karl Rosaen138a0412009-04-23 19:00:21 -07001076 /**
1077 * Indicates that we want global search for this activity by setting the globalSearch
1078 * argument for {@link #startSearch} to true.
1079 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001081 @Override
1082 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001083 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001084 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001085 }
1086
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087 public boolean isWorkspaceLocked() {
1088 return mWorkspaceLoading || mWaitingForResult;
1089 }
1090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001092 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 showAddDialog(mMenuAddInfo);
1094 }
1095
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001096 void addAppWidget(Intent data) {
1097 // TODO: catch bad widget exception when sent
1098 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001100 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1101 if (SEARCH_WIDGET.equals(customWidget)) {
1102 // We don't need this any more, since this isn't a real app widget.
1103 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1104 // add the search widget
1105 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001107 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1108
1109 if (appWidget.configure != null) {
1110 // Launch over to configure widget, if needed
1111 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1112 intent.setComponent(appWidget.configure);
1113 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1114
1115 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1116 } else {
1117 // Otherwise just add it
1118 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1119 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 }
1121 }
Romain Guycbb89e42009-06-08 15:52:54 -07001122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 void addSearch() {
1124 final Widget info = Widget.makeSearch();
1125 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001126
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 final int[] xy = mCellCoordinates;
1128 final int spanX = info.spanX;
1129 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1134 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 final View view = mInflater.inflate(info.layoutResource, null);
1137 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001138 Search search = (Search) view.findViewById(R.id.widget_search);
1139 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1142 }
1143
Romain Guy73b979d2009-06-09 12:57:21 -07001144 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001145 // Handle case where user selected "Applications"
1146 String applicationName = getResources().getString(R.string.group_applications);
1147 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001148
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001149 if (applicationName != null && applicationName.equals(shortcutName)) {
1150 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1151 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001152
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001153 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1154 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001155 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001156 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001157 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 }
1160
1161 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001162 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001163 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001164 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001165
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001166 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001167 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001168 } else {
1169 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 }
1172
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 UserFolderInfo folderInfo = new UserFolderInfo();
1175 folderInfo.title = getText(R.string.folder_name);
1176
1177 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1178 cellInfo.screen = mWorkspace.getCurrentScreen();
1179 if (!findSingleSlot(cellInfo)) return;
1180
1181 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001182 LauncherModel.addItemToDatabase(this, folderInfo,
1183 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186
1187 // Create the view
1188 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1189 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1190 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 }
Romain Guycbb89e42009-06-08 15:52:54 -07001193
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 void removeFolder(FolderInfo folder) {
1195 mFolders.remove(folder.id);
1196 }
1197
1198 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 cellInfo.screen = mWorkspace.getCurrentScreen();
1200 if (!findSingleSlot(cellInfo)) return;
1201
1202 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1203
1204 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1206 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1208 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
1210 }
1211
1212 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1213 CellLayout.CellInfo cellInfo, boolean notify) {
1214
1215 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1216 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1217
1218 Drawable icon = null;
1219 boolean filtered = false;
1220 Intent.ShortcutIconResource iconResource = null;
1221
1222 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1223 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1224 try {
1225 iconResource = (Intent.ShortcutIconResource) extra;
1226 final PackageManager packageManager = context.getPackageManager();
1227 Resources resources = packageManager.getResourcesForApplication(
1228 iconResource.packageName);
1229 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1230 icon = resources.getDrawable(id);
1231 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001232 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 }
1234 }
1235
1236 if (icon == null) {
1237 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1238 }
1239
1240 final LiveFolderInfo info = new LiveFolderInfo();
1241 info.icon = icon;
1242 info.filtered = filtered;
1243 info.title = name;
1244 info.iconResource = iconResource;
1245 info.uri = data.getData();
1246 info.baseIntent = baseIntent;
1247 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1248 LiveFolders.DISPLAY_MODE_GRID);
1249
1250 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1251 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001252 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253
1254 return info;
1255 }
1256
1257 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1258 final int[] xy = new int[2];
1259 if (findSlot(cellInfo, xy, 1, 1)) {
1260 cellInfo.cellX = xy[0];
1261 cellInfo.cellY = xy[1];
1262 return true;
1263 }
1264 return false;
1265 }
1266
1267 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1268 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1269 boolean[] occupied = mSavedState != null ?
1270 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1271 cellInfo = mWorkspace.findAllVacantCells(occupied);
1272 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1273 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1274 return false;
1275 }
1276 }
1277 return true;
1278 }
1279
1280 private void showNotifications() {
1281 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1282 if (statusBar != null) {
1283 statusBar.expand();
1284 }
1285 }
1286
1287 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001288 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001290 Intent chooser = Intent.createChooser(pickWallpaper,
1291 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001292 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1293 // Removed in Eclair MR1
1294// WallpaperManager wm = (WallpaperManager)
1295// getSystemService(Context.WALLPAPER_SERVICE);
1296// WallpaperInfo wi = wm.getWallpaperInfo();
1297// if (wi != null && wi.getSettingsActivity() != null) {
1298// LabeledIntent li = new LabeledIntent(getPackageName(),
1299// R.string.configure_wallpaper, 0);
1300// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1301// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1302// }
Mike Clerona0618e42009-10-22 13:55:21 -07001303 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
1305
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001306 /**
1307 * Registers various content observers. The current implementation registers
1308 * only a favorites observer to keep track of the favorites applications.
1309 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001310 private void registerContentObservers() {
1311 ContentResolver resolver = getContentResolver();
1312 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1313 true, mWidgetObserver);
1314 }
1315
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 @Override
1317 public boolean dispatchKeyEvent(KeyEvent event) {
1318 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1319 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001321 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001322 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001323 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001324 dumpState();
1325 return true;
1326 }
1327 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001328 }
1329 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1330 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001331 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 return true;
1333 }
1334 }
1335
1336 return super.dispatchKeyEvent(event);
1337 }
1338
Joe Onorato88ec0992009-11-19 13:16:06 -08001339 @Override
1340 public void onBackPressed() {
1341 if (isAllAppsVisible()) {
1342 closeAllApps(true);
1343 } else {
1344 closeFolder();
1345 }
1346 dismissPreview(mPreviousView);
1347 dismissPreview(mNextView);
1348 }
1349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 private void closeFolder() {
1351 Folder folder = mWorkspace.getOpenFolder();
1352 if (folder != null) {
1353 closeFolder(folder);
1354 }
1355 }
1356
1357 void closeFolder(Folder folder) {
1358 folder.getInfo().opened = false;
1359 ViewGroup parent = (ViewGroup) folder.getParent();
1360 if (parent != null) {
1361 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001362 if (folder instanceof DropTarget) {
1363 // Live folders aren't DropTargets.
1364 mDragController.removeDropTarget((DropTarget)folder);
1365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 }
1367 folder.onClose();
1368 }
1369
1370 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001371 * Re-listen when widgets are reset.
1372 */
1373 private void onAppWidgetReset() {
1374 mAppWidgetHost.startListening();
1375 }
1376
1377 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001378 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1379 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001381 private void unbindDesktopItems() {
1382 for (ItemInfo item: mDesktopItems) {
1383 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 /**
1388 * Launches the intent referred by the clicked shortcut.
1389 *
1390 * @param v The view representing the clicked shortcut.
1391 */
1392 public void onClick(View v) {
1393 Object tag = v.getTag();
1394 if (tag instanceof ApplicationInfo) {
1395 // Open shortcut
1396 final Intent intent = ((ApplicationInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001397 int[] pos = new int[2];
1398 v.getLocationOnScreen(pos);
1399 intent.setSourceBounds(
1400 new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 startActivitySafely(intent);
1402 } else if (tag instanceof FolderInfo) {
1403 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001404 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001405 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001406 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001407 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001408 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 }
1411 }
1412
1413 void startActivitySafely(Intent intent) {
1414 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1415 try {
1416 startActivity(intent);
1417 } catch (ActivityNotFoundException e) {
1418 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1419 } catch (SecurityException e) {
1420 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001421 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1423 "or use the exported attribute for this activity.", e);
1424 }
1425 }
1426
1427 private void handleFolderClick(FolderInfo folderInfo) {
1428 if (!folderInfo.opened) {
1429 // Close any open folder
1430 closeFolder();
1431 // Open the requested folder
1432 openFolder(folderInfo);
1433 } else {
1434 // Find the open folder...
1435 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1436 int folderScreen;
1437 if (openFolder != null) {
1438 folderScreen = mWorkspace.getScreenForView(openFolder);
1439 // .. and close it
1440 closeFolder(openFolder);
1441 if (folderScreen != mWorkspace.getCurrentScreen()) {
1442 // Close any folder open on the current screen
1443 closeFolder();
1444 // Pull the folder onto this screen
1445 openFolder(folderInfo);
1446 }
1447 }
1448 }
1449 }
1450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 /**
1452 * Opens the user fodler described by the specified tag. The opening of the folder
1453 * is animated relative to the specified View. If the View is null, no animation
1454 * is played.
1455 *
1456 * @param folderInfo The FolderInfo describing the folder to open.
1457 */
1458 private void openFolder(FolderInfo folderInfo) {
1459 Folder openFolder;
1460
1461 if (folderInfo instanceof UserFolderInfo) {
1462 openFolder = UserFolder.fromXml(this);
1463 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001464 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 } else {
1466 return;
1467 }
1468
Joe Onorato00acb122009-08-04 16:04:30 -04001469 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 openFolder.setLauncher(this);
1471
1472 openFolder.bind(folderInfo);
1473 folderInfo.opened = true;
1474
1475 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1476 openFolder.onOpen();
1477 }
1478
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001480 switch (v.getId()) {
1481 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001482 if (!isAllAppsVisible()) {
1483 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1484 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1485 showPreviousPreview(v);
1486 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001487 return true;
1488 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001489 if (!isAllAppsVisible()) {
1490 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1491 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1492 showNextPreview(v);
1493 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001494 return true;
1495 }
1496
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 return false;
1499 }
1500
1501 if (!(v instanceof CellLayout)) {
1502 v = (View) v.getParent();
1503 }
1504
1505 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1506
1507 // This happens when long clicking an item with the dpad/trackball
1508 if (cellInfo == null) {
1509 return true;
1510 }
1511
1512 if (mWorkspace.allowLongPress()) {
1513 if (cellInfo.cell == null) {
1514 if (cellInfo.valid) {
1515 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001516 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 showAddDialog(cellInfo);
1518 }
1519 } else {
1520 if (!(cellInfo.cell instanceof Folder)) {
1521 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001522 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1523 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 mWorkspace.startDrag(cellInfo);
1525 }
1526 }
1527 }
1528 return true;
1529 }
1530
Romain Guye6b8e2f2009-11-10 11:56:55 -08001531 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001532 private void dismissPreview(final View v) {
1533 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001534 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001535 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1536 public void onDismiss() {
1537 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1538 int count = group.getChildCount();
1539 for (int i = 0; i < count; i++) {
1540 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1541 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001542 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1543 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1544
1545 v.setTag(R.id.workspace, null);
1546 v.setTag(R.id.icon, null);
1547 window.setOnDismissListener(null);
1548 }
1549 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001550 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001551 }
1552 v.setTag(null);
1553 }
1554
1555 private void showPreviousPreview(View anchor) {
1556 int current = mWorkspace.getCurrentScreen();
1557 if (current <= 0) return;
1558
Romain Guy9d31e9f2009-11-11 18:54:28 -08001559 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001560 }
1561
1562 private void showNextPreview(View anchor) {
1563 int current = mWorkspace.getCurrentScreen();
1564 if (current >= mWorkspace.getChildCount() - 1) return;
1565
Romain Guy9d31e9f2009-11-11 18:54:28 -08001566 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guya6abce82009-11-10 02:54:41 -08001567 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001568
Romain Guya6abce82009-11-10 02:54:41 -08001569 private void showPreviews(final View anchor, int start, int end) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001570 Resources resources = getResources();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001571
Romain Guya6abce82009-11-10 02:54:41 -08001572 Workspace workspace = mWorkspace;
1573 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001574
1575 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001576
1577 Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001578 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001579 int extraW = (int) ((r.left + r.right) * max);
1580 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001581
1582 int aW = cell.getWidth() - extraW;
1583 float w = aW / max;
1584
1585 int width = cell.getWidth();
1586 int height = cell.getHeight();
1587 int x = cell.getLeftPadding();
1588 int y = cell.getTopPadding();
1589 width -= (x + cell.getRightPadding());
1590 height -= (y + cell.getBottomPadding());
1591
1592 float scale = w / width;
1593
1594 int count = end - start;
1595
1596 final float sWidth = width * scale;
1597 float sHeight = height * scale;
1598
Romain Guye6b8e2f2009-11-10 11:56:55 -08001599 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001600
Romain Guye6b8e2f2009-11-10 11:56:55 -08001601 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1602 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001603
1604 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001605 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001606 cell = (CellLayout) workspace.getChildAt(i);
1607
Romain Guye6b8e2f2009-11-10 11:56:55 -08001608 Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
1609 Bitmap.Config.ARGB_8888);
1610
1611 Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001612 c.scale(scale, scale);
1613 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1614 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001615
Romain Guy9d31e9f2009-11-11 18:54:28 -08001616 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001617 image.setImageBitmap(bitmap);
1618 image.setTag(i);
1619 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001620 image.setOnFocusChangeListener(handler);
1621 image.setFocusable(true);
1622 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001623
1624 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001625 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1626
1627 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001628 }
Romain Guye6b8e2f2009-11-10 11:56:55 -08001629
1630 PopupWindow p = new PopupWindow(this);
1631 p.setContentView(preview);
1632 p.setWidth((int) (sWidth * count + extraW));
1633 p.setHeight((int) (sHeight + extraH));
1634 p.setAnimationStyle(R.style.AnimationPreview);
1635 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001636 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001637 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001638 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001639
Romain Guye6b8e2f2009-11-10 11:56:55 -08001640 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1641 public void onDismiss() {
1642 dismissPreview(anchor);
1643 }
1644 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001645
1646 anchor.setTag(p);
1647 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001648 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001649 }
1650
Romain Guy9d31e9f2009-11-11 18:54:28 -08001651 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001652 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001653
Romain Guye6b8e2f2009-11-10 11:56:55 -08001654 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001655 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001656 }
1657
1658 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001659 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001660 v.post(this);
1661 }
1662
1663 public void run() {
1664 dismissPreview(mAnchor);
1665 }
1666
1667 public void onFocusChange(View v, boolean hasFocus) {
1668 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001669 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001670 }
Romain Guya6abce82009-11-10 02:54:41 -08001671 }
1672 }
1673
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001674 View getDrawerHandle() {
1675 return mHandleView;
1676 }
1677
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 Workspace getWorkspace() {
1679 return mWorkspace;
1680 }
1681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 @Override
1683 protected Dialog onCreateDialog(int id) {
1684 switch (id) {
1685 case DIALOG_CREATE_SHORTCUT:
1686 return new CreateShortcut().createDialog();
1687 case DIALOG_RENAME_FOLDER:
1688 return new RenameFolder().createDialog();
1689 }
1690
1691 return super.onCreateDialog(id);
1692 }
1693
1694 @Override
1695 protected void onPrepareDialog(int id, Dialog dialog) {
1696 switch (id) {
1697 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 break;
1699 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001700 if (mFolderInfo != null) {
1701 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1702 final CharSequence text = mFolderInfo.title;
1703 input.setText(text);
1704 input.setSelection(0, text.length());
1705 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 break;
1707 }
1708 }
1709
1710 void showRenameDialog(FolderInfo info) {
1711 mFolderInfo = info;
1712 mWaitingForResult = true;
1713 showDialog(DIALOG_RENAME_FOLDER);
1714 }
1715
1716 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1717 mAddItemCellInfo = cellInfo;
1718 mWaitingForResult = true;
1719 showDialog(DIALOG_CREATE_SHORTCUT);
1720 }
1721
Romain Guy73b979d2009-06-09 12:57:21 -07001722 private void pickShortcut(int requestCode, int title) {
1723 Bundle bundle = new Bundle();
1724
1725 ArrayList<String> shortcutNames = new ArrayList<String>();
1726 shortcutNames.add(getString(R.string.group_applications));
1727 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1728
1729 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1730 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1731 R.drawable.ic_launcher_application));
1732 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1733
1734 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1735 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1736 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1737 pickIntent.putExtras(bundle);
1738
1739 startActivityForResult(pickIntent, requestCode);
1740 }
1741
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 private class RenameFolder {
1743 private EditText mInput;
1744
1745 Dialog createDialog() {
1746 mWaitingForResult = true;
1747 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1748 mInput = (EditText) layout.findViewById(R.id.folder_name);
1749
1750 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1751 builder.setIcon(0);
1752 builder.setTitle(getString(R.string.rename_folder_title));
1753 builder.setCancelable(true);
1754 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1755 public void onCancel(DialogInterface dialog) {
1756 cleanup();
1757 }
1758 });
1759 builder.setNegativeButton(getString(R.string.cancel_action),
1760 new Dialog.OnClickListener() {
1761 public void onClick(DialogInterface dialog, int which) {
1762 cleanup();
1763 }
1764 }
1765 );
1766 builder.setPositiveButton(getString(R.string.rename_action),
1767 new Dialog.OnClickListener() {
1768 public void onClick(DialogInterface dialog, int which) {
1769 changeFolderName();
1770 }
1771 }
1772 );
1773 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001774
1775 final AlertDialog dialog = builder.create();
1776 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1777 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001778 mInput.requestFocus();
1779 InputMethodManager inputManager = (InputMethodManager)
1780 getSystemService(Context.INPUT_METHOD_SERVICE);
1781 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001782 }
1783 });
1784
1785 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 }
1787
1788 private void changeFolderName() {
1789 final String name = mInput.getText().toString();
1790 if (!TextUtils.isEmpty(name)) {
1791 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001792 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 mFolderInfo.title = name;
1794 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1795
Joe Onorato9c1289c2009-08-17 11:03:03 -04001796 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001797 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001798 mModel.setWorkspaceDirty();
1799 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 } else {
1801 final FolderIcon folderIcon = (FolderIcon)
1802 mWorkspace.getViewForTag(mFolderInfo);
1803 if (folderIcon != null) {
1804 folderIcon.setText(name);
1805 getWorkspace().requestLayout();
1806 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001807 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808 mModel.setWorkspaceDirty();
1809 mWorkspaceLoading = true;
1810 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 }
1812 }
1813 }
1814 cleanup();
1815 }
1816
1817 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 dismissDialog(DIALOG_RENAME_FOLDER);
1819 mWaitingForResult = false;
1820 mFolderInfo = null;
1821 }
1822 }
1823
Joe Onoratofb0ca672009-09-14 17:55:46 -04001824 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001825 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001826 }
1827
Joe Onorato3a8820b2009-11-10 15:06:42 -08001828 boolean isAllAppsOpaque() {
1829 return mAllAppsGrid.isOpaque();
1830 }
1831
1832 void showAllApps(boolean animated) {
1833 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato7bb17492009-09-24 17:51:01 -07001834 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001835
Joe Onorato3a8820b2009-11-10 15:06:42 -08001836 mWorkspace.startFading(false);
1837
Mike Cleronb6082fa02009-10-19 17:03:36 -07001838 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001839 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001840
Joe Onorato7c312c12009-08-13 21:36:53 -07001841 // TODO: fade these two too
1842 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001843 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001844 }
1845
Joe Onoratob2061212009-11-24 16:13:54 -05001846 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001847 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001848 * - Home from workspace
1849 * - from center screen
1850 * - from other screens
1851 * - Home from all apps
1852 * - Back from all apps
1853 * - Launch app from workspace and quit
1854 * - with back
1855 * - with home
1856 * - Launch app from all apps and quit
1857 * - with back
1858 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001859 * - Go to a screen that's not the default, then all
1860 * apps, and launch and app, and go back
1861 * - with back
1862 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001863 * - On workspace, long press power and go back
1864 * - with back
1865 * - with home
1866 * - On all apps, long press power and go back
1867 * - with back
1868 * - with home
1869 * - On workspace, power off
1870 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001871 * - Launch an app and turn off the screen while in that app
1872 * - Go back with home key
1873 * - Go back with back key
1874 * - From all apps
1875 * - From workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001876 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001877 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001878 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001879 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001880 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001881 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001882 mWorkspace.startFading(true);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001883
Joe Onorato7c312c12009-08-13 21:36:53 -07001884 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001885 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001886 mDeleteZone.setVisibility(View.VISIBLE);
1887 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001888 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001889 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001890 }
1891
Joe Onorato7c312c12009-08-13 21:36:53 -07001892 void lockAllApps() {
1893 // TODO
1894 }
1895
1896 void unlockAllApps() {
1897 // TODO
1898 }
1899
Joe Onorato7404ee42009-07-31 11:54:44 -07001900 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 * Displays the shortcut creation dialog and launches, if necessary, the
1902 * appropriate activity.
1903 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001904 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001905 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1906 DialogInterface.OnShowListener {
1907
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001909
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 Dialog createDialog() {
1911 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001912
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001914
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1916 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001917 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001918
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 builder.setInverseBackgroundForced(true);
1920
1921 AlertDialog dialog = builder.create();
1922 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001923 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001924 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 return dialog;
1927 }
1928
1929 public void onCancel(DialogInterface dialog) {
1930 mWaitingForResult = false;
1931 cleanup();
1932 }
1933
Romain Guycbb89e42009-06-08 15:52:54 -07001934 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001935 }
1936
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001938 try {
1939 dismissDialog(DIALOG_CREATE_SHORTCUT);
1940 } catch (Exception e) {
1941 // An exception is thrown if the dialog is not visible, which is fine
1942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 }
1944
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001945 /**
1946 * Handle the action clicked in the "Add to home" dialog.
1947 */
1948 public void onClick(DialogInterface dialog, int which) {
1949 Resources res = getResources();
1950 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001951
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001952 switch (which) {
1953 case AddAdapter.ITEM_SHORTCUT: {
1954 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001955 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001956 break;
1957 }
Romain Guycbb89e42009-06-08 15:52:54 -07001958
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001959 case AddAdapter.ITEM_APPWIDGET: {
1960 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001961
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001962 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1963 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1964 // add the search widget
1965 ArrayList<AppWidgetProviderInfo> customInfo =
1966 new ArrayList<AppWidgetProviderInfo>();
1967 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1968 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1969 info.label = getString(R.string.group_search);
1970 info.icon = R.drawable.ic_search_widget;
1971 customInfo.add(info);
1972 pickIntent.putParcelableArrayListExtra(
1973 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1974 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1975 Bundle b = new Bundle();
1976 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1977 customExtras.add(b);
1978 pickIntent.putParcelableArrayListExtra(
1979 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1980 // start the pick activity
1981 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1982 break;
1983 }
Romain Guycbb89e42009-06-08 15:52:54 -07001984
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001985 case AddAdapter.ITEM_LIVE_FOLDER: {
1986 // Insert extra item to handle inserting folder
1987 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001988
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001989 ArrayList<String> shortcutNames = new ArrayList<String>();
1990 shortcutNames.add(res.getString(R.string.group_folder));
1991 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001992
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001993 ArrayList<ShortcutIconResource> shortcutIcons =
1994 new ArrayList<ShortcutIconResource>();
1995 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1996 R.drawable.ic_launcher_folder));
1997 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1998
1999 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2000 pickIntent.putExtra(Intent.EXTRA_INTENT,
2001 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2002 pickIntent.putExtra(Intent.EXTRA_TITLE,
2003 getText(R.string.title_select_live_folder));
2004 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002005
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002006 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2007 break;
2008 }
2009
2010 case AddAdapter.ITEM_WALLPAPER: {
2011 startWallpaper();
2012 break;
2013 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
2015 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002016
2017 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07002018 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 }
2020
2021 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002022 * Receives notifications when applications are added/removed.
2023 */
2024 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2025 @Override
2026 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002027 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002028 String reason = intent.getStringExtra("reason");
2029 if (!"homekey".equals(reason)) {
2030 boolean animate = true;
2031 /*
2032 if ("globalactions".equals(reason)) {
2033 // For some reason (probably the fading), this animation is
2034 // choppy, so don't show it.
2035 animate = false;
2036 }
2037 */
2038 closeAllApps(animate);
2039 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002040 }
2041 }
2042
2043 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002044 * Receives notifications whenever the appwidgets are reset.
2045 */
2046 private class AppWidgetResetObserver extends ContentObserver {
2047 public AppWidgetResetObserver() {
2048 super(new Handler());
2049 }
2050
2051 @Override
2052 public void onChange(boolean selfChange) {
2053 onAppWidgetReset();
2054 }
2055 }
2056
2057 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002058 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002060 public int getCurrentWorkspaceScreen() {
2061 return mWorkspace.getCurrentScreen();
2062 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002063
Joe Onorato9c1289c2009-08-17 11:03:03 -04002064 /**
2065 * Refreshes the shortcuts shown on the workspace.
2066 *
2067 * Implementation of the method from LauncherModel.Callbacks.
2068 */
2069 public void startBinding() {
2070 final Workspace workspace = mWorkspace;
2071 int count = workspace.getChildCount();
2072 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002073 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002074 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2075 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002076
Joe Onorato9c1289c2009-08-17 11:03:03 -04002077 if (DEBUG_USER_INTERFACE) {
2078 android.widget.Button finishButton = new android.widget.Button(this);
2079 finishButton.setText("Finish");
2080 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2081
2082 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2083 public void onClick(View v) {
2084 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002085 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002086 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
2088 }
2089
2090 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002091 * Bind the items start-end from the list.
2092 *
2093 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2096
2097 final Workspace workspace = mWorkspace;
2098
2099 for (int i=start; i<end; i++) {
2100 final ItemInfo item = shortcuts.get(i);
2101 mDesktopItems.add(item);
2102 switch (item.itemType) {
2103 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2104 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2105 final View shortcut = createShortcut((ApplicationInfo) item);
2106 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2107 false);
2108 break;
2109 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2110 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2111 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2112 (UserFolderInfo) item);
2113 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2114 false);
2115 break;
2116 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2117 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2118 R.layout.live_folder_icon, this,
2119 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2120 (LiveFolderInfo) item);
2121 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2122 false);
2123 break;
2124 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2125 final int screen = workspace.getCurrentScreen();
2126 final View view = mInflater.inflate(R.layout.widget_search,
2127 (ViewGroup) workspace.getChildAt(screen), false);
2128
2129 Search search = (Search) view.findViewById(R.id.widget_search);
2130 search.setLauncher(this);
2131
2132 final Widget widget = (Widget) item;
2133 view.setTag(widget);
2134
2135 workspace.addWidget(view, widget, false);
2136 break;
2137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 }
2139
Joe Onorato9c1289c2009-08-17 11:03:03 -04002140 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 }
2142
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 /**
2144 * Implementation of the method from LauncherModel.Callbacks.
2145 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002146 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2147 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002150
2151 /**
2152 * Add the views for a widget to the workspace.
2153 *
2154 * Implementation of the method from LauncherModel.Callbacks.
2155 */
2156 public void bindAppWidget(LauncherAppWidgetInfo item) {
2157 final Workspace workspace = mWorkspace;
2158
2159 final int appWidgetId = item.appWidgetId;
2160 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2161 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2162
Joe Onorato9c1289c2009-08-17 11:03:03 -04002163 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2164 item.hostView.setTag(item);
2165
2166 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2167 item.cellY, item.spanX, item.spanY, false);
2168
2169 workspace.requestLayout();
2170
2171 mDesktopItems.add(item);
2172 }
2173
2174 /**
2175 * Callback saying that there aren't any more items to bind.
2176 *
2177 * Implementation of the method from LauncherModel.Callbacks.
2178 */
2179 public void finishBindingItems() {
2180 if (mSavedState != null) {
2181 if (!mWorkspace.hasFocus()) {
2182 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2183 }
2184
2185 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2186 if (userFolders != null) {
2187 for (long folderId : userFolders) {
2188 final FolderInfo info = mFolders.get(folderId);
2189 if (info != null) {
2190 openFolder(info);
2191 }
2192 }
2193 final Folder openFolder = mWorkspace.getOpenFolder();
2194 if (openFolder != null) {
2195 openFolder.requestFocus();
2196 }
2197 }
2198
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199 mSavedState = null;
2200 }
2201
2202 if (mSavedInstanceState != null) {
2203 super.onRestoreInstanceState(mSavedInstanceState);
2204 mSavedInstanceState = null;
2205 }
2206
Joe Onorato9c1289c2009-08-17 11:03:03 -04002207 mWorkspaceLoading = false;
2208 }
2209
2210 /**
2211 * Add the icons for all apps.
2212 *
2213 * Implementation of the method from LauncherModel.Callbacks.
2214 */
2215 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002216 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002217 }
2218
2219 /**
2220 * A package was installed.
2221 *
2222 * Implementation of the method from LauncherModel.Callbacks.
2223 */
2224 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2225 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002226 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002227 }
2228
2229 /**
2230 * A package was updated.
2231 *
2232 * Implementation of the method from LauncherModel.Callbacks.
2233 */
2234 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2235 removeDialog(DIALOG_CREATE_SHORTCUT);
2236 mWorkspace.updateShortcutsForPackage(packageName);
Joe Onoratof0be2132009-11-24 19:34:29 -05002237 mAllAppsGrid.updateApps(packageName, apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002238 }
2239
2240 /**
2241 * A package was uninstalled.
2242 *
2243 * Implementation of the method from LauncherModel.Callbacks.
2244 */
2245 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2246 removeDialog(DIALOG_CREATE_SHORTCUT);
2247 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002248 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002249 }
Joe Onoratobe386092009-11-17 17:32:16 -08002250
2251 /**
2252 * Prints out out state for debugging.
2253 */
2254 public void dumpState() {
2255 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002256 Log.d(TAG, "mSavedState=" + mSavedState);
2257 Log.d(TAG, "mIsNewIntent=" + mIsNewIntent);
2258 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2259 Log.d(TAG, "mRestoring=" + mRestoring);
2260 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2261 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2262 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2263 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002264 mModel.dumpState();
2265 mAllAppsGrid.dumpState();
2266 Log.d(TAG, "END launcher2 dump state");
2267 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268}