blob: 6c42281063439e07628dc61d181226db0581cb14 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
20import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070022import android.app.ISearchManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.SearchManager;
24import android.app.StatusBarManager;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070025import android.app.WallpaperInfo;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.ComponentName;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ActivityInfo;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070035import android.content.pm.LabeledIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070039import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080042import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080043import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080046import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.os.Parcelable;
48import android.os.RemoteException;
49import android.os.ServiceManager;
Joe Onorato080d9b62009-11-02 12:01:11 -050050import android.os.SystemClock;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.text.Selection;
53import android.text.SpannableStringBuilder;
54import android.text.TextUtils;
55import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070056import static android.util.Log.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070057import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.view.KeyEvent;
60import android.view.LayoutInflater;
61import android.view.Menu;
62import android.view.MenuItem;
63import android.view.View;
64import android.view.ViewGroup;
Romain Guy1fbc1c82009-11-09 20:43:08 -080065import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.view.View.OnLongClickListener;
67import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.widget.TextView;
70import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080071import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080072import android.widget.PopupWindow;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070073import android.appwidget.AppWidgetManager;
74import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040077import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070078import java.io.DataOutputStream;
79import java.io.FileNotFoundException;
80import java.io.IOException;
81import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082
83/**
84 * Default launcher application.
85 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040086public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080087 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 static final boolean LOGD = false;
91
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 static final boolean PROFILE_STARTUP = false;
93 static final boolean PROFILE_ROTATE = false;
94 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070095
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 private static final int WALLPAPER_SCREENS_SPAN = 2;
97
98 private static final int MENU_GROUP_ADD = 1;
99 private static final int MENU_ADD = Menu.FIRST + 1;
100 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
101 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
102 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700103 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104
105 private static final int REQUEST_CREATE_SHORTCUT = 1;
106 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700107 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 private static final int REQUEST_PICK_APPLICATION = 6;
109 private static final int REQUEST_PICK_SHORTCUT = 7;
110 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700111 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700112 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
114 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
115
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700116 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
117 static final String SEARCH_WIDGET = "search_widget";
118
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800119 static final int SCREEN_COUNT = 5;
120 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700122 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato7c312c12009-08-13 21:36:53 -0700124 static final int DIALOG_CREATE_SHORTCUT = 1;
125 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Romain Guy98d01652009-06-30 16:21:04 -0700127 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 // Type: int
130 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
131 // Type: boolean
132 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
133 // Type: long
134 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
149 // Type: int[]
150 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
151 // Type: boolean
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
153 // Type: long
154 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
155
Joe Onorato9c1289c2009-08-17 11:03:03 -0400156 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800159 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800161 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private LayoutInflater mInflater;
164
Joe Onorato00acb122009-08-04 16:04:30 -0400165 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700167
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700168 private AppWidgetManager mAppWidgetManager;
169 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private CellLayout.CellInfo mAddItemCellInfo;
172 private CellLayout.CellInfo mMenuAddInfo;
173 private final int[] mCellCoordinates = new int[2];
174 private FolderInfo mFolderInfo;
175
Joe Onorato7c312c12009-08-13 21:36:53 -0700176 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700178 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private Bundle mSavedState;
181
182 private SpannableStringBuilder mDefaultKeySsb = null;
183
Mike LeBeau736cf282009-07-02 17:46:59 -0700184 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 private boolean mWorkspaceLoading = true;
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private boolean mRestoring;
189 private boolean mWaitingForResult;
Joe Onoratoff3862d2009-10-07 10:35:51 -0700190 private boolean mExitingBecauseOfLaunch;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
192 private Bundle mSavedInstanceState;
193
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 private LauncherModel mModel;
195
Romain Guy84f296c2009-11-04 15:00:44 -0800196 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
197 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700198
Joe Onorato080d9b62009-11-02 12:01:11 -0500199 public static long lastStartTime;
Romain Guy1fbc1c82009-11-09 20:43:08 -0800200 private ImageView mPreviousView;
201 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 @Override
204 protected void onCreate(Bundle savedInstanceState) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500205 lastStartTime = SystemClock.uptimeMillis();
206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400210 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700212
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
215 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 if (PROFILE_STARTUP) {
218 android.os.Debug.startMethodTracing("/sdcard/launcher");
219 }
220
221 checkForLocaleChange();
222 setWallpaperDimension();
223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 setContentView(R.layout.launcher);
225 setupViews();
226
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800227 registerContentObservers();
228
Joe Onorato7c312c12009-08-13 21:36:53 -0700229 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 mSavedState = savedInstanceState;
232 restoreState(mSavedState);
233
234 if (PROFILE_STARTUP) {
235 android.os.Debug.stopMethodTracing();
236 }
237
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700238 // We have a new AllAppsView, we need to re-bind everything, and it could have
239 // changed in our absence.
240 mModel.setAllAppsDirty();
241 mModel.setWorkspaceDirty();
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400244 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 }
246
247 // For handling default keys
248 mDefaultKeySsb = new SpannableStringBuilder();
249 Selection.setSelection(mDefaultKeySsb, 0);
250 }
Romain Guycbb89e42009-06-08 15:52:54 -0700251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700253 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
254 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700255
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 final Configuration configuration = getResources().getConfiguration();
257
Romain Guy98d01652009-06-30 16:21:04 -0700258 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 final String locale = configuration.locale.toString();
260
Romain Guy98d01652009-06-30 16:21:04 -0700261 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 final int mcc = configuration.mcc;
263
Romain Guy98d01652009-06-30 16:21:04 -0700264 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 final int mnc = configuration.mnc;
266
Romain Guy84f296c2009-11-04 15:00:44 -0800267 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
Romain Guy84f296c2009-11-04 15:00:44 -0800269 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700270 localeConfiguration.locale = locale;
271 localeConfiguration.mcc = mcc;
272 localeConfiguration.mnc = mnc;
273
274 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700276 }
277 }
278
279 private static class LocaleConfiguration {
280 public String locale;
281 public int mcc = -1;
282 public int mnc = -1;
283 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700284
Romain Guy98d01652009-06-30 16:21:04 -0700285 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
286 DataInputStream in = null;
287 try {
288 in = new DataInputStream(context.openFileInput(PREFERENCES));
289 configuration.locale = in.readUTF();
290 configuration.mcc = in.readInt();
291 configuration.mnc = in.readInt();
292 } catch (FileNotFoundException e) {
293 // Ignore
294 } catch (IOException e) {
295 // Ignore
296 } finally {
297 if (in != null) {
298 try {
299 in.close();
300 } catch (IOException e) {
301 // Ignore
302 }
303 }
304 }
305 }
306
307 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
308 DataOutputStream out = null;
309 try {
310 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
311 out.writeUTF(configuration.locale);
312 out.writeInt(configuration.mcc);
313 out.writeInt(configuration.mnc);
314 out.flush();
315 } catch (FileNotFoundException e) {
316 // Ignore
317 } catch (IOException e) {
318 //noinspection ResultOfMethodCallIgnored
319 context.getFileStreamPath(PREFERENCES).delete();
320 } finally {
321 if (out != null) {
322 try {
323 out.close();
324 } catch (IOException e) {
325 // Ignore
326 }
327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 }
329 }
330
331 static int getScreen() {
332 synchronized (sLock) {
333 return sScreen;
334 }
335 }
336
337 static void setScreen(int screen) {
338 synchronized (sLock) {
339 sScreen = screen;
340 }
341 }
342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700344 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345
346 Display display = getWindowManager().getDefaultDisplay();
347 boolean isPortrait = display.getWidth() < display.getHeight();
348
349 final int width = isPortrait ? display.getWidth() : display.getHeight();
350 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700351 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 }
353
354 @Override
355 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700356 mWaitingForResult = false;
357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 // The pattern used here is that a user PICKs a specific application,
359 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700360
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
362 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700363
Romain Guy94dabf12009-07-21 10:55:43 -0700364 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 switch (requestCode) {
366 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 break;
369 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700370 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 break;
372 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 break;
375 case REQUEST_PICK_LIVE_FOLDER:
376 addLiveFolder(data);
377 break;
378 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700381 case REQUEST_PICK_APPWIDGET:
382 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700384 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400385 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700387 case REQUEST_PICK_WALLPAPER:
388 // We just wanted the activity result here so we can clear mWaitingForResult
389 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
Romain Guy18042c82009-11-06 11:44:55 -0800391 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
392 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
393 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700394 // Clean up the appWidgetId if we canceled
395 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
396 if (appWidgetId != -1) {
397 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 }
401
402 @Override
403 protected void onResume() {
Joe Onorato080d9b62009-11-02 12:01:11 -0500404 if (lastStartTime == 0) {
405 lastStartTime = SystemClock.uptimeMillis();
406 }
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 super.onResume();
409
410 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400411 mWorkspaceLoading = true;
412 mModel.startLoader(this, true);
413 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700415
Mike LeBeau736cf282009-07-02 17:46:59 -0700416 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
417 // onNewIntent), then close the search dialog if needed, because it probably
418 // came from the user pressing 'home' (rather than, for example, pressing 'back').
419 if (mIsNewIntent) {
420 // Post to a handler so that this happens after the search dialog tries to open
421 // itself again.
422 mWorkspace.post(new Runnable() {
423 public void run() {
424 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
425 ServiceManager.getService(Context.SEARCH_SERVICE));
426 try {
427 searchManagerService.stopSearch();
428 } catch (RemoteException e) {
429 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700430 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700431 }
432 });
433 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700434
Mike LeBeau736cf282009-07-02 17:46:59 -0700435 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 }
437
438 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700439 protected void onPause() {
440 super.onPause();
Joe Onoratoff3862d2009-10-07 10:35:51 -0700441 if (mExitingBecauseOfLaunch) {
442 mExitingBecauseOfLaunch = false;
443 closeAllApps(false);
444 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800445 dismissPreview(mPreviousView);
446 dismissPreview(mNextView);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700447 }
Romain Guycbb89e42009-06-08 15:52:54 -0700448
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700449 @Override
450 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400451 // Flag the loader to stop early before switching
452 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700453
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700454 if (PROFILE_ROTATE) {
455 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
456 }
457 return null;
458 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 private boolean acceptFilter() {
461 final InputMethodManager inputManager = (InputMethodManager)
462 getSystemService(Context.INPUT_METHOD_SERVICE);
463 return !inputManager.isFullscreenMode();
464 }
465
466 @Override
467 public boolean onKeyDown(int keyCode, KeyEvent event) {
468 boolean handled = super.onKeyDown(keyCode, event);
469 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
470 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
471 keyCode, event);
472 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700473 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700474 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700475 // showSearchDialog()
476 // If there are multiple keystrokes before the search dialog takes focus,
477 // onSearchRequested() will be called for every keystroke,
478 // but it is idempotent, so it's fine.
479 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 }
481 }
482
483 return handled;
484 }
485
Karl Rosaen138a0412009-04-23 19:00:21 -0700486 private String getTypedText() {
487 return mDefaultKeySsb.toString();
488 }
489
490 private void clearTypedText() {
491 mDefaultKeySsb.clear();
492 mDefaultKeySsb.clearSpans();
493 Selection.setSelection(mDefaultKeySsb, 0);
494 }
495
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 /**
497 * Restores the previous state, if it exists.
498 *
499 * @param savedState The previous state.
500 */
501 private void restoreState(Bundle savedState) {
502 if (savedState == null) {
503 return;
504 }
505
506 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
507 if (currentScreen > -1) {
508 mWorkspace.setCurrentScreen(currentScreen);
509 }
510
511 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
512 if (addScreen > -1) {
513 mAddItemCellInfo = new CellLayout.CellInfo();
514 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
515 addItemCellInfo.valid = true;
516 addItemCellInfo.screen = addScreen;
517 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
518 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
519 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
520 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
521 addItemCellInfo.findVacantCellsFromOccupied(
522 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
523 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
524 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
525 mRestoring = true;
526 }
527
528 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
529 if (renameFolder) {
530 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400531 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532 mRestoring = true;
533 }
534 }
535
536 /**
537 * Finds all the views we need and configure them properly.
538 */
539 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400540 DragController dragController = mDragController;
541
Romain Guyb1b69f52009-08-10 15:10:15 -0700542 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400543 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544
Joe Onorato7c312c12009-08-13 21:36:53 -0700545 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700546 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700547 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700548 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700549 // Manage focusability manually since this thing is always visible
550 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700551
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
553 final Workspace workspace = mWorkspace;
554
Joe Onorato7c312c12009-08-13 21:36:53 -0700555 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
556 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557
Joe Onorato7c312c12009-08-13 21:36:53 -0700558 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700559 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700560 mHandleView.setOnClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800561
562 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
563 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
564
565 Drawable previous = mPreviousView.getDrawable();
566 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800567 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568
Romain Guy1fbc1c82009-11-09 20:43:08 -0800569 mPreviousView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800570 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800571
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400573 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575
576 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400577 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700578 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579
Joe Onorato00acb122009-08-04 16:04:30 -0400580 dragController.setDragScoller(workspace);
581 dragController.setDragListener(deleteZone);
582 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700583
Joe Onorato00acb122009-08-04 16:04:30 -0400584 // The order here is bottom to top.
585 dragController.addDropTarget(workspace);
586 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 }
588
Romain Guy8a73c512009-11-09 19:19:59 -0800589 @SuppressWarnings({"UnusedDeclaration"})
590 public void previousScreen(View v) {
591 mWorkspace.scrollLeft();
592 }
593
594 @SuppressWarnings({"UnusedDeclaration"})
595 public void nextScreen(View v) {
596 mWorkspace.scrollRight();
597 }
598
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 /**
600 * Creates a view representing a shortcut.
601 *
602 * @param info The data structure describing the shortcut.
603 *
604 * @return A View inflated from R.layout.application.
605 */
606 View createShortcut(ApplicationInfo info) {
607 return createShortcut(R.layout.application,
608 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
609 }
610
611 /**
612 * Creates a view representing a shortcut inflated from the specified resource.
613 *
614 * @param layoutResId The id of the XML layout used to create the shortcut.
615 * @param parent The group the shortcut belongs to.
616 * @param info The data structure describing the shortcut.
617 *
618 * @return A View inflated from layoutResId.
619 */
620 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
621 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
622
Joe Onorato5162ea92009-09-03 09:39:42 -0700623 if (info.icon == null) {
624 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
625 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700627 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 info.filtered = true;
629 }
630
631 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
632 favorite.setText(info.title);
633 favorite.setTag(info);
634 favorite.setOnClickListener(this);
635
636 return favorite;
637 }
638
639 /**
640 * Add an application shortcut to the workspace.
641 *
642 * @param data The intent describing the application.
643 * @param cellInfo The position on screen where to create the shortcut.
644 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400645 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 cellInfo.screen = mWorkspace.getCurrentScreen();
647 if (!findSingleSlot(cellInfo)) return;
648
Romain Guy73b979d2009-06-09 12:57:21 -0700649 final ApplicationInfo info = infoFromApplicationIntent(context, data);
650 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700652 }
653 }
654
655 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 ComponentName component = data.getComponent();
657 PackageManager packageManager = context.getPackageManager();
658 ActivityInfo activityInfo = null;
659 try {
660 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
661 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700662 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
Romain Guycbb89e42009-06-08 15:52:54 -0700664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 if (activityInfo != null) {
666 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 itemInfo.title = activityInfo.loadLabel(packageManager);
669 if (itemInfo.title == null) {
670 itemInfo.title = activityInfo.name;
671 }
Romain Guycbb89e42009-06-08 15:52:54 -0700672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
674 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
675 itemInfo.icon = activityInfo.loadIcon(packageManager);
676 itemInfo.container = ItemInfo.NO_ID;
677
Romain Guy73b979d2009-06-09 12:57:21 -0700678 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
Romain Guy73b979d2009-06-09 12:57:21 -0700680
681 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
Romain Guycbb89e42009-06-08 15:52:54 -0700683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 /**
685 * Add a shortcut to the workspace.
686 *
687 * @param data The intent describing the shortcut.
688 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 cellInfo.screen = mWorkspace.getCurrentScreen();
692 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
695
696 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
699 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 }
701 }
702
Romain Guycbb89e42009-06-08 15:52:54 -0700703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700705 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 * @param data The intent describing the appWidgetId.
708 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700713
Romain Guy18042c82009-11-06 11:44:55 -0800714 if (LOGD) d(LOG_TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700715
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700717
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700720 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 // Try finding open space on Launcher screen
723 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800724 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
725 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
726 return;
727 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700729 // Build Launcher-specific widget info and save to database
730 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 launcherInfo.spanX = spans[0];
732 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 LauncherModel.addItemToDatabase(this, launcherInfo,
735 LauncherSettings.Favorites.CONTAINER_DESKTOP,
736 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
737
738 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700740
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700742 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700743
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700744 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700746
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400748 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 }
750 }
Romain Guycbb89e42009-06-08 15:52:54 -0700751
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
753 mDesktopItems.remove(launcherInfo);
754 launcherInfo.hostView = null;
755 }
756
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700757 public LauncherAppWidgetHost getAppWidgetHost() {
758 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
Romain Guycbb89e42009-06-08 15:52:54 -0700760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 static ApplicationInfo addShortcut(Context context, Intent data,
762 CellLayout.CellInfo cellInfo, boolean notify) {
763
Romain Guy73b979d2009-06-09 12:57:21 -0700764 final ApplicationInfo info = infoFromShortcutIntent(context, data);
765 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
766 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
767
768 return info;
769 }
770
771 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
773 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
774 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
775
776 Drawable icon = null;
777 boolean filtered = false;
778 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700779 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780
781 if (bitmap != null) {
782 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
783 filtered = true;
784 customIcon = true;
785 } else {
786 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700787 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700789 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 final PackageManager packageManager = context.getPackageManager();
791 Resources resources = packageManager.getResourcesForApplication(
792 iconResource.packageName);
793 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
794 icon = resources.getDrawable(id);
795 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700796 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 }
798 }
799 }
800
801 if (icon == null) {
802 icon = context.getPackageManager().getDefaultActivityIcon();
803 }
804
805 final ApplicationInfo info = new ApplicationInfo();
806 info.icon = icon;
807 info.filtered = filtered;
808 info.title = name;
809 info.intent = intent;
810 info.customIcon = customIcon;
811 info.iconResource = iconResource;
812
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 return info;
814 }
815
816 @Override
817 protected void onNewIntent(Intent intent) {
818 super.onNewIntent(intent);
819
820 // Close the menu
821 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
822 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700823
Joe Onorato742966b2009-10-07 10:26:17 -0700824 // Whatever we were doing is hereby canceled.
825 mWaitingForResult = false;
826
Mike LeBeau736cf282009-07-02 17:46:59 -0700827 // Set this flag so that onResume knows to close the search dialog if it's open,
828 // because this was a new intent (thus a press of 'home' or some such) rather than
829 // for example onResume being called when the user pressed the 'back' button.
830 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831
832 try {
833 dismissDialog(DIALOG_CREATE_SHORTCUT);
834 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 } catch (Exception e) {
836 // An exception is thrown if the dialog is not visible, which is fine
837 }
838
839 try {
840 dismissDialog(DIALOG_RENAME_FOLDER);
841 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 } catch (Exception e) {
843 // An exception is thrown if the dialog is not visible, which is fine
844 }
845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
847 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700848
Romain Guy94dabf12009-07-21 10:55:43 -0700849 if (!mWorkspace.isDefaultScreenShowing()) {
850 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700852
Joe Onorato7bb17492009-09-24 17:51:01 -0700853 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700854
855 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 if (v != null && v.getWindowToken() != null) {
857 InputMethodManager imm = (InputMethodManager)getSystemService(
858 INPUT_METHOD_SERVICE);
859 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
860 }
861 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700862 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 }
864 }
865 }
866
867 @Override
868 protected void onRestoreInstanceState(Bundle savedInstanceState) {
869 // Do not call super here
870 mSavedInstanceState = savedInstanceState;
871 }
872
873 @Override
874 protected void onSaveInstanceState(Bundle outState) {
875 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
876
877 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
878 if (folders.size() > 0) {
879 final int count = folders.size();
880 long[] ids = new long[count];
881 for (int i = 0; i < count; i++) {
882 final FolderInfo info = folders.get(i).getInfo();
883 ids[i] = info.id;
884 }
885 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
886 } else {
887 super.onSaveInstanceState(outState);
888 }
889
Romain Guy3cf604f2009-06-16 13:12:53 -0700890 final boolean isConfigurationChange = getChangingConfigurations() != 0;
891
Romain Guy5a941392009-04-28 15:18:25 -0700892 // When the drawer is opened and we are saving the state because of a
893 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400894 // TODO should not do this if the drawer is currently closing.
895 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898
899 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
900 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
901 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
902
903 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
904 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
905 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
906 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
907 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
908 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
909 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
910 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
911 layout.getOccupiedCells());
912 }
913
914 if (mFolderInfo != null && mWaitingForResult) {
915 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
916 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
917 }
918 }
919
920 @Override
921 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700923
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700925 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700927 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 }
929
930 TextKeyListener.getInstance().release();
931
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934 unbindDesktopItems();
935 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800936
937 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800938
939 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800940 dismissPreview(mNextView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 }
942
943 @Override
944 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700945 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 super.startActivityForResult(intent, requestCode);
947 }
948
949 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700950 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700952
Joe Onorato7bb17492009-09-24 17:51:01 -0700953 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700954
Karl Rosaen138a0412009-04-23 19:00:21 -0700955 // Slide the search widget to the top, if it's on the current screen,
956 // otherwise show the search dialog immediately.
957 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
958 if (searchWidget == null) {
959 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
960 } else {
961 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
962 // show the currently typed text in the search widget while sliding
963 searchWidget.setQuery(getTypedText());
964 }
965 }
Romain Guycbb89e42009-06-08 15:52:54 -0700966
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 /**
968 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700969 *
970 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 */
Romain Guycbb89e42009-06-08 15:52:54 -0700972 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700973 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700974
Karl Rosaen138a0412009-04-23 19:00:21 -0700975 if (initialQuery == null) {
976 // Use any text typed in the launcher as the initial query
977 initialQuery = getTypedText();
978 clearTypedText();
979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 if (appSearchData == null) {
981 appSearchData = new Bundle();
982 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
983 }
Romain Guycbb89e42009-06-08 15:52:54 -0700984
Karl Rosaen138a0412009-04-23 19:00:21 -0700985 final SearchManager searchManager =
986 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
987
988 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
989 if (searchWidget != null) {
990 // This gets called when the user leaves the search dialog to go back to
991 // the Launcher.
992 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
993 public void onCancel() {
994 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700995 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700996 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700997 });
998 }
Romain Guycbb89e42009-06-08 15:52:54 -0700999
Karl Rosaen138a0412009-04-23 19:00:21 -07001000 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001001 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 }
1003
Karl Rosaen138a0412009-04-23 19:00:21 -07001004 /**
1005 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001006 */
Romain Guy5a941392009-04-28 15:18:25 -07001007 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001008 // Close search dialog
1009 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001010 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001011 // Restore search widget to its normal position
1012 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1013 if (searchWidget != null) {
1014 searchWidget.stopSearch(false);
1015 }
1016 }
Romain Guycbb89e42009-06-08 15:52:54 -07001017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 @Override
1019 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 if (isWorkspaceLocked()) {
1021 return false;
1022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023
1024 super.onCreateOptionsMenu(menu);
1025 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1026 .setIcon(android.R.drawable.ic_menu_add)
1027 .setAlphabeticShortcut('A');
1028 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1029 .setIcon(android.R.drawable.ic_menu_gallery)
1030 .setAlphabeticShortcut('W');
1031 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1032 .setIcon(android.R.drawable.ic_search_category_default)
1033 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1034 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1035 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1036 .setAlphabeticShortcut('N');
1037
1038 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001039 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1040 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041
1042 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1043 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1044 .setIntent(settings);
1045
1046 return true;
1047 }
1048
1049 @Override
1050 public boolean onPrepareOptionsMenu(Menu menu) {
1051 super.onPrepareOptionsMenu(menu);
1052
1053 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1054 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1055
1056 return true;
1057 }
1058
1059 @Override
1060 public boolean onOptionsItemSelected(MenuItem item) {
1061 switch (item.getItemId()) {
1062 case MENU_ADD:
1063 addItems();
1064 return true;
1065 case MENU_WALLPAPER_SETTINGS:
1066 startWallpaper();
1067 return true;
1068 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001069 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 return true;
1071 case MENU_NOTIFICATIONS:
1072 showNotifications();
1073 return true;
1074 }
1075
1076 return super.onOptionsItemSelected(item);
1077 }
Romain Guycbb89e42009-06-08 15:52:54 -07001078
Karl Rosaen138a0412009-04-23 19:00:21 -07001079 /**
1080 * Indicates that we want global search for this activity by setting the globalSearch
1081 * argument for {@link #startSearch} to true.
1082 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001084 @Override
1085 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001086 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001087 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001088 }
1089
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 public boolean isWorkspaceLocked() {
1091 return mWorkspaceLoading || mWaitingForResult;
1092 }
1093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001095 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 showAddDialog(mMenuAddInfo);
1097 }
1098
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001099 void addAppWidget(Intent data) {
1100 // TODO: catch bad widget exception when sent
1101 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001103 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1104 if (SEARCH_WIDGET.equals(customWidget)) {
1105 // We don't need this any more, since this isn't a real app widget.
1106 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1107 // add the search widget
1108 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001110 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1111
1112 if (appWidget.configure != null) {
1113 // Launch over to configure widget, if needed
1114 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1115 intent.setComponent(appWidget.configure);
1116 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1117
1118 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1119 } else {
1120 // Otherwise just add it
1121 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 }
1124 }
Romain Guycbb89e42009-06-08 15:52:54 -07001125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 void addSearch() {
1127 final Widget info = Widget.makeSearch();
1128 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 final int[] xy = mCellCoordinates;
1131 final int spanX = info.spanX;
1132 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1137 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 final View view = mInflater.inflate(info.layoutResource, null);
1140 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001141 Search search = (Search) view.findViewById(R.id.widget_search);
1142 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001143
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1145 }
1146
Romain Guy73b979d2009-06-09 12:57:21 -07001147 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001148 // Handle case where user selected "Applications"
1149 String applicationName = getResources().getString(R.string.group_applications);
1150 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001152 if (applicationName != null && applicationName.equals(shortcutName)) {
1153 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1154 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001155
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001156 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1157 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001158 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001159 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001160 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 }
1163
1164 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001165 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001166 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001167 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001168
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001169 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001170 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001171 } else {
1172 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
1175
Joe Onorato9c1289c2009-08-17 11:03:03 -04001176 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 UserFolderInfo folderInfo = new UserFolderInfo();
1178 folderInfo.title = getText(R.string.folder_name);
1179
1180 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1181 cellInfo.screen = mWorkspace.getCurrentScreen();
1182 if (!findSingleSlot(cellInfo)) return;
1183
1184 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 LauncherModel.addItemToDatabase(this, folderInfo,
1186 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189
1190 // Create the view
1191 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1192 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1193 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 }
Romain Guycbb89e42009-06-08 15:52:54 -07001196
Joe Onorato9c1289c2009-08-17 11:03:03 -04001197 void removeFolder(FolderInfo folder) {
1198 mFolders.remove(folder.id);
1199 }
1200
1201 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 cellInfo.screen = mWorkspace.getCurrentScreen();
1203 if (!findSingleSlot(cellInfo)) return;
1204
1205 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1206
1207 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1209 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1211 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213 }
1214
1215 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1216 CellLayout.CellInfo cellInfo, boolean notify) {
1217
1218 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1219 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1220
1221 Drawable icon = null;
1222 boolean filtered = false;
1223 Intent.ShortcutIconResource iconResource = null;
1224
1225 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1226 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1227 try {
1228 iconResource = (Intent.ShortcutIconResource) extra;
1229 final PackageManager packageManager = context.getPackageManager();
1230 Resources resources = packageManager.getResourcesForApplication(
1231 iconResource.packageName);
1232 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1233 icon = resources.getDrawable(id);
1234 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001235 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 }
1237 }
1238
1239 if (icon == null) {
1240 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1241 }
1242
1243 final LiveFolderInfo info = new LiveFolderInfo();
1244 info.icon = icon;
1245 info.filtered = filtered;
1246 info.title = name;
1247 info.iconResource = iconResource;
1248 info.uri = data.getData();
1249 info.baseIntent = baseIntent;
1250 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1251 LiveFolders.DISPLAY_MODE_GRID);
1252
1253 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1254 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001255 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256
1257 return info;
1258 }
1259
1260 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1261 final int[] xy = new int[2];
1262 if (findSlot(cellInfo, xy, 1, 1)) {
1263 cellInfo.cellX = xy[0];
1264 cellInfo.cellY = xy[1];
1265 return true;
1266 }
1267 return false;
1268 }
1269
1270 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1271 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1272 boolean[] occupied = mSavedState != null ?
1273 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1274 cellInfo = mWorkspace.findAllVacantCells(occupied);
1275 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1276 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1277 return false;
1278 }
1279 }
1280 return true;
1281 }
1282
1283 private void showNotifications() {
1284 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1285 if (statusBar != null) {
1286 statusBar.expand();
1287 }
1288 }
1289
1290 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001291 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001293 Intent chooser = Intent.createChooser(pickWallpaper,
1294 getText(R.string.chooser_wallpaper));
1295 WallpaperManager wm = (WallpaperManager)
1296 getSystemService(Context.WALLPAPER_SERVICE);
1297 WallpaperInfo wi = wm.getWallpaperInfo();
1298 if (wi != null && wi.getSettingsActivity() != null) {
1299 LabeledIntent li = new LabeledIntent(getPackageName(),
1300 R.string.configure_wallpaper, 0);
1301 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1302 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1303 }
Mike Clerona0618e42009-10-22 13:55:21 -07001304 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
1306
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001307 private void registerContentObservers() {
1308 ContentResolver resolver = getContentResolver();
1309 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1310 true, mWidgetObserver);
1311 }
1312
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 @Override
1314 public boolean dispatchKeyEvent(KeyEvent event) {
1315 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1316 switch (event.getKeyCode()) {
1317 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 return true;
1319 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001320 return true;
1321 }
1322 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1323 switch (event.getKeyCode()) {
1324 case KeyEvent.KEYCODE_BACK:
1325 if (!event.isCanceled()) {
1326 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001327 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001328 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001329 } else {
1330 closeFolder();
1331 }
Romain Guya6abce82009-11-10 02:54:41 -08001332 dismissPreview(mPreviousView);
1333 dismissPreview(mNextView);
Dianne Hackborn67800862009-07-24 17:15:20 -07001334 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001335 return true;
1336 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 return true;
1338 }
1339 }
1340
1341 return super.dispatchKeyEvent(event);
1342 }
1343
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 private void closeFolder() {
1345 Folder folder = mWorkspace.getOpenFolder();
1346 if (folder != null) {
1347 closeFolder(folder);
1348 }
1349 }
1350
1351 void closeFolder(Folder folder) {
1352 folder.getInfo().opened = false;
1353 ViewGroup parent = (ViewGroup) folder.getParent();
1354 if (parent != null) {
1355 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001356 if (folder instanceof DropTarget) {
1357 // Live folders aren't DropTargets.
1358 mDragController.removeDropTarget((DropTarget)folder);
1359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361 folder.onClose();
1362 }
1363
1364 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001365 * Re-listen when widgets are reset.
1366 */
1367 private void onAppWidgetReset() {
1368 mAppWidgetHost.startListening();
1369 }
1370
1371 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1373 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 private void unbindDesktopItems() {
1376 for (ItemInfo item: mDesktopItems) {
1377 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
1379 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 /**
1382 * Launches the intent referred by the clicked shortcut.
1383 *
1384 * @param v The view representing the clicked shortcut.
1385 */
1386 public void onClick(View v) {
1387 Object tag = v.getTag();
1388 if (tag instanceof ApplicationInfo) {
1389 // Open shortcut
1390 final Intent intent = ((ApplicationInfo) tag).intent;
1391 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001392 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 } else if (tag instanceof FolderInfo) {
1394 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001395 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001396 Log.d(TAG, "onClick");
1397 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001398 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001399 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001400 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
1403 }
1404
1405 void startActivitySafely(Intent intent) {
1406 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1407 try {
1408 startActivity(intent);
1409 } catch (ActivityNotFoundException e) {
1410 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1411 } catch (SecurityException e) {
1412 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001413 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1415 "or use the exported attribute for this activity.", e);
1416 }
1417 }
1418
1419 private void handleFolderClick(FolderInfo folderInfo) {
1420 if (!folderInfo.opened) {
1421 // Close any open folder
1422 closeFolder();
1423 // Open the requested folder
1424 openFolder(folderInfo);
1425 } else {
1426 // Find the open folder...
1427 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1428 int folderScreen;
1429 if (openFolder != null) {
1430 folderScreen = mWorkspace.getScreenForView(openFolder);
1431 // .. and close it
1432 closeFolder(openFolder);
1433 if (folderScreen != mWorkspace.getCurrentScreen()) {
1434 // Close any folder open on the current screen
1435 closeFolder();
1436 // Pull the folder onto this screen
1437 openFolder(folderInfo);
1438 }
1439 }
1440 }
1441 }
1442
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 /**
1444 * Opens the user fodler described by the specified tag. The opening of the folder
1445 * is animated relative to the specified View. If the View is null, no animation
1446 * is played.
1447 *
1448 * @param folderInfo The FolderInfo describing the folder to open.
1449 */
1450 private void openFolder(FolderInfo folderInfo) {
1451 Folder openFolder;
1452
1453 if (folderInfo instanceof UserFolderInfo) {
1454 openFolder = UserFolder.fromXml(this);
1455 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001456 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 } else {
1458 return;
1459 }
1460
Joe Onorato00acb122009-08-04 16:04:30 -04001461 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 openFolder.setLauncher(this);
1463
1464 openFolder.bind(folderInfo);
1465 folderInfo.opened = true;
1466
1467 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1468 openFolder.onOpen();
1469 }
1470
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001472 switch (v.getId()) {
1473 case R.id.previous_screen:
1474 showPreviousPreview(v);
1475 return true;
1476 case R.id.next_screen:
1477 showNextPreview(v);
1478 return true;
1479 }
1480
Joe Onorato9c1289c2009-08-17 11:03:03 -04001481 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 return false;
1483 }
1484
1485 if (!(v instanceof CellLayout)) {
1486 v = (View) v.getParent();
1487 }
1488
1489 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1490
1491 // This happens when long clicking an item with the dpad/trackball
1492 if (cellInfo == null) {
1493 return true;
1494 }
1495
1496 if (mWorkspace.allowLongPress()) {
1497 if (cellInfo.cell == null) {
1498 if (cellInfo.valid) {
1499 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001500 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 showAddDialog(cellInfo);
1502 }
1503 } else {
1504 if (!(cellInfo.cell instanceof Folder)) {
1505 // User long pressed on an item
1506 mWorkspace.startDrag(cellInfo);
1507 }
1508 }
1509 }
1510 return true;
1511 }
1512
Romain Guy1fbc1c82009-11-09 20:43:08 -08001513 private void dismissPreview(View v) {
1514 PopupWindow window = (PopupWindow) v.getTag();
1515 if (window != null) {
Romain Guya6abce82009-11-10 02:54:41 -08001516 window.setOnDismissListener(null);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001517 window.dismiss();
1518 ((ImageView) v.getTag(R.id.workspace)).setImageBitmap(null);
1519 ((Bitmap) v.getTag(R.id.icon)).recycle();
1520
1521 v.setTag(R.id.workspace, null);
1522 v.setTag(R.id.icon, null);
1523 }
1524 v.setTag(null);
1525 }
1526
1527 private void showPreviousPreview(View anchor) {
1528 int current = mWorkspace.getCurrentScreen();
1529 if (current <= 0) return;
1530
Romain Guya6abce82009-11-10 02:54:41 -08001531 showPreviews(anchor, 0, current);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001532 }
1533
1534 private void showNextPreview(View anchor) {
1535 int current = mWorkspace.getCurrentScreen();
1536 if (current >= mWorkspace.getChildCount() - 1) return;
1537
Romain Guya6abce82009-11-10 02:54:41 -08001538 showPreviews(anchor, current + 1, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001539 }
1540
Romain Guya6abce82009-11-10 02:54:41 -08001541 @Override
1542 public void onWindowFocusChanged(boolean hasFocus) {
1543 super.onWindowFocusChanged(hasFocus);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001544
Romain Guya6abce82009-11-10 02:54:41 -08001545 if (!hasFocus) {
1546 dismissPreview(mPreviousView);
1547 dismissPreview(mNextView);
1548 }
1549 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001550
Romain Guya6abce82009-11-10 02:54:41 -08001551 private void showPreviews(final View anchor, int start, int end) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001552 ImageView preview = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001553 preview.requestFocus();
1554
Romain Guy1fbc1c82009-11-09 20:43:08 -08001555 Drawable d = getResources().getDrawable(R.drawable.preview_popup);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001556
1557 Rect r = new Rect();
1558 d.getPadding(r);
1559 int extraW = r.left + r.right;
1560 int extraH = r.top + r.bottom;
1561
Romain Guya6abce82009-11-10 02:54:41 -08001562 Workspace workspace = mWorkspace;
1563 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
1564
1565 float max = workspace.getChildCount() - 1;
1566
1567 int aW = cell.getWidth() - extraW;
1568 float w = aW / max;
1569
1570 int width = cell.getWidth();
1571 int height = cell.getHeight();
1572 int x = cell.getLeftPadding();
1573 int y = cell.getTopPadding();
1574 width -= (x + cell.getRightPadding());
1575 height -= (y + cell.getBottomPadding());
1576
1577 float scale = w / width;
1578
1579 int count = end - start;
1580
1581 final float sWidth = width * scale;
1582 float sHeight = height * scale;
1583
1584 Bitmap bitmap = Bitmap.createBitmap((int) (sWidth * count),
1585 (int) sHeight, Bitmap.Config.ARGB_8888);
1586
1587 PopupWindow p = new PopupWindow(this);
1588 p.setContentView(preview);
1589 p.setWidth(bitmap.getWidth() + extraW);
1590 p.setHeight(bitmap.getHeight() + extraH);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001591 p.setAnimationStyle(R.style.AnimationPreview);
Romain Guya6abce82009-11-10 02:54:41 -08001592 p.setOutsideTouchable(true);
1593 p.setBackgroundDrawable(d);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001594 p.showAsDropDown(anchor, 0, 0);
1595
Romain Guya6abce82009-11-10 02:54:41 -08001596 Canvas c = new Canvas(bitmap);
1597
1598 for (int i = start; i < end; i++) {
1599 cell = (CellLayout) workspace.getChildAt(i);
1600
1601 c.save();
1602 c.scale(scale, scale);
1603 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1604 cell.dispatchDraw(c);
1605 c.restore();
1606
1607 c.translate(sWidth, 0.0f);
1608 }
1609
1610 preview.setImageBitmap(bitmap);
1611
1612 PreviewTouchHandler handler = new PreviewTouchHandler(anchor, sWidth);
1613 preview.setOnClickListener(handler);
1614 preview.setOnTouchListener(handler);
1615 p.setOnDismissListener(handler);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001616
1617 anchor.setTag(p);
1618 anchor.setTag(R.id.workspace, preview);
1619 anchor.setTag(R.id.icon, bitmap);
1620 }
1621
Romain Guya6abce82009-11-10 02:54:41 -08001622 class PreviewTouchHandler implements View.OnTouchListener, View.OnClickListener,
1623 PopupWindow.OnDismissListener {
1624
1625 private final View mAnchor;
1626 private final float mWidth;
1627 private float mTouchX;
1628
1629 public PreviewTouchHandler(View anchor, float width) {
1630 mAnchor = anchor;
1631 mWidth = width;
1632 }
1633
1634 public boolean onTouch(View v, MotionEvent event) {
1635 if (event.getAction() == MotionEvent.ACTION_UP) {
1636 mTouchX = event.getX();
1637 }
1638 return false;
1639 }
1640
1641 public void onClick(View v) {
1642 int screen = 0;
1643 if (mAnchor == mNextView) {
1644 screen = mWorkspace.getCurrentScreen() + (int) (mTouchX / mWidth) + 1;
1645 } else if (mAnchor == mPreviousView) {
1646 screen = (int) (mTouchX / mWidth);
1647 }
1648 mWorkspace.snapToScreen(screen);
1649 dismissPreview(mAnchor);
1650 }
1651
1652 public void onDismiss() {
1653 dismissPreview(mAnchor);
1654 }
1655 }
1656
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001657 View getDrawerHandle() {
1658 return mHandleView;
1659 }
1660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 Workspace getWorkspace() {
1662 return mWorkspace;
1663 }
1664
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 @Override
1666 protected Dialog onCreateDialog(int id) {
1667 switch (id) {
1668 case DIALOG_CREATE_SHORTCUT:
1669 return new CreateShortcut().createDialog();
1670 case DIALOG_RENAME_FOLDER:
1671 return new RenameFolder().createDialog();
1672 }
1673
1674 return super.onCreateDialog(id);
1675 }
1676
1677 @Override
1678 protected void onPrepareDialog(int id, Dialog dialog) {
1679 switch (id) {
1680 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 break;
1682 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001683 if (mFolderInfo != null) {
1684 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1685 final CharSequence text = mFolderInfo.title;
1686 input.setText(text);
1687 input.setSelection(0, text.length());
1688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 break;
1690 }
1691 }
1692
1693 void showRenameDialog(FolderInfo info) {
1694 mFolderInfo = info;
1695 mWaitingForResult = true;
1696 showDialog(DIALOG_RENAME_FOLDER);
1697 }
1698
1699 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1700 mAddItemCellInfo = cellInfo;
1701 mWaitingForResult = true;
1702 showDialog(DIALOG_CREATE_SHORTCUT);
1703 }
1704
Romain Guy73b979d2009-06-09 12:57:21 -07001705 private void pickShortcut(int requestCode, int title) {
1706 Bundle bundle = new Bundle();
1707
1708 ArrayList<String> shortcutNames = new ArrayList<String>();
1709 shortcutNames.add(getString(R.string.group_applications));
1710 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1711
1712 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1713 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1714 R.drawable.ic_launcher_application));
1715 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1716
1717 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1718 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1719 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1720 pickIntent.putExtras(bundle);
1721
1722 startActivityForResult(pickIntent, requestCode);
1723 }
1724
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 private class RenameFolder {
1726 private EditText mInput;
1727
1728 Dialog createDialog() {
1729 mWaitingForResult = true;
1730 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1731 mInput = (EditText) layout.findViewById(R.id.folder_name);
1732
1733 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1734 builder.setIcon(0);
1735 builder.setTitle(getString(R.string.rename_folder_title));
1736 builder.setCancelable(true);
1737 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1738 public void onCancel(DialogInterface dialog) {
1739 cleanup();
1740 }
1741 });
1742 builder.setNegativeButton(getString(R.string.cancel_action),
1743 new Dialog.OnClickListener() {
1744 public void onClick(DialogInterface dialog, int which) {
1745 cleanup();
1746 }
1747 }
1748 );
1749 builder.setPositiveButton(getString(R.string.rename_action),
1750 new Dialog.OnClickListener() {
1751 public void onClick(DialogInterface dialog, int which) {
1752 changeFolderName();
1753 }
1754 }
1755 );
1756 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001757
1758 final AlertDialog dialog = builder.create();
1759 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1760 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001761 mInput.requestFocus();
1762 InputMethodManager inputManager = (InputMethodManager)
1763 getSystemService(Context.INPUT_METHOD_SERVICE);
1764 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001765 }
1766 });
1767
1768 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 }
1770
1771 private void changeFolderName() {
1772 final String name = mInput.getText().toString();
1773 if (!TextUtils.isEmpty(name)) {
1774 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001775 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 mFolderInfo.title = name;
1777 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1778
Joe Onorato9c1289c2009-08-17 11:03:03 -04001779 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001780 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001781 mModel.setWorkspaceDirty();
1782 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 } else {
1784 final FolderIcon folderIcon = (FolderIcon)
1785 mWorkspace.getViewForTag(mFolderInfo);
1786 if (folderIcon != null) {
1787 folderIcon.setText(name);
1788 getWorkspace().requestLayout();
1789 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001790 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001791 mModel.setWorkspaceDirty();
1792 mWorkspaceLoading = true;
1793 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
1795 }
1796 }
1797 cleanup();
1798 }
1799
1800 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 dismissDialog(DIALOG_RENAME_FOLDER);
1802 mWaitingForResult = false;
1803 mFolderInfo = null;
1804 }
1805 }
1806
Joe Onoratofb0ca672009-09-14 17:55:46 -04001807 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001808 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001809 }
1810
Jason Samsfd22dac2009-09-20 17:24:16 -07001811 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001812 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001813 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001814
Mike Cleronb6082fa2009-10-19 17:03:36 -07001815 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001816 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001817
Joe Onorato7c312c12009-08-13 21:36:53 -07001818 // TODO: fade these two too
1819 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001820 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001821 }
1822
Joe Onorato7bb17492009-09-24 17:51:01 -07001823 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001824 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001825 mAllAppsGrid.zoom(0.0f);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001826 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001827 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001828
Mike Cleronb6082fa2009-10-19 17:03:36 -07001829
Joe Onorato7c312c12009-08-13 21:36:53 -07001830 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001831 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001832 mDeleteZone.setVisibility(View.VISIBLE);
1833 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001834 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001835 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001836 }
1837
Joe Onorato7c312c12009-08-13 21:36:53 -07001838 void lockAllApps() {
1839 // TODO
1840 }
1841
1842 void unlockAllApps() {
1843 // TODO
1844 }
1845
Joe Onorato7404ee42009-07-31 11:54:44 -07001846 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 * Displays the shortcut creation dialog and launches, if necessary, the
1848 * appropriate activity.
1849 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001850 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001851 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1852 DialogInterface.OnShowListener {
1853
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001855
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 Dialog createDialog() {
1857 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001858
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001860
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1862 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001863 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 builder.setInverseBackgroundForced(true);
1866
1867 AlertDialog dialog = builder.create();
1868 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001869 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001870 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 return dialog;
1873 }
1874
1875 public void onCancel(DialogInterface dialog) {
1876 mWaitingForResult = false;
1877 cleanup();
1878 }
1879
Romain Guycbb89e42009-06-08 15:52:54 -07001880 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001881 }
1882
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 dismissDialog(DIALOG_CREATE_SHORTCUT);
1885 }
1886
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001887 /**
1888 * Handle the action clicked in the "Add to home" dialog.
1889 */
1890 public void onClick(DialogInterface dialog, int which) {
1891 Resources res = getResources();
1892 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001893
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001894 switch (which) {
1895 case AddAdapter.ITEM_SHORTCUT: {
1896 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001897 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001898 break;
1899 }
Romain Guycbb89e42009-06-08 15:52:54 -07001900
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001901 case AddAdapter.ITEM_APPWIDGET: {
1902 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001903
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001904 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1905 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1906 // add the search widget
1907 ArrayList<AppWidgetProviderInfo> customInfo =
1908 new ArrayList<AppWidgetProviderInfo>();
1909 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1910 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1911 info.label = getString(R.string.group_search);
1912 info.icon = R.drawable.ic_search_widget;
1913 customInfo.add(info);
1914 pickIntent.putParcelableArrayListExtra(
1915 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1916 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1917 Bundle b = new Bundle();
1918 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1919 customExtras.add(b);
1920 pickIntent.putParcelableArrayListExtra(
1921 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1922 // start the pick activity
1923 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1924 break;
1925 }
Romain Guycbb89e42009-06-08 15:52:54 -07001926
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001927 case AddAdapter.ITEM_LIVE_FOLDER: {
1928 // Insert extra item to handle inserting folder
1929 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001930
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001931 ArrayList<String> shortcutNames = new ArrayList<String>();
1932 shortcutNames.add(res.getString(R.string.group_folder));
1933 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001934
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001935 ArrayList<ShortcutIconResource> shortcutIcons =
1936 new ArrayList<ShortcutIconResource>();
1937 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1938 R.drawable.ic_launcher_folder));
1939 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1940
1941 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1942 pickIntent.putExtra(Intent.EXTRA_INTENT,
1943 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1944 pickIntent.putExtra(Intent.EXTRA_TITLE,
1945 getText(R.string.title_select_live_folder));
1946 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001947
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001948 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1949 break;
1950 }
1951
1952 case AddAdapter.ITEM_WALLPAPER: {
1953 startWallpaper();
1954 break;
1955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
1957 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001958
1959 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 }
1962
1963 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001964 * Receives notifications whenever the appwidgets are reset.
1965 */
1966 private class AppWidgetResetObserver extends ContentObserver {
1967 public AppWidgetResetObserver() {
1968 super(new Handler());
1969 }
1970
1971 @Override
1972 public void onChange(boolean selfChange) {
1973 onAppWidgetReset();
1974 }
1975 }
1976
1977 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001978 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001980 public int getCurrentWorkspaceScreen() {
1981 return mWorkspace.getCurrentScreen();
1982 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001983
Joe Onorato9c1289c2009-08-17 11:03:03 -04001984 /**
1985 * Refreshes the shortcuts shown on the workspace.
1986 *
1987 * Implementation of the method from LauncherModel.Callbacks.
1988 */
1989 public void startBinding() {
1990 final Workspace workspace = mWorkspace;
1991 int count = workspace.getChildCount();
1992 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001993 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001994 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1995 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001996
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997 if (DEBUG_USER_INTERFACE) {
1998 android.widget.Button finishButton = new android.widget.Button(this);
1999 finishButton.setText("Finish");
2000 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2001
2002 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2003 public void onClick(View v) {
2004 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002005 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
2008 }
2009
2010 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002011 * Bind the items start-end from the list.
2012 *
2013 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002015 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2016
2017 final Workspace workspace = mWorkspace;
2018
2019 for (int i=start; i<end; i++) {
2020 final ItemInfo item = shortcuts.get(i);
2021 mDesktopItems.add(item);
2022 switch (item.itemType) {
2023 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2024 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2025 final View shortcut = createShortcut((ApplicationInfo) item);
2026 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2027 false);
2028 break;
2029 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2030 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2031 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2032 (UserFolderInfo) item);
2033 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2034 false);
2035 break;
2036 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2037 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2038 R.layout.live_folder_icon, this,
2039 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2040 (LiveFolderInfo) item);
2041 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2042 false);
2043 break;
2044 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2045 final int screen = workspace.getCurrentScreen();
2046 final View view = mInflater.inflate(R.layout.widget_search,
2047 (ViewGroup) workspace.getChildAt(screen), false);
2048
2049 Search search = (Search) view.findViewById(R.id.widget_search);
2050 search.setLauncher(this);
2051
2052 final Widget widget = (Widget) item;
2053 view.setTag(widget);
2054
2055 workspace.addWidget(view, widget, false);
2056 break;
2057 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 }
2059
Joe Onorato9c1289c2009-08-17 11:03:03 -04002060 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 }
2062
Joe Onorato9c1289c2009-08-17 11:03:03 -04002063 /**
2064 * Implementation of the method from LauncherModel.Callbacks.
2065 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002066 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2067 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002068 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002070
2071 /**
2072 * Add the views for a widget to the workspace.
2073 *
2074 * Implementation of the method from LauncherModel.Callbacks.
2075 */
2076 public void bindAppWidget(LauncherAppWidgetInfo item) {
2077 final Workspace workspace = mWorkspace;
2078
2079 final int appWidgetId = item.appWidgetId;
2080 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2081 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2082
Joe Onorato9c1289c2009-08-17 11:03:03 -04002083 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2084 item.hostView.setTag(item);
2085
2086 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2087 item.cellY, item.spanX, item.spanY, false);
2088
2089 workspace.requestLayout();
2090
2091 mDesktopItems.add(item);
2092 }
2093
2094 /**
2095 * Callback saying that there aren't any more items to bind.
2096 *
2097 * Implementation of the method from LauncherModel.Callbacks.
2098 */
2099 public void finishBindingItems() {
2100 if (mSavedState != null) {
2101 if (!mWorkspace.hasFocus()) {
2102 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2103 }
2104
2105 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2106 if (userFolders != null) {
2107 for (long folderId : userFolders) {
2108 final FolderInfo info = mFolders.get(folderId);
2109 if (info != null) {
2110 openFolder(info);
2111 }
2112 }
2113 final Folder openFolder = mWorkspace.getOpenFolder();
2114 if (openFolder != null) {
2115 openFolder.requestFocus();
2116 }
2117 }
2118
2119 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
2120 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07002121 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002122 }
2123
2124 mSavedState = null;
2125 }
2126
2127 if (mSavedInstanceState != null) {
2128 super.onRestoreInstanceState(mSavedInstanceState);
2129 mSavedInstanceState = null;
2130 }
2131
Joe Onorato9c1289c2009-08-17 11:03:03 -04002132 Log.d(TAG, "finishBindingItems done");
2133 mWorkspaceLoading = false;
2134 }
2135
2136 /**
2137 * Add the icons for all apps.
2138 *
2139 * Implementation of the method from LauncherModel.Callbacks.
2140 */
2141 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002142 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 }
2144
2145 /**
2146 * A package was installed.
2147 *
2148 * Implementation of the method from LauncherModel.Callbacks.
2149 */
2150 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2151 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002152 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002153 }
2154
2155 /**
2156 * A package was updated.
2157 *
2158 * Implementation of the method from LauncherModel.Callbacks.
2159 */
2160 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2161 removeDialog(DIALOG_CREATE_SHORTCUT);
2162 mWorkspace.updateShortcutsForPackage(packageName);
2163 }
2164
2165 /**
2166 * A package was uninstalled.
2167 *
2168 * Implementation of the method from LauncherModel.Callbacks.
2169 */
2170 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2171 removeDialog(DIALOG_CREATE_SHORTCUT);
2172 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002173 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175}