blob: aa6b7cedf13f9ec97a3ad3a7454e7835474aef59 [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;
22import android.app.SearchManager;
23import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070024import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080026import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080028import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
30import android.content.DialogInterface;
31import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070032import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080033import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ActivityInfo;
35import android.content.pm.PackageManager;
36import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070038import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080041import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080042import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080044import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080046import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.os.Parcelable;
Joe Onoratobe386092009-11-17 17:32:16 -080048import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.text.Selection;
51import android.text.SpannableStringBuilder;
52import android.text.TextUtils;
53import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070054import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080056import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.KeyEvent;
58import android.view.LayoutInflater;
59import android.view.Menu;
60import android.view.MenuItem;
61import android.view.View;
62import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.View.OnLongClickListener;
64import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.widget.TextView;
67import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080068import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080069import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080070import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070071import android.appwidget.AppWidgetManager;
72import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040075import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070076import java.io.DataOutputStream;
77import java.io.FileNotFoundException;
78import java.io.IOException;
79import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
81/**
82 * Default launcher application.
83 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040084public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080085 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080086 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 static final boolean LOGD = false;
88
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 static final boolean PROFILE_STARTUP = false;
90 static final boolean PROFILE_ROTATE = false;
91 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070092
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 private static final int WALLPAPER_SCREENS_SPAN = 2;
94
95 private static final int MENU_GROUP_ADD = 1;
96 private static final int MENU_ADD = Menu.FIRST + 1;
97 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
98 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
99 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700100 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
102 private static final int REQUEST_CREATE_SHORTCUT = 1;
103 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700104 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 private static final int REQUEST_PICK_APPLICATION = 6;
106 private static final int REQUEST_PICK_SHORTCUT = 7;
107 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700108 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700109 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
111 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
112
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700113 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
114 static final String SEARCH_WIDGET = "search_widget";
115
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800116 static final int SCREEN_COUNT = 5;
117 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700119 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Joe Onorato7c312c12009-08-13 21:36:53 -0700121 static final int DIALOG_CREATE_SHORTCUT = 1;
122 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Romain Guy98d01652009-06-30 16:21:04 -0700124 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 // Type: int
127 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
128 // Type: boolean
129 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
130 // Type: long
131 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
146 // Type: int[]
147 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
148 // Type: boolean
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
150 // Type: long
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
152
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800156 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800158 private final BroadcastReceiver mCloseSystemDialogsReceiver
159 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800160 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private LayoutInflater mInflater;
163
Joe Onorato00acb122009-08-04 16:04:30 -0400164 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700166
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private AppWidgetManager mAppWidgetManager;
168 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private CellLayout.CellInfo mAddItemCellInfo;
171 private CellLayout.CellInfo mMenuAddInfo;
172 private final int[] mCellCoordinates = new int[2];
173 private FolderInfo mFolderInfo;
174
Joe Onorato7c312c12009-08-13 21:36:53 -0700175 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700176 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700177 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private Bundle mSavedState;
180
181 private SpannableStringBuilder mDefaultKeySsb = null;
182
Mike LeBeau736cf282009-07-02 17:46:59 -0700183 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 private boolean mWorkspaceLoading = true;
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private boolean mRestoring;
188 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
190 private Bundle mSavedInstanceState;
191
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 private LauncherModel mModel;
193
Romain Guy84f296c2009-11-04 15:00:44 -0800194 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
195 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700196
Romain Guy1fbc1c82009-11-09 20:43:08 -0800197 private ImageView mPreviousView;
198 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 @Override
201 protected void onCreate(Bundle savedInstanceState) {
202 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400205 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700207
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700209 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
210 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 if (PROFILE_STARTUP) {
213 android.os.Debug.startMethodTracing("/sdcard/launcher");
214 }
215
216 checkForLocaleChange();
217 setWallpaperDimension();
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 setContentView(R.layout.launcher);
220 setupViews();
221
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800222 registerContentObservers();
223
Joe Onorato7c312c12009-08-13 21:36:53 -0700224 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700225
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 mSavedState = savedInstanceState;
227 restoreState(mSavedState);
228
229 if (PROFILE_STARTUP) {
230 android.os.Debug.stopMethodTracing();
231 }
232
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700233 // We have a new AllAppsView, we need to re-bind everything, and it could have
234 // changed in our absence.
235 mModel.setAllAppsDirty();
236 mModel.setWorkspaceDirty();
237
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400239 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 }
241
242 // For handling default keys
243 mDefaultKeySsb = new SpannableStringBuilder();
244 Selection.setSelection(mDefaultKeySsb, 0);
245 }
Romain Guycbb89e42009-06-08 15:52:54 -0700246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700248 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
249 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 final Configuration configuration = getResources().getConfiguration();
252
Romain Guy98d01652009-06-30 16:21:04 -0700253 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 final String locale = configuration.locale.toString();
255
Romain Guy98d01652009-06-30 16:21:04 -0700256 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 final int mcc = configuration.mcc;
258
Romain Guy98d01652009-06-30 16:21:04 -0700259 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 final int mnc = configuration.mnc;
261
Romain Guy84f296c2009-11-04 15:00:44 -0800262 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
Romain Guy84f296c2009-11-04 15:00:44 -0800264 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700265 localeConfiguration.locale = locale;
266 localeConfiguration.mcc = mcc;
267 localeConfiguration.mnc = mnc;
268
269 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700271 }
272 }
273
274 private static class LocaleConfiguration {
275 public String locale;
276 public int mcc = -1;
277 public int mnc = -1;
278 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700279
Romain Guy98d01652009-06-30 16:21:04 -0700280 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
281 DataInputStream in = null;
282 try {
283 in = new DataInputStream(context.openFileInput(PREFERENCES));
284 configuration.locale = in.readUTF();
285 configuration.mcc = in.readInt();
286 configuration.mnc = in.readInt();
287 } catch (FileNotFoundException e) {
288 // Ignore
289 } catch (IOException e) {
290 // Ignore
291 } finally {
292 if (in != null) {
293 try {
294 in.close();
295 } catch (IOException e) {
296 // Ignore
297 }
298 }
299 }
300 }
301
302 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
303 DataOutputStream out = null;
304 try {
305 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
306 out.writeUTF(configuration.locale);
307 out.writeInt(configuration.mcc);
308 out.writeInt(configuration.mnc);
309 out.flush();
310 } catch (FileNotFoundException e) {
311 // Ignore
312 } catch (IOException e) {
313 //noinspection ResultOfMethodCallIgnored
314 context.getFileStreamPath(PREFERENCES).delete();
315 } finally {
316 if (out != null) {
317 try {
318 out.close();
319 } catch (IOException e) {
320 // Ignore
321 }
322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 }
324 }
325
326 static int getScreen() {
327 synchronized (sLock) {
328 return sScreen;
329 }
330 }
331
332 static void setScreen(int screen) {
333 synchronized (sLock) {
334 sScreen = screen;
335 }
336 }
337
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700339 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340
341 Display display = getWindowManager().getDefaultDisplay();
342 boolean isPortrait = display.getWidth() < display.getHeight();
343
344 final int width = isPortrait ? display.getWidth() : display.getHeight();
345 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700346 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 }
348
349 @Override
350 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700351 mWaitingForResult = false;
352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 // The pattern used here is that a user PICKs a specific application,
354 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
357 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700358
Romain Guy94dabf12009-07-21 10:55:43 -0700359 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 switch (requestCode) {
361 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400362 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 break;
364 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700365 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 break;
367 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 break;
370 case REQUEST_PICK_LIVE_FOLDER:
371 addLiveFolder(data);
372 break;
373 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400374 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700376 case REQUEST_PICK_APPWIDGET:
377 addAppWidget(data);
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_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700382 case REQUEST_PICK_WALLPAPER:
383 // We just wanted the activity result here so we can clear mWaitingForResult
384 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
Romain Guy18042c82009-11-06 11:44:55 -0800386 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
387 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
388 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700389 // Clean up the appWidgetId if we canceled
390 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
391 if (appWidgetId != -1) {
392 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 }
396
397 @Override
398 protected void onResume() {
399 super.onResume();
400
Joe Onorato7e4ed992009-12-03 13:10:49 -0800401 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
402 registerReceiver(mCloseSystemDialogsReceiver, filter);
403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400405 mWorkspaceLoading = true;
406 mModel.startLoader(this, true);
407 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700409
Mike LeBeau736cf282009-07-02 17:46:59 -0700410 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
411 // onNewIntent), then close the search dialog if needed, because it probably
412 // came from the user pressing 'home' (rather than, for example, pressing 'back').
413 if (mIsNewIntent) {
414 // Post to a handler so that this happens after the search dialog tries to open
415 // itself again.
416 mWorkspace.post(new Runnable() {
417 public void run() {
Bjorn Bringert98047dc2009-11-21 22:08:04 +0000418 stopSearch();
Mike LeBeau736cf282009-07-02 17:46:59 -0700419 }
420 });
421 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700422
Mike LeBeau736cf282009-07-02 17:46:59 -0700423 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425
426 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 protected void onPause() {
428 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800429 dismissPreview(mPreviousView);
430 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800431 mDragController.cancelDrag();
Joe Onorato7e4ed992009-12-03 13:10:49 -0800432 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 }
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700435 @Override
436 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400437 // Flag the loader to stop early before switching
438 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700439
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700440 if (PROFILE_ROTATE) {
441 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
442 }
443 return null;
444 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 private boolean acceptFilter() {
447 final InputMethodManager inputManager = (InputMethodManager)
448 getSystemService(Context.INPUT_METHOD_SERVICE);
449 return !inputManager.isFullscreenMode();
450 }
451
452 @Override
453 public boolean onKeyDown(int keyCode, KeyEvent event) {
454 boolean handled = super.onKeyDown(keyCode, event);
455 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
456 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
457 keyCode, event);
458 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700459 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700460 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700461 // showSearchDialog()
462 // If there are multiple keystrokes before the search dialog takes focus,
463 // onSearchRequested() will be called for every keystroke,
464 // but it is idempotent, so it's fine.
465 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 }
467 }
468
469 return handled;
470 }
471
Karl Rosaen138a0412009-04-23 19:00:21 -0700472 private String getTypedText() {
473 return mDefaultKeySsb.toString();
474 }
475
476 private void clearTypedText() {
477 mDefaultKeySsb.clear();
478 mDefaultKeySsb.clearSpans();
479 Selection.setSelection(mDefaultKeySsb, 0);
480 }
481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 /**
483 * Restores the previous state, if it exists.
484 *
485 * @param savedState The previous state.
486 */
487 private void restoreState(Bundle savedState) {
488 if (savedState == null) {
489 return;
490 }
491
Joe Onorato3a8820b2009-11-10 15:06:42 -0800492 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
493 if (allApps) {
494 showAllApps(false);
495 }
496
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
498 if (currentScreen > -1) {
499 mWorkspace.setCurrentScreen(currentScreen);
500 }
501
502 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
503 if (addScreen > -1) {
504 mAddItemCellInfo = new CellLayout.CellInfo();
505 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
506 addItemCellInfo.valid = true;
507 addItemCellInfo.screen = addScreen;
508 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
509 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
510 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
511 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
512 addItemCellInfo.findVacantCellsFromOccupied(
513 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
514 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
515 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
516 mRestoring = true;
517 }
518
519 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
520 if (renameFolder) {
521 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400522 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800523 mRestoring = true;
524 }
525 }
526
527 /**
528 * Finds all the views we need and configure them properly.
529 */
530 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400531 DragController dragController = mDragController;
532
Romain Guyb1b69f52009-08-10 15:10:15 -0700533 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400534 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535
Joe Onorato7c312c12009-08-13 21:36:53 -0700536 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700537 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700538 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700539 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700540 // Manage focusability manually since this thing is always visible
541 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700542
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
544 final Workspace workspace = mWorkspace;
545
Joe Onorato7c312c12009-08-13 21:36:53 -0700546 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
547 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548
Joe Onorato7c312c12009-08-13 21:36:53 -0700549 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700550 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700551 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800552 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800553
554 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
555 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
556
557 Drawable previous = mPreviousView.getDrawable();
558 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800559 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560
Joe Onorato0d44e942009-11-16 18:20:51 -0800561 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800562 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800563 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800564 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800565
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400567 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569
570 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400571 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700572 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573
Joe Onorato00acb122009-08-04 16:04:30 -0400574 dragController.setDragScoller(workspace);
575 dragController.setDragListener(deleteZone);
576 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700577
Joe Onorato00acb122009-08-04 16:04:30 -0400578 // The order here is bottom to top.
579 dragController.addDropTarget(workspace);
580 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 }
582
Romain Guy8a73c512009-11-09 19:19:59 -0800583 @SuppressWarnings({"UnusedDeclaration"})
584 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800585 if (!isAllAppsVisible()) {
586 mWorkspace.scrollLeft();
587 }
Romain Guy8a73c512009-11-09 19:19:59 -0800588 }
589
590 @SuppressWarnings({"UnusedDeclaration"})
591 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800592 if (!isAllAppsVisible()) {
593 mWorkspace.scrollRight();
594 }
Romain Guy8a73c512009-11-09 19:19:59 -0800595 }
596
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 /**
598 * Creates a view representing a shortcut.
599 *
600 * @param info The data structure describing the shortcut.
601 *
602 * @return A View inflated from R.layout.application.
603 */
604 View createShortcut(ApplicationInfo info) {
605 return createShortcut(R.layout.application,
606 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
607 }
608
609 /**
610 * Creates a view representing a shortcut inflated from the specified resource.
611 *
612 * @param layoutResId The id of the XML layout used to create the shortcut.
613 * @param parent The group the shortcut belongs to.
614 * @param info The data structure describing the shortcut.
615 *
616 * @return A View inflated from layoutResId.
617 */
618 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
619 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
620
Joe Onorato5162ea92009-09-03 09:39:42 -0700621 if (info.icon == null) {
622 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
623 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700625 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 info.filtered = true;
627 }
628
629 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
630 favorite.setText(info.title);
631 favorite.setTag(info);
632 favorite.setOnClickListener(this);
633
634 return favorite;
635 }
636
637 /**
638 * Add an application shortcut to the workspace.
639 *
640 * @param data The intent describing the application.
641 * @param cellInfo The position on screen where to create the shortcut.
642 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 cellInfo.screen = mWorkspace.getCurrentScreen();
645 if (!findSingleSlot(cellInfo)) return;
646
Romain Guy73b979d2009-06-09 12:57:21 -0700647 final ApplicationInfo info = infoFromApplicationIntent(context, data);
648 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700650 }
651 }
652
653 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 ComponentName component = data.getComponent();
655 PackageManager packageManager = context.getPackageManager();
656 ActivityInfo activityInfo = null;
657 try {
658 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
659 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800660 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
Romain Guycbb89e42009-06-08 15:52:54 -0700662
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 if (activityInfo != null) {
664 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700665
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 itemInfo.title = activityInfo.loadLabel(packageManager);
667 if (itemInfo.title == null) {
668 itemInfo.title = activityInfo.name;
669 }
Romain Guycbb89e42009-06-08 15:52:54 -0700670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
672 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
673 itemInfo.icon = activityInfo.loadIcon(packageManager);
674 itemInfo.container = ItemInfo.NO_ID;
675
Romain Guy73b979d2009-06-09 12:57:21 -0700676 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
Romain Guy73b979d2009-06-09 12:57:21 -0700678
679 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
Romain Guycbb89e42009-06-08 15:52:54 -0700681
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 /**
683 * Add a shortcut to the workspace.
684 *
685 * @param data The intent describing the shortcut.
686 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400688 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 cellInfo.screen = mWorkspace.getCurrentScreen();
690 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700691
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
693
694 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
697 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 }
699 }
700
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700703 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700705 * @param data The intent describing the appWidgetId.
706 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700710 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700711
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800712 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700714 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700715
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 // Try finding open space on Launcher screen
721 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800722 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
723 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
724 return;
725 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700727 // Build Launcher-specific widget info and save to database
728 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 launcherInfo.spanX = spans[0];
730 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700731
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 LauncherModel.addItemToDatabase(this, launcherInfo,
733 LauncherSettings.Favorites.CONTAINER_DESKTOP,
734 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
735
736 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700738
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700740 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700741
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700742 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700744
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400746 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 }
748 }
Romain Guycbb89e42009-06-08 15:52:54 -0700749
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
751 mDesktopItems.remove(launcherInfo);
752 launcherInfo.hostView = null;
753 }
754
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700755 public LauncherAppWidgetHost getAppWidgetHost() {
756 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 }
Romain Guycbb89e42009-06-08 15:52:54 -0700758
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 static ApplicationInfo addShortcut(Context context, Intent data,
760 CellLayout.CellInfo cellInfo, boolean notify) {
761
Romain Guy73b979d2009-06-09 12:57:21 -0700762 final ApplicationInfo info = infoFromShortcutIntent(context, data);
763 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
764 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
765
766 return info;
767 }
768
769 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
771 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
772 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
773
774 Drawable icon = null;
775 boolean filtered = false;
776 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700777 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778
779 if (bitmap != null) {
780 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
781 filtered = true;
782 customIcon = true;
783 } else {
784 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700785 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700787 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 final PackageManager packageManager = context.getPackageManager();
789 Resources resources = packageManager.getResourcesForApplication(
790 iconResource.packageName);
791 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
792 icon = resources.getDrawable(id);
793 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800794 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 }
796 }
797 }
798
799 if (icon == null) {
800 icon = context.getPackageManager().getDefaultActivityIcon();
801 }
802
803 final ApplicationInfo info = new ApplicationInfo();
804 info.icon = icon;
805 info.filtered = filtered;
806 info.title = name;
807 info.intent = intent;
808 info.customIcon = customIcon;
809 info.iconResource = iconResource;
810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 return info;
812 }
813
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800814 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800815 getWindow().closeAllPanels();
816
817 try {
818 dismissDialog(DIALOG_CREATE_SHORTCUT);
819 // Unlock the workspace if the dialog was showing
820 } catch (Exception e) {
821 // An exception is thrown if the dialog is not visible, which is fine
822 }
823
824 try {
825 dismissDialog(DIALOG_RENAME_FOLDER);
826 // Unlock the workspace if the dialog was showing
827 } catch (Exception e) {
828 // An exception is thrown if the dialog is not visible, which is fine
829 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800830
831 // Whatever we were doing is hereby canceled.
832 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800833 }
834
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 @Override
836 protected void onNewIntent(Intent intent) {
837 super.onNewIntent(intent);
838
839 // Close the menu
840 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800841 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800842 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700843
Mike LeBeau736cf282009-07-02 17:46:59 -0700844 // Set this flag so that onResume knows to close the search dialog if it's open,
845 // because this was a new intent (thus a press of 'home' or some such) rather than
846 // for example onResume being called when the user pressed the 'back' button.
847 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848
Joe Onorato14f122b2009-11-19 14:06:36 -0800849 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
850 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
851 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800852 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800853 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800854 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800855 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700856
Joe Onorato3a8820b2009-11-10 15:06:42 -0800857 final View v = getWindow().peekDecorView();
858 if (v != null && v.getWindowToken() != null) {
859 InputMethodManager imm = (InputMethodManager)getSystemService(
860 INPUT_METHOD_SERVICE);
861 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 }
863 }
864 }
865
866 @Override
867 protected void onRestoreInstanceState(Bundle savedInstanceState) {
868 // Do not call super here
869 mSavedInstanceState = savedInstanceState;
870 }
871
872 @Override
873 protected void onSaveInstanceState(Bundle outState) {
874 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
875
876 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
877 if (folders.size() > 0) {
878 final int count = folders.size();
879 long[] ids = new long[count];
880 for (int i = 0; i < count; i++) {
881 final FolderInfo info = folders.get(i).getInfo();
882 ids[i] = info.id;
883 }
884 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
885 } else {
886 super.onSaveInstanceState(outState);
887 }
888
Joe Onoratofb0ca672009-09-14 17:55:46 -0400889 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800890 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700892 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893
894 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
895 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
896 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
897
898 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
899 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
900 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
901 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
902 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
903 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
904 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
905 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
906 layout.getOccupiedCells());
907 }
908
909 if (mFolderInfo != null && mWaitingForResult) {
910 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
911 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
912 }
913 }
914
915 @Override
916 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700918
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700920 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800922 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924
925 TextKeyListener.getInstance().release();
926
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 unbindDesktopItems();
930 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800931
932 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800933
934 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800935 dismissPreview(mNextView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
937
938 @Override
939 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700940 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 super.startActivityForResult(intent, requestCode);
942 }
943
944 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700945 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700947
Joe Onorato7bb17492009-09-24 17:51:01 -0700948 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700949
Karl Rosaen138a0412009-04-23 19:00:21 -0700950 // Slide the search widget to the top, if it's on the current screen,
951 // otherwise show the search dialog immediately.
952 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
953 if (searchWidget == null) {
954 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
955 } else {
956 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
957 // show the currently typed text in the search widget while sliding
958 searchWidget.setQuery(getTypedText());
959 }
960 }
Romain Guycbb89e42009-06-08 15:52:54 -0700961
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 /**
963 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700964 *
965 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700966 */
Romain Guycbb89e42009-06-08 15:52:54 -0700967 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700968 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700969
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 if (initialQuery == null) {
971 // Use any text typed in the launcher as the initial query
972 initialQuery = getTypedText();
973 clearTypedText();
974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 if (appSearchData == null) {
976 appSearchData = new Bundle();
977 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
978 }
Romain Guycbb89e42009-06-08 15:52:54 -0700979
Karl Rosaen138a0412009-04-23 19:00:21 -0700980 final SearchManager searchManager =
981 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
982
983 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
984 if (searchWidget != null) {
985 // This gets called when the user leaves the search dialog to go back to
986 // the Launcher.
987 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
988 public void onCancel() {
989 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700990 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700991 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700992 });
993 }
Romain Guycbb89e42009-06-08 15:52:54 -0700994
Karl Rosaen138a0412009-04-23 19:00:21 -0700995 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700996 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 }
998
Karl Rosaen138a0412009-04-23 19:00:21 -0700999 /**
1000 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001001 */
Romain Guy5a941392009-04-28 15:18:25 -07001002 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001003 // Close search dialog
1004 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001005 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001006 // Restore search widget to its normal position
1007 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1008 if (searchWidget != null) {
1009 searchWidget.stopSearch(false);
1010 }
1011 }
Romain Guycbb89e42009-06-08 15:52:54 -07001012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 @Override
1014 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015 if (isWorkspaceLocked()) {
1016 return false;
1017 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018
1019 super.onCreateOptionsMenu(menu);
1020 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1021 .setIcon(android.R.drawable.ic_menu_add)
1022 .setAlphabeticShortcut('A');
1023 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1024 .setIcon(android.R.drawable.ic_menu_gallery)
1025 .setAlphabeticShortcut('W');
1026 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1027 .setIcon(android.R.drawable.ic_search_category_default)
1028 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1029 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1030 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1031 .setAlphabeticShortcut('N');
1032
1033 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001034 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1035 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036
1037 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1038 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1039 .setIntent(settings);
1040
1041 return true;
1042 }
1043
1044 @Override
1045 public boolean onPrepareOptionsMenu(Menu menu) {
1046 super.onPrepareOptionsMenu(menu);
1047
1048 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1049 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1050
1051 return true;
1052 }
1053
1054 @Override
1055 public boolean onOptionsItemSelected(MenuItem item) {
1056 switch (item.getItemId()) {
1057 case MENU_ADD:
1058 addItems();
1059 return true;
1060 case MENU_WALLPAPER_SETTINGS:
1061 startWallpaper();
1062 return true;
1063 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001064 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 return true;
1066 case MENU_NOTIFICATIONS:
1067 showNotifications();
1068 return true;
1069 }
1070
1071 return super.onOptionsItemSelected(item);
1072 }
Romain Guycbb89e42009-06-08 15:52:54 -07001073
Karl Rosaen138a0412009-04-23 19:00:21 -07001074 /**
1075 * Indicates that we want global search for this activity by setting the globalSearch
1076 * argument for {@link #startSearch} to true.
1077 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001079 @Override
1080 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001081 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001082 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001083 }
1084
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085 public boolean isWorkspaceLocked() {
1086 return mWorkspaceLoading || mWaitingForResult;
1087 }
1088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001090 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 showAddDialog(mMenuAddInfo);
1092 }
1093
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001094 void addAppWidget(Intent data) {
1095 // TODO: catch bad widget exception when sent
1096 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001098 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1099 if (SEARCH_WIDGET.equals(customWidget)) {
1100 // We don't need this any more, since this isn't a real app widget.
1101 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1102 // add the search widget
1103 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001105 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1106
1107 if (appWidget.configure != null) {
1108 // Launch over to configure widget, if needed
1109 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1110 intent.setComponent(appWidget.configure);
1111 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1112
1113 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1114 } else {
1115 // Otherwise just add it
1116 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
1119 }
Romain Guycbb89e42009-06-08 15:52:54 -07001120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 void addSearch() {
1122 final Widget info = Widget.makeSearch();
1123 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 final int[] xy = mCellCoordinates;
1126 final int spanX = info.spanX;
1127 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1132 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 final View view = mInflater.inflate(info.layoutResource, null);
1135 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001136 Search search = (Search) view.findViewById(R.id.widget_search);
1137 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1140 }
1141
Romain Guy73b979d2009-06-09 12:57:21 -07001142 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001143 // Handle case where user selected "Applications"
1144 String applicationName = getResources().getString(R.string.group_applications);
1145 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001146
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001147 if (applicationName != null && applicationName.equals(shortcutName)) {
1148 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1149 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001150
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001151 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1152 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001153 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001154 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001155 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 }
1158
1159 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001160 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001161 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001162 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001163
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001164 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001165 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001166 } else {
1167 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170
Joe Onorato9c1289c2009-08-17 11:03:03 -04001171 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 UserFolderInfo folderInfo = new UserFolderInfo();
1173 folderInfo.title = getText(R.string.folder_name);
1174
1175 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1176 cellInfo.screen = mWorkspace.getCurrentScreen();
1177 if (!findSingleSlot(cellInfo)) return;
1178
1179 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001180 LauncherModel.addItemToDatabase(this, folderInfo,
1181 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001183 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184
1185 // Create the view
1186 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1187 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1188 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001189 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 }
Romain Guycbb89e42009-06-08 15:52:54 -07001191
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 void removeFolder(FolderInfo folder) {
1193 mFolders.remove(folder.id);
1194 }
1195
1196 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 cellInfo.screen = mWorkspace.getCurrentScreen();
1198 if (!findSingleSlot(cellInfo)) return;
1199
1200 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1201
1202 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1204 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1206 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 }
1208 }
1209
1210 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1211 CellLayout.CellInfo cellInfo, boolean notify) {
1212
1213 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1214 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1215
1216 Drawable icon = null;
1217 boolean filtered = false;
1218 Intent.ShortcutIconResource iconResource = null;
1219
1220 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1221 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1222 try {
1223 iconResource = (Intent.ShortcutIconResource) extra;
1224 final PackageManager packageManager = context.getPackageManager();
1225 Resources resources = packageManager.getResourcesForApplication(
1226 iconResource.packageName);
1227 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1228 icon = resources.getDrawable(id);
1229 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001230 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 }
1232 }
1233
1234 if (icon == null) {
1235 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1236 }
1237
1238 final LiveFolderInfo info = new LiveFolderInfo();
1239 info.icon = icon;
1240 info.filtered = filtered;
1241 info.title = name;
1242 info.iconResource = iconResource;
1243 info.uri = data.getData();
1244 info.baseIntent = baseIntent;
1245 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1246 LiveFolders.DISPLAY_MODE_GRID);
1247
1248 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1249 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001250 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251
1252 return info;
1253 }
1254
1255 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1256 final int[] xy = new int[2];
1257 if (findSlot(cellInfo, xy, 1, 1)) {
1258 cellInfo.cellX = xy[0];
1259 cellInfo.cellY = xy[1];
1260 return true;
1261 }
1262 return false;
1263 }
1264
1265 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1266 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1267 boolean[] occupied = mSavedState != null ?
1268 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1269 cellInfo = mWorkspace.findAllVacantCells(occupied);
1270 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1271 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1272 return false;
1273 }
1274 }
1275 return true;
1276 }
1277
1278 private void showNotifications() {
1279 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1280 if (statusBar != null) {
1281 statusBar.expand();
1282 }
1283 }
1284
1285 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001286 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001288 Intent chooser = Intent.createChooser(pickWallpaper,
1289 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001290 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1291 // Removed in Eclair MR1
1292// WallpaperManager wm = (WallpaperManager)
1293// getSystemService(Context.WALLPAPER_SERVICE);
1294// WallpaperInfo wi = wm.getWallpaperInfo();
1295// if (wi != null && wi.getSettingsActivity() != null) {
1296// LabeledIntent li = new LabeledIntent(getPackageName(),
1297// R.string.configure_wallpaper, 0);
1298// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1299// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1300// }
Mike Clerona0618e42009-10-22 13:55:21 -07001301 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
1303
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001304 /**
1305 * Registers various content observers. The current implementation registers
1306 * only a favorites observer to keep track of the favorites applications.
1307 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001308 private void registerContentObservers() {
1309 ContentResolver resolver = getContentResolver();
1310 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1311 true, mWidgetObserver);
1312 }
1313
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 @Override
1315 public boolean dispatchKeyEvent(KeyEvent event) {
1316 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1317 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001319 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001320 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001321 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001322 dumpState();
1323 return true;
1324 }
1325 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001326 }
1327 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1328 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001329 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 return true;
1331 }
1332 }
1333
1334 return super.dispatchKeyEvent(event);
1335 }
1336
Joe Onorato88ec0992009-11-19 13:16:06 -08001337 @Override
1338 public void onBackPressed() {
1339 if (isAllAppsVisible()) {
1340 closeAllApps(true);
1341 } else {
1342 closeFolder();
1343 }
1344 dismissPreview(mPreviousView);
1345 dismissPreview(mNextView);
1346 }
1347
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 private void closeFolder() {
1349 Folder folder = mWorkspace.getOpenFolder();
1350 if (folder != null) {
1351 closeFolder(folder);
1352 }
1353 }
1354
1355 void closeFolder(Folder folder) {
1356 folder.getInfo().opened = false;
1357 ViewGroup parent = (ViewGroup) folder.getParent();
1358 if (parent != null) {
1359 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001360 if (folder instanceof DropTarget) {
1361 // Live folders aren't DropTargets.
1362 mDragController.removeDropTarget((DropTarget)folder);
1363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 }
1365 folder.onClose();
1366 }
1367
1368 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001369 * Re-listen when widgets are reset.
1370 */
1371 private void onAppWidgetReset() {
1372 mAppWidgetHost.startListening();
1373 }
1374
1375 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001376 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1377 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001379 private void unbindDesktopItems() {
1380 for (ItemInfo item: mDesktopItems) {
1381 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 }
1383 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 /**
1386 * Launches the intent referred by the clicked shortcut.
1387 *
1388 * @param v The view representing the clicked shortcut.
1389 */
1390 public void onClick(View v) {
1391 Object tag = v.getTag();
1392 if (tag instanceof ApplicationInfo) {
1393 // Open shortcut
1394 final Intent intent = ((ApplicationInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001395 int[] pos = new int[2];
1396 v.getLocationOnScreen(pos);
1397 intent.setSourceBounds(
1398 new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 startActivitySafely(intent);
1400 } else if (tag instanceof FolderInfo) {
1401 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001402 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001403 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001404 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001405 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001406 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 }
1409 }
1410
1411 void startActivitySafely(Intent intent) {
1412 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1413 try {
1414 startActivity(intent);
1415 } catch (ActivityNotFoundException e) {
1416 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1417 } catch (SecurityException e) {
1418 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001419 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1421 "or use the exported attribute for this activity.", e);
1422 }
1423 }
1424
1425 private void handleFolderClick(FolderInfo folderInfo) {
1426 if (!folderInfo.opened) {
1427 // Close any open folder
1428 closeFolder();
1429 // Open the requested folder
1430 openFolder(folderInfo);
1431 } else {
1432 // Find the open folder...
1433 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1434 int folderScreen;
1435 if (openFolder != null) {
1436 folderScreen = mWorkspace.getScreenForView(openFolder);
1437 // .. and close it
1438 closeFolder(openFolder);
1439 if (folderScreen != mWorkspace.getCurrentScreen()) {
1440 // Close any folder open on the current screen
1441 closeFolder();
1442 // Pull the folder onto this screen
1443 openFolder(folderInfo);
1444 }
1445 }
1446 }
1447 }
1448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 /**
1450 * Opens the user fodler described by the specified tag. The opening of the folder
1451 * is animated relative to the specified View. If the View is null, no animation
1452 * is played.
1453 *
1454 * @param folderInfo The FolderInfo describing the folder to open.
1455 */
1456 private void openFolder(FolderInfo folderInfo) {
1457 Folder openFolder;
1458
1459 if (folderInfo instanceof UserFolderInfo) {
1460 openFolder = UserFolder.fromXml(this);
1461 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001462 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 } else {
1464 return;
1465 }
1466
Joe Onorato00acb122009-08-04 16:04:30 -04001467 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 openFolder.setLauncher(this);
1469
1470 openFolder.bind(folderInfo);
1471 folderInfo.opened = true;
1472
1473 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1474 openFolder.onOpen();
1475 }
1476
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001478 switch (v.getId()) {
1479 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001480 if (!isAllAppsVisible()) {
1481 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1482 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001483 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001484 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001485 return true;
1486 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001487 if (!isAllAppsVisible()) {
1488 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1489 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001490 showPreviews(v);
1491 }
1492 return true;
1493 case R.id.all_apps_button:
1494 if (!isAllAppsVisible()) {
1495 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1496 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1497 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001498 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001499 return true;
1500 }
1501
Joe Onorato9c1289c2009-08-17 11:03:03 -04001502 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 return false;
1504 }
1505
1506 if (!(v instanceof CellLayout)) {
1507 v = (View) v.getParent();
1508 }
1509
1510 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1511
1512 // This happens when long clicking an item with the dpad/trackball
1513 if (cellInfo == null) {
1514 return true;
1515 }
1516
1517 if (mWorkspace.allowLongPress()) {
1518 if (cellInfo.cell == null) {
1519 if (cellInfo.valid) {
1520 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001521 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 showAddDialog(cellInfo);
1523 }
1524 } else {
1525 if (!(cellInfo.cell instanceof Folder)) {
1526 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001527 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1528 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 mWorkspace.startDrag(cellInfo);
1530 }
1531 }
1532 }
1533 return true;
1534 }
1535
Romain Guye6b8e2f2009-11-10 11:56:55 -08001536 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001537 private void dismissPreview(final View v) {
1538 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001539 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001540 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1541 public void onDismiss() {
1542 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1543 int count = group.getChildCount();
1544 for (int i = 0; i < count; i++) {
1545 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1546 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001547 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1548 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1549
1550 v.setTag(R.id.workspace, null);
1551 v.setTag(R.id.icon, null);
1552 window.setOnDismissListener(null);
1553 }
1554 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001555 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001556 }
1557 v.setTag(null);
1558 }
1559
Romain Guyf8e6a802009-12-07 17:48:02 -08001560 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001561 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001562 }
1563
Romain Guya6abce82009-11-10 02:54:41 -08001564 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001565 final Resources resources = getResources();
1566 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001567
Romain Guya6abce82009-11-10 02:54:41 -08001568 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001569
1570 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001571
Romain Guyf8e6a802009-12-07 17:48:02 -08001572 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001573 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001574 int extraW = (int) ((r.left + r.right) * max);
1575 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001576
1577 int aW = cell.getWidth() - extraW;
1578 float w = aW / max;
1579
1580 int width = cell.getWidth();
1581 int height = cell.getHeight();
1582 int x = cell.getLeftPadding();
1583 int y = cell.getTopPadding();
1584 width -= (x + cell.getRightPadding());
1585 height -= (y + cell.getBottomPadding());
1586
1587 float scale = w / width;
1588
1589 int count = end - start;
1590
1591 final float sWidth = width * scale;
1592 float sHeight = height * scale;
1593
Romain Guye6b8e2f2009-11-10 11:56:55 -08001594 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001595
Romain Guye6b8e2f2009-11-10 11:56:55 -08001596 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1597 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001598
1599 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001600 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001601 cell = (CellLayout) workspace.getChildAt(i);
1602
Romain Guyf8e6a802009-12-07 17:48:02 -08001603 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001604 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001605
1606 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001607 c.scale(scale, scale);
1608 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1609 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001610
Romain Guy9d31e9f2009-11-11 18:54:28 -08001611 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001612 image.setImageBitmap(bitmap);
1613 image.setTag(i);
1614 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001615 image.setOnFocusChangeListener(handler);
1616 image.setFocusable(true);
1617 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001618
1619 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001620 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1621
1622 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001623 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001624
1625 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001626 p.setContentView(preview);
1627 p.setWidth((int) (sWidth * count + extraW));
1628 p.setHeight((int) (sHeight + extraH));
1629 p.setAnimationStyle(R.style.AnimationPreview);
1630 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001631 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001632 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001633 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001634
Romain Guye6b8e2f2009-11-10 11:56:55 -08001635 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1636 public void onDismiss() {
1637 dismissPreview(anchor);
1638 }
1639 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001640
1641 anchor.setTag(p);
1642 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001643 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001644 }
1645
Romain Guy9d31e9f2009-11-11 18:54:28 -08001646 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001647 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001648
Romain Guye6b8e2f2009-11-10 11:56:55 -08001649 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001650 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001651 }
1652
1653 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001654 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001655 v.post(this);
1656 }
1657
1658 public void run() {
1659 dismissPreview(mAnchor);
1660 }
1661
1662 public void onFocusChange(View v, boolean hasFocus) {
1663 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001664 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001665 }
Romain Guya6abce82009-11-10 02:54:41 -08001666 }
1667 }
1668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 Workspace getWorkspace() {
1670 return mWorkspace;
1671 }
1672
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 @Override
1674 protected Dialog onCreateDialog(int id) {
1675 switch (id) {
1676 case DIALOG_CREATE_SHORTCUT:
1677 return new CreateShortcut().createDialog();
1678 case DIALOG_RENAME_FOLDER:
1679 return new RenameFolder().createDialog();
1680 }
1681
1682 return super.onCreateDialog(id);
1683 }
1684
1685 @Override
1686 protected void onPrepareDialog(int id, Dialog dialog) {
1687 switch (id) {
1688 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 break;
1690 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001691 if (mFolderInfo != null) {
1692 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1693 final CharSequence text = mFolderInfo.title;
1694 input.setText(text);
1695 input.setSelection(0, text.length());
1696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 break;
1698 }
1699 }
1700
1701 void showRenameDialog(FolderInfo info) {
1702 mFolderInfo = info;
1703 mWaitingForResult = true;
1704 showDialog(DIALOG_RENAME_FOLDER);
1705 }
1706
1707 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1708 mAddItemCellInfo = cellInfo;
1709 mWaitingForResult = true;
1710 showDialog(DIALOG_CREATE_SHORTCUT);
1711 }
1712
Romain Guy73b979d2009-06-09 12:57:21 -07001713 private void pickShortcut(int requestCode, int title) {
1714 Bundle bundle = new Bundle();
1715
1716 ArrayList<String> shortcutNames = new ArrayList<String>();
1717 shortcutNames.add(getString(R.string.group_applications));
1718 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1719
1720 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1721 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1722 R.drawable.ic_launcher_application));
1723 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1724
1725 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1726 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1727 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1728 pickIntent.putExtras(bundle);
1729
1730 startActivityForResult(pickIntent, requestCode);
1731 }
1732
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 private class RenameFolder {
1734 private EditText mInput;
1735
1736 Dialog createDialog() {
1737 mWaitingForResult = true;
1738 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1739 mInput = (EditText) layout.findViewById(R.id.folder_name);
1740
1741 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1742 builder.setIcon(0);
1743 builder.setTitle(getString(R.string.rename_folder_title));
1744 builder.setCancelable(true);
1745 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1746 public void onCancel(DialogInterface dialog) {
1747 cleanup();
1748 }
1749 });
1750 builder.setNegativeButton(getString(R.string.cancel_action),
1751 new Dialog.OnClickListener() {
1752 public void onClick(DialogInterface dialog, int which) {
1753 cleanup();
1754 }
1755 }
1756 );
1757 builder.setPositiveButton(getString(R.string.rename_action),
1758 new Dialog.OnClickListener() {
1759 public void onClick(DialogInterface dialog, int which) {
1760 changeFolderName();
1761 }
1762 }
1763 );
1764 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001765
1766 final AlertDialog dialog = builder.create();
1767 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1768 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001769 mInput.requestFocus();
1770 InputMethodManager inputManager = (InputMethodManager)
1771 getSystemService(Context.INPUT_METHOD_SERVICE);
1772 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001773 }
1774 });
1775
1776 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 }
1778
1779 private void changeFolderName() {
1780 final String name = mInput.getText().toString();
1781 if (!TextUtils.isEmpty(name)) {
1782 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001783 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 mFolderInfo.title = name;
1785 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1786
Joe Onorato9c1289c2009-08-17 11:03:03 -04001787 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001788 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789 mModel.setWorkspaceDirty();
1790 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 } else {
1792 final FolderIcon folderIcon = (FolderIcon)
1793 mWorkspace.getViewForTag(mFolderInfo);
1794 if (folderIcon != null) {
1795 folderIcon.setText(name);
1796 getWorkspace().requestLayout();
1797 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001798 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001799 mModel.setWorkspaceDirty();
1800 mWorkspaceLoading = true;
1801 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 }
1803 }
1804 }
1805 cleanup();
1806 }
1807
1808 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 dismissDialog(DIALOG_RENAME_FOLDER);
1810 mWaitingForResult = false;
1811 mFolderInfo = null;
1812 }
1813 }
1814
Joe Onoratofb0ca672009-09-14 17:55:46 -04001815 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001816 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001817 }
1818
Joe Onorato3a8820b2009-11-10 15:06:42 -08001819 void showAllApps(boolean animated) {
1820 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001821
Mike Cleronb6082fa02009-10-19 17:03:36 -07001822 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001823 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001824
Joe Onorato7c312c12009-08-13 21:36:53 -07001825 // TODO: fade these two too
1826 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001827 }
1828
Joe Onoratob2061212009-11-24 16:13:54 -05001829 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001830 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001831 * - Home from workspace
1832 * - from center screen
1833 * - from other screens
1834 * - Home from all apps
1835 * - Back from all apps
1836 * - Launch app from workspace and quit
1837 * - with back
1838 * - with home
1839 * - Launch app from all apps and quit
1840 * - with back
1841 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001842 * - Go to a screen that's not the default, then all
1843 * apps, and launch and app, and go back
1844 * - with back
1845 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001846 * - On workspace, long press power and go back
1847 * - with back
1848 * - with home
1849 * - On all apps, long press power and go back
1850 * - with back
1851 * - with home
1852 * - On workspace, power off
1853 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001854 * - Launch an app and turn off the screen while in that app
1855 * - Go back with home key
1856 * - Go back with back key
1857 * - From all apps
1858 * - From workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001859 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001860 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001861 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001862 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001863 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001864 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001865 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001866 }
1867
Joe Onorato7c312c12009-08-13 21:36:53 -07001868 void lockAllApps() {
1869 // TODO
1870 }
1871
1872 void unlockAllApps() {
1873 // TODO
1874 }
1875
Joe Onorato7404ee42009-07-31 11:54:44 -07001876 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 * Displays the shortcut creation dialog and launches, if necessary, the
1878 * appropriate activity.
1879 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001880 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001881 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1882 DialogInterface.OnShowListener {
1883
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001885
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 Dialog createDialog() {
1887 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001888
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001890
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1892 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001893 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001894
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 builder.setInverseBackgroundForced(true);
1896
1897 AlertDialog dialog = builder.create();
1898 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001899 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001900 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 return dialog;
1903 }
1904
1905 public void onCancel(DialogInterface dialog) {
1906 mWaitingForResult = false;
1907 cleanup();
1908 }
1909
Romain Guycbb89e42009-06-08 15:52:54 -07001910 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001911 }
1912
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001914 try {
1915 dismissDialog(DIALOG_CREATE_SHORTCUT);
1916 } catch (Exception e) {
1917 // An exception is thrown if the dialog is not visible, which is fine
1918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
1920
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001921 /**
1922 * Handle the action clicked in the "Add to home" dialog.
1923 */
1924 public void onClick(DialogInterface dialog, int which) {
1925 Resources res = getResources();
1926 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001927
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001928 switch (which) {
1929 case AddAdapter.ITEM_SHORTCUT: {
1930 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001931 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001932 break;
1933 }
Romain Guycbb89e42009-06-08 15:52:54 -07001934
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001935 case AddAdapter.ITEM_APPWIDGET: {
1936 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001937
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001938 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1939 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1940 // add the search widget
1941 ArrayList<AppWidgetProviderInfo> customInfo =
1942 new ArrayList<AppWidgetProviderInfo>();
1943 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1944 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1945 info.label = getString(R.string.group_search);
1946 info.icon = R.drawable.ic_search_widget;
1947 customInfo.add(info);
1948 pickIntent.putParcelableArrayListExtra(
1949 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1950 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1951 Bundle b = new Bundle();
1952 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1953 customExtras.add(b);
1954 pickIntent.putParcelableArrayListExtra(
1955 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1956 // start the pick activity
1957 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1958 break;
1959 }
Romain Guycbb89e42009-06-08 15:52:54 -07001960
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001961 case AddAdapter.ITEM_LIVE_FOLDER: {
1962 // Insert extra item to handle inserting folder
1963 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001964
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001965 ArrayList<String> shortcutNames = new ArrayList<String>();
1966 shortcutNames.add(res.getString(R.string.group_folder));
1967 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001968
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001969 ArrayList<ShortcutIconResource> shortcutIcons =
1970 new ArrayList<ShortcutIconResource>();
1971 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1972 R.drawable.ic_launcher_folder));
1973 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1974
1975 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1976 pickIntent.putExtra(Intent.EXTRA_INTENT,
1977 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1978 pickIntent.putExtra(Intent.EXTRA_TITLE,
1979 getText(R.string.title_select_live_folder));
1980 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001981
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001982 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1983 break;
1984 }
1985
1986 case AddAdapter.ITEM_WALLPAPER: {
1987 startWallpaper();
1988 break;
1989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
1991 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001992
1993 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
1996
1997 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001998 * Receives notifications when applications are added/removed.
1999 */
2000 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2001 @Override
2002 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002003 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002004 String reason = intent.getStringExtra("reason");
2005 if (!"homekey".equals(reason)) {
2006 boolean animate = true;
2007 /*
2008 if ("globalactions".equals(reason)) {
2009 // For some reason (probably the fading), this animation is
2010 // choppy, so don't show it.
2011 animate = false;
2012 }
2013 */
2014 closeAllApps(animate);
2015 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002016 }
2017 }
2018
2019 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002020 * Receives notifications whenever the appwidgets are reset.
2021 */
2022 private class AppWidgetResetObserver extends ContentObserver {
2023 public AppWidgetResetObserver() {
2024 super(new Handler());
2025 }
2026
2027 @Override
2028 public void onChange(boolean selfChange) {
2029 onAppWidgetReset();
2030 }
2031 }
2032
2033 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002034 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002036 public int getCurrentWorkspaceScreen() {
2037 return mWorkspace.getCurrentScreen();
2038 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002039
Joe Onorato9c1289c2009-08-17 11:03:03 -04002040 /**
2041 * Refreshes the shortcuts shown on the workspace.
2042 *
2043 * Implementation of the method from LauncherModel.Callbacks.
2044 */
2045 public void startBinding() {
2046 final Workspace workspace = mWorkspace;
2047 int count = workspace.getChildCount();
2048 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002049 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002050 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2051 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002052
Joe Onorato9c1289c2009-08-17 11:03:03 -04002053 if (DEBUG_USER_INTERFACE) {
2054 android.widget.Button finishButton = new android.widget.Button(this);
2055 finishButton.setText("Finish");
2056 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2057
2058 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2059 public void onClick(View v) {
2060 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002061 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002062 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 }
2064 }
2065
2066 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002067 * Bind the items start-end from the list.
2068 *
2069 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002071 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2072
2073 final Workspace workspace = mWorkspace;
2074
2075 for (int i=start; i<end; i++) {
2076 final ItemInfo item = shortcuts.get(i);
2077 mDesktopItems.add(item);
2078 switch (item.itemType) {
2079 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2080 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2081 final View shortcut = createShortcut((ApplicationInfo) item);
2082 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2083 false);
2084 break;
2085 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2086 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2087 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2088 (UserFolderInfo) item);
2089 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2090 false);
2091 break;
2092 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2093 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2094 R.layout.live_folder_icon, this,
2095 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2096 (LiveFolderInfo) item);
2097 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2098 false);
2099 break;
2100 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2101 final int screen = workspace.getCurrentScreen();
2102 final View view = mInflater.inflate(R.layout.widget_search,
2103 (ViewGroup) workspace.getChildAt(screen), false);
2104
2105 Search search = (Search) view.findViewById(R.id.widget_search);
2106 search.setLauncher(this);
2107
2108 final Widget widget = (Widget) item;
2109 view.setTag(widget);
2110
2111 workspace.addWidget(view, widget, false);
2112 break;
2113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 }
2115
Joe Onorato9c1289c2009-08-17 11:03:03 -04002116 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 }
2118
Joe Onorato9c1289c2009-08-17 11:03:03 -04002119 /**
2120 * Implementation of the method from LauncherModel.Callbacks.
2121 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002122 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2123 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002124 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002126
2127 /**
2128 * Add the views for a widget to the workspace.
2129 *
2130 * Implementation of the method from LauncherModel.Callbacks.
2131 */
2132 public void bindAppWidget(LauncherAppWidgetInfo item) {
2133 final Workspace workspace = mWorkspace;
2134
2135 final int appWidgetId = item.appWidgetId;
2136 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2137 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2138
Joe Onorato9c1289c2009-08-17 11:03:03 -04002139 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2140 item.hostView.setTag(item);
2141
2142 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2143 item.cellY, item.spanX, item.spanY, false);
2144
2145 workspace.requestLayout();
2146
2147 mDesktopItems.add(item);
2148 }
2149
2150 /**
2151 * Callback saying that there aren't any more items to bind.
2152 *
2153 * Implementation of the method from LauncherModel.Callbacks.
2154 */
2155 public void finishBindingItems() {
2156 if (mSavedState != null) {
2157 if (!mWorkspace.hasFocus()) {
2158 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2159 }
2160
2161 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2162 if (userFolders != null) {
2163 for (long folderId : userFolders) {
2164 final FolderInfo info = mFolders.get(folderId);
2165 if (info != null) {
2166 openFolder(info);
2167 }
2168 }
2169 final Folder openFolder = mWorkspace.getOpenFolder();
2170 if (openFolder != null) {
2171 openFolder.requestFocus();
2172 }
2173 }
2174
Joe Onorato9c1289c2009-08-17 11:03:03 -04002175 mSavedState = null;
2176 }
2177
2178 if (mSavedInstanceState != null) {
2179 super.onRestoreInstanceState(mSavedInstanceState);
2180 mSavedInstanceState = null;
2181 }
2182
Joe Onorato9c1289c2009-08-17 11:03:03 -04002183 mWorkspaceLoading = false;
2184 }
2185
2186 /**
2187 * Add the icons for all apps.
2188 *
2189 * Implementation of the method from LauncherModel.Callbacks.
2190 */
2191 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002192 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002193 }
2194
2195 /**
2196 * A package was installed.
2197 *
2198 * Implementation of the method from LauncherModel.Callbacks.
2199 */
2200 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2201 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002202 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002203 }
2204
2205 /**
2206 * A package was updated.
2207 *
2208 * Implementation of the method from LauncherModel.Callbacks.
2209 */
2210 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2211 removeDialog(DIALOG_CREATE_SHORTCUT);
2212 mWorkspace.updateShortcutsForPackage(packageName);
Joe Onoratof0be2132009-11-24 19:34:29 -05002213 mAllAppsGrid.updateApps(packageName, apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002214 }
2215
2216 /**
2217 * A package was uninstalled.
2218 *
2219 * Implementation of the method from LauncherModel.Callbacks.
2220 */
2221 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2222 removeDialog(DIALOG_CREATE_SHORTCUT);
2223 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002224 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002225 }
Joe Onoratobe386092009-11-17 17:32:16 -08002226
2227 /**
2228 * Prints out out state for debugging.
2229 */
2230 public void dumpState() {
2231 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002232 Log.d(TAG, "mSavedState=" + mSavedState);
2233 Log.d(TAG, "mIsNewIntent=" + mIsNewIntent);
2234 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2235 Log.d(TAG, "mRestoring=" + mRestoring);
2236 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2237 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2238 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2239 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002240 mModel.dumpState();
2241 mAllAppsGrid.dumpState();
2242 Log.d(TAG, "END launcher2 dump state");
2243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244}