blob: 08ef8439cdae684a5ab166aa16a00766616cc5ee [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
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000019import com.android.common.Search;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Activity;
22import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.Dialog;
24import android.app.SearchManager;
25import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080028import android.content.BroadcastReceiver;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080029import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070033import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070037import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080040import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080041import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080043import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080045import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Parcelable;
Joe Onoratobe386092009-11-17 17:32:16 -080047import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.text.Selection;
50import android.text.SpannableStringBuilder;
51import android.text.TextUtils;
52import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070053import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080055import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.view.KeyEvent;
57import android.view.LayoutInflater;
58import android.view.Menu;
59import android.view.MenuItem;
60import android.view.View;
61import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.view.View.OnLongClickListener;
63import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.widget.TextView;
66import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080067import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080068import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080069import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070070import android.appwidget.AppWidgetManager;
71import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070075import java.io.DataOutputStream;
76import java.io.FileNotFoundException;
77import java.io.IOException;
78import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
80/**
81 * Default launcher application.
82 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040083public final class Launcher extends Activity
Daniel Sandlerc351eb82010-03-03 15:05:19 -050084 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080085 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 static final boolean LOGD = false;
87
Joe Onorato9c1289c2009-08-17 11:03:03 -040088 static final boolean PROFILE_STARTUP = false;
89 static final boolean PROFILE_ROTATE = false;
90 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070091
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 private static final int WALLPAPER_SCREENS_SPAN = 2;
93
94 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -080095 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
96
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 private static final int MENU_ADD = Menu.FIRST + 1;
98 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
99 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
100 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700101 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
103 private static final int REQUEST_CREATE_SHORTCUT = 1;
104 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700105 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 private static final int REQUEST_PICK_APPLICATION = 6;
107 private static final int REQUEST_PICK_SHORTCUT = 7;
108 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700109 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700110 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
112 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
113
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800114 static final int SCREEN_COUNT = 5;
115 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700117 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato7c312c12009-08-13 21:36:53 -0700119 static final int DIALOG_CREATE_SHORTCUT = 1;
120 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Romain Guy98d01652009-06-30 16:21:04 -0700122 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
124 // Type: int
125 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
126 // Type: boolean
127 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
128 // Type: long
129 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
130 // Type: int
131 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
144 // Type: int[]
145 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
146 // Type: boolean
147 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
148 // Type: long
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
150
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800154 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800156 private final BroadcastReceiver mCloseSystemDialogsReceiver
157 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800158 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 private LayoutInflater mInflater;
161
Joe Onorato00acb122009-08-04 16:04:30 -0400162 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700164
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700165 private AppWidgetManager mAppWidgetManager;
166 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private CellLayout.CellInfo mAddItemCellInfo;
169 private CellLayout.CellInfo mMenuAddInfo;
170 private final int[] mCellCoordinates = new int[2];
171 private FolderInfo mFolderInfo;
172
Joe Onorato7c312c12009-08-13 21:36:53 -0700173 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700174 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700175 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private Bundle mSavedState;
178
179 private SpannableStringBuilder mDefaultKeySsb = null;
180
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 private boolean mWorkspaceLoading = true;
182
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800183 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private boolean mRestoring;
185 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
187 private Bundle mSavedInstanceState;
188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800190 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191
Romain Guy84f296c2009-11-04 15:00:44 -0800192 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
193 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700194
Romain Guy1fbc1c82009-11-09 20:43:08 -0800195 private ImageView mPreviousView;
196 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 @Override
199 protected void onCreate(Bundle savedInstanceState) {
200 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700201
Joe Onorato0589f0f2010-02-08 13:44:00 -0800202 LauncherApplication app = ((LauncherApplication)getApplication());
203 mModel = app.setLauncher(this);
204 mIconCache = app.getIconCache();
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);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800245
246 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
247 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 }
Romain Guycbb89e42009-06-08 15:52:54 -0700249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700251 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
252 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 final Configuration configuration = getResources().getConfiguration();
255
Romain Guy98d01652009-06-30 16:21:04 -0700256 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 final String locale = configuration.locale.toString();
258
Romain Guy98d01652009-06-30 16:21:04 -0700259 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 final int mcc = configuration.mcc;
261
Romain Guy98d01652009-06-30 16:21:04 -0700262 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 final int mnc = configuration.mnc;
264
Romain Guy84f296c2009-11-04 15:00:44 -0800265 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266
Romain Guy84f296c2009-11-04 15:00:44 -0800267 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700268 localeConfiguration.locale = locale;
269 localeConfiguration.mcc = mcc;
270 localeConfiguration.mnc = mnc;
271
272 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800273 mIconCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700274 }
275 }
276
277 private static class LocaleConfiguration {
278 public String locale;
279 public int mcc = -1;
280 public int mnc = -1;
281 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700282
Romain Guy98d01652009-06-30 16:21:04 -0700283 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
284 DataInputStream in = null;
285 try {
286 in = new DataInputStream(context.openFileInput(PREFERENCES));
287 configuration.locale = in.readUTF();
288 configuration.mcc = in.readInt();
289 configuration.mnc = in.readInt();
290 } catch (FileNotFoundException e) {
291 // Ignore
292 } catch (IOException e) {
293 // Ignore
294 } finally {
295 if (in != null) {
296 try {
297 in.close();
298 } catch (IOException e) {
299 // Ignore
300 }
301 }
302 }
303 }
304
305 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
306 DataOutputStream out = null;
307 try {
308 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
309 out.writeUTF(configuration.locale);
310 out.writeInt(configuration.mcc);
311 out.writeInt(configuration.mnc);
312 out.flush();
313 } catch (FileNotFoundException e) {
314 // Ignore
315 } catch (IOException e) {
316 //noinspection ResultOfMethodCallIgnored
317 context.getFileStreamPath(PREFERENCES).delete();
318 } finally {
319 if (out != null) {
320 try {
321 out.close();
322 } catch (IOException e) {
323 // Ignore
324 }
325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 }
327 }
328
329 static int getScreen() {
330 synchronized (sLock) {
331 return sScreen;
332 }
333 }
334
335 static void setScreen(int screen) {
336 synchronized (sLock) {
337 sScreen = screen;
338 }
339 }
340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700342 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343
344 Display display = getWindowManager().getDefaultDisplay();
345 boolean isPortrait = display.getWidth() < display.getHeight();
346
347 final int width = isPortrait ? display.getWidth() : display.getHeight();
348 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700349 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 }
351
352 @Override
353 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700354 mWaitingForResult = false;
355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 // The pattern used here is that a user PICKs a specific application,
357 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
360 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700361
Romain Guy94dabf12009-07-21 10:55:43 -0700362 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 switch (requestCode) {
364 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400365 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 break;
367 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800368 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 break;
370 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400371 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 break;
373 case REQUEST_PICK_LIVE_FOLDER:
374 addLiveFolder(data);
375 break;
376 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400377 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700379 case REQUEST_PICK_APPWIDGET:
380 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400383 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700385 case REQUEST_PICK_WALLPAPER:
386 // We just wanted the activity result here so we can clear mWaitingForResult
387 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 }
Romain Guy18042c82009-11-06 11:44:55 -0800389 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
390 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
391 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700392 // Clean up the appWidgetId if we canceled
393 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
394 if (appWidgetId != -1) {
395 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 }
397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
399
400 @Override
401 protected void onResume() {
402 super.onResume();
403
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800404 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400407 mWorkspaceLoading = true;
408 mModel.startLoader(this, true);
409 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 }
411 }
412
413 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700414 protected void onPause() {
415 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800416 dismissPreview(mPreviousView);
417 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800418 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700419 }
Romain Guycbb89e42009-06-08 15:52:54 -0700420
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700421 @Override
422 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400423 // Flag the loader to stop early before switching
424 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700426 if (PROFILE_ROTATE) {
427 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
428 }
429 return null;
430 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700431
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800432 // We can't hide the IME if it was forced open. So don't bother
433 /*
434 @Override
435 public void onWindowFocusChanged(boolean hasFocus) {
436 super.onWindowFocusChanged(hasFocus);
437
438 if (hasFocus) {
439 final InputMethodManager inputManager = (InputMethodManager)
440 getSystemService(Context.INPUT_METHOD_SERVICE);
441 WindowManager.LayoutParams lp = getWindow().getAttributes();
442 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
443 android.os.Handler()) {
444 protected void onReceiveResult(int resultCode, Bundle resultData) {
445 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
446 }
447 });
448 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
449 }
450 }
451 */
452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 private boolean acceptFilter() {
454 final InputMethodManager inputManager = (InputMethodManager)
455 getSystemService(Context.INPUT_METHOD_SERVICE);
456 return !inputManager.isFullscreenMode();
457 }
458
459 @Override
460 public boolean onKeyDown(int keyCode, KeyEvent event) {
461 boolean handled = super.onKeyDown(keyCode, event);
462 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
463 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
464 keyCode, event);
465 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700466 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700467 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700468 // showSearchDialog()
469 // If there are multiple keystrokes before the search dialog takes focus,
470 // onSearchRequested() will be called for every keystroke,
471 // but it is idempotent, so it's fine.
472 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 }
474 }
475
Joe Onorato8a9625e2010-01-28 15:55:35 -0800476 // Eat the long press event so the keyboard doesn't come up.
477 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
478 return true;
479 }
480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 return handled;
482 }
483
Karl Rosaen138a0412009-04-23 19:00:21 -0700484 private String getTypedText() {
485 return mDefaultKeySsb.toString();
486 }
487
488 private void clearTypedText() {
489 mDefaultKeySsb.clear();
490 mDefaultKeySsb.clearSpans();
491 Selection.setSelection(mDefaultKeySsb, 0);
492 }
493
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 /**
495 * Restores the previous state, if it exists.
496 *
497 * @param savedState The previous state.
498 */
499 private void restoreState(Bundle savedState) {
500 if (savedState == null) {
501 return;
502 }
503
Joe Onorato3a8820b2009-11-10 15:06:42 -0800504 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
505 if (allApps) {
506 showAllApps(false);
507 }
508
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
510 if (currentScreen > -1) {
511 mWorkspace.setCurrentScreen(currentScreen);
512 }
513
514 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
515 if (addScreen > -1) {
516 mAddItemCellInfo = new CellLayout.CellInfo();
517 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
518 addItemCellInfo.valid = true;
519 addItemCellInfo.screen = addScreen;
520 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
521 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
522 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
523 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
524 addItemCellInfo.findVacantCellsFromOccupied(
525 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
526 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
527 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
528 mRestoring = true;
529 }
530
531 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
532 if (renameFolder) {
533 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400534 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 mRestoring = true;
536 }
537 }
538
539 /**
540 * Finds all the views we need and configure them properly.
541 */
542 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400543 DragController dragController = mDragController;
544
Romain Guyb1b69f52009-08-10 15:10:15 -0700545 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400546 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547
Joe Onorato7c312c12009-08-13 21:36:53 -0700548 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700549 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700550 mAllAppsGrid.setDragController(dragController);
Daniel Sandler388f6792010-03-02 14:08:08 -0500551 ((View)mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700552 // Manage focusability manually since this thing is always visible
Daniel Sandler388f6792010-03-02 14:08:08 -0500553 ((View)mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700554
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
556 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500557 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558
Joe Onorato7c312c12009-08-13 21:36:53 -0700559 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
560 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561
Joe Onorato7c312c12009-08-13 21:36:53 -0700562 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700563 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700564 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800565 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800566
567 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
568 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
569
570 Drawable previous = mPreviousView.getDrawable();
571 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800572 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573
Joe Onorato0d44e942009-11-16 18:20:51 -0800574 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800575 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800576 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800577 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800578
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400580 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582
583 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400584 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700585 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586
Joe Onorato00acb122009-08-04 16:04:30 -0400587 dragController.setDragScoller(workspace);
588 dragController.setDragListener(deleteZone);
589 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800590 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700591
Joe Onorato00acb122009-08-04 16:04:30 -0400592 // The order here is bottom to top.
593 dragController.addDropTarget(workspace);
594 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 }
596
Romain Guy8a73c512009-11-09 19:19:59 -0800597 @SuppressWarnings({"UnusedDeclaration"})
598 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800599 if (!isAllAppsVisible()) {
600 mWorkspace.scrollLeft();
601 }
Romain Guy8a73c512009-11-09 19:19:59 -0800602 }
603
604 @SuppressWarnings({"UnusedDeclaration"})
605 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800606 if (!isAllAppsVisible()) {
607 mWorkspace.scrollRight();
608 }
Romain Guy8a73c512009-11-09 19:19:59 -0800609 }
610
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 /**
612 * Creates a view representing a shortcut.
613 *
614 * @param info The data structure describing the shortcut.
615 *
616 * @return A View inflated from R.layout.application.
617 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800618 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 return createShortcut(R.layout.application,
620 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
621 }
622
623 /**
624 * Creates a view representing a shortcut inflated from the specified resource.
625 *
626 * @param layoutResId The id of the XML layout used to create the shortcut.
627 * @param parent The group the shortcut belongs to.
628 * @param info The data structure describing the shortcut.
629 *
630 * @return A View inflated from layoutResId.
631 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800632 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
634
Joe Onorato0589f0f2010-02-08 13:44:00 -0800635 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
636 new FastBitmapDrawable(info.getIcon(mIconCache)),
637 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 favorite.setText(info.title);
639 favorite.setTag(info);
640 favorite.setOnClickListener(this);
641
642 return favorite;
643 }
644
645 /**
646 * Add an application shortcut to the workspace.
647 *
648 * @param data The intent describing the application.
649 * @param cellInfo The position on screen where to create the shortcut.
650 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 cellInfo.screen = mWorkspace.getCurrentScreen();
653 if (!findSingleSlot(cellInfo)) return;
654
Joe Onorato0589f0f2010-02-08 13:44:00 -0800655 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
656 data, context);
657
Romain Guy73b979d2009-06-09 12:57:21 -0700658 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800659 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800661 info.container = ItemInfo.NO_ID;
662 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
663 } else {
664 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
666 }
Romain Guycbb89e42009-06-08 15:52:54 -0700667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 /**
669 * Add a shortcut to the workspace.
670 *
671 * @param data The intent describing the shortcut.
672 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400674 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 cellInfo.screen = mWorkspace.getCurrentScreen();
676 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700677
Joe Onorato0589f0f2010-02-08 13:44:00 -0800678 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679
680 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400682 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
683 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
685 }
686
Romain Guycbb89e42009-06-08 15:52:54 -0700687
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700689 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 * @param data The intent describing the appWidgetId.
692 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400694 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700696 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700697
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800698 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700699
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700700 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700702 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700704 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 // Try finding open space on Launcher screen
707 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800708 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
709 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
710 return;
711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700713 // Build Launcher-specific widget info and save to database
714 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 launcherInfo.spanX = spans[0];
716 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700717
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 LauncherModel.addItemToDatabase(this, launcherInfo,
719 LauncherSettings.Favorites.CONTAINER_DESKTOP,
720 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
721
722 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400723 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700724
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700726 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700727
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400732 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 }
734 }
Romain Guycbb89e42009-06-08 15:52:54 -0700735
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
737 mDesktopItems.remove(launcherInfo);
738 launcherInfo.hostView = null;
739 }
740
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700741 public LauncherAppWidgetHost getAppWidgetHost() {
742 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 }
Romain Guycbb89e42009-06-08 15:52:54 -0700744
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800745 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800746 getWindow().closeAllPanels();
747
748 try {
749 dismissDialog(DIALOG_CREATE_SHORTCUT);
750 // Unlock the workspace if the dialog was showing
751 } catch (Exception e) {
752 // An exception is thrown if the dialog is not visible, which is fine
753 }
754
755 try {
756 dismissDialog(DIALOG_RENAME_FOLDER);
757 // Unlock the workspace if the dialog was showing
758 } catch (Exception e) {
759 // An exception is thrown if the dialog is not visible, which is fine
760 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800761
762 // Whatever we were doing is hereby canceled.
763 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800764 }
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 @Override
767 protected void onNewIntent(Intent intent) {
768 super.onNewIntent(intent);
769
770 // Close the menu
771 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800772 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800773 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700774
Joe Onorato14f122b2009-11-19 14:06:36 -0800775 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
776 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
777 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800778 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800779 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800780 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800781 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700782
Joe Onorato3a8820b2009-11-10 15:06:42 -0800783 final View v = getWindow().peekDecorView();
784 if (v != null && v.getWindowToken() != null) {
785 InputMethodManager imm = (InputMethodManager)getSystemService(
786 INPUT_METHOD_SERVICE);
787 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 }
789 }
790 }
791
792 @Override
793 protected void onRestoreInstanceState(Bundle savedInstanceState) {
794 // Do not call super here
795 mSavedInstanceState = savedInstanceState;
796 }
797
798 @Override
799 protected void onSaveInstanceState(Bundle outState) {
800 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
801
802 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
803 if (folders.size() > 0) {
804 final int count = folders.size();
805 long[] ids = new long[count];
806 for (int i = 0; i < count; i++) {
807 final FolderInfo info = folders.get(i).getInfo();
808 ids[i] = info.id;
809 }
810 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
811 } else {
812 super.onSaveInstanceState(outState);
813 }
814
Joe Onoratofb0ca672009-09-14 17:55:46 -0400815 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800816 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819
820 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
821 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
822 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
823
824 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
825 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
826 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
827 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
828 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
829 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
830 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
831 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
832 layout.getOccupiedCells());
833 }
834
835 if (mFolderInfo != null && mWaitingForResult) {
836 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
837 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
838 }
839 }
840
841 @Override
842 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700846 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800848 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 }
850
851 TextKeyListener.getInstance().release();
852
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854
Joe Onorato9c1289c2009-08-17 11:03:03 -0400855 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800856
857 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800858
859 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800860 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800861
862 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 }
864
865 @Override
866 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700867 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 super.startActivityForResult(intent, requestCode);
869 }
870
871 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700872 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700874
Joe Onorato7bb17492009-09-24 17:51:01 -0700875 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700876
Karl Rosaen138a0412009-04-23 19:00:21 -0700877 if (initialQuery == null) {
878 // Use any text typed in the launcher as the initial query
879 initialQuery = getTypedText();
880 clearTypedText();
881 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 if (appSearchData == null) {
883 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +0000884 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
Romain Guycbb89e42009-06-08 15:52:54 -0700886
Karl Rosaen138a0412009-04-23 19:00:21 -0700887 final SearchManager searchManager =
888 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -0700889 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700890 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
892
893 @Override
894 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400895 if (isWorkspaceLocked()) {
896 return false;
897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898
899 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800900
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
902 .setIcon(android.R.drawable.ic_menu_add)
903 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800904 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 .setIcon(android.R.drawable.ic_menu_gallery)
906 .setAlphabeticShortcut('W');
907 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
908 .setIcon(android.R.drawable.ic_search_category_default)
909 .setAlphabeticShortcut(SearchManager.MENU_KEY);
910 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
911 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
912 .setAlphabeticShortcut('N');
913
914 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700915 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
916 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917
918 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
919 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
920 .setIntent(settings);
921
922 return true;
923 }
924
925 @Override
926 public boolean onPrepareOptionsMenu(Menu menu) {
927 super.onPrepareOptionsMenu(menu);
928
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800929 // If all apps is animating, don't show the menu, because we don't know
930 // which one to show.
931 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
932 return false;
933 }
934
935 // Only show the add and wallpaper options when we're not in all apps.
936 boolean visible = !mAllAppsGrid.isOpaque();
937 menu.setGroupVisible(MENU_GROUP_ADD, visible);
938 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
939
940 // Disable add if the workspace is full.
941 if (visible) {
942 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
943 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945
946 return true;
947 }
948
949 @Override
950 public boolean onOptionsItemSelected(MenuItem item) {
951 switch (item.getItemId()) {
952 case MENU_ADD:
953 addItems();
954 return true;
955 case MENU_WALLPAPER_SETTINGS:
956 startWallpaper();
957 return true;
958 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700959 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 return true;
961 case MENU_NOTIFICATIONS:
962 showNotifications();
963 return true;
964 }
965
966 return super.onOptionsItemSelected(item);
967 }
Romain Guycbb89e42009-06-08 15:52:54 -0700968
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 /**
970 * Indicates that we want global search for this activity by setting the globalSearch
971 * argument for {@link #startSearch} to true.
972 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700974 @Override
975 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -0700976 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -0700977 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700978 }
979
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980 public boolean isWorkspaceLocked() {
981 return mWorkspaceLoading || mWaitingForResult;
982 }
983
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -0500985 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 showAddDialog(mMenuAddInfo);
987 }
988
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700989 void addAppWidget(Intent data) {
990 // TODO: catch bad widget exception when sent
991 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000992 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993
Bjorn Bringert7984c942009-12-09 15:38:25 +0000994 if (appWidget.configure != null) {
995 // Launch over to configure widget, if needed
996 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
997 intent.setComponent(appWidget.configure);
998 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
999
Romain Guy8e633c52010-03-04 12:51:36 -08001000 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001002 // Otherwise just add it
1003 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 }
1005 }
Romain Guycbb89e42009-06-08 15:52:54 -07001006
Joe Onoratodeb98af2010-02-19 14:59:39 -08001007 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001008 // Handle case where user selected "Applications"
1009 String applicationName = getResources().getString(R.string.group_applications);
1010 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001011
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001012 if (applicationName != null && applicationName.equals(shortcutName)) {
1013 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1014 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001015
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001016 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1017 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001018 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001019 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001020 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023
1024 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001025 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001026 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001027 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001028
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001029 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001030 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001031 } else {
1032 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 }
1035
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 UserFolderInfo folderInfo = new UserFolderInfo();
1038 folderInfo.title = getText(R.string.folder_name);
1039
1040 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1041 cellInfo.screen = mWorkspace.getCurrentScreen();
1042 if (!findSingleSlot(cellInfo)) return;
1043
1044 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045 LauncherModel.addItemToDatabase(this, folderInfo,
1046 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049
1050 // Create the view
1051 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1052 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1053 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 }
Romain Guycbb89e42009-06-08 15:52:54 -07001056
Joe Onorato9c1289c2009-08-17 11:03:03 -04001057 void removeFolder(FolderInfo folder) {
1058 mFolders.remove(folder.id);
1059 }
1060
1061 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 cellInfo.screen = mWorkspace.getCurrentScreen();
1063 if (!findSingleSlot(cellInfo)) return;
1064
1065 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1066
1067 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1069 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001070 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1071 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 }
1073 }
1074
1075 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1076 CellLayout.CellInfo cellInfo, boolean notify) {
1077
1078 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1079 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1080
1081 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 Intent.ShortcutIconResource iconResource = null;
1083
1084 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1085 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1086 try {
1087 iconResource = (Intent.ShortcutIconResource) extra;
1088 final PackageManager packageManager = context.getPackageManager();
1089 Resources resources = packageManager.getResourcesForApplication(
1090 iconResource.packageName);
1091 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1092 icon = resources.getDrawable(id);
1093 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001094 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
1096 }
1097
1098 if (icon == null) {
1099 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1100 }
1101
1102 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001103 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 info.title = name;
1105 info.iconResource = iconResource;
1106 info.uri = data.getData();
1107 info.baseIntent = baseIntent;
1108 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1109 LiveFolders.DISPLAY_MODE_GRID);
1110
1111 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1112 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001113 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114
1115 return info;
1116 }
1117
1118 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1119 final int[] xy = new int[2];
1120 if (findSlot(cellInfo, xy, 1, 1)) {
1121 cellInfo.cellX = xy[0];
1122 cellInfo.cellY = xy[1];
1123 return true;
1124 }
1125 return false;
1126 }
1127
1128 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1129 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1130 boolean[] occupied = mSavedState != null ?
1131 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1132 cellInfo = mWorkspace.findAllVacantCells(occupied);
1133 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1134 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1135 return false;
1136 }
1137 }
1138 return true;
1139 }
1140
1141 private void showNotifications() {
1142 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1143 if (statusBar != null) {
1144 statusBar.expand();
1145 }
1146 }
1147
1148 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001149 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001151 Intent chooser = Intent.createChooser(pickWallpaper,
1152 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001153 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1154 // Removed in Eclair MR1
1155// WallpaperManager wm = (WallpaperManager)
1156// getSystemService(Context.WALLPAPER_SERVICE);
1157// WallpaperInfo wi = wm.getWallpaperInfo();
1158// if (wi != null && wi.getSettingsActivity() != null) {
1159// LabeledIntent li = new LabeledIntent(getPackageName(),
1160// R.string.configure_wallpaper, 0);
1161// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1162// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1163// }
Mike Clerona0618e42009-10-22 13:55:21 -07001164 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 }
1166
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001167 /**
1168 * Registers various content observers. The current implementation registers
1169 * only a favorites observer to keep track of the favorites applications.
1170 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001171 private void registerContentObservers() {
1172 ContentResolver resolver = getContentResolver();
1173 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1174 true, mWidgetObserver);
1175 }
1176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 @Override
1178 public boolean dispatchKeyEvent(KeyEvent event) {
1179 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1180 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001182 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001183 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001184 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001185 dumpState();
1186 return true;
1187 }
1188 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001189 }
1190 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1191 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001192 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 return true;
1194 }
1195 }
1196
1197 return super.dispatchKeyEvent(event);
1198 }
1199
Joe Onorato88ec0992009-11-19 13:16:06 -08001200 @Override
1201 public void onBackPressed() {
1202 if (isAllAppsVisible()) {
1203 closeAllApps(true);
1204 } else {
1205 closeFolder();
1206 }
1207 dismissPreview(mPreviousView);
1208 dismissPreview(mNextView);
1209 }
1210
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 private void closeFolder() {
1212 Folder folder = mWorkspace.getOpenFolder();
1213 if (folder != null) {
1214 closeFolder(folder);
1215 }
1216 }
1217
1218 void closeFolder(Folder folder) {
1219 folder.getInfo().opened = false;
1220 ViewGroup parent = (ViewGroup) folder.getParent();
1221 if (parent != null) {
1222 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001223 if (folder instanceof DropTarget) {
1224 // Live folders aren't DropTargets.
1225 mDragController.removeDropTarget((DropTarget)folder);
1226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228 folder.onClose();
1229 }
1230
1231 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001232 * Re-listen when widgets are reset.
1233 */
1234 private void onAppWidgetReset() {
1235 mAppWidgetHost.startListening();
1236 }
1237
1238 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1240 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001242 private void unbindDesktopItems() {
1243 for (ItemInfo item: mDesktopItems) {
1244 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 }
1246 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 /**
1249 * Launches the intent referred by the clicked shortcut.
1250 *
1251 * @param v The view representing the clicked shortcut.
1252 */
1253 public void onClick(View v) {
1254 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001255 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001257 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001258 int[] pos = new int[2];
1259 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001260 intent.setSourceBounds(new Rect(pos[0], pos[1],
1261 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 startActivitySafely(intent);
1263 } else if (tag instanceof FolderInfo) {
1264 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001265 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001266 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001267 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001268 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001269 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
1272 }
1273
1274 void startActivitySafely(Intent intent) {
1275 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1276 try {
1277 startActivity(intent);
1278 } catch (ActivityNotFoundException e) {
1279 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1280 } catch (SecurityException e) {
1281 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001282 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1284 "or use the exported attribute for this activity.", e);
1285 }
1286 }
Romain Guy8e633c52010-03-04 12:51:36 -08001287
1288 void startActivityForResultSafely(Intent intent, int requestCode) {
1289 try {
1290 startActivityForResult(intent, requestCode);
1291 } catch (ActivityNotFoundException e) {
1292 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1293 } catch (SecurityException e) {
1294 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1295 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1296 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1297 "or use the exported attribute for this activity.", e);
1298 }
1299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300
1301 private void handleFolderClick(FolderInfo folderInfo) {
1302 if (!folderInfo.opened) {
1303 // Close any open folder
1304 closeFolder();
1305 // Open the requested folder
1306 openFolder(folderInfo);
1307 } else {
1308 // Find the open folder...
1309 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1310 int folderScreen;
1311 if (openFolder != null) {
1312 folderScreen = mWorkspace.getScreenForView(openFolder);
1313 // .. and close it
1314 closeFolder(openFolder);
1315 if (folderScreen != mWorkspace.getCurrentScreen()) {
1316 // Close any folder open on the current screen
1317 closeFolder();
1318 // Pull the folder onto this screen
1319 openFolder(folderInfo);
1320 }
1321 }
1322 }
1323 }
1324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 /**
1326 * Opens the user fodler described by the specified tag. The opening of the folder
1327 * is animated relative to the specified View. If the View is null, no animation
1328 * is played.
1329 *
1330 * @param folderInfo The FolderInfo describing the folder to open.
1331 */
1332 private void openFolder(FolderInfo folderInfo) {
1333 Folder openFolder;
1334
1335 if (folderInfo instanceof UserFolderInfo) {
1336 openFolder = UserFolder.fromXml(this);
1337 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001338 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 } else {
1340 return;
1341 }
1342
Joe Onorato00acb122009-08-04 16:04:30 -04001343 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 openFolder.setLauncher(this);
1345
1346 openFolder.bind(folderInfo);
1347 folderInfo.opened = true;
1348
1349 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1350 openFolder.onOpen();
1351 }
1352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001354 switch (v.getId()) {
1355 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001356 if (!isAllAppsVisible()) {
1357 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1358 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001359 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001360 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001361 return true;
1362 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001363 if (!isAllAppsVisible()) {
1364 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1365 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001366 showPreviews(v);
1367 }
1368 return true;
1369 case R.id.all_apps_button:
1370 if (!isAllAppsVisible()) {
1371 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1372 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1373 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001374 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001375 return true;
1376 }
1377
Joe Onorato9c1289c2009-08-17 11:03:03 -04001378 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 return false;
1380 }
1381
1382 if (!(v instanceof CellLayout)) {
1383 v = (View) v.getParent();
1384 }
1385
1386 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1387
1388 // This happens when long clicking an item with the dpad/trackball
1389 if (cellInfo == null) {
1390 return true;
1391 }
1392
1393 if (mWorkspace.allowLongPress()) {
1394 if (cellInfo.cell == null) {
1395 if (cellInfo.valid) {
1396 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001397 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001398 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1399 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 showAddDialog(cellInfo);
1401 }
1402 } else {
1403 if (!(cellInfo.cell instanceof Folder)) {
1404 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001405 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1406 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 mWorkspace.startDrag(cellInfo);
1408 }
1409 }
1410 }
1411 return true;
1412 }
1413
Romain Guye6b8e2f2009-11-10 11:56:55 -08001414 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001415 private void dismissPreview(final View v) {
1416 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001417 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001418 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1419 public void onDismiss() {
1420 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1421 int count = group.getChildCount();
1422 for (int i = 0; i < count; i++) {
1423 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1424 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001425 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1426 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1427
1428 v.setTag(R.id.workspace, null);
1429 v.setTag(R.id.icon, null);
1430 window.setOnDismissListener(null);
1431 }
1432 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001433 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001434 }
1435 v.setTag(null);
1436 }
1437
Romain Guyf8e6a802009-12-07 17:48:02 -08001438 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001439 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001440 }
1441
Romain Guya6abce82009-11-10 02:54:41 -08001442 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001443 final Resources resources = getResources();
1444 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001445
Romain Guya6abce82009-11-10 02:54:41 -08001446 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001447
1448 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001449
Romain Guyf8e6a802009-12-07 17:48:02 -08001450 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001451 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001452 int extraW = (int) ((r.left + r.right) * max);
1453 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001454
1455 int aW = cell.getWidth() - extraW;
1456 float w = aW / max;
1457
1458 int width = cell.getWidth();
1459 int height = cell.getHeight();
1460 int x = cell.getLeftPadding();
1461 int y = cell.getTopPadding();
1462 width -= (x + cell.getRightPadding());
1463 height -= (y + cell.getBottomPadding());
1464
1465 float scale = w / width;
1466
1467 int count = end - start;
1468
1469 final float sWidth = width * scale;
1470 float sHeight = height * scale;
1471
Romain Guye6b8e2f2009-11-10 11:56:55 -08001472 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001473
Romain Guye6b8e2f2009-11-10 11:56:55 -08001474 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1475 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001476
1477 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001478 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001479 cell = (CellLayout) workspace.getChildAt(i);
1480
Romain Guyf8e6a802009-12-07 17:48:02 -08001481 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001482 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001483
1484 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001485 c.scale(scale, scale);
1486 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1487 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001488
Romain Guy9d31e9f2009-11-11 18:54:28 -08001489 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001490 image.setImageBitmap(bitmap);
1491 image.setTag(i);
1492 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001493 image.setOnFocusChangeListener(handler);
1494 image.setFocusable(true);
1495 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001496
1497 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001498 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1499
1500 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001501 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001502
1503 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001504 p.setContentView(preview);
1505 p.setWidth((int) (sWidth * count + extraW));
1506 p.setHeight((int) (sHeight + extraH));
1507 p.setAnimationStyle(R.style.AnimationPreview);
1508 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001509 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001510 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001511 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001512
Romain Guye6b8e2f2009-11-10 11:56:55 -08001513 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1514 public void onDismiss() {
1515 dismissPreview(anchor);
1516 }
1517 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001518
1519 anchor.setTag(p);
1520 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001521 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001522 }
1523
Romain Guy9d31e9f2009-11-11 18:54:28 -08001524 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001525 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001526
Romain Guye6b8e2f2009-11-10 11:56:55 -08001527 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001528 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001529 }
1530
1531 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001532 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001533 v.post(this);
1534 }
1535
1536 public void run() {
1537 dismissPreview(mAnchor);
1538 }
1539
1540 public void onFocusChange(View v, boolean hasFocus) {
1541 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001542 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001543 }
Romain Guya6abce82009-11-10 02:54:41 -08001544 }
1545 }
1546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 Workspace getWorkspace() {
1548 return mWorkspace;
1549 }
1550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 @Override
1552 protected Dialog onCreateDialog(int id) {
1553 switch (id) {
1554 case DIALOG_CREATE_SHORTCUT:
1555 return new CreateShortcut().createDialog();
1556 case DIALOG_RENAME_FOLDER:
1557 return new RenameFolder().createDialog();
1558 }
1559
1560 return super.onCreateDialog(id);
1561 }
1562
1563 @Override
1564 protected void onPrepareDialog(int id, Dialog dialog) {
1565 switch (id) {
1566 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 break;
1568 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001569 if (mFolderInfo != null) {
1570 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1571 final CharSequence text = mFolderInfo.title;
1572 input.setText(text);
1573 input.setSelection(0, text.length());
1574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 break;
1576 }
1577 }
1578
1579 void showRenameDialog(FolderInfo info) {
1580 mFolderInfo = info;
1581 mWaitingForResult = true;
1582 showDialog(DIALOG_RENAME_FOLDER);
1583 }
1584
1585 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1586 mAddItemCellInfo = cellInfo;
1587 mWaitingForResult = true;
1588 showDialog(DIALOG_CREATE_SHORTCUT);
1589 }
1590
Joe Onoratodeb98af2010-02-19 14:59:39 -08001591 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001592 Bundle bundle = new Bundle();
1593
1594 ArrayList<String> shortcutNames = new ArrayList<String>();
1595 shortcutNames.add(getString(R.string.group_applications));
1596 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1597
1598 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1599 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1600 R.drawable.ic_launcher_application));
1601 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1602
1603 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1604 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001605 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001606 pickIntent.putExtras(bundle);
1607
Joe Onoratodeb98af2010-02-19 14:59:39 -08001608 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001609 }
1610
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 private class RenameFolder {
1612 private EditText mInput;
1613
1614 Dialog createDialog() {
1615 mWaitingForResult = true;
1616 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1617 mInput = (EditText) layout.findViewById(R.id.folder_name);
1618
1619 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1620 builder.setIcon(0);
1621 builder.setTitle(getString(R.string.rename_folder_title));
1622 builder.setCancelable(true);
1623 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1624 public void onCancel(DialogInterface dialog) {
1625 cleanup();
1626 }
1627 });
1628 builder.setNegativeButton(getString(R.string.cancel_action),
1629 new Dialog.OnClickListener() {
1630 public void onClick(DialogInterface dialog, int which) {
1631 cleanup();
1632 }
1633 }
1634 );
1635 builder.setPositiveButton(getString(R.string.rename_action),
1636 new Dialog.OnClickListener() {
1637 public void onClick(DialogInterface dialog, int which) {
1638 changeFolderName();
1639 }
1640 }
1641 );
1642 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001643
1644 final AlertDialog dialog = builder.create();
1645 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1646 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001647 mInput.requestFocus();
1648 InputMethodManager inputManager = (InputMethodManager)
1649 getSystemService(Context.INPUT_METHOD_SERVICE);
1650 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001651 }
1652 });
1653
1654 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 }
1656
1657 private void changeFolderName() {
1658 final String name = mInput.getText().toString();
1659 if (!TextUtils.isEmpty(name)) {
1660 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001661 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 mFolderInfo.title = name;
1663 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1664
Joe Onorato9c1289c2009-08-17 11:03:03 -04001665 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001666 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001667 mModel.setWorkspaceDirty();
1668 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 } else {
1670 final FolderIcon folderIcon = (FolderIcon)
1671 mWorkspace.getViewForTag(mFolderInfo);
1672 if (folderIcon != null) {
1673 folderIcon.setText(name);
1674 getWorkspace().requestLayout();
1675 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001676 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001677 mModel.setWorkspaceDirty();
1678 mWorkspaceLoading = true;
1679 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 }
1681 }
1682 }
1683 cleanup();
1684 }
1685
1686 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 dismissDialog(DIALOG_RENAME_FOLDER);
1688 mWaitingForResult = false;
1689 mFolderInfo = null;
1690 }
1691 }
1692
Joe Onoratofb0ca672009-09-14 17:55:46 -04001693 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001694 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001695 }
1696
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001697 // AllAppsView.Watcher
1698 public void zoomed(float zoom) {
1699 if (zoom == 1.0f) {
1700 mWorkspace.setVisibility(View.GONE);
1701 }
1702 }
1703
Joe Onorato3a8820b2009-11-10 15:06:42 -08001704 void showAllApps(boolean animated) {
1705 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001706
Daniel Sandler388f6792010-03-02 14:08:08 -05001707 ((View)mAllAppsGrid).setFocusable(true);
1708 ((View)mAllAppsGrid).requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001709
Joe Onorato7c312c12009-08-13 21:36:53 -07001710 // TODO: fade these two too
1711 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001712 }
1713
Joe Onoratob2061212009-11-24 16:13:54 -05001714 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001715 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001716 * - Home from workspace
1717 * - from center screen
1718 * - from other screens
1719 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001720 * - from center screen
1721 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001722 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001723 * - from center screen
1724 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001725 * - Launch app from workspace and quit
1726 * - with back
1727 * - with home
1728 * - Launch app from all apps and quit
1729 * - with back
1730 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001731 * - Go to a screen that's not the default, then all
1732 * apps, and launch and app, and go back
1733 * - with back
1734 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001735 * - On workspace, long press power and go back
1736 * - with back
1737 * - with home
1738 * - On all apps, long press power and go back
1739 * - with back
1740 * - with home
1741 * - On workspace, power off
1742 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001743 * - Launch an app and turn off the screen while in that app
1744 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001745 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001746 * - From all apps
1747 * - From workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001748 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001749 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001750 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001751 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001752 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05001753 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001754 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001755 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001756 }
1757
Joe Onorato7c312c12009-08-13 21:36:53 -07001758 void lockAllApps() {
1759 // TODO
1760 }
1761
1762 void unlockAllApps() {
1763 // TODO
1764 }
1765
Joe Onorato7404ee42009-07-31 11:54:44 -07001766 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 * Displays the shortcut creation dialog and launches, if necessary, the
1768 * appropriate activity.
1769 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001770 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001771 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1772 DialogInterface.OnShowListener {
1773
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001775
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 Dialog createDialog() {
1777 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001780
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1782 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001783 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001784
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 builder.setInverseBackgroundForced(true);
1786
1787 AlertDialog dialog = builder.create();
1788 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001789 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001790 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 return dialog;
1793 }
1794
1795 public void onCancel(DialogInterface dialog) {
1796 mWaitingForResult = false;
1797 cleanup();
1798 }
1799
Romain Guycbb89e42009-06-08 15:52:54 -07001800 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001801 }
1802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001804 try {
1805 dismissDialog(DIALOG_CREATE_SHORTCUT);
1806 } catch (Exception e) {
1807 // An exception is thrown if the dialog is not visible, which is fine
1808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
1810
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001811 /**
1812 * Handle the action clicked in the "Add to home" dialog.
1813 */
1814 public void onClick(DialogInterface dialog, int which) {
1815 Resources res = getResources();
1816 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001817
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001818 switch (which) {
1819 case AddAdapter.ITEM_SHORTCUT: {
1820 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08001821 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001822 break;
1823 }
Romain Guycbb89e42009-06-08 15:52:54 -07001824
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001825 case AddAdapter.ITEM_APPWIDGET: {
1826 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001827
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001828 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1829 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001830 // start the pick activity
1831 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1832 break;
1833 }
Romain Guycbb89e42009-06-08 15:52:54 -07001834
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001835 case AddAdapter.ITEM_LIVE_FOLDER: {
1836 // Insert extra item to handle inserting folder
1837 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001838
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001839 ArrayList<String> shortcutNames = new ArrayList<String>();
1840 shortcutNames.add(res.getString(R.string.group_folder));
1841 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001842
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001843 ArrayList<ShortcutIconResource> shortcutIcons =
1844 new ArrayList<ShortcutIconResource>();
1845 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1846 R.drawable.ic_launcher_folder));
1847 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1848
1849 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1850 pickIntent.putExtra(Intent.EXTRA_INTENT,
1851 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1852 pickIntent.putExtra(Intent.EXTRA_TITLE,
1853 getText(R.string.title_select_live_folder));
1854 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001855
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001856 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1857 break;
1858 }
1859
1860 case AddAdapter.ITEM_WALLPAPER: {
1861 startWallpaper();
1862 break;
1863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 }
1865 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001866
1867 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001868 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 }
1870
1871 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001872 * Receives notifications when applications are added/removed.
1873 */
1874 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1875 @Override
1876 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001877 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05001878 String reason = intent.getStringExtra("reason");
1879 if (!"homekey".equals(reason)) {
1880 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001881 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05001882 animate = false;
1883 }
Joe Onoratob2061212009-11-24 16:13:54 -05001884 closeAllApps(animate);
1885 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001886 }
1887 }
1888
1889 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001890 * Receives notifications whenever the appwidgets are reset.
1891 */
1892 private class AppWidgetResetObserver extends ContentObserver {
1893 public AppWidgetResetObserver() {
1894 super(new Handler());
1895 }
1896
1897 @Override
1898 public void onChange(boolean selfChange) {
1899 onAppWidgetReset();
1900 }
1901 }
1902
1903 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001904 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001906 public int getCurrentWorkspaceScreen() {
1907 return mWorkspace.getCurrentScreen();
1908 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001909
Joe Onorato9c1289c2009-08-17 11:03:03 -04001910 /**
1911 * Refreshes the shortcuts shown on the workspace.
1912 *
1913 * Implementation of the method from LauncherModel.Callbacks.
1914 */
1915 public void startBinding() {
1916 final Workspace workspace = mWorkspace;
1917 int count = workspace.getChildCount();
1918 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001919 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001920 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1921 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001922
Joe Onorato9c1289c2009-08-17 11:03:03 -04001923 if (DEBUG_USER_INTERFACE) {
1924 android.widget.Button finishButton = new android.widget.Button(this);
1925 finishButton.setText("Finish");
1926 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1927
1928 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1929 public void onClick(View v) {
1930 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001931 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001932 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 }
1934 }
1935
1936 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001937 * Bind the items start-end from the list.
1938 *
1939 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001941 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1942
1943 final Workspace workspace = mWorkspace;
1944
1945 for (int i=start; i<end; i++) {
1946 final ItemInfo item = shortcuts.get(i);
1947 mDesktopItems.add(item);
1948 switch (item.itemType) {
1949 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1950 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001951 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001952 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1953 false);
1954 break;
1955 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1956 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1957 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1958 (UserFolderInfo) item);
1959 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1960 false);
1961 break;
1962 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1963 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1964 R.layout.live_folder_icon, this,
1965 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1966 (LiveFolderInfo) item);
1967 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1968 false);
1969 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 }
1972
Joe Onorato9c1289c2009-08-17 11:03:03 -04001973 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975
Joe Onorato9c1289c2009-08-17 11:03:03 -04001976 /**
1977 * Implementation of the method from LauncherModel.Callbacks.
1978 */
Joe Onoratoad72e172009-11-06 16:25:04 -05001979 public void bindFolders(HashMap<Long, FolderInfo> folders) {
1980 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001981 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001983
1984 /**
1985 * Add the views for a widget to the workspace.
1986 *
1987 * Implementation of the method from LauncherModel.Callbacks.
1988 */
1989 public void bindAppWidget(LauncherAppWidgetInfo item) {
1990 final Workspace workspace = mWorkspace;
1991
1992 final int appWidgetId = item.appWidgetId;
1993 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1994 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1995
Joe Onorato9c1289c2009-08-17 11:03:03 -04001996 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1997 item.hostView.setTag(item);
1998
1999 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2000 item.cellY, item.spanX, item.spanY, false);
2001
2002 workspace.requestLayout();
2003
2004 mDesktopItems.add(item);
2005 }
2006
2007 /**
2008 * Callback saying that there aren't any more items to bind.
2009 *
2010 * Implementation of the method from LauncherModel.Callbacks.
2011 */
2012 public void finishBindingItems() {
2013 if (mSavedState != null) {
2014 if (!mWorkspace.hasFocus()) {
2015 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2016 }
2017
2018 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2019 if (userFolders != null) {
2020 for (long folderId : userFolders) {
2021 final FolderInfo info = mFolders.get(folderId);
2022 if (info != null) {
2023 openFolder(info);
2024 }
2025 }
2026 final Folder openFolder = mWorkspace.getOpenFolder();
2027 if (openFolder != null) {
2028 openFolder.requestFocus();
2029 }
2030 }
2031
Joe Onorato9c1289c2009-08-17 11:03:03 -04002032 mSavedState = null;
2033 }
2034
2035 if (mSavedInstanceState != null) {
2036 super.onRestoreInstanceState(mSavedInstanceState);
2037 mSavedInstanceState = null;
2038 }
2039
Joe Onorato9c1289c2009-08-17 11:03:03 -04002040 mWorkspaceLoading = false;
2041 }
2042
2043 /**
2044 * Add the icons for all apps.
2045 *
2046 * Implementation of the method from LauncherModel.Callbacks.
2047 */
2048 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002049 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002050 }
2051
2052 /**
2053 * A package was installed.
2054 *
2055 * Implementation of the method from LauncherModel.Callbacks.
2056 */
2057 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2058 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002059 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002060 }
2061
2062 /**
2063 * A package was updated.
2064 *
2065 * Implementation of the method from LauncherModel.Callbacks.
2066 */
2067 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2068 removeDialog(DIALOG_CREATE_SHORTCUT);
2069 mWorkspace.updateShortcutsForPackage(packageName);
Joe Onoratof0be2132009-11-24 19:34:29 -05002070 mAllAppsGrid.updateApps(packageName, apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002071 }
2072
2073 /**
2074 * A package was uninstalled.
2075 *
2076 * Implementation of the method from LauncherModel.Callbacks.
2077 */
2078 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2079 removeDialog(DIALOG_CREATE_SHORTCUT);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002080 mWorkspace.removeItemsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002081 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002082 }
Joe Onoratobe386092009-11-17 17:32:16 -08002083
2084 /**
2085 * Prints out out state for debugging.
2086 */
2087 public void dumpState() {
2088 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002089 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002090 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2091 Log.d(TAG, "mRestoring=" + mRestoring);
2092 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2093 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2094 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2095 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002096 mModel.dumpState();
2097 mAllAppsGrid.dumpState();
2098 Log.d(TAG, "END launcher2 dump state");
2099 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100}